Lutok: test_utils.hpp Source File

Lutok 0.6.1 |

Loading...
Searching...
No Matches
test_utils.hpp
Go to the documentation of this file.
1// Copyright 2011 Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14// may be used to endorse or promote products derived from this software
15// without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
35
36#if !defined(LUTOK_TEST_UTILS_HPP)
37# define LUTOK_TEST_UTILS_HPP
38#else
39# error "test_utils.hpp can only be included once"
40#endif
41
42#include <atf-c++.hpp>
43
44#include "c_gate.hpp"
45#include "exceptions.hpp"
46#include "state.hpp"
47
48
49 namespace {
50
51
61#define REQUIRE_API_ERROR(exp_api_function, statement) \
62 do { \
63 try { \
64 statement; \
65 ATF_FAIL("api_error not raised by " #statement); \
66 } catch (const lutok::api_error& api_error) { \
67 ATF_REQUIRE_EQ(exp_api_function, api_error.api_function()); \
68 } \
69 } while (0)
70
71
79static inline lua_State*
80raw(lutok::state& state)
81{
82 return lutok::state_c_gate(state).c_state();
83}
84
85
92 class stack_balance_checker {
94 lutok::state& _state;
95
97 bool _with_sentinel;
98
100 unsigned int _old_count;
101
102public:
110 stack_balance_checker(lutok::state& state_,
111 const bool with_sentinel_ = true) :
112 _state(state_),
113 _with_sentinel(with_sentinel_),
114 _old_count(_state.get_top())
115 {
116 if (_with_sentinel)
117 _state.push_integer(987654321);
118 }
119
124 ~stack_balance_checker(void)
125 {
126 if (_with_sentinel) {
127 if (!_state.is_number(-1) || _state.to_integer(-1) != 987654321)
128 ATF_FAIL("Stack corrupted: sentinel not found");
129 _state.pop(1);
130 }
131
132 unsigned int new_count = _state.get_top();
133 if (_old_count != new_count)
134 //ATF_FAIL(F("Stack not balanced: before %d, after %d") %
135 // _old_count % new_count);
136 ATF_FAIL("Stack not balanced");
137 }
138};
139
140
141} // anonymous namespace
c_gate.hpp
Provides direct access to the C state of the Lua wrappers.
state.hpp
Provides the state wrapper class for the Lua C state.
exceptions.hpp
Exception types raised by lutok.
anonymous_namespace{test_utils.hpp}::raw
static lua_State * raw(lutok::state &state)
Gets the pointer to the internal lua_State of a state object.
Definition test_utils.hpp:80
lutok::state_c_gate
Gateway to the raw C state of Lua.
Definition c_gate.hpp:55
lutok::state_c_gate::c_state
lua_State * c_state(void)
Returns the C native Lua state.
Definition c_gate.cpp:73
lutok::state
A RAII model for the Lua state.
Definition state.hpp:71
anonymous_namespace{test_utils.hpp}::stack_balance_checker::stack_balance_checker
stack_balance_checker(lutok::state &state_, const bool with_sentinel_=true)
Constructs a new stack balance checker.
Definition test_utils.hpp:110
anonymous_namespace{test_utils.hpp}::stack_balance_checker::~stack_balance_checker
~stack_balance_checker(void)
Destructor for the object.
Definition test_utils.hpp:124
anonymous_namespace{test_utils.hpp}::stack_balance_checker::_state
lutok::state & _state
The Lua state.
Definition test_utils.hpp:94
anonymous_namespace{test_utils.hpp}::stack_balance_checker::_old_count
unsigned int _old_count
The height of the stack on creation.
Definition test_utils.hpp:100
anonymous_namespace{test_utils.hpp}::stack_balance_checker::_with_sentinel
bool _with_sentinel
Whether to install a sentinel on the stack for balance enforcement.
Definition test_utils.hpp:97