2015-01-13 11:30:24 +00:00
|
|
|
# NAME
|
|
|
|
# expect - assert a list of expected values match an actual value.
|
|
|
|
#
|
|
|
|
# SYNOPSIS
|
|
|
|
# expect <expected>...
|
|
|
|
# <condition>
|
2015-01-13 19:25:03 +00:00
|
|
|
# --to-be-false exit status is falsy
|
|
|
|
# --to-be-true exit status is truthy
|
2015-01-13 20:44:27 +00:00
|
|
|
# --to-contain <actual> values exist in <expected> list
|
|
|
|
# --to-no-contain <actual> values does not exist in <expected> list
|
2015-01-13 19:25:03 +00:00
|
|
|
# --to-equal <actual> value equals <expected> value
|
2015-01-13 11:30:24 +00:00
|
|
|
# <actual>
|
|
|
|
#
|
|
|
|
# EXAMPLE
|
|
|
|
# import plugins/fish-spec
|
|
|
|
#
|
|
|
|
# function describe_my_test
|
|
|
|
# function it_does_my_task
|
|
|
|
# set -l result (my_task $data)
|
|
|
|
# expect $result --to-equal 0
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# spec.run
|
|
|
|
#/
|
2015-01-10 04:14:02 +00:00
|
|
|
function expect
|
2015-01-13 19:05:43 +00:00
|
|
|
# Abort if last call to `expect` finished with $status 1. This allows to
|
|
|
|
# stop individual tests from running if at least one expect call fails.
|
|
|
|
if [ $status -eq 1 ]
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2015-01-13 20:44:27 +00:00
|
|
|
for i in (seq (count $argv))
|
|
|
|
if [ (echo $argv[$i] | grep '\-\-') ] # Expectation argument found
|
|
|
|
set -g condition $argv[$i]
|
|
|
|
set -g expected $argv[1..(math "$i - 1")]
|
2015-01-13 19:25:03 +00:00
|
|
|
|
2015-01-13 20:44:27 +00:00
|
|
|
# No comparison required e.g. --to-be-true
|
|
|
|
if not [ (count $argv) = $i ]
|
|
|
|
set -g actual $argv[(math "$i + 1")..-1]
|
|
|
|
end
|
|
|
|
|
|
|
|
break
|
|
|
|
end
|
2015-01-13 19:25:03 +00:00
|
|
|
end
|
|
|
|
|
2015-01-13 11:30:24 +00:00
|
|
|
# Test conditions and save success/fail $status to return later.
|
|
|
|
switch $condition
|
2015-01-13 19:25:03 +00:00
|
|
|
case --to-be-false
|
|
|
|
eval "$expected"
|
|
|
|
test $status -ne 0
|
|
|
|
case --to-be-true
|
|
|
|
eval "$expected"
|
|
|
|
test $status -eq 0
|
2015-01-13 11:30:24 +00:00
|
|
|
case --to-contain
|
2015-01-13 20:44:27 +00:00
|
|
|
set result 0
|
|
|
|
for item in $actual
|
|
|
|
contains -- "$item" $expected
|
|
|
|
or set result $status
|
|
|
|
end
|
|
|
|
test $result -eq 0
|
2015-01-13 11:30:24 +00:00
|
|
|
case --to-not-contain
|
2015-01-13 20:44:27 +00:00
|
|
|
set result 0
|
|
|
|
for item in $actual
|
|
|
|
not contains -- "$item" $expected
|
|
|
|
or set result $status
|
|
|
|
end
|
|
|
|
test $result -eq 0
|
2015-01-13 19:25:03 +00:00
|
|
|
case --to-eq\*
|
|
|
|
test "$expected" = "$actual"
|
2015-01-10 04:14:02 +00:00
|
|
|
end
|
2015-01-13 11:30:24 +00:00
|
|
|
set result $status
|
|
|
|
if [ $result -eq 0 ]
|
|
|
|
spec.log --ok
|
|
|
|
else
|
|
|
|
spec.log --fail red \n"Expected" yellow "$expected" \
|
|
|
|
red \n(echo $condition | tr "-" " " \
|
|
|
|
| cut -c 3-) yellow "$actual"
|
|
|
|
end
|
|
|
|
return $result
|
2015-01-10 04:14:02 +00:00
|
|
|
end
|