blob: dae51b6e43c2fa7e480e186a4b55cd8e2d7b6d93 (
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
|
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("5")
|> result.map(dynamic.from)
|> should.equal(Ok(dynamic.from(5)))
}
pub fn decode_empty_test() {
json.decode("")
|> should.equal(Error(json.UnexpectedEndOfInput))
}
pub fn decode_unexpected_byte_test() {
json.decode("[}")
|> should.equal(Error(json.UnexpectedByte("0x7D", 1)))
}
pub fn encode_string_test() {
json.string("hello")
|> should_encode("\"hello\"")
}
pub fn encode_null_test() {
json.null()
|> should_encode("null")
}
pub fn encode_object_test() {
json.object([#("foo", json.int(5))])
|> should_encode("{\"foo\":5}")
}
pub fn encode_list_test() {
json.list([json.int(5), json.int(6)])
|> should_encode("[5,6]")
}
pub fn encode_nullable_some_test() {
json.nullable(Some(5), the: json.int)
|> should_encode("5")
}
pub fn encode_nullable_none_test() {
json.nullable(None, the: json.int)
|> should_encode("null")
}
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))
}
|