xref: /freebsd/contrib/lutok/test_utils.hpp (revision c697fb7f7cc9bedc5beee44d35b771c4e87b335a)
1*c697fb7fSBrooks Davis // Copyright 2011 Google Inc.
2*c697fb7fSBrooks Davis // All rights reserved.
3*c697fb7fSBrooks Davis //
4*c697fb7fSBrooks Davis // Redistribution and use in source and binary forms, with or without
5*c697fb7fSBrooks Davis // modification, are permitted provided that the following conditions are
6*c697fb7fSBrooks Davis // met:
7*c697fb7fSBrooks Davis //
8*c697fb7fSBrooks Davis // * Redistributions of source code must retain the above copyright
9*c697fb7fSBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*c697fb7fSBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*c697fb7fSBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*c697fb7fSBrooks Davis //   documentation and/or other materials provided with the distribution.
13*c697fb7fSBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*c697fb7fSBrooks Davis //   may be used to endorse or promote products derived from this software
15*c697fb7fSBrooks Davis //   without specific prior written permission.
16*c697fb7fSBrooks Davis //
17*c697fb7fSBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*c697fb7fSBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*c697fb7fSBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*c697fb7fSBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*c697fb7fSBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*c697fb7fSBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*c697fb7fSBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*c697fb7fSBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*c697fb7fSBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*c697fb7fSBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*c697fb7fSBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*c697fb7fSBrooks Davis 
29*c697fb7fSBrooks Davis /// \file test_utils.hpp
30*c697fb7fSBrooks Davis /// Utilities for tests of the lua modules.
31*c697fb7fSBrooks Davis ///
32*c697fb7fSBrooks Davis /// This file is intended to be included once, and only once, for every test
33*c697fb7fSBrooks Davis /// program that needs it.  All the code is herein contained to simplify the
34*c697fb7fSBrooks Davis /// dependency chain in the build rules.
35*c697fb7fSBrooks Davis 
36*c697fb7fSBrooks Davis #if !defined(LUTOK_TEST_UTILS_HPP)
37*c697fb7fSBrooks Davis #   define LUTOK_TEST_UTILS_HPP
38*c697fb7fSBrooks Davis #else
39*c697fb7fSBrooks Davis #   error "test_utils.hpp can only be included once"
40*c697fb7fSBrooks Davis #endif
41*c697fb7fSBrooks Davis 
42*c697fb7fSBrooks Davis #include <atf-c++.hpp>
43*c697fb7fSBrooks Davis 
44*c697fb7fSBrooks Davis #include "c_gate.hpp"
45*c697fb7fSBrooks Davis #include "exceptions.hpp"
46*c697fb7fSBrooks Davis #include "state.hpp"
47*c697fb7fSBrooks Davis 
48*c697fb7fSBrooks Davis 
49*c697fb7fSBrooks Davis namespace {
50*c697fb7fSBrooks Davis 
51*c697fb7fSBrooks Davis 
52*c697fb7fSBrooks Davis /// Checks that a given expression raises a particular lutok::api_error.
53*c697fb7fSBrooks Davis ///
54*c697fb7fSBrooks Davis /// We cannot make any assumptions regarding the error text provided by Lua, so
55*c697fb7fSBrooks Davis /// we resort to checking only which API function raised the error (because our
56*c697fb7fSBrooks Davis /// code is the one hardcoding these strings).
57*c697fb7fSBrooks Davis ///
58*c697fb7fSBrooks Davis /// \param exp_api_function The name of the Lua C API function that causes the
59*c697fb7fSBrooks Davis ///     error.
60*c697fb7fSBrooks Davis /// \param statement The statement to execute.
61*c697fb7fSBrooks Davis #define REQUIRE_API_ERROR(exp_api_function, statement) \
62*c697fb7fSBrooks Davis     do { \
63*c697fb7fSBrooks Davis         try { \
64*c697fb7fSBrooks Davis             statement; \
65*c697fb7fSBrooks Davis             ATF_FAIL("api_error not raised by " #statement); \
66*c697fb7fSBrooks Davis         } catch (const lutok::api_error& api_error) { \
67*c697fb7fSBrooks Davis             ATF_REQUIRE_EQ(exp_api_function, api_error.api_function()); \
68*c697fb7fSBrooks Davis         } \
69*c697fb7fSBrooks Davis     } while (0)
70*c697fb7fSBrooks Davis 
71*c697fb7fSBrooks Davis 
72*c697fb7fSBrooks Davis /// Gets the pointer to the internal lua_State of a state object.
73*c697fb7fSBrooks Davis ///
74*c697fb7fSBrooks Davis /// This is pure syntactic sugar to simplify typing in the test cases.
75*c697fb7fSBrooks Davis ///
76*c697fb7fSBrooks Davis /// \param state The Lua state.
77*c697fb7fSBrooks Davis ///
78*c697fb7fSBrooks Davis /// \return The internal lua_State of the input Lua state.
79*c697fb7fSBrooks Davis static inline lua_State*
raw(lutok::state & state)80*c697fb7fSBrooks Davis raw(lutok::state& state)
81*c697fb7fSBrooks Davis {
82*c697fb7fSBrooks Davis     return lutok::state_c_gate(state).c_state();
83*c697fb7fSBrooks Davis }
84*c697fb7fSBrooks Davis 
85*c697fb7fSBrooks Davis 
86*c697fb7fSBrooks Davis /// Ensures that the Lua stack maintains its original height upon exit.
87*c697fb7fSBrooks Davis ///
88*c697fb7fSBrooks Davis /// Use an instance of this class to check that a piece of code does not have
89*c697fb7fSBrooks Davis /// side-effects on the Lua stack.
90*c697fb7fSBrooks Davis ///
91*c697fb7fSBrooks Davis /// To be used within a test case only.
92*c697fb7fSBrooks Davis class stack_balance_checker {
93*c697fb7fSBrooks Davis     /// The Lua state.
94*c697fb7fSBrooks Davis     lutok::state& _state;
95*c697fb7fSBrooks Davis 
96*c697fb7fSBrooks Davis     /// Whether to install a sentinel on the stack for balance enforcement.
97*c697fb7fSBrooks Davis     bool _with_sentinel;
98*c697fb7fSBrooks Davis 
99*c697fb7fSBrooks Davis     /// The height of the stack on creation.
100*c697fb7fSBrooks Davis     unsigned int _old_count;
101*c697fb7fSBrooks Davis 
102*c697fb7fSBrooks Davis public:
103*c697fb7fSBrooks Davis     /// Constructs a new stack balance checker.
104*c697fb7fSBrooks Davis     ///
105*c697fb7fSBrooks Davis     /// \param state_ The Lua state to validate.
106*c697fb7fSBrooks Davis     /// \param with_sentinel_ If true, insert a sentinel item into the stack and
107*c697fb7fSBrooks Davis     ///     validate upon exit that the item is still there.  This is an attempt
108*c697fb7fSBrooks Davis     ///     to ensure that already-existing items are not removed from the stack
109*c697fb7fSBrooks Davis     ///     by the code under test.
stack_balance_checker(lutok::state & state_,const bool with_sentinel_=true)110*c697fb7fSBrooks Davis     stack_balance_checker(lutok::state& state_,
111*c697fb7fSBrooks Davis                           const bool with_sentinel_ = true) :
112*c697fb7fSBrooks Davis         _state(state_),
113*c697fb7fSBrooks Davis         _with_sentinel(with_sentinel_),
114*c697fb7fSBrooks Davis         _old_count(_state.get_top())
115*c697fb7fSBrooks Davis     {
116*c697fb7fSBrooks Davis         if (_with_sentinel)
117*c697fb7fSBrooks Davis             _state.push_integer(987654321);
118*c697fb7fSBrooks Davis     }
119*c697fb7fSBrooks Davis 
120*c697fb7fSBrooks Davis     /// Destructor for the object.
121*c697fb7fSBrooks Davis     ///
122*c697fb7fSBrooks Davis     /// If the stack height does not match the height when the instance was
123*c697fb7fSBrooks Davis     /// created, this fails the test case.
~stack_balance_checker(void)124*c697fb7fSBrooks Davis     ~stack_balance_checker(void)
125*c697fb7fSBrooks Davis     {
126*c697fb7fSBrooks Davis         if (_with_sentinel) {
127*c697fb7fSBrooks Davis             if (!_state.is_number(-1) || _state.to_integer(-1) != 987654321)
128*c697fb7fSBrooks Davis                 ATF_FAIL("Stack corrupted: sentinel not found");
129*c697fb7fSBrooks Davis             _state.pop(1);
130*c697fb7fSBrooks Davis         }
131*c697fb7fSBrooks Davis 
132*c697fb7fSBrooks Davis         unsigned int new_count = _state.get_top();
133*c697fb7fSBrooks Davis         if (_old_count != new_count)
134*c697fb7fSBrooks Davis             //ATF_FAIL(F("Stack not balanced: before %d, after %d") %
135*c697fb7fSBrooks Davis             //         _old_count % new_count);
136*c697fb7fSBrooks Davis             ATF_FAIL("Stack not balanced");
137*c697fb7fSBrooks Davis     }
138*c697fb7fSBrooks Davis };
139*c697fb7fSBrooks Davis 
140*c697fb7fSBrooks Davis 
141*c697fb7fSBrooks Davis }  // anonymous namespace
142