odin-jsonschema

Implementation of JSON schema for Odin
Log | Files | Refs | LICENSE

check.odin (1056B)


      1 package check
      2 
      3 import "core:encoding/json"
      4 import "core:testing"
      5 
      6 SAMPLE :: #load("sample.json", string)
      7 
      8 @(test)
      9 parses :: proc(t: ^testing.T) {
     10 	root: Root
     11 	err := json.unmarshal_string(SAMPLE, &root)
     12 	testing.expectf(t, err == nil, "unmarshal failed: %v", err)
     13 	// NOTE: core:encoding/json tries union variants in declaration order and
     14 	// skips unknown object keys, so the first object variant that parses wins.
     15 	circle, is_circle := root.shape.(Circle)
     16 	testing.expect(t, is_circle, "shape should parse as Circle")
     17 	testing.expect_value(t, circle.radius, 2.5)
     18 	value, is_string := root.value.(string)
     19 	testing.expect(t, is_string, "value should parse as string")
     20 	testing.expect_value(t, value, "hi")
     21 }
     22 
     23 @(test)
     24 parses_number_variant :: proc(t: ^testing.T) {
     25 	root: Root
     26 	err := json.unmarshal_string(`{ "shape": { "radius": 1 }, "value": 9.5 }`, &root)
     27 	testing.expectf(t, err == nil, "unmarshal failed: %v", err)
     28 	value, is_number := root.value.(f64)
     29 	testing.expect(t, is_number, "value should parse as f64")
     30 	testing.expect_value(t, value, 9.5)
     31 }