2
0

test_object.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. const Vector<Multiplayer::RPCConfig> get_rpc_methods() const override {
  87. return Vector<Multiplayer::RPCConfig>();
  88. }
  89. ScriptLanguage *get_language() override {
  90. return nullptr;
  91. }
  92. };
  93. TEST_CASE("[Object] Core getters") {
  94. Object object;
  95. CHECK_MESSAGE(
  96. object.is_class("Object"),
  97. "is_class() should return the expected value.");
  98. CHECK_MESSAGE(
  99. object.get_class() == "Object",
  100. "The returned class should match the expected value.");
  101. CHECK_MESSAGE(
  102. object.get_class_name() == "Object",
  103. "The returned class name should match the expected value.");
  104. CHECK_MESSAGE(
  105. object.get_class_static() == "Object",
  106. "The returned static class should match the expected value.");
  107. CHECK_MESSAGE(
  108. object.get_save_class() == "Object",
  109. "The returned save class should match the expected value.");
  110. List<String> inheritance_list;
  111. object.get_inheritance_list_static(&inheritance_list);
  112. CHECK_MESSAGE(
  113. inheritance_list.size() == 1,
  114. "The inheritance list should consist of Object only");
  115. CHECK_MESSAGE(
  116. inheritance_list[0] == "Object",
  117. "The inheritance list should consist of Object only");
  118. }
  119. TEST_CASE("[Object] Metadata") {
  120. const String meta_path = "hello/world complex métadata\n\n\t\tpath";
  121. Object object;
  122. object.set_meta(meta_path, Color(0, 1, 0));
  123. CHECK_MESSAGE(
  124. Color(object.get_meta(meta_path)).is_equal_approx(Color(0, 1, 0)),
  125. "The returned object metadata after setting should match the expected value.");
  126. List<StringName> meta_list;
  127. object.get_meta_list(&meta_list);
  128. CHECK_MESSAGE(
  129. meta_list.size() == 1,
  130. "The metadata list should only contain 1 item after adding one metadata item.");
  131. object.remove_meta(meta_path);
  132. // Also try removing nonexistent metadata (it should do nothing, without printing an error message).
  133. object.remove_meta("I don't exist");
  134. ERR_PRINT_OFF;
  135. CHECK_MESSAGE(
  136. object.get_meta(meta_path) == Variant(),
  137. "The returned object metadata after removing should match the expected value.");
  138. ERR_PRINT_ON;
  139. List<StringName> meta_list2;
  140. object.get_meta_list(&meta_list2);
  141. CHECK_MESSAGE(
  142. meta_list2.size() == 0,
  143. "The metadata list should contain 0 items after removing all metadata items.");
  144. }
  145. TEST_CASE("[Object] Construction") {
  146. Object object;
  147. CHECK_MESSAGE(
  148. !object.is_ref_counted(),
  149. "Object is not a RefCounted.");
  150. Object *p_db = ObjectDB::get_instance(object.get_instance_id());
  151. CHECK_MESSAGE(
  152. p_db == &object,
  153. "The database pointer returned by the object id should reference same object.");
  154. }
  155. TEST_CASE("[Object] Script instance property setter") {
  156. Object object;
  157. _MockScriptInstance *script_instance = memnew(_MockScriptInstance);
  158. object.set_script_instance(script_instance);
  159. bool valid = false;
  160. object.set("some_name", 100, &valid);
  161. CHECK(valid);
  162. Variant actual_value;
  163. CHECK_MESSAGE(
  164. script_instance->get("some_name", actual_value),
  165. "The assigned script instance should successfully retrieve value by name.");
  166. CHECK_MESSAGE(
  167. actual_value == Variant(100),
  168. "The returned value should equal the one which was set by the object.");
  169. }
  170. TEST_CASE("[Object] Script instance property getter") {
  171. Object object;
  172. _MockScriptInstance *script_instance = memnew(_MockScriptInstance);
  173. script_instance->set("some_name", 100); // Make sure script instance has the property
  174. object.set_script_instance(script_instance);
  175. bool valid = false;
  176. const Variant &actual_value = object.get("some_name", &valid);
  177. CHECK(valid);
  178. CHECK_MESSAGE(
  179. actual_value == Variant(100),
  180. "The returned value should equal the one which was set by the script instance.");
  181. }
  182. TEST_CASE("[Object] Built-in property setter") {
  183. GDREGISTER_CLASS(_TestDerivedObject);
  184. _TestDerivedObject derived_object;
  185. bool valid = false;
  186. derived_object.set("property", 100, &valid);
  187. CHECK(valid);
  188. CHECK_MESSAGE(
  189. derived_object.get_property() == 100,
  190. "The property value should equal the one which was set with built-in setter.");
  191. }
  192. TEST_CASE("[Object] Built-in property getter") {
  193. GDREGISTER_CLASS(_TestDerivedObject);
  194. _TestDerivedObject derived_object;
  195. derived_object.set_property(100);
  196. bool valid = false;
  197. const Variant &actual_value = derived_object.get("property", &valid);
  198. CHECK(valid);
  199. CHECK_MESSAGE(
  200. actual_value == Variant(100),
  201. "The returned value should equal the one which was set with built-in setter.");
  202. }
  203. TEST_CASE("[Object] Script property setter") {
  204. Object object;
  205. Variant script;
  206. bool valid = false;
  207. object.set(CoreStringNames::get_singleton()->_script, script, &valid);
  208. CHECK(valid);
  209. CHECK_MESSAGE(
  210. object.get_script() == script,
  211. "The object script should be equal to the assigned one.");
  212. }
  213. TEST_CASE("[Object] Script property getter") {
  214. Object object;
  215. Variant script;
  216. object.set_script(script);
  217. bool valid = false;
  218. const Variant &actual_value = object.get(CoreStringNames::get_singleton()->_script, &valid);
  219. CHECK(valid);
  220. CHECK_MESSAGE(
  221. actual_value == script,
  222. "The returned value should be equal to the assigned script.");
  223. }
  224. TEST_CASE("[Object] Absent name setter") {
  225. Object object;
  226. bool valid = true;
  227. object.set("absent_name", 100, &valid);
  228. CHECK(!valid);
  229. }
  230. TEST_CASE("[Object] Absent name getter") {
  231. Object object;
  232. bool valid = true;
  233. const Variant &actual_value = object.get("absent_name", &valid);
  234. CHECK(!valid);
  235. CHECK_MESSAGE(
  236. actual_value == Variant(),
  237. "The returned value should equal nil variant.");
  238. }
  239. } // namespace TestObject
  240. #endif // TEST_OBJECT_H