3
0

ErrorMessageFinderTests.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <Common/ErrorMessageFinder.h>
  10. #include <AzFramework/StringFunc/StringFunc.h>
  11. #include <AzCore/UnitTest/TestTypes.h>
  12. namespace UnitTest
  13. {
  14. // In order to have confidence in our other unit tests, we need to ensure ErrorMessageFinder correctly detects error messages
  15. class ErrorMessageFinderTests
  16. : public LeakDetectionFixture
  17. {
  18. protected:
  19. AZStd::vector<AZStd::string> m_reportedFailures;
  20. void TearDown() override
  21. {
  22. m_reportedFailures.clear();
  23. }
  24. void HandleFailure(const AZStd::string& failureMessage)
  25. {
  26. m_reportedFailures.push_back(failureMessage);
  27. }
  28. void RedirectGtestFailures(ErrorMessageFinder& finder)
  29. {
  30. finder.m_reportFailure = std::bind(&ErrorMessageFinderTests::HandleFailure, this, std::placeholders::_1);
  31. }
  32. bool FailureWasReported(const AZStd::string& forMessage)
  33. {
  34. for (const AZStd::string& failureMessage : m_reportedFailures)
  35. {
  36. if (AzFramework::StringFunc::Find(failureMessage, forMessage) != AZStd::string::npos)
  37. {
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. };
  44. TEST_F(ErrorMessageFinderTests, ConsumeExpectedMessage)
  45. {
  46. ErrorMessageFinder finder;
  47. RedirectGtestFailures(finder);
  48. finder.AddExpectedErrorMessage("this is a test");
  49. AZ_Assert(false, "...this is a test...");
  50. finder.CheckExpectedErrorsFound();
  51. EXPECT_FALSE(FailureWasReported("this is a test"));
  52. }
  53. TEST_F(ErrorMessageFinderTests, ReportExpectedMessageNotReceived)
  54. {
  55. ErrorMessageFinder finder;
  56. RedirectGtestFailures(finder);
  57. finder.AddExpectedErrorMessage("this is a test");
  58. finder.CheckExpectedErrorsFound();
  59. EXPECT_TRUE(FailureWasReported("Expected error 1 time(s) but got 0 time(s): 'this is a test'"));
  60. }
  61. TEST_F(ErrorMessageFinderTests, ReportExpectedMessageFoundTooManyTimes)
  62. {
  63. ErrorMessageFinder finder;
  64. RedirectGtestFailures(finder);
  65. finder.AddExpectedErrorMessage("this is a test", 1);
  66. AZ_Assert(false, "this is a test.");
  67. AZ_Assert(false, "this is a test.");
  68. finder.CheckExpectedErrorsFound();
  69. EXPECT_TRUE(FailureWasReported("Expected error 1 time(s) but got 2 time(s): 'this is a test'"));
  70. }
  71. TEST_F(ErrorMessageFinderTests, ReportUnexpectedMessage)
  72. {
  73. ErrorMessageFinder finder;
  74. RedirectGtestFailures(finder);
  75. finder.AddExpectedErrorMessage("this is a test");
  76. AZ_Assert(false, "...this is a test...");
  77. AZ_Assert(false, "This message is not expected.");
  78. finder.CheckExpectedErrorsFound();
  79. EXPECT_FALSE(FailureWasReported("this is a test"));
  80. EXPECT_TRUE(FailureWasReported("This message is not expected"));
  81. }
  82. }