aboutsummaryrefslogtreecommitdiff
path: root/tests/core/encoding/json
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2022-04-19 15:03:09 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2022-04-19 15:03:09 +0200
commit323e7a2d02819ef0b7137abbdfee8a0d48f5cecb (patch)
treecd342a6713485cd2accd13eedbc424874218b417 /tests/core/encoding/json
parent7654afc2db9e88b1803862310698ca047bf10a01 (diff)
Add JSON unmarshal test.
Diffstat (limited to 'tests/core/encoding/json')
-rw-r--r--tests/core/encoding/json/test_core_json.odin253
1 files changed, 253 insertions, 0 deletions
diff --git a/tests/core/encoding/json/test_core_json.odin b/tests/core/encoding/json/test_core_json.odin
index 285cc04a1..c83710352 100644
--- a/tests/core/encoding/json/test_core_json.odin
+++ b/tests/core/encoding/json/test_core_json.odin
@@ -31,6 +31,7 @@ main :: proc() {
parse_json(&t)
marshal_json(&t)
+ unmarshal_json(&t)
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
if TEST_fail > 0 {
@@ -90,3 +91,255 @@ marshal_json :: proc(t: ^testing.T) {
expect(t, err == nil, "expected json error to be none")
}
+
+PRODUCTS := `
+{
+ "cash": "0",
+ "products": [
+ {
+ "name": "Cog Cola",
+ "cost": "3",
+ "owned": "1",
+
+ "profit": "4",
+ "seconds": 3,
+ "multiplier": 1,
+ "auto_click": false
+ },
+ {
+ "name": "gingerBeer",
+ "cost": "9",
+ "owned": "0",
+
+ "profit": "16",
+ "seconds": 5,
+ "multiplier": 1,
+ "auto_click": false
+ },
+ {
+ "name": "Coffee",
+ "cost": "27",
+ "owned": "0",
+
+ "profit": "64",
+ "seconds": 7,
+ "multiplier": 1,
+ "auto_click": false
+ },
+ {
+ "name": "Haggis",
+ "cost": "81",
+ "owned": "0",
+
+ "profit": "256",
+ "seconds": 11,
+ "multiplier": 1,
+ "auto_click": false
+ },
+ {
+ "name": "Lasagna",
+ "cost": "243",
+ "owned": "0",
+
+ "profit": "1024",
+ "seconds": 13,
+ "multiplier": 1,
+ "auto_click": false
+ },
+ {
+ "name": "Asparagus",
+ "cost": "729",
+ "owned": "0",
+
+ "profit": "4096",
+ "seconds": 17,
+ "multiplier": 1,
+ "auto_click": false
+ },
+ {
+ "name": "Yorkshire Pudding",
+ "cost": "2187",
+ "owned": "0",
+
+ "profit": "16384",
+ "seconds": 19,
+ "multiplier": 1,
+ "auto_click": false
+ },
+ {
+ "name": "Salmon Wrap",
+ "cost": "6561",
+ "owned": "0",
+
+ "profit": "65536",
+ "seconds": 23,
+ "multiplier": 1,
+ "auto_click": false
+ },
+ {
+ "name": "Poke Bowl",
+ "cost": "19683",
+ "owned": "0",
+
+ "profit": "262144",
+ "seconds": 29,
+ "multiplier": 1,
+ "auto_click": false
+ },
+ {
+ "name": "Chili Con Carne",
+ "cost": "59049",
+ "owned": "0",
+
+ "profit": "1048576",
+ "seconds": 59,
+ "multiplier": 1,
+ "auto_click": false
+ },
+ ],
+}
+`
+
+original_data := Game_Marshal{
+ cash = "0",
+ products = {
+ {
+ name = "Cog Cola",
+ cost = "3",
+ owned = "1",
+ profit = "4",
+ seconds = 3,
+ multiplier = 1,
+ auto_click = false,
+ },
+ {
+ name = "gingerBeer",
+ cost = "9",
+ owned = "0",
+ profit = "16",
+ seconds = 5,
+ multiplier = 1,
+ auto_click = false,
+ },
+ {
+ name = "Coffee",
+ cost = "27",
+ owned = "0",
+ profit = "64",
+ seconds = 7,
+ multiplier = 1,
+ auto_click = false,
+ },
+ {
+ name = "Haggis",
+ cost = "81",
+ owned = "0",
+ profit = "256",
+ seconds = 11,
+ multiplier = 1,
+ auto_click = false,
+ },
+ {
+ name = "Lasagna",
+ cost = "243",
+ owned = "0",
+ profit = "1024",
+ seconds = 13,
+ multiplier = 1,
+ auto_click = false,
+ },
+ {
+ name = "Asparagus",
+ cost = "729",
+ owned = "0",
+ profit = "4096",
+ seconds = 17,
+ multiplier = 1,
+ auto_click = false,
+ },
+ {
+ name = "Yorkshire Pudding",
+ cost = "2187",
+ owned = "0",
+ profit = "16384",
+ seconds = 19,
+ multiplier = 1,
+ auto_click = false,
+ },
+ {
+ name = "Salmon Wrap",
+ cost = "6561",
+ owned = "0",
+ profit = "65536",
+ seconds = 23,
+ multiplier = 1,
+ auto_click = false,
+ },
+ {
+ name = "Poke Bowl",
+ cost = "19683",
+ owned = "0",
+ profit = "262144",
+ seconds = 29,
+ multiplier = 1,
+ auto_click = false,
+ },
+ {
+ name = "Chili Con Carne",
+ cost = "59049",
+ owned = "0",
+ profit = "1048576",
+ seconds = 59,
+ multiplier = 1,
+ auto_click = false,
+ },
+ },
+}
+
+Product_Marshal :: struct {
+ name: cstring,
+ owned: string,
+
+ cost: string,
+
+ profit: string,
+ seconds: int,
+ multiplier: int,
+
+ auto_click: bool,
+}
+
+Game_Marshal :: struct {
+ cash: string,
+ products: []Product_Marshal,
+}
+
+cleanup :: proc(g: Game_Marshal) {
+ for p in g.products {
+ delete(p.name)
+ delete(p.owned)
+ delete(p.cost)
+ delete(p.profit)
+ }
+ delete(g.products)
+ delete(g.cash)
+}
+
+@test
+unmarshal_json :: proc(t: ^testing.T) {
+ g: Game_Marshal
+ err := json.unmarshal(transmute([]u8)PRODUCTS, &g, json.DEFAULT_SPECIFICATION)
+ defer cleanup(g)
+
+ expect(t, err == nil, "Expected json error to be nil")
+
+ msg := fmt.tprintf("Expected %v products to have been unmarshaled, got %v", len(original_data.products), len(g.products))
+ expect(t, len(g.products) == len(original_data.products), msg)
+
+ msg = fmt.tprintf("Expected cash to have been unmarshaled as %v, got %v", original_data.cash, g.cash)
+ expect(t, original_data.cash == g.cash, "Cash unmarshaled improperly")
+
+ for p, i in g.products {
+ expect(t, p == original_data.products[i], "Producted unmarshaled improperly")
+ }
+} \ No newline at end of file