Skip to content

Commit fa1d806

Browse files
test(json, builtin, debug): use Json(...) in test blocks
Replace `Json::boolean(...)` and `Json::string(...)` with the `Json(value)` constructor inside `test { ... }` blocks. Bool and String implement `ToJson`, so the produced values are identical and no test expectations change. Scope is limited to test blocks: library code (e.g. the parser builders in `json/parse.mbt` and the `ToJson` impls in `builtin`), doc examples, and test-support helpers defined outside `test` blocks (QuickCheck generators/shrinkers, bench fixtures) are left untouched. Co-Authored-By: SeekMoon <seekmoon@moonbitlang.com>
1 parent 3d826be commit fa1d806

9 files changed

Lines changed: 62 additions & 74 deletions

builtin/json_test.mbt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,26 @@ test "Json number equality" {
3838

3939
///|
4040
test "Json string equality" {
41-
let a = Json::string("hi")
42-
let b = Json::string("hi")
43-
let c = Json::string("bye")
41+
let a = Json("hi")
42+
let b = Json("hi")
43+
let c = Json("bye")
4444
debug_inspect(a == b, content="true")
4545
debug_inspect(a == c, content="false")
4646
}
4747

4848
///|
4949
test "Json array equality" {
50-
let a = Json::array([Json::number(1), Json::string("x")])
51-
let b = Json::array([Json::number(1), Json::string("x")])
50+
let a = Json::array([Json::number(1), Json("x")])
51+
let b = Json::array([Json::number(1), Json("x")])
5252
let c = Json::array([Json::number(2)])
5353
debug_inspect(a == b, content="true")
5454
debug_inspect(a == c, content="false")
5555
}
5656

5757
///|
5858
test "Json object equality" {
59-
let a = Json::object({ "x": Json::number(1), "y": Json::string("a") })
60-
let b = Json::object({ "x": Json::number(1), "y": Json::string("a") })
59+
let a = Json::object({ "x": Json::number(1), "y": Json("a") })
60+
let b = Json::object({ "x": Json::number(1), "y": Json("a") })
6161
let c = Json::object({ "x": Json::number(2) })
6262
debug_inspect(a == b, content="true")
6363
debug_inspect(a == c, content="false")
@@ -66,7 +66,7 @@ test "Json object equality" {
6666
///|
6767
test "Json equality mismatch" {
6868
let a = Json::null()
69-
let b = Json::string("x")
69+
let b = Json("x")
7070
debug_inspect(a == b, content="false")
7171
}
7272

@@ -214,13 +214,13 @@ test "Result to_json with Err value" {
214214
///|
215215
test "Bool::to_json true" {
216216
let result = true.to_json()
217-
@test.assert_eq(result, Json::boolean(true))
217+
@test.assert_eq(result, Json(true))
218218
}
219219

220220
///|
221221
test "to_hex_digit" {
222222
let str = "\n\r\b\t\u{0C}\u{00}"
223-
guard! Json::string(str) is String(escaped)
223+
guard! Json(str) is String(escaped)
224224
@test.assert_eq(escaped, "\n\r\b\t\u{0c}\u{00}")
225225
}
226226

debug/debug_test.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test "inspect" {
3131
///|
3232
test "debug repr for json" {
3333
@debug.debug_inspect(Json::null(), content="Null")
34-
@debug.debug_inspect(Json::boolean(true), content="True")
34+
@debug.debug_inspect(Json(true), content="True")
3535
@debug.debug_inspect(
3636
Json::number(@double.infinity, repr="1e999999999"),
3737
content=(

json/escape_quickcheck_wbtest.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ test "quickcheck: write_escaped matches the per-code-unit model" {
123123
test "quickcheck: stringify/parse roundtrip on adversarial strings" {
124124
@quickcheck.check(count=300, (input : (Array[Int], Bool)) => {
125125
let (seeds, escape_slash) = input
126-
let json = Json::string(adversarial_string(seeds, allow_surrogates=false))
126+
let json = Json(adversarial_string(seeds, allow_surrogates=false))
127127
parse(json.stringify(escape_slash~)) == json
128128
})
129129
}

json/from_json_test.mbt

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ test {
3030

3131
///|
3232
test {
33-
let u = Json::array([Json::number(1), Json::string("str")])
33+
let u = Json::array([Json::number(1), Json("str")])
3434
let err = expect_json_decode_error(
3535
() => ignore((@json.from_json(u) : Array[Int])),
3636
"expected Array[Int] decode failure",
@@ -47,7 +47,7 @@ test {
4747
test {
4848
let u = Json::object({
4949
"x": Json::object({ "xx": Json::number(1) }),
50-
"y": Json::object({ "yy": Json::string("str") }),
50+
"y": Json::object({ "yy": Json("str") }),
5151
})
5252
let err = expect_json_decode_error(
5353
() => ignore((@json.from_json(u) : Map[String, Map[String, Double]])),
@@ -393,22 +393,22 @@ test "bigint" {
393393

394394
///|
395395
test "jsonvalue" {
396-
let u = Json::string("str")
396+
let u = Json("str")
397397
let v : Json = @json.from_json(u)
398398
debug_inspect(v, content="String(\"str\")")
399399
let u = Json::number(123)
400400
let v : Json = @json.from_json(u)
401401
debug_inspect(v, content="Number(123)")
402-
let u = Json::boolean(true)
402+
let u = Json(true)
403403
let v : Json = @json.from_json(u)
404404
debug_inspect(v, content="True")
405-
let u = Json::boolean(false)
405+
let u = Json(false)
406406
let v : Json = @json.from_json(u)
407407
debug_inspect(v, content="False")
408408
let u = null
409409
let v : Json = @json.from_json(u)
410410
debug_inspect(v, content="Null")
411-
let u = Json::array([Json::number(1), Json::string("str")])
411+
let u = Json::array([Json::number(1), Json("str")])
412412
let v : Json = @json.from_json(u)
413413
debug_inspect(
414414
v,
@@ -636,7 +636,7 @@ test "Bool from_json error handling" {
636636
#|JsonDecodeError((Root, "Bool::from_json: expected boolean"))
637637
),
638638
)
639-
let json_string = Json::string("true")
639+
let json_string = Json("true")
640640
let err = expect_json_decode_error(
641641
() => ignore((@json.from_json(json_string) : Bool)),
642642
"expected Bool decode failure",
@@ -662,7 +662,7 @@ test "Int64 from_json error handling" {
662662
#|JsonDecodeError((Root, "Int64::from_json: expected number in string representation"))
663663
),
664664
)
665-
let json_invalid_string = Json::string("not a number")
665+
let json_invalid_string = Json("not a number")
666666
let err = expect_json_decode_error(
667667
() => ignore((@json.from_json(json_invalid_string) : Int64)),
668668
"expected Int64 parse failure",
@@ -677,7 +677,7 @@ test "Int64 from_json error handling" {
677677

678678
///|
679679
test "UInt from_json error handling" {
680-
let json_string = Json::string("123")
680+
let json_string = Json("123")
681681
let err = expect_json_decode_error(
682682
() => ignore((@json.from_json(json_string) : UInt)),
683683
"expected UInt decode failure",
@@ -703,7 +703,7 @@ test "UInt64 from_json error handling" {
703703
#|JsonDecodeError((Root, "UInt64::from_json: expected number in string representation"))
704704
),
705705
)
706-
let json_invalid_string = Json::string("not a number")
706+
let json_invalid_string = Json("not a number")
707707
let err = expect_json_decode_error(
708708
() => ignore((@json.from_json(json_invalid_string) : UInt64)),
709709
"expected UInt64 parse failure",
@@ -759,7 +759,7 @@ test "Char from_json error handling" {
759759
#|JsonDecodeError((Root, "Char::from_json: expected string"))
760760
),
761761
)
762-
let json_empty_string = Json::string("")
762+
let json_empty_string = Json("")
763763
let err = expect_json_decode_error(
764764
() => ignore((@json.from_json(json_empty_string) : Char)),
765765
"expected Char decode failure",
@@ -770,7 +770,7 @@ test "Char from_json error handling" {
770770
#|JsonDecodeError((Root, "Char::from_json: expected single character"))
771771
),
772772
)
773-
let json_long_string = Json::string("abc")
773+
let json_long_string = Json("abc")
774774
let err = expect_json_decode_error(
775775
() => ignore((@json.from_json(json_long_string) : Char)),
776776
"expected Char decode failure",
@@ -796,7 +796,7 @@ test "BigInt from_json error handling" {
796796
#|JsonDecodeError((Root, "BigInt::from_json: expected number in string representation"))
797797
),
798798
)
799-
let json_invalid_number = Json::string("not-a-number")
799+
let json_invalid_number = Json("not-a-number")
800800
let err = expect_json_decode_error(
801801
() => ignore((@json.from_json(json_invalid_number) : BigInt)),
802802
"expected invalid BigInt decode failure",
@@ -811,7 +811,7 @@ test "BigInt from_json error handling" {
811811

812812
///|
813813
test "Array from_json error handling" {
814-
let json_string = Json::string("not an array")
814+
let json_string = Json("not an array")
815815
let err = expect_json_decode_error(
816816
() => ignore((@json.from_json(json_string) : Array[Int])),
817817
"expected Array decode failure",
@@ -826,7 +826,7 @@ test "Array from_json error handling" {
826826

827827
///|
828828
test "FixedArray from_json error handling" {
829-
let json_string = Json::string("not an array")
829+
let json_string = Json("not an array")
830830
let err = expect_json_decode_error(
831831
() => ignore((@json.from_json(json_string) : FixedArray[Int])),
832832
"expected FixedArray decode failure",
@@ -856,7 +856,7 @@ test "Map from_json error handling" {
856856

857857
///|
858858
test "Option from_json error handling" {
859-
let json_string = Json::string("not an option")
859+
let json_string = Json("not an option")
860860
let err = expect_json_decode_error(
861861
() => ignore((@json.from_json(json_string) : Int?)),
862862
"expected Option decode failure",
@@ -871,7 +871,7 @@ test "Option from_json error handling" {
871871

872872
///|
873873
test "Result from_json error handling" {
874-
let json_string = Json::string("not a result")
874+
let json_string = Json("not a result")
875875
let err = expect_json_decode_error(
876876
() => ignore((@json.from_json(json_string) : Result[Int, String])),
877877
"expected Result decode failure",
@@ -908,7 +908,7 @@ test "Result from_json error handling" {
908908

909909
///|
910910
test "Unit from_json error handling" {
911-
let json_string = Json::string("not null")
911+
let json_string = Json("not null")
912912
let err = expect_json_decode_error(
913913
() => ignore((@json.from_json(json_string) : Unit)),
914914
"expected Unit decode failure",
@@ -950,17 +950,17 @@ test "float roundtrip" {
950950
///|
951951
test "float special values" {
952952
// Test NaN
953-
let u = Json::string("NaN")
953+
let u = Json("NaN")
954954
let v : Float = @json.from_json(u)
955955
inspect(v, content="NaN")
956956

957957
// Test Infinity
958-
let u = Json::string("Infinity")
958+
let u = Json("Infinity")
959959
let v : Float = @json.from_json(u)
960960
inspect(v, content="Infinity")
961961

962962
// Test -Infinity
963-
let u = Json::string("-Infinity")
963+
let u = Json("-Infinity")
964964
let v : Float = @json.from_json(u)
965965
inspect(v, content="-Infinity")
966966

@@ -972,7 +972,7 @@ test "float special values" {
972972

973973
///|
974974
test "Float from_json error handling" {
975-
let json_string = Json::string("not a number")
975+
let json_string = Json("not a number")
976976
let err = expect_json_decode_error(
977977
() => ignore((@json.from_json(json_string) : Float)),
978978
"expected Float decode failure",
@@ -1032,14 +1032,14 @@ test "ArrayView::from_json" {
10321032

10331033
///|
10341034
test "from_json special cases" {
1035-
let nan_value : Double = @json.from_json(Json::string("NaN"))
1035+
let nan_value : Double = @json.from_json(Json("NaN"))
10361036
@json.json_inspect(nan_value, content="NaN")
1037-
let inf_value : Double = @json.from_json(Json::string("Infinity"))
1037+
let inf_value : Double = @json.from_json(Json("Infinity"))
10381038
@json.json_inspect(inf_value, content="Infinity")
1039-
let neg_inf_value : Double = @json.from_json(Json::string("-Infinity"))
1039+
let neg_inf_value : Double = @json.from_json(Json("-Infinity"))
10401040
@json.json_inspect(neg_inf_value, content="-Infinity")
10411041
let array_view_err = expect_json_decode_error(
1042-
() => ignore((@json.from_json(Json::string("oops")) : ArrayView[Int])),
1042+
() => ignore((@json.from_json(Json("oops")) : ArrayView[Int])),
10431043
"expected ArrayView::from_json error",
10441044
)
10451045
guard array_view_err
@@ -1055,15 +1055,15 @@ test "from_json special cases" {
10551055
fail("expected Bytes::from_json type error")
10561056
}
10571057
let bytes_escape_err = expect_json_decode_error(
1058-
() => ignore((@json.from_json(Json::string("bad\\\\")) : Bytes)),
1058+
() => ignore((@json.from_json(Json("bad\\\\")) : Bytes)),
10591059
"expected Bytes::from_json escape error",
10601060
)
10611061
guard bytes_escape_err
10621062
is JsonDecodeError((_, "Bytes::from_json: invalid escape sequence")) else {
10631063
fail("expected Bytes::from_json escape error")
10641064
}
10651065
let bytes_invalid_err = expect_json_decode_error(
1066-
() => ignore((@json.from_json(Json::string("\u{0}")) : Bytes)),
1066+
() => ignore((@json.from_json(Json("\u{0}")) : Bytes)),
10671067
"expected Bytes::from_json byte error",
10681068
)
10691069
guard bytes_invalid_err

json/json_test.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,13 @@ test "string from and to json round trip" {
315315
test "escape" {
316316
let s = "http://example.com/"
317317
inspect(
318-
Json::string(s).stringify(escape_slash=true),
318+
Json(s).stringify(escape_slash=true),
319319
content=(
320320
#|"http:\/\/example.com\/"
321321
),
322322
)
323323
inspect(
324-
Json::string(s).stringify(),
324+
Json(s).stringify(),
325325
content=(
326326
#|"http://example.com/"
327327
),

json/json_traverse_test.mbt

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,10 @@ fn Json::prune_loc_test(self : Json) -> Json {
9090
test "prune_loc - primitive values" {
9191
// Null, True, False, Number, String should be preserved as-is
9292
debug_inspect(null.prune_loc(), content="Some(Null)")
93-
debug_inspect(Json::boolean(true).prune_loc(), content="Some(True)")
94-
debug_inspect(Json::boolean(false).prune_loc(), content="Some(False)")
93+
debug_inspect(Json(true).prune_loc(), content="Some(True)")
94+
debug_inspect(Json(false).prune_loc(), content="Some(False)")
9595
debug_inspect(Json::number(42.0).prune_loc(), content="Some(Number(42))")
96-
debug_inspect(
97-
Json::string("hello").prune_loc(),
98-
content="Some(String(\"hello\"))",
99-
)
96+
debug_inspect(Json("hello").prune_loc(), content="Some(String(\"hello\"))")
10097
}
10198

10299
///|
@@ -105,12 +102,7 @@ test "prune_loc - arrays" {
105102
debug_inspect(Json::array([]).prune_loc(), content="Some(Array([]))")
106103

107104
// Array with primitives
108-
let arr1 = Json::array([
109-
null,
110-
Json::boolean(true),
111-
Json::number(123.0),
112-
Json::string("test"),
113-
])
105+
let arr1 = Json::array([null, Json(true), Json::number(123.0), Json("test")])
114106
@json.json_inspect(
115107
arr1.prune_loc_test(),
116108
content=[null, true, 123, "test"], // FIXME: support null
@@ -127,7 +119,7 @@ test "prune_loc - objects without loc" {
127119
debug_inspect(Json::object(Map([])).prune_loc(), content="None")
128120

129121
// Object with no "loc" key
130-
let obj1 : Json = { "name": Json::string("John"), "age": Json::number(30.0) }
122+
let obj1 : Json = { "name": Json("John"), "age": Json::number(30.0) }
131123
@json.json_inspect(obj1.prune_loc(), content=[{ "name": "John", "age": 30 }])
132124
}
133125

@@ -144,8 +136,8 @@ test "prune_loc - objects with loc key" {
144136

145137
// Object with "loc" and other keys - "loc" should be removed
146138
let obj_with_loc : Json = {
147-
"type": Json::string("function"),
148-
"name": Json::string("myFunc"),
139+
"type": Json("function"),
140+
"name": Json("myFunc"),
149141
"loc": Json::object({
150142
"line": Json::number(10.0),
151143
"column": Json::number(5.0),
@@ -160,15 +152,13 @@ test "prune_loc - objects with loc key" {
160152
test "prune_loc - nested objects" {
161153
// Nested object where inner object becomes empty after pruning
162154
let nested1 : Json = {
163-
"outer": Json::object({ "loc": Json::string("should be removed") }),
155+
"outer": Json::object({ "loc": Json("should be removed") }),
164156
"value": Json::number(42.0),
165157
}
166158
@json.json_inspect(nested1.prune_loc(), content=[{ "value": 42 }])
167159

168160
// Nested object where all objects are pruned
169-
let nested2 : Json = {
170-
"inner": Json::object({ "loc": Json::string("remove me") }),
171-
}
161+
let nested2 : Json = { "inner": Json::object({ "loc": Json("remove me") }) }
172162
@json.json_inspect(
173163
nested2.prune_loc(),
174164
content=null, // FIXME: None -> Null

0 commit comments

Comments
 (0)