aboutsummaryrefslogtreecommitdiff
path: root/test/gleam_json_test.gleam
blob: e2f3542cddae400ad258a22e0c716936bb4c3bd2 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import gleam/dynamic
import gleam/json.{type Json}
import gleam/option.{None, Some}
import gleam/string
import gleam/string_builder
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() {
  let assert Error(error) = json.decode(from: "[}", using: dynamic.int)
  let assert json.UnexpectedByte(byte) = error
  let assert "0x7D" = byte
}

pub fn decode_unexpected_format_test() {
  json.decode(from: "[]", using: dynamic.int)
  |> should.equal(Error(json.UnexpectedFormat([empty_list_decode_error()])))
}

pub fn decode_bits_test() {
  json.decode_bits(from: <<"5":utf8>>, using: dynamic.int)
  |> should.equal(Ok(5))
}

pub fn decode_bits_empty_test() {
  json.decode_bits(from: <<"":utf8>>, using: dynamic.int)
  |> should.equal(Error(json.UnexpectedEndOfInput))
}

pub fn decode_bits_unexpected_byte_test() {
  let assert Error(error) = json.decode(from: "[}", using: dynamic.int)
  let assert json.UnexpectedByte(byte) = error
  let assert "0x7D" = byte
}

pub fn decode_bits_unexpected_format_test() {
  json.decode_bits(from: <<"[]":utf8>>, using: dynamic.int)
  |> should.equal(Error(json.UnexpectedFormat([empty_list_decode_error()])))
}

pub fn decode_unexpected_sequence_test() {
  let assert Error(error) =
    json.decode(from: "\"\\uxxxx\"", using: dynamic.float)
  case error {
    json.UnexpectedSequence("\\uxxxx") -> Nil
    json.UnexpectedByte("0x78") -> Nil
    _ -> panic as { "unexpected error: " <> string.inspect(error) }
  }
}

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.1)
  |> should_encode("100.1")
}

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

pub fn encode_empty_object_test() {
  json.object([])
  |> should_encode("{}")
}

pub fn encode_empty_array_test() {
  []
  |> json.array(of: json.int)
  |> should_encode("[]")
}

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_empty_preprocessed_array_test() {
  json.preprocessed_array([])
  |> should_encode("[]")
}

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))
}

@target(erlang)
fn empty_list_decode_error() -> dynamic.DecodeError {
  dynamic.DecodeError(expected: "Int", found: "List", path: [])
}

@target(javascript)
fn empty_list_decode_error() {
  dynamic.DecodeError(expected: "Int", found: "Tuple of 0 elements", path: [])
}