raku_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. test.Logf("Lexing started:")
  12. test.Logf("Lexer buffer: %v", me.buffer)
  13. for token := range me.Output {
  14. test.Logf("Token %s", token)
  15. }
  16. }
  17. func Assert(test *testing.T, ok bool, text string) bool {
  18. if !ok {
  19. test.Error(text)
  20. }
  21. return ok
  22. }
  23. func TestLexing(test *testing.T) {
  24. const input = `
  25. say "hello \"world\\"
  26. to open a door do
  27. set door's open to true
  28. end
  29. to increment variable by value do
  30. variable = variable + value
  31. end
  32. `
  33. lexer := OpenLexer(strings.NewReader(input))
  34. HelperTryLexing(lexer, test)
  35. test.Log("Hi test!")
  36. }
  37. func TestLexing2(test *testing.T) {
  38. const input = `say`
  39. lexer := OpenLexer(strings.NewReader(input))
  40. HelperTryLexing(lexer, test)
  41. test.Log("Hi test!")
  42. }
  43. func TestLexing3(test *testing.T) {
  44. const input = `$sym`
  45. lexer := OpenLexer(strings.NewReader(input))
  46. HelperTryLexing(lexer, test)
  47. test.Log("Hi test!")
  48. }
  49. func TestParseValue(test *testing.T) {
  50. const input = `"hello \"world\\"`
  51. parser := NewParserForText(input)
  52. Assert(test, parser.ParseValue(), "Could not parse value")
  53. tree.Display(parser.Ast)
  54. }
  55. func TestParseValue2(test *testing.T) {
  56. const input = `2.1`
  57. parser := NewParserForText(input)
  58. Assert(test, parser.ParseValue(), "Could not parse value")
  59. tree.Display(parser.Ast)
  60. }
  61. func TestParseValue3(test *testing.T) {
  62. const input = `$sym`
  63. parser := NewParserForText(input)
  64. Assert(test, parser.ParseValue(), "Could not parse value")
  65. tree.Display(parser.Ast)
  66. }
  67. func TestParseEox(test *testing.T) {
  68. const input = `
  69. `
  70. parser := NewParserForText(input)
  71. Assert(test, parser.ParseEOX(), "Could not parse EOX")
  72. tree.Display(parser.Ast)
  73. }
  74. func TestParseEox2(test *testing.T) {
  75. const input = `.
  76. `
  77. parser := NewParserForText(input)
  78. Assert(test, parser.ParseEOX(), "Could not parse EOX")
  79. tree.Display(parser.Ast)
  80. }
  81. func TestParseWord(test *testing.T) {
  82. const input = `say`
  83. parser := NewParserForText(input)
  84. Assert(test, parser.ParseWord(), "Could not parse word")
  85. tree.Display(parser.Ast)
  86. }
  87. func TestParseWordExpression(test *testing.T) {
  88. const input = `say "hello world" three times
  89. `
  90. parser := NewParserForText(input)
  91. Assert(test, parser.ParseWordExpression(), "Could not parse word expression")
  92. tree.Display(parser.Ast)
  93. }
  94. func TestParseWordExpression2(test *testing.T) {
  95. const input = `val + 10 * z
  96. `
  97. parser := NewParserForText(input)
  98. Assert(test, parser.ParseWordExpression(), "Could not parse word expression with operators")
  99. tree.Display(parser.Ast)
  100. }
  101. func TestParseStatements(test *testing.T) {
  102. const input = `val + 10 * z. open door.
  103. `
  104. parser := NewParserForText(input)
  105. Assert(test, parser.ParseStatements(), "Could not parse statements with only a parse word expression with operators")
  106. tree.Display(parser.Ast)
  107. }
  108. func TestParseProgram(test *testing.T) {
  109. const input = `val + 10 * z. open door.
  110. `
  111. parser := NewParserForText(input)
  112. Assert(test, parser.ParseProgram(), "Could not parse program.")
  113. tree.Display(parser.Ast)
  114. }
  115. func TestParseProgram2(test *testing.T) {
  116. const input = `to greet someone [
  117. say "hello" someone
  118. ]
  119. greet bob
  120. if mp < cost do
  121. say "Not enough mana!"
  122. end else do
  123. say "Zap!"
  124. end
  125. `
  126. parser := NewParserForText(input)
  127. Assert(test, parser.ParseProgram(), "Could not parse program.")
  128. tree.Display(parser.Ast)
  129. }
  130. func TestParseblock(test *testing.T) {
  131. // monolog.Setup("raku_test.log", true, false)
  132. const input = `[
  133. say "hello"
  134. say "world"
  135. let i be 3 + 4
  136. let j be 7 + 5
  137. let ij be i * j
  138. return ij
  139. ]
  140. `
  141. parser := NewParserForText(input)
  142. Assert(test, parser.ParseBlock(), "Could not parse block.")
  143. tree.Display(parser.Ast)
  144. // parser.Ast.Dotty()
  145. }
  146. func TestParseProgram3(test *testing.T) {
  147. // monolog.Setup("raku_test.log", true, false)
  148. const input = `let foo be set equal = 3 + 4 bar
  149. `
  150. parser := NewParserForText(input)
  151. Assert(test, parser.ParseProgram(), "Could not parse program.")
  152. tree.Display(parser.Ast)
  153. parser.Ast.Dotty()
  154. }