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