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