func_provider_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 interpreter
  11. import (
  12. "fmt"
  13. "testing"
  14. )
  15. func TestStdlib(t *testing.T) {
  16. res, err := UnitTestEvalAndAST(
  17. `fmt.Sprint([1,2,3])`, nil,
  18. `
  19. identifier: fmt
  20. identifier: Sprint
  21. funccall
  22. list
  23. number: 1
  24. number: 2
  25. number: 3
  26. `[1:])
  27. if err != nil || res != "[1 2 3]" {
  28. t.Error("Unexpected result: ", res, err)
  29. return
  30. }
  31. res, err = UnitTestEvalAndAST(
  32. `fmt.Sprint(math.Pi)`, nil,
  33. `
  34. identifier: fmt
  35. identifier: Sprint
  36. funccall
  37. identifier: math
  38. identifier: Pi
  39. `[1:])
  40. if err != nil || res != "3.141592653589793" {
  41. t.Error("Unexpected result: ", res, err)
  42. return
  43. }
  44. // Negative case
  45. res, err = UnitTestEvalAndAST(
  46. `a.fmtSprint([1,2,3])`, nil,
  47. `
  48. identifier: a
  49. identifier: fmtSprint
  50. funccall
  51. list
  52. number: 1
  53. number: 2
  54. number: 3
  55. `[1:])
  56. if err == nil ||
  57. err.Error() != "ECAL error in ECALTestRuntime: Unknown construct (Unknown function: fmtSprint) (Line:1 Pos:3)" {
  58. t.Error("Unexpected result: ", res, err)
  59. return
  60. }
  61. }
  62. func TestSimpleFunctions(t *testing.T) {
  63. res, err := UnitTestEvalAndAST(
  64. `len([1,2,3])`, nil,
  65. `
  66. identifier: len
  67. funccall
  68. list
  69. number: 1
  70. number: 2
  71. number: 3
  72. `[1:])
  73. if err != nil || res != 3. {
  74. t.Error("Unexpected result: ", res, err)
  75. return
  76. }
  77. res, err = UnitTestEvalAndAST(
  78. `len({"a":1, 2:"b"})`, nil,
  79. `
  80. identifier: len
  81. funccall
  82. map
  83. kvp
  84. string: 'a'
  85. number: 1
  86. kvp
  87. number: 2
  88. string: 'b'
  89. `[1:])
  90. if err != nil || res != 2. {
  91. t.Error("Unexpected result: ", res, err)
  92. return
  93. }
  94. res, err = UnitTestEvalAndAST(
  95. `del([1,2,3], 1)`, nil,
  96. `
  97. identifier: del
  98. funccall
  99. list
  100. number: 1
  101. number: 2
  102. number: 3
  103. number: 1
  104. `[1:])
  105. if err != nil || fmt.Sprint(res) != "[1 3]" {
  106. t.Error("Unexpected result: ", res, err)
  107. return
  108. }
  109. res, err = UnitTestEvalAndAST(
  110. `del({
  111. "a" : 1
  112. "b" : 2
  113. "c" : 3
  114. }, "b")`, nil,
  115. `
  116. identifier: del
  117. funccall
  118. map
  119. kvp
  120. string: 'a'
  121. number: 1
  122. kvp
  123. string: 'b'
  124. number: 2
  125. kvp
  126. string: 'c'
  127. number: 3
  128. string: 'b'
  129. `[1:])
  130. if err != nil || fmt.Sprint(res) != "map[a:1 c:3]" {
  131. t.Error("Unexpected result: ", res, err)
  132. return
  133. }
  134. res, err = UnitTestEvalAndAST(
  135. `add([1,2,3], 4)`, nil,
  136. `
  137. identifier: add
  138. funccall
  139. list
  140. number: 1
  141. number: 2
  142. number: 3
  143. number: 4
  144. `[1:])
  145. if err != nil || fmt.Sprint(res) != "[1 2 3 4]" {
  146. t.Error("Unexpected result: ", res, err)
  147. return
  148. }
  149. res, err = UnitTestEvalAndAST(
  150. `add([1,2,3], 4, 0)`, nil,
  151. `
  152. identifier: add
  153. funccall
  154. list
  155. number: 1
  156. number: 2
  157. number: 3
  158. number: 4
  159. number: 0
  160. `[1:])
  161. if err != nil || fmt.Sprint(res) != "[4 1 2 3]" {
  162. t.Error("Unexpected result: ", res, err)
  163. return
  164. }
  165. res, err = UnitTestEvalAndAST(
  166. `add([1,2,3], 4, 1)`, nil,
  167. `
  168. identifier: add
  169. funccall
  170. list
  171. number: 1
  172. number: 2
  173. number: 3
  174. number: 4
  175. number: 1
  176. `[1:])
  177. if err != nil || fmt.Sprint(res) != "[1 4 2 3]" {
  178. t.Error("Unexpected result: ", res, err)
  179. return
  180. }
  181. res, err = UnitTestEvalAndAST(
  182. `concat([1,2,3], [4,5,6], [7,8,9])`, nil,
  183. `
  184. identifier: concat
  185. funccall
  186. list
  187. number: 1
  188. number: 2
  189. number: 3
  190. list
  191. number: 4
  192. number: 5
  193. number: 6
  194. list
  195. number: 7
  196. number: 8
  197. number: 9
  198. `[1:])
  199. if err != nil || fmt.Sprint(res) != "[1 2 3 4 5 6 7 8 9]" {
  200. t.Error("Unexpected result: ", res, err)
  201. return
  202. }
  203. res, err = UnitTestEvalAndAST(
  204. `dumpenv()`, nil,
  205. `
  206. identifier: dumpenv
  207. funccall
  208. `[1:])
  209. if err != nil || fmt.Sprint(res) != `GlobalScope {
  210. }` {
  211. t.Error("Unexpected result: ", res, err)
  212. return
  213. }
  214. // Negative case
  215. res, err = UnitTestEvalAndAST(
  216. `a.len([1,2,3])`, nil,
  217. `
  218. identifier: a
  219. identifier: len
  220. funccall
  221. list
  222. number: 1
  223. number: 2
  224. number: 3
  225. `[1:])
  226. if err == nil ||
  227. err.Error() != "ECAL error in ECALTestRuntime: Unknown construct (Unknown function: len) (Line:1 Pos:3)" {
  228. t.Error("Unexpected result: ", res, err)
  229. return
  230. }
  231. }