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