Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sh/jshn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,28 @@ json_add_fields() {
done
}

json_get_position() {
local __dest="$1"
eval "export -- \"$__dest=\${JSON_CUR}\"; [ -n \"\${JSON_CUR+x}\" ]"
}

json_move_to() {
local __cur="$1"
_json_set_var JSON_CUR "$cur"
}

json_get_parent_position() {
local __dest="$1" __cur __parent
_json_get_var __cur JSON_CUR
__parent="U_$__cur"
eval "export -- \"$__dest=\${$__parent}\"; [ -n \"\${$__parent+x}\" ]"
}

json_get_root_position() {
local __dest="$1" __cur="J_V"
eval "export -- \"$__dest=\${__cur}\"; [ -n \"\${__cur+x}\" ]"
}

# functions read access to json variables

json_compact() {
Expand Down
120 changes: 120 additions & 0 deletions tests/shunit2/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,124 @@ test_jshn_append_via_json_script() {
set -u
}

test_jshn_get_root_position() {
JSON_PREFIX="${JSON_PREFIX:-}"
. ../../sh/jshn.sh

# __SHUNIT_SHELL_FLAGS='u' results in 'line 6: JSON_UNSET: unbound variable' in json_cleanup()
set +u

local root

# Test getting the root position
json_init
json_add_object "obj"
json_add_array "arr"
json_get_root_position root
assertEquals "J_V" "$root"

set -u
}

test_jshn_get_parent_position() {
JSON_PREFIX="${JSON_PREFIX:-}"
. ../../sh/jshn.sh

# __SHUNIT_SHELL_FLAGS='u' results in 'line 6: JSON_UNSET: unbound variable' in json_cleanup()
set +u

local cur

json_init

json_add_object "obj"
json_add_array "arr"

local obj="$cur"

json_get_parent_position cur
assertEquals "J_T1" "$cur"

set -u
}

test_jshn_get_position() {
JSON_PREFIX="${JSON_PREFIX:-}"
. ../../sh/jshn.sh

# __SHUNIT_SHELL_FLAGS='u' results in 'line 6: JSON_UNSET: unbound variable' in json_cleanup()
set +u

local cur

# Test getting the root position
json_init
json_get_position cur
assertEquals "J_V" "$cur"

local root="$cur"

# ... and the object position
json_add_object "obj"
json_get_position cur
assertEquals "J_T1" "$cur"

local obj="$cur"

# ... and the array position
json_add_array "arr"
json_get_position cur
assertEquals "J_A2" "$cur"

local arr="$cur"

# ... still within the array
json_add_string "first" "one"
json_get_position cur
assertEquals "J_A2" "$cur"

# ... still within the array
json_add_string "second" "two"
json_get_position cur
assertEquals "J_A2" "$cur"

# ... now back to the object
json_select ..
json_get_position cur
assertEquals "$obj" "$cur"

# ... and at the root
json_select ..
json_get_position cur
assertEquals "$root" "$cur"

set -u
}

test_jshn_move_to() {
JSON_PREFIX="${JSON_PREFIX:-}"
. ../../sh/jshn.sh

# __SHUNIT_SHELL_FLAGS='u' results in 'line 6: JSON_UNSET: unbound variable' in json_cleanup()
set +u

local cur cur2

json_init
json_add_object "obj"
json_get_position cur

json_add_array "arr"
json_add_string "first" "one"
json_add_string "second" "two"

json_move_to "$cur"

json_get_position cur2

assertEquals "J_T1" "$cur2"

set -u
}

. ./shunit2/shunit2