Skip to content

Commit

Permalink
Test get tw config value with assert
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Jul 24, 2024
1 parent 4d56ecb commit 7a60f7c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/include/assert.sh
Original file line number Diff line number Diff line change
@@ -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
}

14 changes: 13 additions & 1 deletion lib/include/tw_config.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

source lib/include/assert.sh

twcfg_line=0
twcfg_last_line="firstline"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 7a60f7c

Please sign in to comment.