Skip to content

Commit

Permalink
Add shellspec_syntax_failure_message helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed Feb 23, 2020
1 parent a702a1b commit bf6bb0d
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 171 deletions.
32 changes: 13 additions & 19 deletions lib/core/matchers/be/empty.sh
@@ -1,4 +1,4 @@
#shellcheck shell=sh
#shellcheck shell=sh disable=SC2016

shellspec_syntax 'shellspec_matcher_be_empty_file'
shellspec_syntax 'shellspec_matcher_be_empty_directory'
Expand All @@ -10,15 +10,12 @@ shellspec_matcher_be_empty_file() {
[ -f "${SHELLSPEC_SUBJECT:-}" ] && [ ! -s "${SHELLSPEC_SUBJECT:-}" ]
}

shellspec_matcher__failure_message() {
shellspec_putsn "The specified path is not empty file"
shellspec_putsn "path: $SHELLSPEC_SUBJECT"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "The specified path is empty file"
shellspec_putsn "path: $SHELLSPEC_SUBJECT"
}
shellspec_syntax_failure_message + \
'The specified path is not empty file' \
'path: $SHELLSPEC_SUBJECT'
shellspec_syntax_failure_message - \
'The specified path is empty file' \
'path: $SHELLSPEC_SUBJECT'

shellspec_syntax_param count [ $# -eq 0 ] || return 0
shellspec_matcher_do_match
Expand Down Expand Up @@ -49,15 +46,12 @@ shellspec_matcher_be_empty_directory() {
)
}

shellspec_matcher__failure_message() {
shellspec_putsn "The specified path is not empty directory"
shellspec_putsn "path: $SHELLSPEC_SUBJECT"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "The specified path is empty directory"
shellspec_putsn "path: $SHELLSPEC_SUBJECT"
}
shellspec_syntax_failure_message + \
'The specified path is not empty directory' \
'path: $SHELLSPEC_SUBJECT'
shellspec_syntax_failure_message - \
'The specified path is empty directory' \
'path: $SHELLSPEC_SUBJECT'

