helper_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 scope
  11. import (
  12. "fmt"
  13. "testing"
  14. "devt.de/krotik/ecal/parser"
  15. )
  16. func TestNameFromASTNode(t *testing.T) {
  17. n, _ := parser.Parse("", "foo")
  18. if res := NameFromASTNode(n); res != "block: identifier (Line:1 Pos:1)" {
  19. t.Error("Unexpected result:", res)
  20. return
  21. }
  22. }
  23. func TestScopeConversion(t *testing.T) {
  24. vs := NewScope("foo")
  25. vs.SetValue("a", 1)
  26. vs.SetValue("b", 2)
  27. vs.SetValue("c", 3)
  28. vs2 := ToScope("foo", ToObject(vs))
  29. if vs.String() != vs2.String() {
  30. t.Error("Unexpected result:", vs.String(), vs2.String())
  31. return
  32. }
  33. }
  34. func TestConvertJSONToECALObject(t *testing.T) {
  35. testJSONStructure := map[string]interface{}{
  36. "foo": []interface{}{
  37. map[string]interface{}{
  38. "bar": "123",
  39. },
  40. },
  41. }
  42. res := ConvertJSONToECALObject(testJSONStructure)
  43. if typeString := fmt.Sprintf("%#v", res); typeString !=
  44. `map[interface {}]interface {}{"foo":[]interface {}{map[interface {}]interface {}{"bar":"123"}}}` {
  45. t.Error("Unexpected result:", typeString)
  46. return
  47. }
  48. res = ConvertECALToJSONObject(res)
  49. if typeString := fmt.Sprintf("%#v", res); typeString !=
  50. `map[string]interface {}{"foo":[]interface {}{map[string]interface {}{"bar":"123"}}}` {
  51. t.Error("Unexpected result:", typeString)
  52. return
  53. }
  54. testJSONStructure2 := map[interface{}]interface{}{"data": map[interface{}]interface{}{"obj": []map[string]interface{}{{"key": "LovelyRabbit"}}}}
  55. res = ConvertJSONToECALObject(testJSONStructure2)
  56. if typeString := fmt.Sprintf("%#v", res); typeString !=
  57. `map[interface {}]interface {}{"data":map[interface {}]interface {}{"obj":[]interface {}{map[interface {}]interface {}{"key":"LovelyRabbit"}}}}` {
  58. t.Error("Unexpected result:", typeString)
  59. return
  60. }
  61. res = ConvertECALToJSONObject(res)
  62. if typeString := fmt.Sprintf("%#v", res); typeString !=
  63. `map[string]interface {}{"data":map[string]interface {}{"obj":[]interface {}{map[string]interface {}{"key":"LovelyRabbit"}}}}` {
  64. t.Error("Unexpected result:", typeString)
  65. return
  66. }
  67. }