test_variant_utility.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**************************************************************************/
  2. /* test_variant_utility.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_VARIANT_UTILITY_H
  31. #define TEST_VARIANT_UTILITY_H
  32. #include "core/variant/variant_utility.h"
  33. #include "tests/test_macros.h"
  34. namespace TestVariantUtility {
  35. TEST_CASE("[VariantUtility] Type conversion") {
  36. Variant converted;
  37. converted = VariantUtilityFunctions::type_convert("Hi!", Variant::Type::NIL);
  38. CHECK(converted.get_type() == Variant::Type::NIL);
  39. CHECK(converted == Variant());
  40. converted = VariantUtilityFunctions::type_convert("Hi!", Variant::Type::INT);
  41. CHECK(converted.get_type() == Variant::Type::INT);
  42. CHECK(converted == Variant(0));
  43. converted = VariantUtilityFunctions::type_convert("123", Variant::Type::INT);
  44. CHECK(converted.get_type() == Variant::Type::INT);
  45. CHECK(converted == Variant(123));
  46. converted = VariantUtilityFunctions::type_convert(123, Variant::Type::STRING);
  47. CHECK(converted.get_type() == Variant::Type::STRING);
  48. CHECK(converted == Variant("123"));
  49. converted = VariantUtilityFunctions::type_convert(123.4, Variant::Type::INT);
  50. CHECK(converted.get_type() == Variant::Type::INT);
  51. CHECK(converted == Variant(123));
  52. converted = VariantUtilityFunctions::type_convert(5, Variant::Type::VECTOR2);
  53. CHECK(converted.get_type() == Variant::Type::VECTOR2);
  54. CHECK(converted == Variant(Vector2(0, 0)));
  55. converted = VariantUtilityFunctions::type_convert(Vector3(1, 2, 3), Variant::Type::VECTOR2);
  56. CHECK(converted.get_type() == Variant::Type::VECTOR2);
  57. CHECK(converted == Variant(Vector2(1, 2)));
  58. converted = VariantUtilityFunctions::type_convert(Vector2(1, 2), Variant::Type::VECTOR4);
  59. CHECK(converted.get_type() == Variant::Type::VECTOR4);
  60. CHECK(converted == Variant(Vector4(1, 2, 0, 0)));
  61. converted = VariantUtilityFunctions::type_convert(Vector4(1.2, 3.4, 5.6, 7.8), Variant::Type::VECTOR3I);
  62. CHECK(converted.get_type() == Variant::Type::VECTOR3I);
  63. CHECK(converted == Variant(Vector3i(1, 3, 5)));
  64. {
  65. Basis basis = Basis::from_scale(Vector3(1.2, 3.4, 5.6));
  66. Transform3D transform = Transform3D(basis, Vector3());
  67. converted = VariantUtilityFunctions::type_convert(transform, Variant::Type::BASIS);
  68. CHECK(converted.get_type() == Variant::Type::BASIS);
  69. CHECK(converted == basis);
  70. converted = VariantUtilityFunctions::type_convert(basis, Variant::Type::TRANSFORM3D);
  71. CHECK(converted.get_type() == Variant::Type::TRANSFORM3D);
  72. CHECK(converted == transform);
  73. converted = VariantUtilityFunctions::type_convert(basis, Variant::Type::STRING);
  74. CHECK(converted.get_type() == Variant::Type::STRING);
  75. CHECK(converted == Variant("[X: (1.2, 0.0, 0.0), Y: (0.0, 3.4, 0.0), Z: (0.0, 0.0, 5.6)]"));
  76. }
  77. {
  78. Array arr;
  79. arr.push_back(1.2);
  80. arr.push_back(3.4);
  81. arr.push_back(5.6);
  82. PackedFloat64Array packed;
  83. packed.push_back(1.2);
  84. packed.push_back(3.4);
  85. packed.push_back(5.6);
  86. converted = VariantUtilityFunctions::type_convert(arr, Variant::Type::PACKED_FLOAT64_ARRAY);
  87. CHECK(converted.get_type() == Variant::Type::PACKED_FLOAT64_ARRAY);
  88. CHECK(converted == packed);
  89. converted = VariantUtilityFunctions::type_convert(packed, Variant::Type::ARRAY);
  90. CHECK(converted.get_type() == Variant::Type::ARRAY);
  91. CHECK(converted == arr);
  92. }
  93. {
  94. // Check that using Variant::call_utility_function also works.
  95. Vector<const Variant *> args;
  96. Variant data_arg = "Hi!";
  97. args.push_back(&data_arg);
  98. Variant type_arg = Variant::Type::NIL;
  99. args.push_back(&type_arg);
  100. Callable::CallError call_error;
  101. Variant::call_utility_function("type_convert", &converted, (const Variant **)args.ptr(), 2, call_error);
  102. CHECK(converted.get_type() == Variant::Type::NIL);
  103. CHECK(converted == Variant());
  104. type_arg = Variant::Type::INT;
  105. Variant::call_utility_function("type_convert", &converted, (const Variant **)args.ptr(), 2, call_error);
  106. CHECK(converted.get_type() == Variant::Type::INT);
  107. CHECK(converted == Variant(0));
  108. data_arg = "123";
  109. Variant::call_utility_function("type_convert", &converted, (const Variant **)args.ptr(), 2, call_error);
  110. CHECK(converted.get_type() == Variant::Type::INT);
  111. CHECK(converted == Variant(123));
  112. }
  113. }
  114. } // namespace TestVariantUtility
  115. #endif // TEST_VARIANT_UTILITY_H