prettyprinter_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 TestArithmeticExpressionPrinting(t *testing.T) {
  14. input := "a + b * 5 /2-1"
  15. expectedOutput := `
  16. minus
  17. plus
  18. identifier: a
  19. div
  20. times
  21. identifier: b
  22. number: 5
  23. number: 2
  24. number: 1
  25. `[1:]
  26. if err := UnitTestPrettyPrinting(input, expectedOutput,
  27. "a + b * 5 / 2 - 1"); err != nil {
  28. t.Error(err)
  29. return
  30. }
  31. input = `-a + "\"'b"`
  32. expectedOutput = `
  33. plus
  34. minus
  35. identifier: a
  36. string: '"'b'
  37. `[1:]
  38. if err := UnitTestPrettyPrinting(input, expectedOutput,
  39. `-a + "\"'b"`); err != nil {
  40. t.Error(err)
  41. return
  42. }
  43. input = `a // 5 % (50 + 1)`
  44. expectedOutput = `
  45. modint
  46. divint
  47. identifier: a
  48. number: 5
  49. plus
  50. number: 50
  51. number: 1
  52. `[1:]
  53. if err := UnitTestPrettyPrinting(input, expectedOutput,
  54. `a // 5 % (50 + 1)`); err != nil {
  55. t.Error(err)
  56. return
  57. }
  58. input = "(a + 1) * 5 / (6 - 2)"
  59. expectedOutput = `
  60. div
  61. times
  62. plus
  63. identifier: a
  64. number: 1
  65. number: 5
  66. minus
  67. number: 6
  68. number: 2
  69. `[1:]
  70. if err := UnitTestPrettyPrinting(input, expectedOutput,
  71. "(a + 1) * 5 / (6 - 2)"); err != nil {
  72. t.Error(err)
  73. return
  74. }
  75. input = "a + (1 * 5) / 6 - 2"
  76. expectedOutput = `
  77. minus
  78. plus
  79. identifier: a
  80. div
  81. times
  82. number: 1
  83. number: 5
  84. number: 6
  85. number: 2
  86. `[1:]
  87. if err := UnitTestPrettyPrinting(input, expectedOutput,
  88. "a + 1 * 5 / 6 - 2"); err != nil {
  89. t.Error(err)
  90. return
  91. }
  92. }
  93. func TestLogicalExpressionPrinting(t *testing.T) {
  94. input := "not (a + 1) * 5 and tRue or not 1 - 5 != '!test'"
  95. expectedOutput := `
  96. or
  97. and
  98. not
  99. times
  100. plus
  101. identifier: a
  102. number: 1
  103. number: 5
  104. true
  105. not
  106. !=
  107. minus
  108. number: 1
  109. number: 5
  110. string: '!test'
  111. `[1:]
  112. if err := UnitTestPrettyPrinting(input, expectedOutput,
  113. "not (a + 1) * 5 and true or not 1 - 5 != \"!test\""); err != nil {
  114. t.Error(err)
  115. return
  116. }
  117. input = "not x < null and a > b or 1 <= c and 2 >= false or c == true"
  118. expectedOutput = `
  119. or
  120. or
  121. and
  122. not
  123. <
  124. identifier: x
  125. null
  126. >
  127. identifier: a
  128. identifier: b
  129. and
  130. <=
  131. number: 1
  132. identifier: c
  133. >=
  134. number: 2
  135. false
  136. ==
  137. identifier: c
  138. true
  139. `[1:]
  140. if err := UnitTestPrettyPrinting(input, expectedOutput,
  141. "not x < null and a > b or 1 <= c and 2 >= false or c == true"); err != nil {
  142. t.Error(err)
  143. return
  144. }
  145. input = "a hasPrefix 'a' and b hassuffix 'c' or d like '^.*' and 3 notin x"
  146. expectedOutput = `
  147. or
  148. and
  149. hasprefix
  150. identifier: a
  151. string: 'a'
  152. hassuffix
  153. identifier: b
  154. string: 'c'
  155. and
  156. like
  157. identifier: d
  158. string: '^.*'
  159. notin
  160. number: 3
  161. identifier: x
  162. `[1:]
  163. if err := UnitTestPrettyPrinting(input, expectedOutput,
  164. `a hasprefix "a" and b hassuffix "c" or d like "^.*" and 3 notin x`); err != nil {
  165. t.Error(err)
  166. return
  167. }
  168. }