From 9a9257910054db76093ed62313d8a61cd9cb5cb4 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Fri, 17 Apr 2020 02:25:33 -0700 Subject: [PATCH] test: Add suite for DSL parsing library --- lib/dsl.sh | 4 +- test/suite/lib_dsl.sh | 98 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 test/suite/lib_dsl.sh diff --git a/lib/dsl.sh b/lib/dsl.sh index 77b78f6..b8e085f 100644 --- a/lib/dsl.sh +++ b/lib/dsl.sh @@ -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. diff --git a/test/suite/lib_dsl.sh b/test/suite/lib_dsl.sh new file mode 100644 index 0000000..c19c9bd --- /dev/null +++ b/test/suite/lib_dsl.sh @@ -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 +}