Skip to content

Commit

Permalink
test: more test for json stringnify (moonbitlang#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
qazxcdswe123 authored Aug 15, 2024
1 parent 36d61f6 commit 8f6a887
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
3 changes: 1 addition & 2 deletions json/json.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ fn escape(str : String) -> String {
}

let buf = Buffer::new(size_hint=str.length())
for i = 0; i < str.length(); i = i + 1 {
let c = str[i]
for c in str {
match c {
'"' | '\\' | '/' => buf.write_string("\\\{c}")
'\n' => buf.write_string("\\n")
Expand Down
4 changes: 3 additions & 1 deletion json/json_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,13 @@ test "stringify escape" {
"\\": "/",
"\x1F": "\x01",
"actual": "\x00",
"escpae": "\x00",
"actual": "a,\"b\",c",
}
inspect!(
json.stringify(),
content=
#|{"\b":"\"hello\"","\n":"\n\r\t\f","\\":"\/","\u001f":"\u0001","actual":"\u0000"}
#|{"\b":"\"hello\"","\n":"\n\r\t\f","\\":"\/","\u001f":"\u0001","actual":"a,\"b\",c","escpae":"\u0000"}
,
)
match @json.parse?(json.stringify()) {
Expand Down
8 changes: 4 additions & 4 deletions json/to_json_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ test "String::to_json" {
,
)
inspect!(
"\n".to_json(),
"\n\r\b\t\\".to_json(),
content=
#|String("\\n")
#|String("\\n\\r\\b\\t\\")
,
)
inspect!(
"\\".to_json(),
"\x0c".to_json(),
content=
#|String("\\")
#|String("\\f")
,
)
}
Expand Down

0 comments on commit 8f6a887

Please sign in to comment.