aboutsummaryrefslogtreecommitdiff
path: root/tests/core/encoding
diff options
context:
space:
mode:
authorDaniel Gavin <danielgavin5@hotmail.com>2021-11-07 14:35:52 +0100
committerDaniel Gavin <danielgavin5@hotmail.com>2021-11-07 14:35:52 +0100
commit5b074ceee519a97b13d71aa0257defcd04cc4a63 (patch)
treeaf744a9bbca0e6a495761d6754029c9191200c39 /tests/core/encoding
parent40eed2952787415d90d403f11452850286e8c574 (diff)
Add json encoding test + fix enum not being set on success.
Diffstat (limited to 'tests/core/encoding')
-rw-r--r--tests/core/encoding/test_core_json.odin90
1 files changed, 90 insertions, 0 deletions
diff --git a/tests/core/encoding/test_core_json.odin b/tests/core/encoding/test_core_json.odin
new file mode 100644
index 000000000..f536eb4c6
--- /dev/null
+++ b/tests/core/encoding/test_core_json.odin
@@ -0,0 +1,90 @@
+package test_core_json
+
+import "core:encoding/json"
+import "core:testing"
+import "core:fmt"
+
+TEST_count := 0
+TEST_fail := 0
+
+when ODIN_TEST {
+ expect :: testing.expect
+ log :: testing.log
+} else {
+ expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
+ fmt.printf("[%v] ", loc)
+ TEST_count += 1
+ if !condition {
+ TEST_fail += 1
+ fmt.println(message)
+ return
+ }
+ fmt.println(" PASS")
+ }
+ log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
+ fmt.printf("[%v] ", loc)
+ fmt.printf("log: %v\n", v)
+ }
+}
+
+main :: proc() {
+ t := testing.T{}
+
+ parse_json(&t)
+ marshal_json(&t)
+
+ fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
+}
+
+@test
+parse_json :: proc(t: ^testing.T) {
+
+ json_data := `
+ {
+ "firstName": "John",
+ "lastName": "Smith",
+ "isAlive": true,
+ "age": 27,
+ "address": {
+ "streetAddress": "21 2nd Street",
+ "city": "New York",
+ "state": "NY",
+ "postalCode": "10021-3100"
+ },
+ "phoneNumbers": [
+ {
+ "type": "home",
+ "number": "212 555-1234"
+ },
+ {
+ "type": "office",
+ "number": "646 555-4567"
+ }
+ ],
+ "children": [],
+ "spouse": null
+ }
+ `
+
+ _, err := json.parse(transmute([]u8)json_data)
+
+ expect(t, err == .None, "expected json error to be none")
+}
+
+@test
+marshal_json :: proc(t: ^testing.T) {
+
+ My_Struct :: struct {
+ a: int,
+ b: int,
+ }
+
+ my_struct := My_Struct {
+ a = 2,
+ b = 5,
+ }
+
+ _, err := json.marshal(my_struct)
+
+ expect(t, err == .None, "expected json error to be none")
+}