test_object.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*************************************************************************/
  2. /* test_object.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TEST_OBJECT_H
  31. #define TEST_OBJECT_H
  32. #include "core/core_string_names.h"
  33. #include "core/object/object.h"
  34. #include "thirdparty/doctest/doctest.h"
  35. // Declared in global namespace because of GDCLASS macro warning (Windows):
  36. // "Unqualified friend declaration referring to type outside of the nearest enclosing namespace
  37. // is a Microsoft extension; add a nested name specifier".
  38. class _TestDerivedObject : public Object {
  39. GDCLASS(_TestDerivedObject, Object);
  40. int property_value;
  41. protected:
  42. static void _bind_methods() {
  43. ClassDB::bind_method(D_METHOD("set_property", "property"), &_TestDerivedObject::set_property);
  44. ClassDB::bind_method(D_METHOD("get_property"), &_TestDerivedObject::get_property);
  45. ADD_PROPERTY(PropertyInfo(Variant::INT, "property"), "set_property", "get_property");
  46. }
  47. public:
  48. void set_property(int value) { property_value = value; }
  49. int get_property() const { return property_value; }
  50. };
  51. namespace TestObject {
  52. class _MockScriptInstance : public ScriptInstance {
  53. StringName property_name = "NO_NAME";
  54. Variant property_value;
  55. public:
  56. bool set(const StringName &p_name, const Variant &p_value) override {
  57. property_name = p_name;
  58. property_value = p_value;
  59. return true;
  60. }
  61. bool get(const StringName &p_name, Variant &r_ret) const override {
  62. if (property_name == p_name) {
  63. r_ret = property_value;
  64. return true;
  65. }
  66. return false;
  67. }
  68. void get_property_list(List<PropertyInfo> *p_properties) const override {
  69. }
  70. Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid) const override {
  71. return Variant::PACKED_FLOAT32_ARRAY;
  72. }
  73. void get_method_list(List<MethodInfo> *p_list) const override {
  74. }
  75. bool has_method(const StringName &p_method) const override {
  76. return false;
  77. }
  78. Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {
  79. return Variant();
  80. }
  81. void notification(int p_notification) override {
  82. }
  83. Ref<Script> get_script() const override {
  84. return Ref<Script>();
  85. }
  86. Vector<ScriptNetData> get_rpc_methods() const override {
  87. return Vector<ScriptNetData>();
  88. }
  89. uint16_t get_rpc_method_id(const StringName &p_method) const override {
  90. return 0;
  91. }
  92. StringName get_rpc_method(uint16_t p_id) const override {
  93. return StringName();
  94. }
  95. MultiplayerAPI::RPCMode get_rpc_mode_by_id(uint16_t p_id) const override {
  96. return MultiplayerAPI::RPC_MODE_PUPPET;
  97. }
  98. MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const override {
  99. return MultiplayerAPI::RPC_MODE_PUPPET;
  100. }
  101. Vector<ScriptNetData> get_rset_properties() const override {
  102. return Vector<ScriptNetData>();
  103. }
  104. uint16_t get_rset_property_id(const StringName &p_variable) const override {
  105. return 0;
  106. }
  107. StringName get_rset_property(uint16_t p_id) const override {
  108. return StringName();
  109. }
  110. MultiplayerAPI::RPCMode get_rset_mode_by_id(uint16_t p_id) const override {
  111. return MultiplayerAPI::RPC_MODE_PUPPET;
  112. }
  113. MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const override {
  114. return MultiplayerAPI::RPC_MODE_PUPPET;
  115. }
  116. ScriptLanguage *get_language() override {
  117. return nullptr;
  118. }
  119. };
  120. TEST_CASE("[Object] Core getters") {
  121. Object object;
  122. CHECK_MESSAGE(
  123. object.is_class("Object"),
  124. "is_class() should return the expected value.");
  125. CHECK_MESSAGE(
  126. object.get_class() == "Object",
  127. "The returned class should match the expected value.");
  128. CHECK_MESSAGE(
  129. object.get_class_name() == "Object",
  130. "The returned class name should match the expected value.");
  131. CHECK_MESSAGE(
  132. object.get_class_static() == "Object",
  133. "The returned static class should match the expected value.");
  134. CHECK_MESSAGE(
  135. object.get_save_class() == "Object",
  136. "The returned save class should match the expected value.");
  137. List<String> inheritance_list;
  138. object.get_inheritance_list_static(&inheritance_list);
  139. CHECK_MESSAGE(
  140. inheritance_list.size() == 1,
  141. "The inheritance list should consist of Object only");
  142. CHECK_MESSAGE(
  143. inheritance_list[0] == "Object",
  144. "The inheritance list should consist of Object only");
  145. }
  146. TEST_CASE("[Object] Metadata") {
  147. const String meta_path = "hello/world complex métadata\n\n\t\tpath";
  148. Object object;
  149. object.set_meta(meta_path, Color(0, 1, 0));
  150. CHECK_MESSAGE(
  151. Color(object.get_meta(meta_path)).is_equal_approx(Color(0, 1, 0)),
  152. "The returned object metadata after setting should match the expected value.");
  153. List<String> meta_list;
  154. object.get_meta_list(&meta_list);
  155. CHECK_MESSAGE(
  156. meta_list.size() == 1,
  157. "The metadata list should only contain 1 item after adding one metadata item.");
  158. object.remove_meta(meta_path);
  159. // Also try removing nonexistent metadata (it should do nothing, without printing an error message).
  160. object.remove_meta("I don't exist");
  161. ERR_PRINT_OFF;
  162. CHECK_MESSAGE(
  163. object.get_meta(meta_path) == Variant(),
  164. "The returned object metadata after removing should match the expected value.");
  165. ERR_PRINT_ON;
  166. List<String> meta_list2;
  167. object.get_meta_list(&meta_list2);
  168. CHECK_MESSAGE(
  169. meta_list2.size() == 0,
  170. "The metadata list should contain 0 items after removing all metadata items.");
  171. }
  172. TEST_CASE("[Object] Construction") {
  173. Object object;
  174. CHECK_MESSAGE(
  175. !object.is_reference(),
  176. "Object is not a Reference.");
  177. Object *p_db = ObjectDB::get_instance(object.get_instance_id());
  178. CHECK_MESSAGE(
  179. p_db == &object,
  180. "The database pointer returned by the object id should reference same object.");
  181. }
  182. TEST_CASE("[Object] Script instance property setter") {
  183. Object object;
  184. _MockScriptInstance *script_instance = memnew(_MockScriptInstance);
  185. object.set_script_instance(script_instance);
  186. bool valid = false;
  187. object.set("some_name", 100, &valid);
  188. CHECK(valid);
  189. Variant actual_value;
  190. CHECK_MESSAGE(
  191. script_instance->get("some_name", actual_value),
  192. "The assigned script instance should successfully retrieve value by name.");
  193. CHECK_MESSAGE(
  194. actual_value == Variant(100),
  195. "The returned value should equal the one which was set by the object.");
  196. }
  197. TEST_CASE("[Object] Script instance property getter") {
  198. Object object;
  199. _MockScriptInstance *script_instance = memnew(_MockScriptInstance);
  200. script_instance->set("some_name", 100); // Make sure script instance has the property
  201. object.set_script_instance(script_instance);
  202. bool valid = false;
  203. const Variant &actual_value = object.get("some_name", &valid);
  204. CHECK(valid);
  205. CHECK_MESSAGE(
  206. actual_value == Variant(100),
  207. "The returned value should equal the one which was set by the script instance.");
  208. }
  209. TEST_CASE("[Object] Built-in property setter") {
  210. ClassDB::register_class<_TestDerivedObject>();
  211. _TestDerivedObject derived_object;
  212. bool valid = false;
  213. derived_object.set("property", 100, &valid);
  214. CHECK(valid);
  215. CHECK_MESSAGE(
  216. derived_object.get_property() == 100,
  217. "The property value should equal the one which was set with built-in setter.");
  218. }
  219. TEST_CASE("[Object] Built-in property getter") {
  220. ClassDB::register_class<_TestDerivedObject>();
  221. _TestDerivedObject derived_object;
  222. derived_object.set_property(100);
  223. bool valid = false;
  224. const Variant &actual_value = derived_object.get("property", &valid);
  225. CHECK(valid);
  226. CHECK_MESSAGE(
  227. actual_value == Variant(100),
  228. "The returned value should equal the one which was set with built-in setter.");
  229. }
  230. TEST_CASE("[Object] Script property setter") {
  231. Object object;
  232. Variant script;
  233. bool valid = false;
  234. object.set(CoreStringNames::get_singleton()->_script, script, &valid);
  235. CHECK(valid);
  236. CHECK_MESSAGE(
  237. object.get_script() == script,
  238. "The object script should be equal to the assigned one.");
  239. }
  240. TEST_CASE("[Object] Script property getter") {
  241. Object object;
  242. Variant script;
  243. object.set_script(script);
  244. bool valid = false;
  245. const Variant &actual_value = object.get(CoreStringNames::get_singleton()->_script, &valid);
  246. CHECK(valid);
  247. CHECK_MESSAGE(
  248. actual_value == script,
  249. "The returned value should be equal to the assigned script.");
  250. }
  251. TEST_CASE("[Object] Absent name setter") {
  252. Object object;
  253. bool valid = true;
  254. object.set("absent_name", 100, &valid);
  255. CHECK(!valid);
  256. }
  257. TEST_CASE("[Object] Absent name getter") {
  258. Object object;
  259. bool valid = true;
  260. const Variant &actual_value = object.get("absent_name", &valid);
  261. CHECK(!valid);
  262. CHECK_MESSAGE(
  263. actual_value == Variant(),
  264. "The returned value should equal nil variant.");
  265. }
  266. } // namespace TestObject
  267. #endif // TEST_OBJECT_H