-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathjson_test.mbt
169 lines (147 loc) · 3.57 KB
/
json_test.mbt
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
163
164
165
166
167
168
169
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///|
test "Json default value" {
let j = @builtin.Json::default()
inspect!(j, content="False")
}
///|
test "Int64 to_json" {
let i : Int64 = 42L
inspect!(i.to_json(), content="String(\"42\")")
}
///|
test "UInt to_json" {
let x = 42U
let json = UInt::to_json(x)
inspect!(json, content="Number(42)")
}
///|
test "double to json with positive infinity" {
let pos_inf = 1.0 / 0.0
inspect!(pos_inf.to_json(), content="Null")
}
///|
test "test_float_to_json" {
let float : Float = 3.14
let json_val = float.to_json()
inspect!(json_val, content="Number(3.140000104904175)")
}
///|
test "to_json on empty fixed array" {
let arr : FixedArray[Int] = FixedArray::make(0, 0)
inspect!(arr.to_json(), content="Array([])")
}
///|
test "test empty array view to json" {
let arr : Array[Int] = []
let view : ArrayView[Int] = arr[:]
let json = @builtin.ArrayView::to_json(view)
inspect!(json, content="Array([])")
}
///|
test "test map with string values" {
let m : Map[String, Int] = {}
inspect!(m.to_json(), content="Object({})")
}
///|
test "Option::to_json with None" {
let none : Int? = None
inspect!(none.to_json(), content="Null")
}
///|
test "Option::to_json with Some value" {
let some : Int? = Some(42)
inspect!(some.to_json(), content="Array([Number(42)])")
}
///|
test "Result to_json with Ok value" {
let ok : Result[Int, String] = Ok(42)
inspect!(ok.to_json(), content="Object({\"Ok\": Number(42)})")
}
///|
test "Unit::to_json" {
let r = ().to_json()
inspect!(r, content="Null")
}
///|
test "Bool::to_json false" {
inspect!(false.to_json(), content="False")
}
///|
test "test UInt64::to_json" {
let num : UInt64 = UInt64::default()
let json = num.to_json()
inspect!(json, content="String(\"0\")")
}
///|
test "escape control characters" {
let str = "abc\x01def" // Control character with code 1
let json = str.to_json()
inspect!(
json,
content=
#|String("abc\x01def")
,
)
}
///|
test "test carriage return and backspace" {
let test_string = "CR\rBS\b"
let json = test_string.to_json()
inspect!(
json,
content=
#|String("CR\rBS\b")
,
)
}
///|
test "test form feed" {
let test_string = "Form\u000CFeed"
let json = test_string.to_json()
inspect!(
json,
content=
#|String("Form\x0cFeed")
,
)
}
///|
test "to_json BigInt" {
let n = BigInt::from_int(42)
inspect!(@builtin.BigInt::to_json(n), content="String(\"42\")")
}
///|
test "Result to_json with Err value" {
let err : Result[Int, String] = Err("error")
inspect!(err.to_json(), content="Object({\"Err\": String(\"error\")})")
}
///|
test "Bool::to_json true" {
let result = true.to_json()
assert_eq!(result, Json::True)
}
///|
test "to_hex_digit" {
let str = "\n\r\b\t\x0C\x00"
let escaped = str.to_json().as_string().unwrap()
inspect!(escaped, content="\n\r\b\t\x0c\x00")
}
///|
test "Char::to_json converts char to JSON string" {
let c = 'A'
let json = c.to_json()
inspect!(json, content="String(\"A\")")
}