Lutok  0.4
 All Classes Namespaces Files Functions Variables Macros
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 
79 static inline lua_State*
81 {
82  return lutok::state_c_gate(state).c_state();
83 }
84 
85 
95 
98 
100  unsigned int _old_count;
101 
102 public:
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 
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
lutok::state & _state
The Lua state.
Definition: test_utils.hpp:94
int get_top(void)
Wrapper around lua_gettop.
Definition: state.cpp:384
lua_State * c_state(void)
Returns the C native Lua state.
Definition: c_gate.cpp:73
Ensures that the Lua stack maintains its original height upon exit.
Definition: test_utils.hpp:92
static lua_State * raw(lutok::state &state)
Gets the pointer to the internal lua_State of a state object.
Definition: test_utils.hpp:80
void pop(const int)
Wrapper around lua_pop.
Definition: state.cpp:654
bool _with_sentinel
Whether to install a sentinel on the stack for balance enforcement.
Definition: test_utils.hpp:97
~stack_balance_checker(void)
Destructor for the object.
Definition: test_utils.hpp:124
long to_integer(const int)
Wrapper around lua_tointeger.
Definition: state.cpp:838
A RAII model for the Lua state.
Definition: state.hpp:76
bool is_number(const int)
Wrapper around lua_isnumber.
Definition: state.cpp:442
unsigned int _old_count
The height of the stack on creation.
Definition: test_utils.hpp:100
Provides direct access to the C state of the Lua wrappers.
Gateway to the raw C state of Lua.
Definition: c_gate.hpp:55
stack_balance_checker(lutok::state &state_, const bool with_sentinel_=true)
Constructs a new stack balance checker.
Definition: test_utils.hpp:110
Provides the state wrapper class for the Lua C state.
Exception types raised by lutok.
void push_integer(const int)
Wrapper around lua_pushinteger.
Definition: state.cpp:709