RunnerTest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <AzTest/AzTest.h>
  9. #include <AzTest/Utils.h>
  10. class AzTestRunnerTest
  11. : public ::testing::Test
  12. {
  13. };
  14. //-------------------------------------------------------------------------------------------------
  15. struct EndsWithParam
  16. {
  17. EndsWithParam(std::string arg, std::string ending, bool expected)
  18. : m_arg(arg)
  19. , m_ending(ending)
  20. , m_expected(expected)
  21. {
  22. }
  23. const std::string m_arg;
  24. const std::string m_ending;
  25. const bool m_expected;
  26. };
  27. void PrintTo(const EndsWithParam& p, std::ostream* os)
  28. {
  29. *os << "arg:" << p.m_arg << ", ending:" << p.m_ending << ", expected:" << p.m_expected;
  30. }
  31. class EndsWithTest
  32. : public ::testing::TestWithParam<EndsWithParam>
  33. {
  34. };
  35. TEST_P(EndsWithTest, CallEndsWith)
  36. {
  37. EndsWithParam p = GetParam();
  38. bool b = AZ::Test::EndsWith(p.m_arg, p.m_ending);
  39. ASSERT_EQ(p.m_expected, b);
  40. }
  41. INSTANTIATE_TEST_SUITE_P(All, EndsWithTest, ::testing::Values(
  42. EndsWithParam("foo.dll", ".dll", true),
  43. EndsWithParam("foo.dll", ".dxx", false),
  44. EndsWithParam("abcdef", "bcd", false), // value found in middle
  45. EndsWithParam("a", "bcd", false), // pattern too long
  46. EndsWithParam("abc", "", true), // empty pattern
  47. EndsWithParam("", "abc", false), // empty value
  48. EndsWithParam("", "", true) // both empty
  49. ));
  50. //-------------------------------------------------------------------------------------------------
  51. struct RemoveParam
  52. {
  53. std::vector<std::string> before;
  54. int startIndex;
  55. int endIndex;
  56. std::vector<std::string> expected;
  57. };
  58. std::ostream& operator<< (std::ostream& os, const RemoveParam& param)
  59. {
  60. os << "before:{";
  61. bool first = true;
  62. for (auto b : param.before)
  63. {
  64. if (!first)
  65. {
  66. os << ", ";
  67. }
  68. os << b;
  69. first = false;
  70. }
  71. os << "}, startIndex:" << param.startIndex << ", endIndex:" << param.endIndex << ", expected:{";
  72. first = true;
  73. for (auto e : param.expected)
  74. {
  75. if (!first)
  76. {
  77. os << ", ";
  78. }
  79. os << e;
  80. first = false;
  81. }
  82. os << "}";
  83. return os;
  84. }
  85. class RemoveParametersTest
  86. : public ::testing::TestWithParam<RemoveParam>
  87. {};
  88. TEST_P(RemoveParametersTest, Foo)
  89. {
  90. const RemoveParam& p = GetParam();
  91. // create "main"-like parameters
  92. int argc = (int)p.before.size();
  93. char** argv = new char*[argc];
  94. for (int i = 0; i < argc; i++)
  95. {
  96. argv[i] = const_cast<char*>(p.before[i].c_str());
  97. }
  98. AZ::Test::RemoveParameters(argc, argv, p.startIndex, p.endIndex);
  99. ASSERT_EQ(p.expected.size(), argc);
  100. for (int i = 0; i < p.expected.size(); i++)
  101. {
  102. std::string actual(argv[i]);
  103. EXPECT_EQ(p.expected[i], actual);
  104. }
  105. // everything beyond the end of the original data is nulled out if it was removed
  106. for (int j = (int)p.expected.size(); j < p.before.size(); j++)
  107. {
  108. EXPECT_TRUE(nullptr == argv[j]);
  109. }
  110. delete[] argv;
  111. }
  112. INSTANTIATE_TEST_SUITE_P(All, RemoveParametersTest, ::testing::Values(
  113. RemoveParam { { "a", "b" }, 0, 0, { "b" } } // remove from start
  114. ,RemoveParam { { "a", "b" }, 1, 1, { "a" } } // remove from end
  115. ,RemoveParam { { "a", "b", "c" }, 1, 1, { "a", "c" } } // remove from middle
  116. ,RemoveParam { { "a", "b", "c" }, 1, 99, { "a" } } // remove beyond end
  117. ,RemoveParam { { "a", "b", "c" }, -99, 1, { "c" } } // remove before begin
  118. ,RemoveParam { { "a", "b", "c" }, -99, 99, { } } // remove all
  119. ,RemoveParam { { "a", "b", "c" }, 2, 0, {"a", "b", "c"} } // inverted range doesn't remove anything
  120. ));
  121. class AzTestRunnnerGlobalParamsTest
  122. : public ::testing::Test
  123. {
  124. public:
  125. void SetUp() override
  126. {
  127. ::testing::Test::SetUp();
  128. m_priorFilter = ::testing::GTEST_FLAG(filter);
  129. ::testing::GTEST_FLAG(filter) = "*"; // emulate no command line filter args
  130. }
  131. void TearDown() override
  132. {
  133. ::testing::GTEST_FLAG(filter) = m_priorFilter;
  134. ::testing::Test::TearDown();
  135. }
  136. std::string m_priorFilter; // std::string to emulate same environment as gtest, not AZ
  137. };
  138. // saves args into compatible with other platforms format (clang is picky)
  139. // but also restores GTEST_FLAG(filter) before and after applying changes to them,
  140. // allowing us to test the effect params have on GTEST_FLAG(filter)
  141. class ScopedArgs
  142. {
  143. public:
  144. ScopedArgs(int argc, const char** const argv)
  145. {
  146. m_savedParams = ::testing::GTEST_FLAG(filter);
  147. m_argc = argc;
  148. m_argv = new char*[argc];
  149. for (int i = 0; i < m_argc; i++)
  150. {
  151. m_argv[i] = const_cast<char*>(argv[i]);
  152. }
  153. }
  154. ~ScopedArgs()
  155. {
  156. delete[] m_argv;
  157. ::testing::GTEST_FLAG(filter) = m_savedParams;
  158. }
  159. int m_argc = 0;
  160. char **m_argv = nullptr;
  161. std::string m_savedParams; // intentional std::string, exists outside of memory alloc area.
  162. private:
  163. ScopedArgs() = delete;
  164. AZ_DISABLE_COPY_MOVE(ScopedArgs);
  165. };
  166. TEST_F(AzTestRunnnerGlobalParamsTest, ApplyGlobalParameters_NothingSpecial_RemainsUnchanged)
  167. {
  168. const char* argv[] = { "hello", "--world", "test" };
  169. ScopedArgs args(static_cast<int>(AZ_ARRAY_SIZE(argv)), argv );
  170. AZ::Test::ApplyGlobalParameters(&args.m_argc, args.m_argv);
  171. EXPECT_EQ(args.m_argc, 3);
  172. }
  173. AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);