From ceb45b67aab75f215cb0cca3415acfe4d52c99b4 Mon Sep 17 00:00:00 2001 From: zihang Date: Wed, 8 Jan 2025 17:16:59 +0800 Subject: [PATCH] test: record from/to json behavior --- json/from_json.mbt | 2 +- json/from_json_test.mbt | 50 ++++++++++++++++++++++++++++++++++++++++- json/to_json_test.mbt | 40 +++++++++++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 4 deletions(-) diff --git a/json/from_json.mbt b/json/from_json.mbt index 390f2e407..63d7e92a4 100644 --- a/json/from_json.mbt +++ b/json/from_json.mbt @@ -1,4 +1,4 @@ -// Copyright 2024 International Digital Economy Academy +// 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. diff --git a/json/from_json_test.mbt b/json/from_json_test.mbt index e6c20b03d..650cc3b1b 100644 --- a/json/from_json_test.mbt +++ b/json/from_json_test.mbt @@ -1,4 +1,4 @@ -// Copyright 2024 International Digital Economy Academy +// 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. @@ -382,3 +382,51 @@ test "jsonvalue" { , ) } + +test "int roundtrip" { + let u = (123).to_json() + let v : Int = @json.from_json!(u) + inspect!(v, content="123") + let u = (-123).to_json() + let v : Int = @json.from_json!(u) + inspect!(v, content="-123") +} + +test "uint roundtrip" { + let u = 123U.to_json() + let v : UInt = @json.from_json!(u) + inspect!(v, content="123") + let u = 4294967295U.to_json() + let v : UInt = @json.from_json!(u) + inspect!(v, content="4294967295") +} + +test "uint" { + let v : UInt = @json.from_json!(Number(4294967295)) + inspect!(v, content="4294967295") + let v : UInt = @json.from_json!(Number(-1)) + inspect!(v, content="0") + let v : UInt = @json.from_json!(Number(4294967296)) + inspect!(v, content="4294967295") +} + +test "int64 roundtrip" { + let u = 123L.to_json() + let v : Int64 = @json.from_json!(u) + inspect!(v, content="123") + let u = 9223372036854775807L.to_json() + let v : Int64 = @json.from_json!(u) + inspect!(v, content="9223372036854775807") + let u = (-9223372036854775808L).to_json() + let v : Int64 = @json.from_json!(u) + inspect!(v, content="-9223372036854775808") +} + +test "uint64 roundtrip" { + let u = 123UL.to_json() + let v : UInt64 = @json.from_json!(u) + inspect!(v, content="123") + let u = 18446744073709551615UL.to_json() + let v : UInt64 = @json.from_json!(u) + inspect!(v, content="18446744073709551615") +} diff --git a/json/to_json_test.mbt b/json/to_json_test.mbt index ba956f09c..0288a7555 100644 --- a/json/to_json_test.mbt +++ b/json/to_json_test.mbt @@ -1,4 +1,4 @@ -// Copyright 2024 International Digital Economy Academy +// 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. @@ -21,8 +21,44 @@ test "Int::to_json" { inspect!((42).to_json(), content="Number(42)") } +test "UInt::to_json" { + inspect!(42U.to_json(), content="Number(42)") +} + test "Int64::to_json" { - inspect!(42L.to_json(), content="Number(42)") + inspect!( + 42L.to_json(), + content= + #|String("42") + , + ) + inspect!( + (-9223372036854775808L).to_json(), + content= + #|String("-9223372036854775808") + , + ) + inspect!( + 9223372036854775807L.to_json(), + content= + #|String("9223372036854775807") + , + ) +} + +test "UInt64::to_json" { + inspect!( + 42UL.to_json(), + content= + #|String("42") + , + ) + inspect!( + 18446744073709551615UL.to_json(), + content= + #|String("18446744073709551615") + , + ) } test "Double::to_json" {