Skip to content

Commit

Permalink
Add atf_check_not_equal function to atf-sh to check unequal values
Browse files Browse the repository at this point in the history
  • Loading branch information
shivansh authored and jmmv committed Jul 22, 2017
1 parent 0d47dc9 commit 0824235
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ DON'T FORGET TO BUMP THE -version-info PRE-RELEASE IF NECESSARY!
* Issue #31: Added require.progs metadata properties to the tests that
need a compiler to run.

* Added the atf_check_not_equal function to atf-sh to check for
unequal values.


Changes in version 0.21
***********************
Expand Down
11 changes: 10 additions & 1 deletion atf-sh/atf-sh.3
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.Dd October 13, 2014
.Dd June 08, 2017
.Dt ATF-SH 3
.Os
.Sh NAME
.Nm atf_add_test_case ,
.Nm atf_check ,
.Nm atf_check_equal ,
.Nm atf_check_not_equal ,
.Nm atf_config_get ,
.Nm atf_config_has ,
.Nm atf_expect_death ,
Expand All @@ -54,6 +55,9 @@
.Nm atf_check_equal
.Qq expected_expression
.Qq actual_expression
.Nm atf_check_not_equal
.Qq expected_expression
.Qq actual_expression
.Nm atf_config_get
.Qq var_name
.Nm atf_config_has
Expand Down Expand Up @@ -307,6 +311,11 @@ This function takes two expressions, evaluates them and, if their
results differ, aborts the test case with an appropriate failure message.
The common style is to put the expected value in the first parameter and the
actual value in the second parameter.
.It Nm atf_check_not_equal Qo expected_expression Qc Qo actual_expression Qc
This function takes two expressions, evaluates them and, if their
results are equal, aborts the test case with an appropriate failure message.
The common style is to put the expected value in the first parameter and the
actual value in the second parameter.
.El
.Sh EXAMPLES
The following shows a complete test program with a single test case that
Expand Down
24 changes: 24 additions & 0 deletions atf-sh/atf_check_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ equal_body()
grep '^failed: \${x} != \${y} (a != b)$' resfile
}

atf_test_case not_equal
not_equal_head()
{
atf_set "descr" "Verifies that atf_check_not_equal works"
}
not_equal_body()
{
h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"

atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_not_equal_ok"

atf_check -s eq:1 -o ignore -e ignore -x \
"${h} -r resfile atf_check_not_equal_fail"
atf_check -s eq:0 -o ignore -e empty grep '^failed: a == b (a == b)$' \
resfile

atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_not_equal_eval_ok"

atf_check -s eq:1 -o ignore -e ignore -x \
"${h} -r resfile atf_check_not_equal_eval_fail"
atf_check -s eq:0 -o ignore -e empty \
grep '^failed: \${x} == \${y} (a == b)$' resfile
}

atf_test_case flush_stdout_on_death
flush_stdout_on_death_body()
{
Expand Down
17 changes: 17 additions & 0 deletions atf-sh/libatf-sh.subr
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ atf_check_equal()
atf_fail "${1} != ${2} (${_val1} != ${_val2})"
}

#
# atf_check_not_equal expected_expression actual_expression
#
# Checks that expected_expression's value does not match actual_expression's
# and, if it does, raises an error. Ideally expected_expression and
# actual_expression should be provided quoted (not expanded) so that
# the error message is helpful; otherwise it will only show the values,
# not the expressions themselves.
#
atf_check_not_equal()
{
eval _val1=\"${1}\"
eval _val2=\"${2}\"
test "${_val1}" != "${_val2}" || \
atf_fail "${1} == ${2} (${_val1} == ${_val2})"
}

#
# atf_config_get varname [defvalue]
#
Expand Down
48 changes: 48 additions & 0 deletions atf-sh/misc_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,50 @@ atf_check_equal_eval_fail_body()
atf_check_equal '${x}' '${y}'
}

atf_test_case atf_check_not_equal_ok
atf_check_not_equal_ok_head()
{
atf_set "descr" "Helper test case for the t_atf_check test program"
}
atf_check_not_equal_ok_body()
{
atf_check_not_equal a b
}

atf_test_case atf_check_not_equal_fail
atf_check_not_equal_fail_head()
{
atf_set "descr" "Helper test case for the t_atf_check test program"
}
atf_check_not_equal_fail_body()
{
atf_check_not_equal a a
}

atf_test_case atf_check_not_equal_eval_ok
atf_check_not_equal_eval_ok_head()
{
atf_set "descr" "Helper test case for the t_atf_check test program"
}
atf_check_not_equal_eval_ok_body()
{
x=a
y=b
atf_check_not_equal '${x}' '${y}'
}

atf_test_case atf_check_not_equal_eval_fail
atf_check_not_equal_eval_fail_head()
{
atf_set "descr" "Helper test case for the t_atf_check test program"
}
atf_check_not_equal_eval_fail_body()
{
x=a
y=a
atf_check_not_equal '${x}' '${y}'
}

atf_test_case atf_check_flush_stdout
atf_check_flush_stdout_head()
{
Expand Down Expand Up @@ -285,6 +329,10 @@ atf_init_test_cases()
atf_add_test_case atf_check_equal_fail
atf_add_test_case atf_check_equal_eval_ok
atf_add_test_case atf_check_equal_eval_fail
atf_add_test_case atf_check_not_equal_ok
atf_add_test_case atf_check_not_equal_fail
atf_add_test_case atf_check_not_equal_eval_ok
atf_add_test_case atf_check_not_equal_eval_fail
atf_add_test_case atf_check_flush_stdout

# Add helper tests for t_config.
Expand Down

0 comments on commit 0824235

Please sign in to comment.