raku_test.go 6.3 KB

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