Path: blob/main/contrib/lutok/operations_test.cpp
102669 views
// 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#include "operations.hpp"2930#include <fstream>3132#include <atf-c++.hpp>3334#include "exceptions.hpp"35#include "state.ipp"36#include "test_utils.hpp"373839namespace {404142/// Addition function for injection into Lua.43///44/// \pre stack(-2) The first summand.45/// \pre stack(-1) The second summand.46/// \post stack(-1) The result of the sum.47///48/// \param state The Lua state.49///50/// \return The number of results (1).51static int52hook_add(lutok::state& state)53{54state.push_integer(state.to_integer(-1) + state.to_integer(-2));55return 1;56}575859/// Multiplication function for injection into Lua.60///61/// \pre stack(-2) The first factor.62/// \pre stack(-1) The second factor.63/// \post stack(-1) The product.64///65/// \param state The Lua state.66///67/// \return The number of results (1).68static int69hook_multiply(lutok::state& state)70{71state.push_integer(state.to_integer(-1) * state.to_integer(-2));72return 1;73}747576} // anonymous namespace777879ATF_TEST_CASE_WITHOUT_HEAD(create_module__empty);80ATF_TEST_CASE_BODY(create_module__empty)81{82lutok::state state;83std::map< std::string, lutok::cxx_function > members;84lutok::create_module(state, "my_math", members);8586state.open_base();87lutok::do_string(state, "return next(my_math) == nil", 0, 1, 0);88ATF_REQUIRE(state.to_boolean(-1));89state.pop(1);90}919293ATF_TEST_CASE_WITHOUT_HEAD(create_module__one);94ATF_TEST_CASE_BODY(create_module__one)95{96lutok::state state;97std::map< std::string, lutok::cxx_function > members;98members["add"] = hook_add;99lutok::create_module(state, "my_math", members);100101lutok::do_string(state, "return my_math.add(10, 20)", 0, 1, 0);102ATF_REQUIRE_EQ(30, state.to_integer(-1));103state.pop(1);104}105106107ATF_TEST_CASE_WITHOUT_HEAD(create_module__many);108ATF_TEST_CASE_BODY(create_module__many)109{110lutok::state state;111std::map< std::string, lutok::cxx_function > members;112members["add"] = hook_add;113members["multiply"] = hook_multiply;114members["add2"] = hook_add;115lutok::create_module(state, "my_math", members);116117lutok::do_string(state, "return my_math.add(10, 20)", 0, 1, 0);118ATF_REQUIRE_EQ(30, state.to_integer(-1));119lutok::do_string(state, "return my_math.multiply(10, 20)", 0, 1, 0);120ATF_REQUIRE_EQ(200, state.to_integer(-1));121lutok::do_string(state, "return my_math.add2(20, 30)", 0, 1, 0);122ATF_REQUIRE_EQ(50, state.to_integer(-1));123state.pop(3);124}125126127ATF_TEST_CASE_WITHOUT_HEAD(do_file__some_args);128ATF_TEST_CASE_BODY(do_file__some_args)129{130std::ofstream output("test.lua");131output << "local a1, a2 = ...\nreturn a1 * 2, a2 * 2\n";132output.close();133134lutok::state state;135state.push_integer(456);136state.push_integer(3);137state.push_integer(5);138state.push_integer(123);139ATF_REQUIRE_EQ(2, lutok::do_file(state, "test.lua", 3, -1, 0));140ATF_REQUIRE_EQ(3, state.get_top());141ATF_REQUIRE_EQ(456, state.to_integer(-3));142ATF_REQUIRE_EQ(6, state.to_integer(-2));143ATF_REQUIRE_EQ(10, state.to_integer(-1));144state.pop(3);145}146147148ATF_TEST_CASE_WITHOUT_HEAD(do_file__any_results);149ATF_TEST_CASE_BODY(do_file__any_results)150{151std::ofstream output("test.lua");152output << "return 10, 20, 30\n";153output.close();154155lutok::state state;156ATF_REQUIRE_EQ(3, lutok::do_file(state, "test.lua", 0, -1, 0));157ATF_REQUIRE_EQ(3, state.get_top());158ATF_REQUIRE_EQ(10, state.to_integer(-3));159ATF_REQUIRE_EQ(20, state.to_integer(-2));160ATF_REQUIRE_EQ(30, state.to_integer(-1));161state.pop(3);162}163164165ATF_TEST_CASE_WITHOUT_HEAD(do_file__no_results);166ATF_TEST_CASE_BODY(do_file__no_results)167{168std::ofstream output("test.lua");169output << "return 10, 20, 30\n";170output.close();171172lutok::state state;173ATF_REQUIRE_EQ(0, lutok::do_file(state, "test.lua", 0, 0, 0));174ATF_REQUIRE_EQ(0, state.get_top());175}176177178ATF_TEST_CASE_WITHOUT_HEAD(do_file__many_results);179ATF_TEST_CASE_BODY(do_file__many_results)180{181std::ofstream output("test.lua");182output << "return 10, 20, 30\n";183output.close();184185lutok::state state;186ATF_REQUIRE_EQ(2, lutok::do_file(state, "test.lua", 0, 2, 0));187ATF_REQUIRE_EQ(2, state.get_top());188ATF_REQUIRE_EQ(10, state.to_integer(-2));189ATF_REQUIRE_EQ(20, state.to_integer(-1));190state.pop(2);191}192193194ATF_TEST_CASE_WITHOUT_HEAD(do_file__not_found);195ATF_TEST_CASE_BODY(do_file__not_found)196{197lutok::state state;198stack_balance_checker checker(state);199ATF_REQUIRE_THROW_RE(lutok::file_not_found_error, "missing.lua",200lutok::do_file(state, "missing.lua", 0, 0, 0));201}202203204ATF_TEST_CASE_WITHOUT_HEAD(do_file__error);205ATF_TEST_CASE_BODY(do_file__error)206{207std::ofstream output("test.lua");208output << "a b c\n";209output.close();210211lutok::state state;212stack_balance_checker checker(state);213ATF_REQUIRE_THROW_RE(lutok::error, "Failed to load Lua file 'test.lua'",214lutok::do_file(state, "test.lua", 0, 0, 0));215}216217218ATF_TEST_CASE_WITHOUT_HEAD(do_file__error_with_errfunc);219ATF_TEST_CASE_BODY(do_file__error_with_errfunc)220{221std::ofstream output("test.lua");222output << "unknown_function()\n";223output.close();224225lutok::state state;226lutok::eval(state, "function(message) return 'This is an error!' end", 1);227{228stack_balance_checker checker(state);229ATF_REQUIRE_THROW_RE(lutok::error, "This is an error!",230lutok::do_file(state, "test.lua", 0, 0, -2));231}232state.pop(1);233}234235236ATF_TEST_CASE_WITHOUT_HEAD(do_string__some_args);237ATF_TEST_CASE_BODY(do_string__some_args)238{239lutok::state state;240state.push_integer(456);241state.push_integer(3);242state.push_integer(5);243state.push_integer(123);244ATF_REQUIRE_EQ(2, lutok::do_string(245state, "local a1, a2 = ...\nreturn a1 * 2, a2 * 2\n", 3, -1, 0));246ATF_REQUIRE_EQ(3, state.get_top());247ATF_REQUIRE_EQ(456, state.to_integer(-3));248ATF_REQUIRE_EQ(6, state.to_integer(-2));249ATF_REQUIRE_EQ(10, state.to_integer(-1));250state.pop(3);251}252253254ATF_TEST_CASE_WITHOUT_HEAD(do_string__any_results);255ATF_TEST_CASE_BODY(do_string__any_results)256{257lutok::state state;258ATF_REQUIRE_EQ(3, lutok::do_string(state, "return 10, 20, 30", 0, -1, 0));259ATF_REQUIRE_EQ(3, state.get_top());260ATF_REQUIRE_EQ(10, state.to_integer(-3));261ATF_REQUIRE_EQ(20, state.to_integer(-2));262ATF_REQUIRE_EQ(30, state.to_integer(-1));263state.pop(3);264}265266267ATF_TEST_CASE_WITHOUT_HEAD(do_string__no_results);268ATF_TEST_CASE_BODY(do_string__no_results)269{270lutok::state state;271ATF_REQUIRE_EQ(0, lutok::do_string(state, "return 10, 20, 30", 0, 0, 0));272ATF_REQUIRE_EQ(0, state.get_top());273}274275276ATF_TEST_CASE_WITHOUT_HEAD(do_string__many_results);277ATF_TEST_CASE_BODY(do_string__many_results)278{279lutok::state state;280ATF_REQUIRE_EQ(2, lutok::do_string(state, "return 10, 20, 30", 0, 2, 0));281ATF_REQUIRE_EQ(2, state.get_top());282ATF_REQUIRE_EQ(10, state.to_integer(-2));283ATF_REQUIRE_EQ(20, state.to_integer(-1));284state.pop(2);285}286287288ATF_TEST_CASE_WITHOUT_HEAD(do_string__error);289ATF_TEST_CASE_BODY(do_string__error)290{291lutok::state state;292stack_balance_checker checker(state);293ATF_REQUIRE_THROW_RE(lutok::error, "Failed to process Lua string 'a b c'",294lutok::do_string(state, "a b c", 0, 0, 0));295}296297298ATF_TEST_CASE_WITHOUT_HEAD(do_string__error_with_errfunc);299ATF_TEST_CASE_BODY(do_string__error_with_errfunc)300{301lutok::state state;302lutok::eval(state, "function(message) return 'This is an error!' end", 1);303{304stack_balance_checker checker(state);305ATF_REQUIRE_THROW_RE(lutok::error, "This is an error!",306lutok::do_string(state, "unknown_function()",3070, 0, -2));308}309state.pop(1);310}311312313ATF_TEST_CASE_WITHOUT_HEAD(eval__one_result);314ATF_TEST_CASE_BODY(eval__one_result)315{316lutok::state state;317stack_balance_checker checker(state);318lutok::eval(state, "3 + 10", 1);319ATF_REQUIRE_EQ(13, state.to_integer(-1));320state.pop(1);321}322323324ATF_TEST_CASE_WITHOUT_HEAD(eval__many_results);325ATF_TEST_CASE_BODY(eval__many_results)326{327lutok::state state;328stack_balance_checker checker(state);329lutok::eval(state, "5, 8, 10", 3);330ATF_REQUIRE_EQ(5, state.to_integer(-3));331ATF_REQUIRE_EQ(8, state.to_integer(-2));332ATF_REQUIRE_EQ(10, state.to_integer(-1));333state.pop(3);334}335336337ATF_TEST_CASE_WITHOUT_HEAD(eval__error);338ATF_TEST_CASE_BODY(eval__error)339{340lutok::state state;341stack_balance_checker checker(state);342ATF_REQUIRE_THROW(lutok::error,343lutok::eval(state, "non_existent.method()", 1));344}345346347ATF_INIT_TEST_CASES(tcs)348{349ATF_ADD_TEST_CASE(tcs, create_module__empty);350ATF_ADD_TEST_CASE(tcs, create_module__one);351ATF_ADD_TEST_CASE(tcs, create_module__many);352353ATF_ADD_TEST_CASE(tcs, do_file__some_args);354ATF_ADD_TEST_CASE(tcs, do_file__any_results);355ATF_ADD_TEST_CASE(tcs, do_file__no_results);356ATF_ADD_TEST_CASE(tcs, do_file__many_results);357ATF_ADD_TEST_CASE(tcs, do_file__not_found);358ATF_ADD_TEST_CASE(tcs, do_file__error);359ATF_ADD_TEST_CASE(tcs, do_file__error_with_errfunc);360361ATF_ADD_TEST_CASE(tcs, do_string__some_args);362ATF_ADD_TEST_CASE(tcs, do_string__any_results);363ATF_ADD_TEST_CASE(tcs, do_string__no_results);364ATF_ADD_TEST_CASE(tcs, do_string__many_results);365ATF_ADD_TEST_CASE(tcs, do_string__error);366ATF_ADD_TEST_CASE(tcs, do_string__error_with_errfunc);367368ATF_ADD_TEST_CASE(tcs, eval__one_result);369ATF_ADD_TEST_CASE(tcs, eval__many_results);370ATF_ADD_TEST_CASE(tcs, eval__error);371}372373374