summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilip Prindeville2025-11-17 17:10:40 +0000
committerÁlvaro Fernández Rojas2025-11-17 18:07:04 +0000
commit440b4e988f39ecb7371fe36fd85f96a0eef23465 (patch)
treec5dc6be1f609da0403355c900dd526f98dfa5175
parent5714bf6ae652b1503022419f9ad766c9c504454b (diff)
tests: shunit2: add dump and setters coverage
Exercise json_dump and json_add_* functions. Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com> Link: https://github.com/openwrt/libubox/pull/35 Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
-rwxr-xr-xtests/shunit2/tests.sh147
1 files changed, 147 insertions, 0 deletions
diff --git a/tests/shunit2/tests.sh b/tests/shunit2/tests.sh
index bb02fcd..c24f634 100755
--- a/tests/shunit2/tests.sh
+++ b/tests/shunit2/tests.sh
@@ -1,3 +1,5 @@
+#!/bin/bash
+
JSON_SCRIPT=tests.json
JSON_SCRIPT_BIN=${TEST_JSON_SCRIPT=:-./json_script-example}
FILE_STDOUT=tests.stdout
@@ -305,4 +307,149 @@ test_jshn_append_no_leading_space() {
assertEquals "first second" "$var"
}
+test_jshn_dump() {
+ JSON_PREFIX="${JSON_PREFIX:-}"
+ . ../../sh/jshn.sh
+
+ set +u
+
+ json_init
+
+ assertEquals '{ }' "$(json_dump)"
+
+ set -u
+}
+
+test_jshn_add_string() {
+ JSON_PREFIX="${JSON_PREFIX:-}"
+ . ../../sh/jshn.sh
+
+ set +u
+
+ json_init
+
+ json_add_string "name" "joe"
+
+ assertEquals '{ "name": "joe" }' "$(json_dump)"
+
+ set -u
+}
+
+test_jshn_add_int() {
+ JSON_PREFIX="${JSON_PREFIX:-}"
+ . ../../sh/jshn.sh
+
+ set +u
+
+ json_init
+
+ json_add_int "number" 1
+
+ assertEquals '{ "number": 1 }' "$(json_dump)"
+
+ set -u
+}
+
+test_jshn_add_boolean() {
+ JSON_PREFIX="${JSON_PREFIX:-}"
+ . ../../sh/jshn.sh
+
+ set +u
+
+ json_init
+
+ json_add_boolean "done" false
+
+ assertEquals '{ "done": false }' "$(json_dump)"
+
+ set -u
+}
+
+test_jshn_add_double() {
+ JSON_PREFIX="${JSON_PREFIX:-}"
+ . ../../sh/jshn.sh
+
+ set +u
+
+ json_init
+
+ json_add_double "power" 1.605
+
+ assertEquals '{ "power": 1.605 }' "$(json_dump)"
+
+ set -u
+}
+
+test_jshn_add_null() {
+ JSON_PREFIX="${JSON_PREFIX:-}"
+ . ../../sh/jshn.sh
+
+ set +u
+
+ json_init
+
+ json_add_null "reference"
+
+ assertEquals '{ "reference": null }' "$(json_dump)"
+
+ set -u
+}
+
+test_jshn_add_object() {
+ JSON_PREFIX="${JSON_PREFIX:-}"
+ . ../../sh/jshn.sh
+
+ set +u
+
+ json_init
+
+ json_add_object "inventory"
+
+ json_add_int "apples" 61
+ json_add_int "pears" 42
+ json_add_int "melons" 5
+
+ json_close_object # inventory
+
+ assertEquals '{ "inventory": { "apples": 61, "pears": 42, "melons": 5 } }' "$(json_dump)"
+
+ set -u
+}
+
+test_jshn_add_array() {
+ JSON_PREFIX="${JSON_PREFIX:-}"
+ . ../../sh/jshn.sh
+
+ set +u
+
+ json_init
+
+ json_add_array "interfaces"
+
+ json_add_string "" "eth0"
+ json_add_string "" "eth1"
+ json_add_string "" "eth2"
+
+ json_close_array # interfaces
+
+ assertEquals '{ "interfaces": [ "eth0", "eth1", "eth2" ] }' "$(json_dump)"
+
+ set -u
+}
+
+test_jshn_add_multi() {
+ JSON_PREFIX="${JSON_PREFIX:-}"
+ . ../../sh/jshn.sh
+
+ set +u
+
+ json_init
+
+ json_add_fields "name:string=joe" "age:int=42" "veteran:boolean=false"
+
+ assertEquals '{ "name": "joe", "age": 42, "veteran": false }' "$(json_dump)"
+
+ set -u
+}
+
. ./shunit2/shunit2