aboutsummaryrefslogtreecommitdiff
path: root/test/gleam_json_test.gleam
blob: fb55c9016bba800542a66b68e362b28580f2b0da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import gleam/dynamic
import gleam/json.{Json}
import gleam/option.{None, Some}
import gleam/string_builder
import gleam/result
import gleeunit
import gleeunit/should

pub fn main() {
  gleeunit.main()
}

pub fn decode_test() {
  json.decode(from: "5", using: dynamic.int)
  |> should.equal(Ok(5))
}

pub fn decode_empty_test() {
  json.decode(from: "", using: dynamic.int)
  |> should.equal(Error(json.UnexpectedEndOfInput))
}

pub fn decode_unexpected_byte_test() {
  json.decode(from: "[}", using: dynamic.int)
  |> should.equal(Error(json.UnexpectedByte("0x7D", 1)))
}

pub fn decode_unexpected_format_test() {
  json.decode(from: "[]", using: dynamic.int)
  |> should.equal(Error(json.UnexpectedFormat([
    dynamic.DecodeError(expected: "Int", found: "List", path: []),
  ])))
}

pub fn encode_string_test() {
  json.string("hello")
  |> should_encode("\"hello\"")
}

pub fn encode_null_test() {
  json.null()
  |> should_encode("null")
}

pub fn encode_int_test() {
  json.int(-50)
  |> should_encode("-50")

  json.int(100)
  |> should_encode("100")
}

pub fn encode_float_test() {
  json.float(-50.5)
  |> should_encode("-50.5")

  json.float(100.0)
  |> should_encode("100.0")
}

pub fn encode_object_test() {
  json.object([#("foo", json.int(5))])
  |> should_encode("{\"foo\":5}")
}

pub fn encode_array_test() {
  [5, 6, 1, 4]
  |> json.array(of: json.int)
  |> should_encode("[5,6,1,4]")
}

pub fn encode_preprocessed_array_test() {
  json.preprocessed_array([json.int(5), json.int(6)])
  |> should_encode("[5,6]")
}

pub fn encode_nullable_some_test() {
  json.nullable(Some(5), of: json.int)
  |> should_encode("5")
}

pub fn encode_nullable_none_test() {
  json.nullable(None, of: json.int)
  |> should_encode("null")
}

pub fn encode_bool_true_test() {
  json.bool(True)
  |> should_encode("true")
}

pub fn encode_bool_false_test() {
  json.bool(False)
  |> should_encode("false")
}

fn should_encode(data: Json, expected: String) {
  data
  |> json.to_string()
  |> should.equal(expected)

  data
  |> json.to_string_builder
  |> string_builder.to_string
  |> should.equal(json.to_string(data))
}