raku_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // raku_test.go
  2. package raku
  3. import (
  4. "strings"
  5. "testing"
  6. _ "github.com/beoran/woe/monolog"
  7. "github.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.ParseWordExpression(), "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.ParseWordExpression(), "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 = `to greet someone {
  132. say "hello" someone
  133. }
  134. greet bob
  135. if mp < cost do
  136. say "Not enough mana!"
  137. end else do
  138. say "Zap!"
  139. end
  140. `
  141. parser := NewParserForText(input)
  142. Assert(test, parser.ParseProgram(), "Could not parse program.")
  143. tree.Display(parser.Ast)
  144. }
  145. func TestParseblock(test *testing.T) {
  146. // monolog.Setup("raku_test.log", true, false)
  147. const input = `{
  148. say "hello"
  149. say "world"
  150. let i be 3 + 4
  151. let j be 7 + 5
  152. let ij be i * j
  153. return ij
  154. }
  155. `
  156. parser := NewParserForText(input)
  157. Assert(test, parser.ParseBlock(), "Could not parse block.")
  158. tree.Display(parser.Ast)
  159. // parser.Ast.Dotty()
  160. }
  161. func TestParseProgram3(test *testing.T) {
  162. // monolog.Setup("raku_test.log", true, false)
  163. const input = `set foo to (3 + 4)
  164. `
  165. parser := NewParserForText(input)
  166. Assert(test, parser.ParseProgram(), "Could not parse program.")
  167. tree.Display(parser.Ast)
  168. parser.Ast.Dotty()
  169. }
  170. func TestParseParenthesis(test *testing.T) {
  171. // monolog.Setup("raku_test.log", true, false)
  172. const input = `(3 + 4 * 5)`
  173. parser := NewParserForText(input)
  174. Assert(test, parser.ParseParenthesis(), "Could not parse parenthesis.")
  175. tree.Display(parser.Ast)
  176. parser.Ast.Dotty()
  177. }
  178. func LexingTest(test *testing.T, input string, expected ...TokenType) {
  179. tokens := LexText(input)
  180. if len(tokens) != len(expected) {
  181. test.Errorf("Amount of tokens does not match expected amount: %d, should be %d", len(tokens), len(expected))
  182. }
  183. for index := 0; index < len(expected); index++ {
  184. want := expected[index]
  185. tok := tokens[index]
  186. if tok.TokenType != want {
  187. test.Errorf("Wrong token type recognized: %v, should be %s", tok, want)
  188. }
  189. }
  190. }
  191. func TestLexingParen(test *testing.T) {
  192. LexingTest(test, "(", TokenOpenParen, TokenEOF)
  193. LexingTest(test, "((", TokenOpenParen, TokenOpenParen, TokenEOF)
  194. }