From 0b312918c60b724482ab557a632ee6223748762e Mon Sep 17 00:00:00 2001 From: peefy Date: Thu, 9 Nov 2023 10:09:25 +0800 Subject: [PATCH] test: add unit tests for the jsonpatch module Signed-off-by: peefy --- jsonpatch/Makefile | 2 ++ jsonpatch/kcl.mod | 2 +- jsonpatch/main.k | 27 +++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 jsonpatch/Makefile diff --git a/jsonpatch/Makefile b/jsonpatch/Makefile new file mode 100644 index 00000000..52487a99 --- /dev/null +++ b/jsonpatch/Makefile @@ -0,0 +1,2 @@ +test: + kcl -D __test__ diff --git a/jsonpatch/kcl.mod b/jsonpatch/kcl.mod index ed92e91a..d29cfed3 100644 --- a/jsonpatch/kcl.mod +++ b/jsonpatch/kcl.mod @@ -1,5 +1,5 @@ [package] name = "jsonpatch" -version = "0.0.1" +version = "0.0.2" description = "`jsonpatch` is a module for applying JSON patches (RFC 6902) for KCL values." diff --git a/jsonpatch/main.k b/jsonpatch/main.k index 207194fa..2a099dc6 100644 --- a/jsonpatch/main.k +++ b/jsonpatch/main.k @@ -117,3 +117,30 @@ make_patch = lambda src: any, dst: any -> JsonPatch { assert False, "TODO: PRs welcome" [] } + +if option("__test__"): + data = { + "firstName": "John", + "lastName": "Doe", + "age": 30, + "address": { + "streetAddress": "1234 Main St", + "city": "New York", + "state": "NY", + "postalCode": "10001" + }, + "phoneNumbers": [ + { + "type": "home", + "number": "212-555-1234" + }, + { + "type": "work", + "number": "646-555-5678" + } + ] + } + phoneNumbers0type: str = get_obj(data, "phoneNumbers/0/type") + newObj = set_obj(data, "phoneNumbers/0/type", "school") + assert phoneNumbers0type == "home" + assert newObj["phoneNumbers"][0]["type"] == "school"