Skip to content

Commit

Permalink
Merge 4294681 into 5be791a
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed Feb 15, 2020
2 parents 5be791a + 4294681 commit e0abf3c
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 4 deletions.
32 changes: 32 additions & 0 deletions lib/core/matchers/match.sh
Expand Up @@ -3,6 +3,18 @@
shellspec_syntax 'shellspec_matcher_match'

shellspec_matcher_match() {
if [ "${1:-}" = "pattern" ]; then
shift
eval shellspec_matcher_match_pattern ${1+'"$@"'}
return $?
fi

shellspec_deprecated "'match' matcher deprecated, use 'match pattern' matcher instead"
shellspec_syntax_param count [ $# -eq 1 ] || return 0
shellspec_matcher_deprecated_match "$@"
}

shellspec_matcher_deprecated_match() {
shellspec_matcher__match() {
# shellcheck disable=SC2034
SHELLSPEC_EXPECT=$1
Expand All @@ -21,3 +33,23 @@ shellspec_matcher_match() {
shellspec_syntax_param count [ $# -eq 1 ] || return 0
shellspec_matcher_do_match "$@"
}

shellspec_matcher_match_pattern() {
shellspec_matcher__match() {
# shellcheck disable=SC2034
SHELLSPEC_EXPECT=$1
[ "${SHELLSPEC_SUBJECT+x}" ] || return 1
shellspec_match_pattern "$SHELLSPEC_SUBJECT" "$1"
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected $1 to match pattern $2"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected $1 not to match pattern $2"
}

shellspec_syntax_param count [ $# -eq 1 ] || return 0
shellspec_matcher_do_match "$@"
}
16 changes: 16 additions & 0 deletions lib/general.sh
Expand Up @@ -405,10 +405,26 @@ if ! shellspec_ends_with_backslash "\\"; then
}
fi

# shellspec_match() deprecated
shellspec_match() {
[ "${2:-}" ] && eval "case \${1:-} in ($2) true ;; (*) false ;; esac &&:"
}

shellspec_match_pattern() {
[ "${2:-}" ] || return 1
shellspec_match_pattern=$2
set -- \\ \" \# \$ \& \' \( \) \; \< \> \` \~ '=' '^' "$SHELLSPEC_TAB" "$1"
while [ $# -gt 1 ]; do
shellspec_replace shellspec_match_pattern "$1" "\\$1"
shift
done
shellspec_replace shellspec_match_pattern " " '" "'
shellspec_replace shellspec_match_pattern "$SHELLSPEC_LF" '"$SHELLSPEC_LF"'
shellspec_replace shellspec_match_pattern "$SHELLSPEC_CR" '"$SHELLSPEC_CR"'
shellspec_replace shellspec_match_pattern "$SHELLSPEC_VT" '"$SHELLSPEC_VT"'
eval "case \$1 in ($shellspec_match_pattern) true ;; (*) false ;; esac &&:" 2>/dev/null
}

shellspec_join() {
IFS=" $IFS"
eval "shift; $1=\${*:-}"
Expand Down
4 changes: 2 additions & 2 deletions lib/libexec/translator.sh
Expand Up @@ -2,7 +2,7 @@

# shellcheck source=lib/libexec.sh
. "${SHELLSPEC_LIB:-./lib}/libexec.sh"
use constants trim match ends_with_backslash
use constants trim match_pattern ends_with_backslash
load grammar

initialize() {
Expand All @@ -25,7 +25,7 @@ check_filter() {
escape_one_line_syntax escaped_syntax "$1"
eval "set -- $escaped_syntax"
if [ $# -gt 0 ]; then
match "$1" "$SHELLSPEC_EXAMPLE_FILTER" && return 0
match_pattern "$1" "$SHELLSPEC_EXAMPLE_FILTER" && return 0
shift
fi
[ "$SHELLSPEC_TAG_FILTER" ] || return 1
Expand Down
45 changes: 44 additions & 1 deletion spec/core/matchers/match_spec.sh
Expand Up @@ -3,7 +3,7 @@
Describe "core/matchers/match.sh"
BeforeRun set_subject matcher_mock

Describe 'match matcher'
Describe 'match (deprecated) matcher'
Example 'example'
The value "foobarbaz" should match "foo*"
The value "foobarbaz" should not match "FOO*"
Expand Down Expand Up @@ -39,4 +39,47 @@ Describe "core/matchers/match.sh"
The stderr should equal SYNTAX_ERROR_WRONG_PARAMETER_COUNT
End
End

Describe 'match pattern matcher'
Example 'example'
The value "foobarbaz" should match pattern "foo*"
The value "foobarbaz" should not match pattern "FOO*"
End

It 'match a string containing a pattern'
subject() { %- "foobarbaz"; }
When run shellspec_matcher_match "foo*"
The status should be success
End

It 'does not match a string not containing a pattern'
subject() { %- "foobarbaz"; }
When run shellspec_matcher_match pattern "FOO*"
The status should be failure
End

It 'does not match undefined'
subject() { false; }
When run shellspec_matcher_match pattern "*"
The status should be failure
End

It 'outputs error if parameters is missing'
subject() { %- "foobarbaz"; }
When run shellspec_matcher_match
The stderr should equal SYNTAX_ERROR_WRONG_PARAMETER_COUNT
End

It 'outputs error if pattern is missing'
subject() { %- "foobarbaz"; }
When run shellspec_matcher_match pattern
The stderr should equal SYNTAX_ERROR_WRONG_PARAMETER_COUNT
End

It 'outputs error if parameters count is invalid'
subject() { %- "foobarbaz"; }
When run shellspec_matcher_match "foo" "bar"
The stderr should equal SYNTAX_ERROR_WRONG_PARAMETER_COUNT
End
End
End
32 changes: 31 additions & 1 deletion spec/general_spec.sh
Expand Up @@ -420,7 +420,7 @@ Describe "general.sh"
End
End

Describe "shellspec_match()"
Describe "shellspec_match() (deprecated)"
It 'returns success if value mactches with pattern'
When call shellspec_match foo "[fF]?*"
The status should be success
Expand All @@ -432,6 +432,36 @@ Describe "general.sh"
End
End

Describe "shellspec_match_pattern()"
It 'can use shell script pattern'
When call shellspec_match_pattern foobar "[fF]??b*"
The status should be success
End

It 'can use negative pattern'
When call shellspec_match_pattern foobar "[!fF]??b*"
The status should be failure
End

It 'can use OR'
When call shellspec_match_pattern foo "foo|bar"
The status should be success
End

It 'escapes symbol internally to avoid syntax error'
string() { echo "!\"#\$%&'()-=^~\\@\`{;+:},<.>/\_"; }
When call shellspec_match_pattern "$(string)" "$(string)"
The status should be success
End

It 'escapes spaces internally to avoid syntax error'
# Do not modify this string
string() { printf "= \b = \f = \n = \r = \t = \v = "; }
When call shellspec_match_pattern "$(string)" "$(string)"
The status should be success
End
End

Describe "shellspec_join()"
Before value=''
It 'joins arguments by space'
Expand Down

0 comments on commit e0abf3c

Please sign in to comment.