rt_value_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 TestCompositionValues(t *testing.T) {
  36. res, err := UnitTestEvalAndAST(
  37. `{"a":1, "b": 2, "c" : 3}`, nil,
  38. `
  39. map
  40. kvp
  41. string: 'a'
  42. number: 1
  43. kvp
  44. string: 'b'
  45. number: 2
  46. kvp
  47. string: 'c'
  48. number: 3
  49. `[1:])
  50. if resStr := scope.EvalToString(res); err != nil || resStr != `{"a":1,"b":2,"c":3}` {
  51. t.Error("Unexpected result: ", resStr, err)
  52. return
  53. }
  54. res, err = UnitTestEvalAndAST(
  55. `{"a":1, "b": {"a":1, "b": 2, "c" : 3}, "c" : 3}`, nil,
  56. `
  57. map
  58. kvp
  59. string: 'a'
  60. number: 1
  61. kvp
  62. string: 'b'
  63. map
  64. kvp
  65. string: 'a'
  66. number: 1
  67. kvp
  68. string: 'b'
  69. number: 2
  70. kvp
  71. string: 'c'
  72. number: 3
  73. kvp
  74. string: 'c'
  75. number: 3
  76. `[1:])
  77. if resStr := scope.EvalToString(res); err != nil || resStr != `{"a":1,"b":{"a":1,"b":2,"c":3},"c":3}` {
  78. t.Error("Unexpected result: ", resStr, err)
  79. return
  80. }
  81. res, err = UnitTestEvalAndAST(
  82. `{"a":1, "b": [1, [2, 3], 4], "c" : 3}`, nil,
  83. `
  84. map
  85. kvp
  86. string: 'a'
  87. number: 1
  88. kvp
  89. string: 'b'
  90. list
  91. number: 1
  92. list
  93. number: 2
  94. number: 3
  95. number: 4
  96. kvp
  97. string: 'c'
  98. number: 3
  99. `[1:])
  100. if resStr := scope.EvalToString(res); err != nil || resStr != `{"a":1,"b":[1,[2,3],4],"c":3}` {
  101. t.Error("Unexpected result: ", resStr, err)
  102. return
  103. }
  104. }