Loading...
Searching...
No Matches
test_utils.hpp
Go to the documentation of this file.
// Copyright 2011 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#if !defined(LUTOK_TEST_UTILS_HPP)
# define LUTOK_TEST_UTILS_HPP
#else
# error "test_utils.hpp can only be included once"
#endif
#include <atf-c++.hpp>
#include "c_gate.hpp"
#include "exceptions.hpp"
#include "state.hpp"
namespace {
#define REQUIRE_API_ERROR(exp_api_function, statement) \
do { \
try { \
statement; \
ATF_FAIL("api_error not raised by " #statement); \
} catch (const lutok::api_error& api_error) { \
ATF_REQUIRE_EQ(exp_api_function, api_error.api_function()); \
} \
} while (0)
static inline lua_State*
raw(lutok::state& state)
{
return lutok::state_c_gate(state).c_state();
}
class stack_balance_checker {
lutok::state& _state;
bool _with_sentinel;
unsigned int _old_count;
public:
stack_balance_checker(lutok::state& state_,
const bool with_sentinel_ = true) :
_state(state_),
_with_sentinel(with_sentinel_),
_old_count(_state.get_top())
{
if (_with_sentinel)
_state.push_integer(987654321);
}
~stack_balance_checker(void)
{
if (_with_sentinel) {
if (!_state.is_number(-1) || _state.to_integer(-1) != 987654321)
ATF_FAIL("Stack corrupted: sentinel not found");
_state.pop(1);
}
unsigned int new_count = _state.get_top();
if (_old_count != new_count)
//ATF_FAIL(F("Stack not balanced: before %d, after %d") %
// _old_count % new_count);
ATF_FAIL("Stack not balanced");
}
};
} // anonymous namespace
state.hpp
Provides the state wrapper class for the Lua C state.
c_gate.hpp
Provides direct access to the C state of the Lua wrappers.
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