The Battle for Wesnoth  1.19.8+dev
test_lua_ptr.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13  COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-test"
17 
18 #include "scripting/lua_ptr.hpp"
19 #include <boost/test/unit_test.hpp>
20 #include <string>
21 #include <vector>
22 
23 struct dummy_object : public enable_lua_ptr<dummy_object> {
24  std::string value;
25  dummy_object(const std::string& s) : enable_lua_ptr<dummy_object>(this), value(s) {}
26 };
27 
28 BOOST_AUTO_TEST_CASE(test_lua_ptr) {
29  std::vector<dummy_object> vec;
30  auto& obj = vec.emplace_back("test");
31  BOOST_CHECK_EQUAL(obj.value, "test");
32  lua_ptr<dummy_object> ptr(obj);
33  BOOST_CHECK(ptr);
34  BOOST_CHECK_EQUAL(ptr.get_ptr(), &obj);
35  {
36  auto obj_moved = std::move(obj);
37  BOOST_CHECK(ptr);
38  BOOST_CHECK_EQUAL(ptr.get_ptr(), &obj_moved);
39  BOOST_CHECK_EQUAL(ptr->value, "test");
40  vec.clear();
41  }
42  BOOST_CHECK(!ptr);
43 }
Tmust inherit enable_lua_ptr<T>
Definition: lua_ptr.hpp:45
T * get_ptr()
Definition: lua_ptr.hpp:48
dummy_object(const std::string &s)
std::string value
BOOST_AUTO_TEST_CASE(test_lua_ptr)
static map_location::direction s