shspec/test/test-expect-core

197 lines
4.5 KiB
Bash

#!/bin/bash
# Core expectation tests
#
# Copyright (C) 2014 Mike Gerwitz
#
# This file is part of shspec.
#
# shspec is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##
describe be
it cannot stand alone
expect shspec:expect:be 0 2
to fail
end
it processes remainder clause as if it did not exist
(
expected="foo bar baz"
shspec:expect:awesome() { shift "$2"; test "$*" == "$expected"; }
expect shspec:expect:be 0 2 awesome $expected
to succeed
)
end
end
describe succeed
it will succeed when command exits with status code 0
expect shspec:expect:succeed 0 2
to succeed
end
it will fail when command exits with non-zero status code
expect shspec:expect:succeed 1 2
to fail
end
end
describe fail
it will succeed when command exits with non-zero status code
expect shspec:expect:fail 1 2
to succeed
end
it will fail when command exits with status code 0
expect shspec:expect:fail 0 2
to fail
end
end
describe not
it will invert the result of an expectation
# exit code of 1, so normally `succeed' would fail
expect shspec:expect:not 1 2 succeed
to succeed
# exit code of 0, so normally `succeed' would succeed
expect shspec:expect:not 0 2 succeed
to fail
end
end
describe output
it will default to asserting against stdout
expect shspec:expect:output 0 2 "test string" <<< "test string"
to succeed
end
# by convention, stderr file passed as third argument
it can assert against stderr output
{
expect shspec:expect:output 0 3 /dev/fd/3 \
"test stderr" on stderr
to succeed
} 3< <( echo "test stderr" )
end
it fails on stdout mismatch
expect shspec:expect:output 0 2 "foo" <<< "bar"
to fail
end
it fails on stderr mismatch
{
expect shspec:expect:output 0 3 /dev/fd/3 "foo" on stderr
to fail
} 3< <( echo bar )
end
it ignores exit code
expect shspec:expect:output 1 2 "foo" <<< "foo"
to succeed
end
# as is good practice for Unix utilities
it expects trailing newline by default
expect shspec:expect:output 0 2 "foo" < <( echo -n foo )
to fail
end
it can disable trailing newline check with "'without newline'" clause
expect shspec:expect:output 0 2 "foo" without newline \
< <( echo -n foo )
to succeed
end
it errors on any unrecognized clause
expect shspec:expect:output 0 2 "foo" fluffy bunnies <<< "foo"
to fail
end
end
describe match
it will default to asserting against stdout
expect shspec:expect:match 0 2 "foo" <<< "foo"
to succeed
end
# by convention, stderr file passed as third argument
it can assert against stderr output
{
expect shspec:expect:match 0 3 /dev/fd/3 "stderr" on stderr
to succeed
} 3< <( echo "test stderr" )
end
it ignores exit code
expect shspec:expect:match 1 2 "foo" <<< "foo"
to succeed
end
it will perform partial match
expect shspec:expect:match 0 2 "foo" <<< "contains foo"
to succeed
end
it will match against a pattern
expect shspec:expect:match 0 2 "^foo.*baz" <<< "foo bar baz"
to succeed
expect shspec:expect:match 0 2 "^foo.*baz" <<< "bar foo bar baz"
to fail
end
end
describe silent
it succeeds when both stdout and stderr are empty
# omitting stderr arg
expect shspec:expect:silent 0 2 < <(:)
to succeed
# empty stderr
expect shspec:expect:silent 0 3 <(:) < <(:)
to succeed
end
it fails when stdout is empty but stderr is not
expect shspec:expect:silent 0 3 <( echo ) < <(:)
to fail
end
it fails when stderr is empty but stdout is not
# omitting stderr arg
expect shspec:expect:silent 0 2 <<< ""
to fail
# empty stderr
expect shspec:expect:silent 0 3 <(:) <<< ""
to fail
end
# no arguments within context of the DSL, that is
it accepts no arguments
expect shspec:expect:silent 0 2 foo < <(:)
to fail
end
end