1#
2# Automated Testing Framework (atf)
3#
4# Copyright (c) 2007, 2008, 2010 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30atf_test_case result_on_stdout
31result_on_stdout_head()
32{
33    atf_set "descr" "Tests that the test case result is printed on stdout" \
34                    "by default"
35}
36result_on_stdout_body()
37{
38    srcdir="$(atf_get_srcdir)"
39    for h in $(get_helpers); do
40        atf_check -s eq:0 -o match:"passed" -o match:"msg" \
41            -e empty "${h}" -s "${srcdir}" result_pass
42        atf_check -s eq:1 -o match:"failed: Failure reason" -o match:"msg" \
43            -e empty "${h}" -s "${srcdir}" result_fail
44        atf_check -s eq:0 -o match:"skipped: Skipped reason" -o match:"msg" \
45            -e empty "${h}" -s "${srcdir}" result_skip
46    done
47}
48
49atf_test_case result_to_file
50result_to_file_head()
51{
52    atf_set "descr" "Tests that the test case result is sent to a file if -r" \
53                    "is used"
54}
55result_to_file_body()
56{
57    srcdir="$(atf_get_srcdir)"
58    for h in $(get_helpers); do
59        atf_check -s eq:0 -o inline:"msg\n" -e empty "${h}" -s "${srcdir}" \
60            -r resfile result_pass
61        atf_check -o inline:"passed\n" cat resfile
62
63        atf_check -s eq:1 -o inline:"msg\n" -e empty "${h}" -s "${srcdir}" \
64            -r resfile result_fail
65        atf_check -o inline:"failed: Failure reason\n" cat resfile
66
67        atf_check -s eq:0 -o inline:"msg\n" -e empty "${h}" -s "${srcdir}" \
68            -r resfile result_skip
69        atf_check -o inline:"skipped: Skipped reason\n" cat resfile
70    done
71}
72
73atf_test_case result_to_file_fail
74result_to_file_fail_head()
75{
76    atf_set "descr" "Tests controlled failure if the test program fails to" \
77        "create the results file"
78    atf_set "require.user" "unprivileged"
79}
80result_to_file_fail_body()
81{
82    mkdir dir
83    chmod 444 dir
84
85    srcdir="$(atf_get_srcdir)"
86
87    for h in $(get_helpers c_helpers cpp_helpers); do
88        atf_check -s signal -o ignore \
89            -e match:"FATAL ERROR: Cannot create.*'dir/resfile'" \
90            "${h}" -s "${srcdir}" -r dir/resfile result_pass
91    done
92
93    for h in $(get_helpers sh_helpers); do
94        atf_check -s exit -o ignore \
95            -e match:"ERROR: Cannot create.*'dir/resfile'" \
96            "${h}" -s "${srcdir}" -r dir/resfile result_pass
97    done
98}
99
100atf_test_case result_exception
101result_exception_head()
102{
103    atf_set "descr" "Tests that an unhandled exception is correctly captured"
104}
105result_exception_body()
106{
107    for h in $(get_helpers cpp_helpers); do
108        atf_check -s exit:1 -o match:'failed: .*This is unhandled' \
109            "${h}" -s "${srcdir}" result_exception
110    done
111}
112
113atf_init_test_cases()
114{
115    atf_add_test_case result_on_stdout
116    atf_add_test_case result_to_file
117    atf_add_test_case result_to_file_fail
118    atf_add_test_case result_exception
119}
120
121# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
122