test_resource.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*************************************************************************/
  2. /* test_resource.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_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. namespace TestResource {
  38. TEST_CASE("[Resource] Duplication") {
  39. Ref<Resource> resource = memnew(Resource);
  40. resource->set_name("Hello world");
  41. Ref<Resource> child_resource = memnew(Resource);
  42. child_resource->set_name("I'm a child resource");
  43. resource->set_meta("other_resource", child_resource);
  44. Ref<Resource> resource_dupe = resource->duplicate();
  45. const Ref<Resource> &resource_dupe_reference = resource_dupe;
  46. resource_dupe->set_name("Changed name");
  47. child_resource->set_name("My name was changed too");
  48. CHECK_MESSAGE(
  49. resource_dupe->get_name() == "Changed name",
  50. "Duplicated resource should have the new name.");
  51. CHECK_MESSAGE(
  52. resource_dupe_reference->get_name() == "Changed name",
  53. "Reference to the duplicated resource should have the new name.");
  54. CHECK_MESSAGE(
  55. resource->get_name() == "Hello world",
  56. "Original resource name should not be affected after editing the duplicate's name.");
  57. CHECK_MESSAGE(
  58. Ref<Resource>(resource_dupe->get_meta("other_resource"))->get_name() == "My name was changed too",
  59. "Duplicated resource should share its child resource with the original.");
  60. }
  61. TEST_CASE("[Resource] Saving and loading") {
  62. Ref<Resource> resource = memnew(Resource);
  63. resource->set_name("Hello world");
  64. resource->set_meta("ExampleMetadata", Vector2i(40, 80));
  65. resource->set_meta("string", "The\nstring\nwith\nunnecessary\nline\n\t\\\nbreaks");
  66. Ref<Resource> child_resource = memnew(Resource);
  67. child_resource->set_name("I'm a child resource");
  68. resource->set_meta("other_resource", child_resource);
  69. const String save_path_binary = OS::get_singleton()->get_cache_path().plus_file("resource.res");
  70. const String save_path_text = OS::get_singleton()->get_cache_path().plus_file("resource.tres");
  71. ResourceSaver::save(resource, save_path_binary);
  72. ResourceSaver::save(resource, save_path_text);
  73. const Ref<Resource> &loaded_resource_binary = ResourceLoader::load(save_path_binary);
  74. CHECK_MESSAGE(
  75. loaded_resource_binary->get_name() == "Hello world",
  76. "The loaded resource name should be equal to the expected value.");
  77. CHECK_MESSAGE(
  78. loaded_resource_binary->get_meta("ExampleMetadata") == Vector2i(40, 80),
  79. "The loaded resource metadata should be equal to the expected value.");
  80. CHECK_MESSAGE(
  81. loaded_resource_binary->get_meta("string") == "The\nstring\nwith\nunnecessary\nline\n\t\\\nbreaks",
  82. "The loaded resource metadata should be equal to the expected value.");
  83. const Ref<Resource> &loaded_child_resource_binary = loaded_resource_binary->get_meta("other_resource");
  84. CHECK_MESSAGE(
  85. loaded_child_resource_binary->get_name() == "I'm a child resource",
  86. "The loaded child resource name should be equal to the expected value.");
  87. const Ref<Resource> &loaded_resource_text = ResourceLoader::load(save_path_text);
  88. CHECK_MESSAGE(
  89. loaded_resource_text->get_name() == "Hello world",
  90. "The loaded resource name should be equal to the expected value.");
  91. CHECK_MESSAGE(
  92. loaded_resource_text->get_meta("ExampleMetadata") == Vector2i(40, 80),
  93. "The loaded resource metadata should be equal to the expected value.");
  94. CHECK_MESSAGE(
  95. loaded_resource_text->get_meta("string") == "The\nstring\nwith\nunnecessary\nline\n\t\\\nbreaks",
  96. "The loaded resource metadata should be equal to the expected value.");
  97. const Ref<Resource> &loaded_child_resource_text = loaded_resource_text->get_meta("other_resource");
  98. CHECK_MESSAGE(
  99. loaded_child_resource_text->get_name() == "I'm a child resource",
  100. "The loaded child resource name should be equal to the expected value.");
  101. }
  102. } // namespace TestResource
  103. #endif // TEST_RESOURCE_H