prettyprinter_test.go 3.6 KB

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