prettyprinter_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package parser
  10. import (
  11. "testing"
  12. )
  13. func TestErrorHandling(t *testing.T) {
  14. input := "c:= a + b"
  15. astres, err := ParseWithRuntime("mytest", input, &DummyRuntimeProvider{})
  16. if err != nil {
  17. t.Errorf("Unexpected parser output:\n%vError: %v", astres, err)
  18. return
  19. }
  20. // Make ast invalid
  21. astres.Children[1].Children[1] = nil
  22. ppres, err := PrettyPrint(astres)
  23. if err == nil || err.Error() != "Nil pointer in AST at level: 2" {
  24. t.Errorf("Unexpected result: %v error: %v", ppres, err)
  25. return
  26. }
  27. }
  28. func TestArithmeticExpressionPrinting(t *testing.T) {
  29. input := "a + b * 5 /2-1"
  30. expectedOutput := `
  31. minus
  32. plus
  33. identifier: a
  34. div
  35. times
  36. identifier: b
  37. number: 5
  38. number: 2
  39. number: 1
  40. `[1:]
  41. if err := UnitTestPrettyPrinting(input, expectedOutput,
  42. "a + b * 5 / 2 - 1"); err != nil {
  43. t.Error(err)
  44. return
  45. }
  46. input = `-a + "\"'b"`
  47. expectedOutput = `
  48. plus
  49. minus
  50. identifier: a
  51. string: '"'b'
  52. `[1:]
  53. if err := UnitTestPrettyPrinting(input, expectedOutput,
  54. `-a + "\"'b"`); err != nil {
  55. t.Error(err)
  56. return
  57. }
  58. input = `a // 5 % (50 + 1)`
  59. expectedOutput = `
  60. modint
  61. divint
  62. identifier: a
  63. number: 5
  64. plus
  65. number: 50
  66. number: 1
  67. `[1:]
  68. if err := UnitTestPrettyPrinting(input, expectedOutput,
  69. `a // 5 % (50 + 1)`); err != nil {
  70. t.Error(err)
  71. return
  72. }
  73. input = "(a + 1) * 5 / (6 - 2)"
  74. expectedOutput = `
  75. div
  76. times
  77. plus
  78. identifier: a
  79. number: 1
  80. number: 5
  81. minus
  82. number: 6
  83. number: 2
  84. `[1:]
  85. if err := UnitTestPrettyPrinting(input, expectedOutput,
  86. "(a + 1) * 5 / (6 - 2)"); err != nil {
  87. t.Error(err)
  88. return
  89. }
  90. input = "a + (1 * 5) / 6 - 2"
  91. expectedOutput = `
  92. minus
  93. plus
  94. identifier: a
  95. div
  96. times
  97. number: 1
  98. number: 5
  99. number: 6
  100. number: 2
  101. `[1:]
  102. if err := UnitTestPrettyPrinting(input, expectedOutput,
  103. "a + 1 * 5 / 6 - 2"); err != nil {
  104. t.Error(err)
  105. return
  106. }
  107. }
  108. func TestLogicalExpressionPrinting(t *testing.T) {
  109. input := "not (a + 1) * 5 and tRue or not 1 - 5 != '!test'"
  110. expectedOutput := `
  111. or
  112. and
  113. not
  114. times
  115. plus
  116. identifier: a
  117. number: 1
  118. number: 5
  119. true
  120. not
  121. !=
  122. minus
  123. number: 1
  124. number: 5
  125. string: '!test'
  126. `[1:]
  127. if err := UnitTestPrettyPrinting(input, expectedOutput,
  128. "not (a + 1) * 5 and true or not 1 - 5 != \"!test\""); err != nil {
  129. t.Error(err)
  130. return
  131. }
  132. input = "not x < null and a > b or 1 <= c and 2 >= false or c == true"
  133. expectedOutput = `
  134. or
  135. or
  136. and
  137. not
  138. <
  139. identifier: x
  140. null
  141. >
  142. identifier: a
  143. identifier: b
  144. and
  145. <=
  146. number: 1
  147. identifier: c
  148. >=
  149. number: 2
  150. false
  151. ==
  152. identifier: c
  153. true
  154. `[1:]
  155. if err := UnitTestPrettyPrinting(input, expectedOutput,
  156. "not x < null and a > b or 1 <= c and 2 >= false or c == true"); err != nil {
  157. t.Error(err)
  158. return
  159. }
  160. input = "a hasPrefix 'a' and b hassuffix 'c' or d like '^.*' and 3 notin x"
  161. expectedOutput = `
  162. or
  163. and
  164. hasprefix
  165. identifier: a
  166. string: 'a'
  167. hassuffix
  168. identifier: b
  169. string: 'c'
  170. and
  171. like
  172. identifier: d
  173. string: '^.*'
  174. notin
  175. number: 3
  176. identifier: x
  177. `[1:]
  178. if err := UnitTestPrettyPrinting(input, expectedOutput,
  179. `a hasprefix "a" and b hassuffix "c" or d like "^.*" and 3 notin x`); err != nil {
  180. t.Error(err)
  181. return
  182. }
  183. }