// Copyright 2011 Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file test_utils.hpp29/// Utilities for tests of the lua modules.30///31/// This file is intended to be included once, and only once, for every test32/// program that needs it. All the code is herein contained to simplify the33/// dependency chain in the build rules.3435#if !defined(LUTOK_TEST_UTILS_HPP)36# define LUTOK_TEST_UTILS_HPP37#else38# error "test_utils.hpp can only be included once"39#endif4041#include <atf-c++.hpp>4243#include "c_gate.hpp"44#include "exceptions.hpp"45#include "state.hpp"464748namespace {495051/// Checks that a given expression raises a particular lutok::api_error.52///53/// We cannot make any assumptions regarding the error text provided by Lua, so54/// we resort to checking only which API function raised the error (because our55/// code is the one hardcoding these strings).56///57/// \param exp_api_function The name of the Lua C API function that causes the58/// error.59/// \param statement The statement to execute.60#define REQUIRE_API_ERROR(exp_api_function, statement) \61do { \62try { \63statement; \64ATF_FAIL("api_error not raised by " #statement); \65} catch (const lutok::api_error& api_error) { \66ATF_REQUIRE_EQ(exp_api_function, api_error.api_function()); \67} \68} while (0)697071/// Gets the pointer to the internal lua_State of a state object.72///73/// This is pure syntactic sugar to simplify typing in the test cases.74///75/// \param state The Lua state.76///77/// \return The internal lua_State of the input Lua state.78static inline lua_State*79raw(lutok::state& state)80{81return lutok::state_c_gate(state).c_state();82}838485/// Ensures that the Lua stack maintains its original height upon exit.86///87/// Use an instance of this class to check that a piece of code does not have88/// side-effects on the Lua stack.89///90/// To be used within a test case only.91class stack_balance_checker {92/// The Lua state.93lutok::state& _state;9495/// Whether to install a sentinel on the stack for balance enforcement.96bool _with_sentinel;9798/// The height of the stack on creation.99unsigned int _old_count;100101public:102/// Constructs a new stack balance checker.103///104/// \param state_ The Lua state to validate.105/// \param with_sentinel_ If true, insert a sentinel item into the stack and106/// validate upon exit that the item is still there. This is an attempt107/// to ensure that already-existing items are not removed from the stack108/// by the code under test.109stack_balance_checker(lutok::state& state_,110const bool with_sentinel_ = true) :111_state(state_),112_with_sentinel(with_sentinel_),113_old_count(_state.get_top())114{115if (_with_sentinel)116_state.push_integer(987654321);117}118119/// Destructor for the object.120///121/// If the stack height does not match the height when the instance was122/// created, this fails the test case.123~stack_balance_checker(void)124{125if (_with_sentinel) {126if (!_state.is_number(-1) || _state.to_integer(-1) != 987654321)127ATF_FAIL("Stack corrupted: sentinel not found");128_state.pop(1);129}130131unsigned int new_count = _state.get_top();132if (_old_count != new_count)133//ATF_FAIL(F("Stack not balanced: before %d, after %d") %134// _old_count % new_count);135ATF_FAIL("Stack not balanced");136}137};138139140} // anonymous namespace141142143