raku_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // raku_test.go
  2. package raku
  3. import (
  4. "strings"
  5. "testing"
  6. _ "gitlab.com/beoran/woe/monolog"
  7. "gitlab.com/beoran/woe/tree"
  8. )
  9. func HelperTryLexing(me *Lexer, test *testing.T) {
  10. go me.Start()
  11. me.Advance()
  12. test.Logf("Lexing started:")
  13. test.Logf("Lexer buffer: %v", me.buffer)
  14. for token := range me.Output {
  15. // test.Logf("Token %s", token)
  16. _ = token
  17. }
  18. }
  19. func LexAll(me *Lexer) []*Token {
  20. res := make([]*Token, 0)
  21. go me.Start()
  22. for token := range me.Output {
  23. res = append(res, token)
  24. }
  25. return res
  26. }
  27. func LexText(input string) []*Token {
  28. lexer := OpenLexer(strings.NewReader(input))
  29. tokens := LexAll(lexer)
  30. return tokens
  31. }
  32. func Assert(test *testing.T, ok bool, text string) bool {
  33. if !ok {
  34. test.Error(text)
  35. }
  36. return ok
  37. }
  38. func TestLexing(test *testing.T) {
  39. const input = `
  40. say "hello \"world\\"
  41. define open a door do
  42. set door's open to true
  43. end
  44. def increment variable by value do
  45. variable = variable + value
  46. end
  47. `
  48. lexer := OpenLexer(strings.NewReader(input))
  49. HelperTryLexing(lexer, test)
  50. test.Log("Hi test!")
  51. }
  52. func TestLexing2(test *testing.T) {
  53. const input = `say`
  54. lexer := OpenLexer(strings.NewReader(input))
  55. HelperTryLexing(lexer, test)
  56. test.Log("Hi test!")
  57. }
  58. func TestLexing3(test *testing.T) {
  59. const input = `$sym`
  60. lexer := OpenLexer(strings.NewReader(input))
  61. HelperTryLexing(lexer, test)
  62. test.Log("Hi test!")
  63. }
  64. func TestParseValue(test *testing.T) {
  65. const input = `"hello \"world\\"`
  66. parser := NewParserForText(input)
  67. Assert(test, parser.ParseValue(), "Could not parse value")
  68. tree.Display(parser.Ast)
  69. }
  70. func TestParseValue2(test *testing.T) {
  71. const input = `2.1`
  72. parser := NewParserForText(input)
  73. Assert(test, parser.ParseValue(), "Could not parse value")
  74. tree.Display(parser.Ast)
  75. }
  76. func TestParseValue3(test *testing.T) {
  77. const input = `$sym`
  78. parser := NewParserForText(input)
  79. Assert(test, parser.ParseValue(), "Could not parse value")
  80. tree.Display(parser.Ast)
  81. }
  82. func TestParseEox(test *testing.T) {
  83. const input = `
  84. `
  85. parser := NewParserForText(input)
  86. Assert(test, parser.ParseEOX(), "Could not parse EOX")
  87. tree.Display(parser.Ast)
  88. }
  89. func TestParseEox2(test *testing.T) {
  90. const input = `.
  91. `
  92. parser := NewParserForText(input)
  93. Assert(test, parser.ParseEOX(), "Could not parse EOX")
  94. tree.Display(parser.Ast)
  95. }
  96. func TestParseWord(test *testing.T) {
  97. const input = `say`
  98. parser := NewParserForText(input)
  99. Assert(test, parser.ParseWord(), "Could not parse word")
  100. tree.Display(parser.Ast)
  101. }
  102. func TestParseWordExpression(test *testing.T) {
  103. const input = `say "hello world" three times
  104. `
  105. parser := NewParserForText(input)
  106. Assert(test, parser.ParseExpression(), "Could not parse word expression")
  107. tree.Display(parser.Ast)
  108. }
  109. func TestParseWordExpression2(test *testing.T) {
  110. const input = `val + 10 * z
  111. `
  112. parser := NewParserForText(input)
  113. Assert(test, parser.ParseExpression(), "Could not parse word expression with operators")
  114. tree.Display(parser.Ast)
  115. }
  116. func TestParseStatements(test *testing.T) {
  117. const input = `val + 10 * z. open door.
  118. `
  119. parser := NewParserForText(input)
  120. Assert(test, parser.ParseStatements(), "Could not parse statements with only a parse word expression with operators")
  121. tree.Display(parser.Ast)
  122. }
  123. func TestParseProgram(test *testing.T) {
  124. const input = `val + 10 * z. open door.
  125. `
  126. parser := NewParserForText(input)
  127. Assert(test, parser.ParseProgram(), "Could not parse program.")
  128. tree.Display(parser.Ast)
  129. }
  130. func TestParseProgram2(test *testing.T) {
  131. const input = `define greet a person do
  132. say "hello" someone
  133. end
  134. greet bob
  135. greet sally
  136. if 0 do
  137. foo
  138. end else {
  139. bar
  140. }
  141. if mp < cost do
  142. say "Not enough mana!"
  143. end else do
  144. say "Zap!"
  145. end
  146. `
  147. parser := NewParserForText(input)
  148. Assert(test, parser.ParseProgram(), "Could not parse program.")
  149. tree.Display(parser.Ast)
  150. parser.Ast.Dotty()
  151. }
  152. func TestParseblock(test *testing.T) {
  153. // monolog.Setup("raku_test.log", true, false)
  154. const input = `{
  155. say "hello"
  156. say "world"
  157. let i be 3 + 4
  158. let j be 7 + 5
  159. let ij be i * j
  160. return ij
  161. }
  162. `
  163. parser := NewParserForText(input)
  164. Assert(test, parser.ParseBlock(), "Could not parse block.")
  165. tree.Display(parser.Ast)
  166. // parser.Ast.Dotty()
  167. }
  168. func TestParseProgram3(test *testing.T) {
  169. // monolog.Setup("raku_test.log", true, false)
  170. const input = `set foo to (3 + 4)
  171. `
  172. parser := NewParserForText(input)
  173. Assert(test, parser.ParseProgram(), "Could not parse program.")
  174. tree.Display(parser.Ast)
  175. parser.Ast.Dotty()
  176. }
  177. func TestParseParenthesis(test *testing.T) {
  178. // monolog.Setup("raku_test.log", true, false)
  179. const input = `(3 + 4 * 5)`
  180. parser := NewParserForText(input)
  181. Assert(test, parser.ParseParenthesis(), "Could not parse parenthesis.")
  182. tree.Display(parser.Ast)
  183. parser.Ast.Dotty()
  184. }
  185. func TestParseBlock2(test *testing.T) {
  186. // monolog.Setup("raku_test.log", true, false)
  187. const input = `{ . }`
  188. parser := NewParserForText(input)
  189. Assert(test, parser.ParseBlock(), "Could not parse block.")
  190. tree.Display(parser.Ast)
  191. parser.Ast.Dotty()
  192. }
  193. func LexingTest(test *testing.T, input string, expected ...TokenType) {
  194. tokens := LexText(input)
  195. if len(tokens) != len(expected) {
  196. test.Errorf("Amount of tokens does not match expected amount: %d, should be %d", len(tokens), len(expected))
  197. }
  198. for index := 0; index < len(expected); index++ {
  199. want := expected[index]
  200. tok := tokens[index]
  201. if tok.TokenType != want {
  202. test.Errorf("Wrong token type recognized: %v, should be %s", tok, want)
  203. }
  204. }
  205. }
  206. func TestLexingParen(test *testing.T) {
  207. LexingTest(test, "(", TokenOpenParen, TokenEOF)
  208. LexingTest(test, "((", TokenOpenParen, TokenOpenParen, TokenEOF)
  209. }
  210. func TestLexingDoEnd(test *testing.T) {
  211. LexingTest(test, "do", TokenKeywordDo, TokenEOF)
  212. LexingTest(test, "end", TokenKeywordEnd, TokenEOF)
  213. LexingTest(test, "do\nend", TokenKeywordDo, TokenEOL, TokenKeywordEnd, TokenEOF)
  214. LexingTest(test, ".}", TokenPeriod, TokenCloseBrace, TokenEOF)
  215. LexingTest(test, "{.}", TokenOpenBrace, TokenPeriod, TokenCloseBrace, TokenEOF)
  216. }