shellspec_syntax_param count [ $# -eq 0 ] || return 0
shellspec_matcher_do_match
Expand Down
16 changes: 7 additions & 9 deletions lib/core/matchers/be/stat.sh
@@ -1,4 +1,4 @@
#shellcheck shell=sh
#shellcheck shell=sh disable=SC2016

shellspec_syntax 'shellspec_matcher_be_exist'
shellspec_syntax 'shellspec_matcher_be_file'
Expand All @@ -23,14 +23,12 @@ shellspec_make_file_matcher() {
shellspec_matcher__match() { \
[ $2 \"\${SHELLSPEC_SUBJECT:-}\" ]; \
}; \
shellspec_matcher__failure_message() { \
shellspec_putsn \"The specified path ${4:-${3%% *} not ${3#* }}\"; \
shellspec_putsn \"path: \$SHELLSPEC_SUBJECT\"; \
}; \
shellspec_matcher__failure_message_when_negated() { \
shellspec_putsn \"The specified path $3\"; \
shellspec_putsn \"path: \$SHELLSPEC_SUBJECT\"; \
}; \
shellspec_syntax_failure_message + \
'The specified path ${4:-${3%% *} not ${3#* }}' \
'path: \$SHELLSPEC_SUBJECT'; \
shellspec_syntax_failure_message - \
'The specified path $3' \
'path: \$SHELLSPEC_SUBJECT'; \
shellspec_syntax_param count [ \$# -eq 0 ] || return 0; \
shellspec_matcher_do_match; \
}
Expand Down
32 changes: 13 additions & 19 deletions lib/core/matchers/be/status.sh
@@ -1,4 +1,4 @@
#shellcheck shell=sh
#shellcheck shell=sh disable=SC2016

shellspec_syntax 'shellspec_matcher_be_success'
shellspec_syntax 'shellspec_matcher_be_failure'
Expand All @@ -8,15 +8,12 @@ shellspec_matcher_be_success() {
[ "${SHELLSPEC_SUBJECT:-}" = 0 ]
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected: success (zero)"
shellspec_putsn " got: failure (non-zero) [status: $1]"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected: failure (non-zero)"
shellspec_putsn " got: success (zero) [status: $1]"
}
shellspec_syntax_failure_message + \
'expected: success (zero)' \
' got: failure (non-zero) [status: $1]'
shellspec_syntax_failure_message - \
'expected: failure (non-zero)' \
' got: success (zero) [status: $1]'

shellspec_syntax_param count [ $# -eq 0 ] || return 0
shellspec_matcher_do_match
Expand All @@ -30,15 +27,12 @@ shellspec_matcher_be_failure() {
return 0
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected: failure (non-zero)"
shellspec_putsn " got: success (zero) [status: $1]"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected: success (zero)"
shellspec_putsn " got: failure (non-zero) [status: $1]"
}
shellspec_syntax_failure_message + \
'expected: failure (non-zero)' \
' got: success (zero) [status: $1]'
shellspec_syntax_failure_message - \
'expected: success (zero)' \
' got: failure (non-zero) [status: $1]'

shellspec_syntax_param count [ $# -eq 0 ] || return 0
shellspec_matcher_do_match
Expand Down
20 changes: 5 additions & 15 deletions lib/core/matchers/be/valid.sh
@@ -1,4 +1,4 @@
#shellcheck shell=sh
#shellcheck shell=sh disable=SC2016

shellspec_syntax 'shellspec_matcher_be_valid_number'
shellspec_syntax 'shellspec_matcher_be_valid_funcname'
Expand All @@ -9,13 +9,8 @@ shellspec_matcher_be_valid_number() {
shellspec_is number "${SHELLSPEC_SUBJECT:-}"
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected $1 is valid as a number"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected $1 is not valid as a number"
}
shellspec_syntax_failure_message + 'expected $1 is valid as a number'
shellspec_syntax_failure_message - 'expected $1 is not valid as a number'

shellspec_syntax_param count [ $# -eq 0 ] || return 0
shellspec_matcher_do_match
Expand All @@ -26,13 +21,8 @@ shellspec_matcher_be_valid_funcname() {
shellspec_is funcname "${SHELLSPEC_SUBJECT:-}"
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected $1 is valid as a funcname"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected $1 is not valid as a funcname"
}
shellspec_syntax_failure_message + 'expected $1 is valid as a funcname'
shellspec_syntax_failure_message - 'expected $1 is not valid as a funcname'

shellspec_syntax_param count [ $# -eq 0 ] || return 0
shellspec_matcher_do_match
Expand Down
62 changes: 25 additions & 37 deletions lib/core/matchers/be/variable.sh
@@ -1,4 +1,4 @@
#shellcheck shell=sh
#shellcheck shell=sh disable=SC2016

shellspec_syntax 'shellspec_matcher_be_defined'
shellspec_syntax 'shellspec_matcher_be_undefined'
Expand All @@ -10,15 +10,12 @@ shellspec_matcher_be_defined() {
[ ${SHELLSPEC_SUBJECT+x} ]
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected: defined (set)"
shellspec_putsn " got: $1"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected: undefined (unset)"
shellspec_putsn " got: $1"
}
shellspec_syntax_failure_message + \
'expected: defined (set)' \
' got: $1'
shellspec_syntax_failure_message - \
'expected: undefined (unset)' \
' got: $1'

shellspec_syntax_param count [ $# -eq 0 ] || return 0
shellspec_matcher_do_match
Expand All @@ -29,15 +26,12 @@ shellspec_matcher_be_undefined() {
! [ ${SHELLSPEC_SUBJECT+x} ]
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected: undefined (unset)"
shellspec_putsn " got: $1"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected: defined (set)"
shellspec_putsn " got: $1"
}
shellspec_syntax_failure_message + \
'expected: undefined (unset)' \
' got: $1'
shellspec_syntax_failure_message - \
'expected: defined (set)' \
' got: $1'

shellspec_syntax_param count [ $# -eq 0 ] || return 0
shellspec_matcher_do_match
Expand All @@ -48,15 +42,12 @@ shellspec_matcher_be_present() {
[ "${SHELLSPEC_SUBJECT:-}" ]
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected: present (non-zero length string)"
shellspec_putsn " got: $1"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected: blank (unset or zero length string)"
shellspec_putsn " got: $1"
}
shellspec_syntax_failure_message + \
'expected: present (non-zero length string)' \
' got: $1'
shellspec_syntax_failure_message - \
'expected: blank (unset or zero length string)' \
' got: $1'

shellspec_syntax_param count [ $# -eq 0 ] || return 0
shellspec_matcher_do_match
Expand All @@ -67,15 +58,12 @@ shellspec_matcher_be_blank() {
! [ "${SHELLSPEC_SUBJECT:-}" ]
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected: blank (unset or zero length string)"
shellspec_putsn " got: $1"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected: present (non-zero length string)"
shellspec_putsn " got: $1"
}
shellspec_syntax_failure_message + \
'expected: blank (unset or zero length string)' \
' got: $1'
shellspec_syntax_failure_message - \
'expected: present (non-zero length string)' \
' got: $1'

shellspec_syntax_param count [ $# -eq 0 ] || return 0
shellspec_matcher_do_match
Expand Down
11 changes: 3 additions & 8 deletions lib/core/matchers/end_with.sh
@@ -1,4 +1,4 @@
#shellcheck shell=sh
#shellcheck shell=sh disable=SC2016

shellspec_syntax 'shellspec_matcher_end_with'
shellspec_syntax_compound 'shellspec_matcher_end'
Expand All @@ -11,13 +11,8 @@ shellspec_matcher_end_with() {
return 1
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected $1 to end with $2"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected $1 not to end with $2"
}
shellspec_syntax_failure_message + 'expected $1 to end with $2'
shellspec_syntax_failure_message - 'expected $1 not to end with $2'

shellspec_syntax_param count [ $# -eq 1 ] || return 0
shellspec_matcher_do_match "$@"
Expand Down
17 changes: 7 additions & 10 deletions lib/core/matchers/equal.sh
@@ -1,4 +1,4 @@
#shellcheck shell=sh
#shellcheck shell=sh disable=SC2016

shellspec_syntax 'shellspec_matcher_equal'
shellspec_syntax_alias 'shellspec_matcher_eq' 'shellspec_matcher_equal'
Expand All @@ -11,15 +11,12 @@ shellspec_matcher_equal() {
return 0
}

shellspec_matcher__failure_message() {
shellspec_putsn "expected: $2"
shellspec_putsn " got: $1"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected: not equal $2"
shellspec_putsn " got: $1"
}
shellspec_syntax_failure_message + \
'expected: $2' \
' got: $1'
shellspec_syntax_failure_message - \
'expected: not equal $2' \
' got: $1'

shellspec_syntax_param count [ $# -eq 1 ] || return 0
shellspec_matcher_do_match "$@"
Expand Down
20 changes: 5 additions & 15 deletions lib/core/matchers/has/stat.sh
@@ -1,4 +1,4 @@
#shellcheck shell=sh
#shellcheck shell=sh disable=SC2016

shellspec_syntax 'shellspec_matcher_has_setgid'
shellspec_syntax 'shellspec_matcher_has_setuid'
Expand All @@ -8,13 +8,8 @@ shellspec_matcher_has_setgid() {
[ -g "${SHELLSPEC_SUBJECT:-}" ]
}

shellspec_matcher__failure_message() {
shellspec_putsn "The $1 not have setgid flag"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "The $1 has setgid flag"
}
shellspec_syntax_failure_message + 'The $1 not have setgid flag'
shellspec_syntax_failure_message - 'The $1 has setgid flag'

[ "${1:-}" = "flag" ] && shift
shellspec_syntax_param count [ $# -eq 0 ] || return 0
Expand All @@ -26,13 +21,8 @@ shellspec_matcher_has_setuid() {
[ -u "${SHELLSPEC_SUBJECT:-}" ]
}

shellspec_matcher__failure_message() {
shellspec_putsn "The $1 not have setuid flag"
}

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "The $1 has setuid flag"
}
shellspec_syntax_failure_message + 'The $1 not have setuid flag'
shellspec_syntax_failure_message - 'The $1 has setuid flag'

[ "${1:-}" = "flag" ] && shift
shellspec_syntax_param count [ $# -eq 0 ] || return 0
Expand Down
11 changes: 3 additions & 8 deletions lib/core/matchers/include.sh
@@ -1,4 +1,4 @@
#shellcheck shell=sh
#shellcheck shell=sh disable=SC2016

shellspec_syntax 'shellspec_matcher_include'

Expand All @@ -9,13 +9,8 @@ shellspec_matcher_include() {
shellspec_includes "$SHELLSPEC_SUBJECT" "$SHELLSPEC_EXPECT"
}

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

shellspec_matcher__failure_message_when_negated() {
shellspec_putsn "expected $1 not to include $2"
}
shellspec_syntax_failure_message + 'expected $1 to include $2'
shellspec_syntax_failure_message - 'expected $1 not to include $2'

shellspec_syntax_param count [ $# -eq 1 ] || return 0
shellspec_matcher_do_match "$@"
Expand Down

0 comments on commit bf6bb0d

Please sign in to comment.