ConfigurableStackTests.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/JSON/document.h>
  9. #include <AzCore/Memory/SystemAllocator.h>
  10. #include <AzCore/Settings/ConfigurableStack.h>
  11. #include <AzCore/Serialization/Json/JsonSerialization.h>
  12. #include <AzCore/Serialization/Json/RegistrationContext.h>
  13. #include <AzCore/Serialization/Json/JsonSystemComponent.h>
  14. #include <AzCore/std/smart_ptr/unique_ptr.h>
  15. #include <AzCore/UnitTest/TestTypes.h>
  16. namespace UnitTest
  17. {
  18. struct ConfigInt
  19. {
  20. AZ_TYPE_INFO(UnitTest::ConfigInt, "{1FAF6E55-7FA4-4FFA-8C41-34F422B8E8AB}");
  21. AZ_CLASS_ALLOCATOR(ConfigInt, AZ::SystemAllocator);
  22. int m_value;
  23. static void Reflect(AZ::ReflectContext* context)
  24. {
  25. if (auto sc = azrtti_cast<AZ::SerializeContext*>(context))
  26. {
  27. sc->Class<ConfigInt>()->Field("Value", &ConfigInt::m_value);
  28. }
  29. }
  30. };
  31. struct ConfigurableStackTests : public LeakDetectionFixture
  32. {
  33. void Reflect(AZ::ReflectContext* context)
  34. {
  35. if (auto sc = azrtti_cast<AZ::SerializeContext*>(context))
  36. {
  37. AZ::JsonSystemComponent::Reflect(sc);
  38. ConfigInt::Reflect(sc);
  39. sc->RegisterGenericType<AZ::ConfigurableStack<ConfigInt>>();
  40. }
  41. else if (auto jrc = azrtti_cast<AZ::JsonRegistrationContext*>(context))
  42. {
  43. AZ::JsonSystemComponent::Reflect(jrc);
  44. }
  45. }
  46. void SetUp() override
  47. {
  48. LeakDetectionFixture::SetUp();
  49. m_serializeContext = AZStd::make_unique<AZ::SerializeContext>();
  50. m_jsonRegistrationContext = AZStd::make_unique<AZ::JsonRegistrationContext>();
  51. Reflect(m_serializeContext.get());
  52. Reflect(m_jsonRegistrationContext.get());
  53. m_deserializationSettings.m_registrationContext = m_jsonRegistrationContext.get();
  54. m_deserializationSettings.m_serializeContext = m_serializeContext.get();
  55. }
  56. void TearDown()
  57. {
  58. m_jsonRegistrationContext->EnableRemoveReflection();
  59. Reflect(m_jsonRegistrationContext.get());
  60. m_jsonRegistrationContext->DisableRemoveReflection();
  61. m_serializeContext->EnableRemoveReflection();
  62. Reflect(m_serializeContext.get());
  63. m_serializeContext->DisableRemoveReflection();
  64. LeakDetectionFixture::TearDown();
  65. }
  66. void ObjectTest(AZStd::string_view jsonText)
  67. {
  68. AZ::ConfigurableStack<ConfigInt> stack;
  69. rapidjson::Document document;
  70. document.Parse(jsonText.data(), jsonText.length());
  71. ASSERT_FALSE(document.HasParseError());
  72. AZ::JsonSerializationResult::ResultCode result = AZ::JsonSerialization::Load(stack, document, m_deserializationSettings);
  73. ASSERT_EQ(AZ::JsonSerializationResult::Processing::Completed, result.GetProcessing());
  74. ASSERT_EQ(4, stack.size());
  75. int numberCounter = 0;
  76. int valueCounter = 42;
  77. for (auto& [name, value] : stack)
  78. {
  79. EXPECT_STREQ(AZStd::string::format("Value%i", numberCounter).c_str(), name.c_str());
  80. EXPECT_EQ(valueCounter, value->m_value);
  81. numberCounter++;
  82. valueCounter++;
  83. }
  84. }
  85. AZStd::unique_ptr<AZ::SerializeContext> m_serializeContext;
  86. AZStd::unique_ptr<AZ::JsonRegistrationContext> m_jsonRegistrationContext;
  87. AZ::JsonDeserializerSettings m_deserializationSettings;
  88. };
  89. TEST_F(ConfigurableStackTests, DeserializeArray)
  90. {
  91. AZ::ConfigurableStack<ConfigInt> stack;
  92. rapidjson::Document document;
  93. document.Parse(
  94. R"([
  95. { "Value": 42 },
  96. { "Value": 43 },
  97. { "Value": 44 },
  98. { "Value": 45 }
  99. ])");
  100. ASSERT_FALSE(document.HasParseError());
  101. AZ::JsonSerializationResult::ResultCode result = AZ::JsonSerialization::Load(stack, document, m_deserializationSettings);
  102. ASSERT_EQ(AZ::JsonSerializationResult::Processing::Completed, result.GetProcessing());
  103. ASSERT_EQ(4, stack.size());
  104. int numberCounter = 0;
  105. int valueCounter = 42;
  106. for (auto& [name, value] : stack)
  107. {
  108. EXPECT_STREQ(AZStd::to_string(numberCounter).c_str(), name.c_str());
  109. EXPECT_EQ(valueCounter, value->m_value);
  110. numberCounter++;
  111. valueCounter++;
  112. }
  113. }
  114. TEST_F(ConfigurableStackTests, DeserializeObject)
  115. {
  116. ObjectTest(
  117. R"({
  118. "Value0": { "Value": 42 },
  119. "Value1": { "Value": 43 },
  120. "Value2": { "Value": 44 },
  121. "Value3": { "Value": 45 }
  122. })");
  123. }
  124. TEST_F(ConfigurableStackTests, DeserializeObjectWithLateBefore)
  125. {
  126. ObjectTest(
  127. R"({
  128. "Value0": { "Value": 42 },
  129. "Value2": { "Value": 44 },
  130. "Value3": { "Value": 45 },
  131. "Value1":
  132. {
  133. "$stack_before": "Value2",
  134. "Value": 43
  135. }
  136. })");
  137. }
  138. TEST_F(ConfigurableStackTests, DeserializeObjectWithEarlyBefore)
  139. {
  140. ObjectTest(
  141. R"({
  142. "Value1":
  143. {
  144. "$stack_before": "Value2",
  145. "Value": 43
  146. },
  147. "Value0": { "Value": 42 },
  148. "Value2": { "Value": 44 },
  149. "Value3": { "Value": 45 }
  150. })");
  151. }
  152. TEST_F(ConfigurableStackTests, DeserializeObjectWithLateAfter)
  153. {
  154. ObjectTest(
  155. R"({
  156. "Value0": { "Value": 42 },
  157. "Value2": { "Value": 44 },
  158. "Value3": { "Value": 45 },
  159. "Value1":
  160. {
  161. "$stack_after": "Value0",
  162. "Value": 43
  163. }
  164. })");
  165. }
  166. TEST_F(ConfigurableStackTests, DeserializeObjectWithEarlyAfter)
  167. {
  168. ObjectTest(
  169. R"({
  170. "Value1":
  171. {
  172. "$stack_after": "Value0",
  173. "Value": 43
  174. },
  175. "Value0": { "Value": 42 },
  176. "Value2": { "Value": 44 },
  177. "Value3": { "Value": 45 }
  178. })");
  179. }
  180. TEST_F(ConfigurableStackTests, DeserializeObjectWithBeforeFirst)
  181. {
  182. ObjectTest(
  183. R"({
  184. "Value1": { "Value": 43 },
  185. "Value2": { "Value": 44 },
  186. "Value3": { "Value": 45 },
  187. "Value0":
  188. {
  189. "$stack_before": "Value1",
  190. "Value": 42
  191. }
  192. })");
  193. }
  194. TEST_F(ConfigurableStackTests, DeserializeObjectWithInsertAfterLast)
  195. {
  196. ObjectTest(
  197. R"({
  198. "Value3":
  199. {
  200. "$stack_after": "Value2",
  201. "Value": 45
  202. },
  203. "Value0": { "Value": 42 },
  204. "Value1": { "Value": 43 },
  205. "Value2": { "Value": 44 }
  206. })");
  207. }
  208. TEST_F(ConfigurableStackTests, DeserializeObjectWithInvalidTarget)
  209. {
  210. AZ::ConfigurableStack<ConfigInt> stack;
  211. rapidjson::Document document;
  212. document.Parse(
  213. R"({
  214. "Value1":
  215. {
  216. "$stack_after": "airplane",
  217. "Value": 43
  218. },
  219. "Value0": { "Value": 42 },
  220. "Value2": { "Value": 44 },
  221. "Value3": { "Value": 45 }
  222. })");
  223. ASSERT_FALSE(document.HasParseError());
  224. AZ::JsonSerializationResult::ResultCode result = AZ::JsonSerialization::Load(stack, document, m_deserializationSettings);
  225. EXPECT_EQ(AZ::JsonSerializationResult::Processing::Completed, result.GetProcessing());
  226. EXPECT_EQ(AZ::JsonSerializationResult::Outcomes::PartialSkip, result.GetOutcome());
  227. EXPECT_EQ(3, stack.size());
  228. }
  229. TEST_F(ConfigurableStackTests, DeserializeObjectWithInvalidTargetType)
  230. {
  231. AZ::ConfigurableStack<ConfigInt> stack;
  232. rapidjson::Document document;
  233. document.Parse(
  234. R"({
  235. "Value1":
  236. {
  237. "$stack_after": 42,
  238. "Value": 43
  239. },
  240. "Value0": { "Value": 42 },
  241. "Value2": { "Value": 44 },
  242. "Value3": { "Value": 45 }
  243. })");
  244. ASSERT_FALSE(document.HasParseError());
  245. AZ::JsonSerializationResult::ResultCode result = AZ::JsonSerialization::Load(stack, document, m_deserializationSettings);
  246. EXPECT_EQ(AZ::JsonSerializationResult::Processing::Completed, result.GetProcessing());
  247. EXPECT_EQ(AZ::JsonSerializationResult::Outcomes::PartialSkip, result.GetOutcome());
  248. EXPECT_EQ(3, stack.size());
  249. }
  250. } // namespace UnitTest