1#! __ATF_SH__
2# Copyright 2012 Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10#   notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above copyright
12#   notice, this list of conditions and the following disclaimer in the
13#   documentation and/or other materials provided with the distribution.
14# * Neither the name of Google Inc. nor the names of its contributors
15#   may be used to endorse or promote products derived from this software
16#   without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30ExamplesDir="__EXAMPLESDIR__"
31LibDir="__LIBDIR__"
32
33
34make_example() {
35    cp "${ExamplesDir}/Makefile" "${ExamplesDir}/${1}.cpp" .
36    make "${1}"
37
38    # Ensure that the binary we just built can find liblutok.  This is
39    # needed because the lutok.pc file (which the Makefile used above
40    # queries) does not provide rpaths to the installed library and
41    # therefore the binary may not be able to locate it.  Hardcoding the
42    # rpath flags into lutok.pc is non-trivial because we simply don't
43    # have any knowledge about what the correct flag to set an rpath is.
44    #
45    # Additionally, setting rpaths is not always the right thing to do.
46    # For example, pkgsrc will automatically change lutok.pc to add the
47    # missing rpath, in which case this is unnecessary.  But in the case
48    # of Fedora, adding rpaths goes against the packaging guidelines.
49    if [ -n "${LD_LIBRARY_PATH}" ]; then
50        export LD_LIBRARY_PATH="${LibDir}:${LD_LIBRARY_PATH}"
51    else
52        export LD_LIBRARY_PATH="${LibDir}"
53    fi
54}
55
56
57example_test_case() {
58    local name="${1}"; shift
59
60    atf_test_case "${name}"
61    eval "${name}_head() { \
62        atf_set 'require.files' '${ExamplesDir}/${name}.cpp'; \
63        atf_set 'require.progs' 'make pkg-config'; \
64    }"
65    eval "${name}_body() { \
66        make_example '${name}'; \
67        ${name}_validate; \
68    }"
69}
70
71
72example_test_case bindings
73bindings_validate() {
74    atf_check -s exit:0 -o inline:'120\n' ./bindings 5
75    atf_check -s exit:1 -e match:'Argument.*must be an integer' ./bindings foo
76    atf_check -s exit:1 -e match:'Argument.*must be positive' ./bindings -5
77}
78
79
80example_test_case hello
81hello_validate() {
82    atf_check -s exit:0 -o inline:'Hello, world!\n' ./hello
83}
84
85
86example_test_case interpreter
87interpreter_validate() {
88    cat >script.lua <<EOF
89test_variable = 12345
90print("From the interpreter: " .. (test_variable - 345))
91EOF
92
93    atf_check -s exit:0 -o match:"From the interpreter: 12000" \
94        -x "./interpreter <script.lua"
95}
96
97
98example_test_case raii
99raii_validate() {
100cat >expout <<EOF
101String in field foo: hello
102String in field bar: 123
103String in field baz: bye
104EOF
105    atf_check -s exit:0 -o file:expout ./raii
106}
107
108
109atf_init_test_cases() {
110    atf_add_test_case bindings
111    atf_add_test_case hello
112    atf_add_test_case interpreter
113    atf_add_test_case raii
114}
115