test: Add suite for DSL parsing library

This commit is contained in:
Ethan P 2020-04-17 02:25:33 -07:00
parent c5927f72af
commit 9a92579100
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
2 changed files with 101 additions and 1 deletions

View File

@ -48,7 +48,7 @@ dsl_parse() {
# Parse the indentation.
# If the indentation is greater than zero, it's considered an option.
[[ "$line_raw" =~ ^( |[[:space:]]{2,}) ]]
[[ "$line_raw" =~ ^( |[[:space:]]{2,}) ]] || true
indent="${BASH_REMATCH[1]}"
line="${line_raw:${#indent}}"
@ -77,6 +77,8 @@ dsl_parse() {
if [[ -n "$DSL_COMMAND" ]]; then
dsl_on_command_commit
fi
return 0
}
# Parses a line into fields.

98
test/suite/lib_dsl.sh Normal file
View File

@ -0,0 +1,98 @@
setup() {
source "${LIB}/dsl.sh"
}
# Expect functions.
expect_dsl_command() {
EXPECTED_DSL_ARGS=("$@")
CALLED="not called"
dsl_on_command() {
expect_equal "$# args" "${#EXPECTED_DSL_ARGS[@]} args"
CALLED="called"
local arg
local i=0
for arg in "$@"; do
expect_equal "$arg" "${EXPECTED_DSL_ARGS[$i]}"
((i++)) || true
done
}
dsl_parse
expect_equal "$CALLED" "called"
}
expect_dsl_option() {
EXPECTED_DSL_ARGS=("$@")
CALLED="not called"
dsl_on_option() {
expect_equal "$# args" "${#EXPECTED_DSL_ARGS[@]} args"
CALLED="called"
local arg
local i=0
for arg in "$@"; do
expect_equal "$arg" "${EXPECTED_DSL_ARGS[$i]}"
((i++)) || true
done
}
dsl_parse
expect_equal "$CALLED" "called"
}
# Stub methods.
dsl_on_command() {
:
}
dsl_on_command_commit() {
:
}
dsl_on_option() {
:
}
# Test cases.
test:parse_command() {
description "Parses a DSL command."
expect_dsl_command "my-command" <<-EOF
my-command
EOF
}
test:parse_simple_args() {
description "Parses a DSL command with simple args"
expect_dsl_command "my-command" "arg1" "arg2" "arg3" <<-EOF
my-command arg1 arg2 arg3
EOF
}
test:parse_quoted_args() {
description "Parses a DSL command with quoted args"
expect_dsl_command "my-command" "arg 1" "" "arg3" <<-EOF
my-command "arg 1" "" "arg3
EOF
}
test:parse_escaped_args() {
description "Parses a DSL command with escaped args"
# Note: Bash will escape the \\ into \. It's only doubled in this heredoc.
expect_dsl_command "my-command" "arg\"1" "arg 2" "arg\\3" <<-EOF
my-command arg\"1 arg\ 2 arg\\\\3
EOF
}
test:parse_option() {
description "Parses a DSL option with simple arguments"
expect_dsl_option "my-option" "1" "2" "3" <<-EOF
my-command
my-option 1 2 3
EOF
}