diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2021-11-07 14:47:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-07 14:47:35 +0100 |
| commit | f84bdee1ba8aa34db164f6b620c24b84ea62ec04 (patch) | |
| tree | af744a9bbca0e6a495761d6754029c9191200c39 | |
| parent | 40eed2952787415d90d403f11452850286e8c574 (diff) | |
| parent | 5b074ceee519a97b13d71aa0257defcd04cc4a63 (diff) | |
Merge pull request #1279 from DanielGavin/fix-json
Add json encoding test + fix enum not being set on success.
| -rw-r--r-- | core/encoding/json/marshal.odin | 4 | ||||
| -rw-r--r-- | core/encoding/json/parser.odin | 7 | ||||
| -rw-r--r-- | tests/core/build.bat | 7 | ||||
| -rw-r--r-- | tests/core/encoding/test_core_json.odin | 90 |
4 files changed, 102 insertions, 6 deletions
diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin index 0c95df924..adbcb95be 100644 --- a/core/encoding/json/marshal.odin +++ b/core/encoding/json/marshal.odin @@ -18,7 +18,7 @@ Marshal_Error :: union { marshal :: proc(v: any, allocator := context.allocator) -> (data: []byte, err: Marshal_Error) { b := strings.make_builder(allocator) - defer if err != nil || data == nil { + defer if err != .None { strings.destroy_builder(&b) } @@ -27,7 +27,7 @@ marshal :: proc(v: any, allocator := context.allocator) -> (data: []byte, err: M if len(b.buf) != 0 { data = b.buf[:] } - return + return data, .None } marshal_to_builder :: proc(b: ^strings.Builder, v: any) -> Marshal_Error { diff --git a/core/encoding/json/parser.odin b/core/encoding/json/parser.odin index af32e7266..c682ec9bd 100644 --- a/core/encoding/json/parser.odin +++ b/core/encoding/json/parser.odin @@ -106,6 +106,7 @@ parse_comma :: proc(p: ^Parser) -> (do_break: bool) { } parse_value :: proc(p: ^Parser) -> (value: Value, err: Error) { + err = .None token := p.curr_token #partial switch token.kind { case .Null: @@ -175,6 +176,7 @@ parse_value :: proc(p: ^Parser) -> (value: Value, err: Error) { } parse_array :: proc(p: ^Parser) -> (value: Value, err: Error) { + err = .None expect_token(p, .Open_Bracket) or_return array: Array @@ -266,15 +268,14 @@ parse_object_body :: proc(p: ^Parser, end_token: Token_Kind) -> (obj: Object, er break } } - return + return obj, .None } parse_object :: proc(p: ^Parser) -> (value: Value, err: Error) { expect_token(p, .Open_Brace) or_return obj := parse_object_body(p, .Close_Brace) or_return expect_token(p, .Close_Brace) or_return - value = obj - return + return obj, .None } diff --git a/tests/core/build.bat b/tests/core/build.bat index e0a98eeb1..176b7f175 100644 --- a/tests/core/build.bat +++ b/tests/core/build.bat @@ -30,4 +30,9 @@ echo --- echo ---
echo Running core:crypto hash tests
echo ---
-%PATH_TO_ODIN% run crypto %COMMON%
\ No newline at end of file +%PATH_TO_ODIN% run crypto %COMMON%
+
+echo ---
+echo Running core:encoding tests
+echo ---
+%PATH_TO_ODIN% run encoding %COMMON%
\ No newline at end of file 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") +} |