test_resource.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**************************************************************************/
  2. /* test_resource.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_RESOURCE_H
  31. #define TEST_RESOURCE_H
  32. #include "core/io/resource.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/io/resource_saver.h"
  35. #include "core/os/os.h"
  36. #include "thirdparty/doctest/doctest.h"
  37. #include "tests/test_macros.h"
  38. namespace TestResource {
  39. TEST_CASE("[Resource] Duplication") {
  40. Ref<Resource> resource = memnew(Resource);
  41. resource->set_name("Hello world");
  42. Ref<Resource> child_resource = memnew(Resource);
  43. child_resource->set_name("I'm a child resource");
  44. resource->set_meta("other_resource", child_resource);
  45. Ref<Resource> resource_dupe = resource->duplicate();
  46. const Ref<Resource> &resource_dupe_reference = resource_dupe;
  47. resource_dupe->set_name("Changed name");
  48. child_resource->set_name("My name was changed too");
  49. CHECK_MESSAGE(
  50. resource_dupe->get_name() == "Changed name",
  51. "Duplicated resource should have the new name.");
  52. CHECK_MESSAGE(
  53. resource_dupe_reference->get_name() == "Changed name",
  54. "Reference to the duplicated resource should have the new name.");
  55. CHECK_MESSAGE(
  56. resource->get_name() == "Hello world",
  57. "Original resource name should not be affected after editing the duplicate's name.");
  58. CHECK_MESSAGE(
  59. Ref<Resource>(resource_dupe->get_meta("other_resource"))->get_name() == "My name was changed too",
  60. "Duplicated resource should share its child resource with the original.");
  61. }
  62. TEST_CASE("[Resource] Saving and loading") {
  63. Ref<Resource> resource = memnew(Resource);
  64. resource->set_name("Hello world");
  65. resource->set_meta("ExampleMetadata", Vector2i(40, 80));
  66. resource->set_meta("string", "The\nstring\nwith\nunnecessary\nline\n\t\\\nbreaks");
  67. Ref<Resource> child_resource = memnew(Resource);
  68. child_resource->set_name("I'm a child resource");
  69. resource->set_meta("other_resource", child_resource);
  70. const String save_path_binary = TestUtils::get_temp_path("resource.res");
  71. const String save_path_text = TestUtils::get_temp_path("resource.tres");
  72. ResourceSaver::save(resource, save_path_binary);
  73. ResourceSaver::save(resource, save_path_text);
  74. const Ref<Resource> &loaded_resource_binary = ResourceLoader::load(save_path_binary);
  75. CHECK_MESSAGE(
  76. loaded_resource_binary->get_name() == "Hello world",
  77. "The loaded resource name should be equal to the expected value.");
  78. CHECK_MESSAGE(
  79. loaded_resource_binary->get_meta("ExampleMetadata") == Vector2i(40, 80),
  80. "The loaded resource metadata should be equal to the expected value.");
  81. CHECK_MESSAGE(
  82. loaded_resource_binary->get_meta("string") == "The\nstring\nwith\nunnecessary\nline\n\t\\\nbreaks",
  83. "The loaded resource metadata should be equal to the expected value.");
  84. const Ref<Resource> &loaded_child_resource_binary = loaded_resource_binary->get_meta("other_resource");
  85. CHECK_MESSAGE(
  86. loaded_child_resource_binary->get_name() == "I'm a child resource",
  87. "The loaded child resource name should be equal to the expected value.");
  88. const Ref<Resource> &loaded_resource_text = ResourceLoader::load(save_path_text);
  89. CHECK_MESSAGE(
  90. loaded_resource_text->get_name() == "Hello world",
  91. "The loaded resource name should be equal to the expected value.");
  92. CHECK_MESSAGE(
  93. loaded_resource_text->get_meta("ExampleMetadata") == Vector2i(40, 80),
  94. "The loaded resource metadata should be equal to the expected value.");
  95. CHECK_MESSAGE(
  96. loaded_resource_text->get_meta("string") == "The\nstring\nwith\nunnecessary\nline\n\t\\\nbreaks",
  97. "The loaded resource metadata should be equal to the expected value.");
  98. const Ref<Resource> &loaded_child_resource_text = loaded_resource_text->get_meta("other_resource");
  99. CHECK_MESSAGE(
  100. loaded_child_resource_text->get_name() == "I'm a child resource",
  101. "The loaded child resource name should be equal to the expected value.");
  102. }
  103. TEST_CASE("[Resource] Breaking circular references on save") {
  104. Ref<Resource> resource_a = memnew(Resource);
  105. resource_a->set_name("A");
  106. Ref<Resource> resource_b = memnew(Resource);
  107. resource_b->set_name("B");
  108. Ref<Resource> resource_c = memnew(Resource);
  109. resource_c->set_name("C");
  110. resource_a->set_meta("next", resource_b);
  111. resource_b->set_meta("next", resource_c);
  112. resource_c->set_meta("next", resource_b);
  113. const String save_path_binary = TestUtils::get_temp_path("resource.res");
  114. const String save_path_text = TestUtils::get_temp_path("resource.tres");
  115. ResourceSaver::save(resource_a, save_path_binary);
  116. // Suppress expected errors caused by the resources above being uncached.
  117. ERR_PRINT_OFF;
  118. ResourceSaver::save(resource_a, save_path_text);
  119. const Ref<Resource> &loaded_resource_a_binary = ResourceLoader::load(save_path_binary);
  120. ERR_PRINT_ON;
  121. CHECK_MESSAGE(
  122. loaded_resource_a_binary->get_name() == "A",
  123. "The loaded resource name should be equal to the expected value.");
  124. const Ref<Resource> &loaded_resource_b_binary = loaded_resource_a_binary->get_meta("next");
  125. CHECK_MESSAGE(
  126. loaded_resource_b_binary->get_name() == "B",
  127. "The loaded child resource name should be equal to the expected value.");
  128. const Ref<Resource> &loaded_resource_c_binary = loaded_resource_b_binary->get_meta("next");
  129. CHECK_MESSAGE(
  130. loaded_resource_c_binary->get_name() == "C",
  131. "The loaded child resource name should be equal to the expected value.");
  132. CHECK_MESSAGE(
  133. !loaded_resource_c_binary->has_meta("next"),
  134. "The loaded child resource circular reference should be NULL.");
  135. const Ref<Resource> &loaded_resource_a_text = ResourceLoader::load(save_path_text);
  136. CHECK_MESSAGE(
  137. loaded_resource_a_text->get_name() == "A",
  138. "The loaded resource name should be equal to the expected value.");
  139. const Ref<Resource> &loaded_resource_b_text = loaded_resource_a_text->get_meta("next");
  140. CHECK_MESSAGE(
  141. loaded_resource_b_text->get_name() == "B",
  142. "The loaded child resource name should be equal to the expected value.");
  143. const Ref<Resource> &loaded_resource_c_text = loaded_resource_b_text->get_meta("next");
  144. CHECK_MESSAGE(
  145. loaded_resource_c_text->get_name() == "C",
  146. "The loaded child resource name should be equal to the expected value.");
  147. CHECK_MESSAGE(
  148. !loaded_resource_c_text->has_meta("next"),
  149. "The loaded child resource circular reference should be NULL.");
  150. // Break circular reference to avoid memory leak
  151. resource_c->remove_meta("next");
  152. }
  153. } // namespace TestResource
  154. #endif // TEST_RESOURCE_H