From 61ecb6ad822212ed650ed5a5a3110f049392f517 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 9 May 2014 02:04:09 -0400 Subject: [PATCH] Added output expectation handler --- src/expect-core | 17 +++++++++++++++++ test/test-expect-core | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/expect-core b/src/expect-core index dfad803..e25a29a 100644 --- a/src/expect-core +++ b/src/expect-core @@ -26,3 +26,20 @@ __INC_EXPECT_CORE=1 _expect--succeed() { test "$1" -eq 0; } _expect--fail() { test "$1" -ne 0; } +_expect--output() +{ + local -r cmp="$2" + shift 2 + local -r clause="$*" + + local nl= + if [ -n "$clause" ]; then + [ "$clause" == 'without newline' ] \ + && nl=-n \ + || _bail "unrecognized output clause: $clause" + fi + + # we will eventually be interested in this output + diff <( echo $nl "$cmp" ) - &>/dev/null +} + diff --git a/test/test-expect-core b/test/test-expect-core index 517e9d6..9cdbde9 100644 --- a/test/test-expect-core +++ b/test/test-expect-core @@ -45,3 +45,37 @@ describe fail end end + +describe output + it will default to asserting against stdout + expect _expect--output 0 "test string" <<< "test string" + to succeed + end + + it ignores exit code + expect _expect--output 1 "foo" <<< "foo" + to succeed + end + + it fails on output mismatch + expect _expect--output 0 "foo" <<< "bar" + to fail + end + + # as is good practice for Unix utilities + it expects trailing newline by default + expect _expect--output 0 "foo" < <( echo -n foo ) + to fail + end + + it can disable trailing newline check with "'without newline'" clause + expect _expect--output 0 "foo" without newline < <( echo -n foo ) + to succeed + end + + it errors on any unrecognized clause + expect _expect--output 0 "foo" fluffy bunnies <<< "foo" + to fail + end +end +