Skip to content

Commit

Permalink
chore(sdk): add missing tests for Json (winglang#4399)
Browse files Browse the repository at this point in the history
Adding missing `Json` tests for tracking issue winglang#2785

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
garysassano authored Oct 9, 2023
1 parent 8b6f604 commit cb2f031
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 54 deletions.
94 changes: 74 additions & 20 deletions examples/tests/sdk_tests/std/json.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
//-----------------------------------------------------------------------------
bring cloud;

test "has()" {
let obj = Json { key1: 1, key2: 2};

assert(Json.has(obj, "key1") == true);
assert(Json.has(obj, "key3") == false);
}

test "get()" {
let assertThrows = (expected: str, block: (): void) => {
let var error = false;
Expand Down Expand Up @@ -104,24 +111,71 @@ test "keys(), values(), entries()" {
}
}

//-----------------------------------------------------------------------------
// tryParse()
assert(Json.tryParse(nil) == nil);
assert(Json.tryParse("boom") == nil);
assert(Json.tryParse("") == nil);

/*
Will add test later:
test "setWithNonMutJsonObject()" {
let var error = "";
try {
let f = MutJson { e: 4 };
f.set("f", 9);
} catch e {
error = e;
}

log(error);
assert(error == "");
test "parse()" {
let obj = Json { key1: 1, key2: 2};
let String = "{\"key\":1,\"key2\":2}";

assert(Json.parse("123") == 123);
assert(Json.parse("true") == true);
assert(Json.parse("\"foo\"") == "foo");
/**
* To be fixed in the compiler
* see https://github.com/winglang/wing/issues/4131
*/
// assert(Json.parse("[1,5,false]") == "[1,5,false]");
// assert(Json.parse(String) == obj);
/**
* To uncomment once Json null is implemented
* see https://github.com/winglang/wing/issues/1819
*/
// assert(Json.parse("null") == null);
}

test "tryParse()" {
let obj = Json { key1: 1, key2: 2};
let String = "{\"key\":1,\"key2\":2}";

assert(Json.tryParse("123") == 123);
assert(Json.tryParse("true") == true);
assert(Json.tryParse("\"foo\"") == "foo");
/**
* To be fixed in the compiler
* see https://github.com/winglang/wing/issues/4131
*/
// assert(Json.tryParse("[1,5,false]") == "[1,5,false]");
// assert(Json.tryParse(String) == obj);
/**
* To uncomment once Json null is implemented
* see https://github.com/winglang/wing/issues/1819
*/
// assert(Json.tryParse("null") == null);
assert(Json.tryParse("foo") == nil);
assert(Json.tryParse("") == nil);
assert(Json.tryParse(nil) == nil);
}
*/

test "deepCopy(), deepCopyMut()" {
let original = Json ({
"string": "wing",
"number": 123,
"array": [1, 2, 3],
"true": true,
"false": false,
"object": {
"key1": "value1",
"key2": 2,
"key3": false,
"key5": [3, 2, 1]
}
});
let mutation = Json{ key1: 1, key2: 2};

let copy = Json.deepCopy(original);
let copyMut = Json.deepCopyMut(original);

assert(copy == copyMut);
copyMut.set("object", mutation);
assert(copy != copyMut);

assert(copyMut.get("object") == mutation);
}
Loading

0 comments on commit cb2f031

Please sign in to comment.