TextParserTests.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/IO/ByteContainerStream.h>
  9. #include <AzCore/Settings/TextParser.h>
  10. #include <AzCore/std/string/conversions.h>
  11. #include <AzCore/std/containers/fixed_unordered_map.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. namespace UnitTest
  14. {
  15. struct TextFileParams
  16. {
  17. AZStd::string_view m_testTextFileName;
  18. AZStd::string_view m_testTextContents;
  19. // The following test below will not have more than 20
  20. AZStd::fixed_vector<AZStd::string_view, 20> m_expectedLines;
  21. };
  22. class TextParserTestFixture
  23. : public LeakDetectionFixture
  24. {
  25. protected:
  26. AZStd::string m_textBuffer;
  27. AZ::IO::ByteContainerStream<AZStd::string> m_textStream{ &m_textBuffer };
  28. };
  29. // Parameterized test fixture for the TextFileParams
  30. class TextParserParamFixture
  31. : public TextParserTestFixture
  32. , public ::testing::WithParamInterface<TextFileParams>
  33. {
  34. void SetUp() override
  35. {
  36. auto textFileParam = GetParam();
  37. // Create the test text stream
  38. m_textStream.Write(textFileParam.m_testTextContents.size(),
  39. textFileParam.m_testTextContents.data());
  40. // Seek back to the beginning of the stream for test to read written data
  41. m_textStream.Seek(0, AZ::IO::GenericStream::ST_SEEK_BEGIN);
  42. }
  43. };
  44. TEST_F(TextParserTestFixture, ParseTextFile_WithEmptyFunction_Fails)
  45. {
  46. AZ::Settings::TextParserSettings parserSettings{ AZ::Settings::TextParserSettings::ParseTextEntryFunc{} };
  47. m_textBuffer = R"(project_path=/TestProject
  48. engine_path=/TestEngine
  49. )";
  50. EXPECT_FALSE(AZ::Settings::ParseTextFile(m_textStream, parserSettings));
  51. }
  52. TEST_F(TextParserTestFixture, ParseTextFile_WithParseTextEntryFunctionWhichAlwaysReturnsFalse_Fails)
  53. {
  54. auto parseTextEntry = [](AZStd::string_view)
  55. {
  56. return false;
  57. };
  58. AZ::Settings::TextParserSettings parserSettings{ parseTextEntry };
  59. m_textBuffer = R"(project_path=/TestProject
  60. engine_path=/TestEngine
  61. )";
  62. EXPECT_FALSE(AZ::Settings::ParseTextFile(m_textStream, parserSettings));
  63. }
  64. TEST_F(TextParserTestFixture, ParseTextFile_WithLineLargerThan4096_Fails)
  65. {
  66. auto parseTextEntry = [](AZStd::string_view)
  67. {
  68. return true;
  69. };
  70. AZ::Settings::TextParserSettings parserSettings{ parseTextEntry };
  71. m_textBuffer = "project_path=/TestProject\n";
  72. // append a line longer than 4096 characters
  73. m_textBuffer += "foo=";
  74. m_textBuffer.append(4096, 'a');
  75. m_textBuffer += '\n';
  76. EXPECT_FALSE(AZ::Settings::ParseTextFile(m_textStream, parserSettings));
  77. }
  78. TEST_F(TextParserTestFixture, ParseTextFile_WithAllLinesSmallerThan4097_Succeeds)
  79. {
  80. auto parseTextEntry = [](AZStd::string_view)
  81. {
  82. return true;
  83. };
  84. AZ::Settings::TextParserSettings parserSettings{ parseTextEntry };
  85. m_textBuffer = "project_path=/TestProject\n";
  86. // append only 4000 characters
  87. m_textBuffer += "foo=";
  88. m_textBuffer.append(4000, 'a');
  89. m_textBuffer += '\n';
  90. EXPECT_TRUE(AZ::Settings::ParseTextFile(m_textStream, parserSettings));
  91. }
  92. TEST_P(TextParserParamFixture, ParseTextFile_ParseContents_Successfully)
  93. {
  94. auto textFileParam = GetParam();
  95. // Parse Text File and write output to a vector for testing
  96. using ParseSettingsVector = AZStd::fixed_vector<AZStd::string_view, 20>;
  97. ParseSettingsVector parseSettingsVector;
  98. auto parseTextEntry = [&parseSettingsVector](AZStd::string_view textEntry)
  99. {
  100. parseSettingsVector.emplace_back(textEntry);
  101. return true;
  102. };
  103. AZ::Settings::TextParserSettings parserSettings{ parseTextEntry };
  104. auto parseOutcome = AZ::Settings::ParseTextFile(m_textStream, parserSettings);
  105. EXPECT_TRUE(parseOutcome);
  106. // Validate that parse lines matches the expected lines
  107. EXPECT_THAT(parseSettingsVector, ::testing::ContainerEq(textFileParam.m_expectedLines));
  108. }
  109. INSTANTIATE_TEST_SUITE_P(
  110. ReadTextFile,
  111. TextParserParamFixture,
  112. ::testing::Values(
  113. // Processes a fake text file
  114. // and properly terminates the file with a newline
  115. TextFileParams{ "fake_uuid.text", R"(
  116. engine.json
  117. project.json
  118. levels/defaultlevel.prefab
  119. )"
  120. , AZStd::fixed_vector<AZStd::string_view, 20>{
  121. AZStd::string_view{ "engine.json" },
  122. AZStd::string_view{ "project.json" },
  123. AZStd::string_view{ "levels/defaultlevel.prefab" }
  124. }},
  125. // Parses a fake text file
  126. // and does not end with a newline
  127. TextFileParams{ "fake_names.text", R"(
  128. shader
  129. material
  130. document property editor)"
  131. , AZStd::fixed_vector<AZStd::string_view, 20>{
  132. AZStd::string_view{ "shader" },
  133. AZStd::string_view{ "material" },
  134. AZStd::string_view{ "document property editor" }
  135. }}
  136. )
  137. );
  138. }