rt_assign_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. "testing"
  13. "devt.de/krotik/ecal/scope"
  14. )
  15. func TestSimpleAssignments(t *testing.T) {
  16. vs := scope.NewScope(scope.GlobalScope)
  17. res, err := UnitTestEvalAndAST(
  18. `a := 42`, vs,
  19. `
  20. :=
  21. identifier: a
  22. number: 42
  23. `[1:])
  24. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  25. a (float64) : 42
  26. }` {
  27. t.Error("Unexpected result: ", vsRes, res, err)
  28. return
  29. }
  30. res, err = UnitTestEvalAndAST(
  31. `a := "test"`, vs,
  32. `
  33. :=
  34. identifier: a
  35. string: 'test'
  36. `[1:])
  37. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  38. a (string) : test
  39. }` {
  40. t.Error("Unexpected result: ", vsRes, res, err)
  41. return
  42. }
  43. res, err = UnitTestEvalAndAST(
  44. `a := [1,2,3]`, vs,
  45. `
  46. :=
  47. identifier: a
  48. list
  49. number: 1
  50. number: 2
  51. number: 3
  52. `[1:])
  53. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  54. a ([]interface {}) : [1,2,3]
  55. }` {
  56. t.Error("Unexpected result: ", vsRes, res, err)
  57. return
  58. }
  59. res, err = UnitTestEvalAndAST(
  60. `a := {
  61. 1 : "foo",
  62. "2" : "bar",
  63. "foobar" : 3,
  64. x : 4,
  65. }`, vs,
  66. `
  67. :=
  68. identifier: a
  69. map
  70. kvp
  71. number: 1
  72. string: 'foo'
  73. kvp
  74. string: '2'
  75. string: 'bar'
  76. kvp
  77. string: 'foobar'
  78. number: 3
  79. kvp
  80. identifier: x
  81. number: 4
  82. `[1:])
  83. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  84. a (map[interface {}]interface {}) : {"1":"foo","2":"bar","foobar":3,"null":4}
  85. }` {
  86. t.Error("Unexpected result: ", vsRes, res, err)
  87. return
  88. }
  89. }
  90. func TestComplexAssignments(t *testing.T) {
  91. vs := scope.NewScope(scope.GlobalScope)
  92. res, err := UnitTestEvalAndAST(
  93. `[a, b] := ["test", [1,2,3]]`, vs,
  94. `
  95. :=
  96. list
  97. identifier: a
  98. identifier: b
  99. list
  100. string: 'test'
  101. list
  102. number: 1
  103. number: 2
  104. number: 3
  105. `[1:])
  106. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  107. a (string) : test
  108. b ([]interface {}) : [1,2,3]
  109. }` {
  110. t.Error("Unexpected result: ", vsRes, res, err)
  111. return
  112. }
  113. res, err = UnitTestEvalAndAST(`
  114. a := {
  115. "b" : [ 0, {
  116. "c" : [ 0, 1, 2, {
  117. "test" : 1
  118. }]
  119. }]
  120. }
  121. a.b[1].c["3"]["test"] := 3
  122. `, vs,
  123. `
  124. statements
  125. :=
  126. identifier: a
  127. map
  128. kvp
  129. string: 'b'
  130. list
  131. number: 0
  132. map
  133. kvp
  134. string: 'c'
  135. list
  136. number: 0
  137. number: 1
  138. number: 2
  139. map
  140. kvp
  141. string: 'test'
  142. number: 1
  143. :=
  144. identifier: a
  145. identifier: b
  146. compaccess
  147. number: 1
  148. identifier: c
  149. compaccess
  150. string: '3'
  151. compaccess
  152. string: 'test'
  153. number: 3
  154. `[1:])
  155. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  156. a (map[interface {}]interface {}) : {"b":[0,{"c":[0,1,2,{"test":3}]}]}
  157. b ([]interface {}) : [1,2,3]
  158. }` {
  159. t.Error("Unexpected result: ", vsRes, res, err)
  160. return
  161. }
  162. res, err = UnitTestEvalAndAST(`
  163. z := a.b[1].c["3"]["test"]
  164. [x, y] := a.b
  165. `, vs,
  166. `
  167. statements
  168. :=
  169. identifier: z
  170. identifier: a
  171. identifier: b
  172. compaccess
  173. number: 1
  174. identifier: c
  175. compaccess
  176. string: '3'
  177. compaccess
  178. string: 'test'
  179. :=
  180. list
  181. identifier: x
  182. identifier: y
  183. identifier: a
  184. identifier: b
  185. `[1:])
  186. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  187. a (map[interface {}]interface {}) : {"b":[0,{"c":[0,1,2,{"test":3}]}]}
  188. b ([]interface {}) : [1,2,3]
  189. x (float64) : 0
  190. y (map[interface {}]interface {}) : {"c":[0,1,2,{"test":3}]}
  191. z (float64) : 3
  192. }` {
  193. t.Error("Unexpected result: ", vsRes, res, err)
  194. return
  195. }
  196. }