The Battle for Wesnoth  1.19.15+dev
test_lua_ptr.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024 - 2025
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  // test move constructor
37  auto obj2 = std::move(obj);
38  BOOST_CHECK(ptr);
39  BOOST_CHECK_EQUAL(ptr.get_ptr(), &obj2);
40  BOOST_CHECK_EQUAL(ptr->value, "test");
41 
42  // NOLINTNEXTLINE(bugprone-use-after-move)
43  lua_ptr<dummy_object> ptr_from_moved_object(obj);
44  BOOST_CHECK(!ptr_from_moved_object);
45 
46  // test move assignment
47  dummy_object obj3{"different"};
48  obj3 = std::move(obj2);
49  BOOST_CHECK(ptr);
50  BOOST_CHECK_EQUAL(ptr.get_ptr(), &obj3);
51  BOOST_CHECK_EQUAL(ptr->value, "test");
52 
53  vec.clear();
54  BOOST_CHECK(ptr);
55  BOOST_CHECK_EQUAL(ptr->value, "test");
56  BOOST_CHECK(!ptr_from_moved_object);
57  }
58  BOOST_CHECK(!ptr);
59 }
Allows creation of lua_ptr<T> instances, but does not affect the lifetime of the T itself.
Definition: lua_ptr.hpp:34
T must inherit enable_lua_ptr<T>
Definition: lua_ptr.hpp:66
T * get_ptr()
Definition: lua_ptr.hpp:69
dummy_object(const std::string &s)
std::string value
BOOST_AUTO_TEST_CASE(test_lua_ptr)
static map_location::direction s