diff --git a/lib/include/assert.sh b/lib/include/assert.sh new file mode 100644 index 0000000..1be9973 --- /dev/null +++ b/lib/include/assert.sh @@ -0,0 +1,18 @@ +# compare two strings and ensure they match or crash +# matching node order of actual, expected, [message] https://nodejs.org/api/assert.html#assertequalactual-expected-message +# matching rust order of actual, expexted https://users.rust-lang.org/t/assert-eq-expected-and-actual/20304/3 +# +# @param actual +# @param expected +# @param [message] +assert_eq() { + local actual="$1" + local expected="$2" + local message="${3:-}" + [ "$actual" = "$expected" ] && return + + printf 'assertion error! %s\n' "$message" 1>&2 + printf ' expected: %s\n' "$expected" 1>&2 + printf ' got: %s\n' "$actual" 1>&2 +} + diff --git a/lib/include/tw_config.sh b/lib/include/tw_config.sh index 09b1579..956bf1c 100644 --- a/lib/include/tw_config.sh +++ b/lib/include/tw_config.sh @@ -1,5 +1,7 @@ #!/bin/bash +source lib/include/assert.sh + twcfg_line=0 twcfg_last_line="firstline" @@ -205,6 +207,16 @@ function twcfg.include_exec() { done < "$config" } +# usage: get_tw_config_value LINE +# given one config value line it extracts only the value +# stripping of the config key and comments +function get_tw_config_value() { + local line="$1" + printf '%s' "$line" | cut -d' ' -f2- | xargs +} + +assert_eq "$(get_tw_config_value 'sv_name "foo"')" "foo" "simple double quotes" + function get_tw_config() { if [ "$#" != "2" ] then @@ -232,7 +244,7 @@ function get_tw_config() { then printf '%s' "$default_value" else - printf '%s' "$found_key" + get_tw_config_value "$found_key" fi }