rt_value_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 TestSimpleValues(t *testing.T) {
  16. res, err := UnitTestEvalAndAST(
  17. `4`, nil,
  18. `
  19. number: 4
  20. `[1:])
  21. if err != nil || res != 4. {
  22. t.Error("Unexpected result: ", res, err)
  23. return
  24. }
  25. res, err = UnitTestEvalAndAST(
  26. `"test"`, nil,
  27. `
  28. string: 'test'
  29. `[1:])
  30. if err != nil || res != "test" {
  31. t.Error("Unexpected result: ", res, err)
  32. return
  33. }
  34. }
  35. func TestStringInterpolation(t *testing.T) {
  36. res, err := UnitTestEvalAndAST(
  37. `"{{'foo'}}test{{'foo'}}test"`, nil,
  38. `
  39. string: '{{'foo'}}test{{'foo'}}test'
  40. `[1:])
  41. if err != nil || res != "footestfootest" {
  42. t.Error("Unexpected result: ", res, err)
  43. return
  44. }
  45. res, err = UnitTestEvalAndAST(
  46. `"{{foo'}}test{{'foo'}}test"`, nil,
  47. `
  48. string: '{{foo'}}test{{'foo'}}test'
  49. `[1:])
  50. if err != nil || res != "#Parse error in String interpolation: foo': Lexical "+
  51. "error (Cannot parse identifier 'foo''. Identifies may only contain [a-zA-Z] "+
  52. "and [a-zA-Z0-9] from the second character) (Line:1 Pos:1)testfootest" {
  53. t.Error("Unexpected result: ", res, err)
  54. return
  55. }
  56. res, err = UnitTestEval(
  57. `"Foo bar {{1+2}}"`, nil)
  58. if err != nil || res != "Foo bar 3" {
  59. t.Error("Unexpected result: ", res, err)
  60. return
  61. }
  62. res, err = UnitTestEval(
  63. `r"Foo bar {{1+2}}"`, nil)
  64. if err != nil || res != "Foo bar {{1+2}}" {
  65. t.Error("Unexpected result: ", res, err)
  66. return
  67. }
  68. res, err = UnitTestEval(
  69. `b:=1;"test{{a:=1;concat([1,2,3], [4,5], [a,b])}}test"`, nil)
  70. if err != nil || res != "test[1 2 3 4 5 1 1]test" {
  71. t.Error("Unexpected result: ", res, err)
  72. return
  73. }
  74. res, err = UnitTestEval(
  75. `b:="foo";"test{{if b { 1 } else { 2 } }}test"`, nil)
  76. if err != nil || res != "test1test" {
  77. t.Error("Unexpected result: ", res, err)
  78. return
  79. }
  80. res, err = UnitTestEval(
  81. `b:=null;"test{{if b { 1 } else { 2 } }}test"`, nil)
  82. if err != nil || res != "test2test" {
  83. t.Error("Unexpected result: ", res, err)
  84. return
  85. }
  86. }
  87. func TestCompositionValues(t *testing.T) {
  88. res, err := UnitTestEvalAndAST(
  89. `{"a":1, "b": 2, "c" : 3}`, nil,
  90. `
  91. map
  92. kvp
  93. string: 'a'
  94. number: 1
  95. kvp
  96. string: 'b'
  97. number: 2
  98. kvp
  99. string: 'c'
  100. number: 3
  101. `[1:])
  102. if resStr := scope.EvalToString(res); err != nil || resStr != `{"a":1,"b":2,"c":3}` {
  103. t.Error("Unexpected result: ", resStr, err)
  104. return
  105. }
  106. res, err = UnitTestEvalAndAST(
  107. `{"a":1, "b": {"a":1, "b": 2, "c" : 3}, "c" : 3}`, nil,
  108. `
  109. map
  110. kvp
  111. string: 'a'
  112. number: 1
  113. kvp
  114. string: 'b'
  115. map
  116. kvp
  117. string: 'a'
  118. number: 1
  119. kvp
  120. string: 'b'
  121. number: 2
  122. kvp
  123. string: 'c'
  124. number: 3
  125. kvp
  126. string: 'c'
  127. number: 3
  128. `[1:])
  129. if resStr := scope.EvalToString(res); err != nil || resStr != `{"a":1,"b":{"a":1,"b":2,"c":3},"c":3}` {
  130. t.Error("Unexpected result: ", resStr, err)
  131. return
  132. }
  133. res, err = UnitTestEvalAndAST(
  134. `{"a":1, "b": [1, [2, 3], 4], "c" : 3}`, nil,
  135. `
  136. map
  137. kvp
  138. string: 'a'
  139. number: 1
  140. kvp
  141. string: 'b'
  142. list
  143. number: 1
  144. list
  145. number: 2
  146. number: 3
  147. number: 4
  148. kvp
  149. string: 'c'
  150. number: 3
  151. `[1:])
  152. if resStr := scope.EvalToString(res); err != nil || resStr != `{"a":1,"b":[1,[2,3],4],"c":3}` {
  153. t.Error("Unexpected result: ", resStr, err)
  154. return
  155. }
  156. }