shithub: mc

ref: 6c385887e98469bf92e40f435d2167ac613984ae
dir: /lib/json/test/parse.myr/

View raw version
use std
use json
use testr

const main = {
	testr.run([
		[.name="null", .fn={ctx
			var j = std.try(json.parse("null"))
			match j
			| &(`json.Null):	std.put("ok\n")
			| val:	testr.fail(ctx, "wrong value {}", val)
			;;
			json.free(j)
		}],
		[.name="bool", .fn={ctx
			var j = std.try(json.parse("true"))
			match j
			| &(`json.Bool true):	std.put("ok\n")
			| val:	testr.fail(ctx, "wrong value {}", val)
			;;
			j = std.try(json.parse("false"))
			match j
			| &(`json.Bool false):	std.put("ok\n")
			| val:	testr.fail(ctx, "wrong value {}", val)
			;;
			json.free(j)
		}],
		[.name="num", .fn={ctx
			var j = std.try(json.parse("123"))
			match j
			| &(`json.Num 123.0):	std.put("ok\n")
			| val:	testr.fail(ctx, "wrong value {}", val)
			;;
			json.free(j)
		}],
		[.name="str", .fn={ctx
			var j = std.try(json.parse("\"some str\""))
			match j
			| &(`json.Str "some str"):	std.put("ok\n")
			| val:	testr.fail(ctx, "wrong value {}", val)
			;;
			json.free(j)
		}],
		[.name="arr", .fn={ctx
			var j = std.try(json.parse("[\"some str\", 123, false]"))
			match j
			| &(`json.Arr a):
				match a[0]
				| &(`json.Str "some str"):	std.put("ok\n")
				| val:	testr.fail(ctx, "wrong value {}", val)
				;;
				match a[1]
				| &(`json.Num 123.0):	std.put("ok\n")
				| val:	testr.fail(ctx, "wrong value {}", val)
				;;
				match a[2]
				| &(`json.Bool false):	std.put("ok\n")
				| val:	testr.fail(ctx, "wrong value {}", val)
				;;
			| val:	testr.fail(ctx, "wrong value {}", val)
			;;
			json.free(j)
		}],
		[.name="obj", .fn={ctx
			var j = std.try(json.parse("{\"key\": 123, \"another\": \"foo\"}"))
			match j
			| &(`json.Obj a):
				match a[0]
				| ("key", &(`json.Num 123.0)):
					std.put("ok\n")
				| val:
					testr.fail(ctx, "wrong value {}", val)
				;;
				match a[1]
				| ("another", &(`json.Str "foo")):
					std.put("ok\n")
				| val:
					testr.fail(ctx, "wrong value {}", val)
				;;
			| val:
				testr.fail(ctx, "wrong value {}", val)
			;;
			json.free(j)
		}],
	][:])
}