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
30Cxx="__CXX__"
31ExamplesDir="__EXAMPLESDIR__"
32LibDir="__LIBDIR__"
33
34
35make_example() {
36    cp "${ExamplesDir}/Makefile" "${ExamplesDir}/${1}.cpp" .
37    make CXX="${Cxx}" "${1}"
38
39    # Ensure that the binary we just built can find liblutok.  This is
40    # needed because the lutok.pc file (which the Makefile used above
41    # queries) does not provide rpaths to the installed library and
42    # therefore the binary may not be able to locate it.  Hardcoding the
43    # rpath flags into lutok.pc is non-trivial because we simply don't
44    # have any knowledge about what the correct flag to set an rpath is.
45    #
46    # Additionally, setting rpaths is not always the right thing to do.
47    # For example, pkgsrc will automatically change lutok.pc to add the
48    # missing rpath, in which case this is unnecessary.  But in the case
49    # of Fedora, adding rpaths goes against the packaging guidelines.
50    if [ -n "${LD_LIBRARY_PATH}" ]; then
51        export LD_LIBRARY_PATH="${LibDir}:${LD_LIBRARY_PATH}"
52    else
53        export LD_LIBRARY_PATH="${LibDir}"
54    fi
55}
56
57
58example_test_case() {
59    local name="${1}"; shift
60
61    atf_test_case "${name}"
62    eval "${name}_head() { \
63        atf_set 'require.files' '${ExamplesDir}/${name}.cpp'; \
64        atf_set 'require.progs' 'make pkg-config'; \
65    }"
66    eval "${name}_body() { \
67        make_example '${name}'; \
68        ${name}_validate; \
69    }"
70}
71
72
73example_test_case bindings
74bindings_validate() {
75    atf_check -s exit:0 -o inline:'120\n' ./bindings 5
76    atf_check -s exit:1 -e match:'Argument.*must be an integer' ./bindings foo
77    atf_check -s exit:1 -e match:'Argument.*must be positive' ./bindings -5
78}
79
80
81example_test_case hello
82hello_validate() {
83    atf_check -s exit:0 -o inline:'Hello, world!\n' ./hello
84}
85
86
87example_test_case interpreter
88interpreter_validate() {
89    cat >script.lua <<EOF
90test_variable = 12345
91print("From the interpreter: " .. (test_variable - 345))
92EOF
93
94    atf_check -s exit:0 -o match:"From the interpreter: 12000" \
95        -x "./interpreter <script.lua"
96}
97
98
99example_test_case raii
100raii_validate() {
101cat >expout <<EOF
102String in field foo: hello
103String in field bar: 123
104String in field baz: bye
105EOF
106    atf_check -s exit:0 -o file:expout ./raii
107}
108
109
110atf_init_test_cases() {
111    atf_add_test_case bindings
112    atf_add_test_case hello
113    atf_add_test_case interpreter
114    atf_add_test_case raii
115}
116