json_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package stdlib_test
  2. import "testing"
  3. func TestJSON(t *testing.T) {
  4. module(t, "json").call("encode", 5).
  5. expect([]byte("5"))
  6. module(t, "json").call("encode", "foobar").
  7. expect([]byte(`"foobar"`))
  8. module(t, "json").call("encode", MAP{"foo": 5}).
  9. expect([]byte("{\"foo\":5}"))
  10. module(t, "json").call("encode", IMAP{"foo": 5}).
  11. expect([]byte("{\"foo\":5}"))
  12. module(t, "json").call("encode", ARR{1, 2, 3}).
  13. expect([]byte("[1,2,3]"))
  14. module(t, "json").call("encode", IARR{1, 2, 3}).
  15. expect([]byte("[1,2,3]"))
  16. module(t, "json").call("encode", MAP{"foo": "bar"}).
  17. expect([]byte("{\"foo\":\"bar\"}"))
  18. module(t, "json").call("encode", MAP{"foo": 1.8}).
  19. expect([]byte("{\"foo\":1.8}"))
  20. module(t, "json").call("encode", MAP{"foo": true}).
  21. expect([]byte("{\"foo\":true}"))
  22. module(t, "json").call("encode", MAP{"foo": '8'}).
  23. expect([]byte("{\"foo\":56}"))
  24. module(t, "json").call("encode", MAP{"foo": []byte("foo")}).
  25. expect([]byte("{\"foo\":\"Zm9v\"}")) // json encoding returns []byte as base64 encoded string
  26. module(t, "json").
  27. call("encode", MAP{"foo": ARR{"bar", 1, 1.8, '8', true}}).
  28. expect([]byte("{\"foo\":[\"bar\",1,1.8,56,true]}"))
  29. module(t, "json").
  30. call("encode", MAP{"foo": IARR{"bar", 1, 1.8, '8', true}}).
  31. expect([]byte("{\"foo\":[\"bar\",1,1.8,56,true]}"))
  32. module(t, "json").
  33. call("encode", MAP{"foo": ARR{ARR{"bar", 1}, ARR{"bar", 1}}}).
  34. expect([]byte("{\"foo\":[[\"bar\",1],[\"bar\",1]]}"))
  35. module(t, "json").
  36. call("encode", MAP{"foo": MAP{"string": "bar"}}).
  37. expect([]byte("{\"foo\":{\"string\":\"bar\"}}"))
  38. module(t, "json").
  39. call("encode", MAP{"foo": IMAP{"string": "bar"}}).
  40. expect([]byte("{\"foo\":{\"string\":\"bar\"}}"))
  41. module(t, "json").
  42. call("encode", MAP{"foo": MAP{"map1": MAP{"string": "bar"}}}).
  43. expect([]byte("{\"foo\":{\"map1\":{\"string\":\"bar\"}}}"))
  44. module(t, "json").
  45. call("encode", ARR{ARR{"bar", 1}, ARR{"bar", 1}}).
  46. expect([]byte("[[\"bar\",1],[\"bar\",1]]"))
  47. module(t, "json").call("decode", `5`).
  48. expect(5)
  49. module(t, "json").call("decode", `"foo"`).
  50. expect("foo")
  51. module(t, "json").call("decode", `[1,2,3,"bar"]`).
  52. expect(ARR{1, 2, 3, "bar"})
  53. module(t, "json").call("decode", `{"foo":5}`).
  54. expect(MAP{"foo": 5})
  55. module(t, "json").call("decode", `{"foo":2.5}`).
  56. expect(MAP{"foo": 2.5})
  57. module(t, "json").call("decode", `{"foo":true}`).
  58. expect(MAP{"foo": true})
  59. module(t, "json").call("decode", `{"foo":"bar"}`).
  60. expect(MAP{"foo": "bar"})
  61. module(t, "json").call("decode", `{"foo":[1,2,3,"bar"]}`).
  62. expect(MAP{"foo": ARR{1, 2, 3, "bar"}})
  63. module(t, "json").
  64. call("indent", []byte("{\"foo\":[\"bar\",1,1.8,56,true]}"), "", " ").
  65. expect([]byte(`{
  66. "foo": [
  67. "bar",
  68. 1,
  69. 1.8,
  70. 56,
  71. true
  72. ]
  73. }`))
  74. module(t, "json").
  75. call("html_escape", []byte(
  76. `{"M":"<html>foo &`+"\xe2\x80\xa8 \xe2\x80\xa9"+`</html>"}`)).
  77. expect([]byte(
  78. `{"M":"\u003chtml\u003efoo \u0026\u2028 \u2029\u003c/html\u003e"}`))
  79. }