matchers.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #pragma once
  2. #include <util/generic/string.h>
  3. #include <gmock/gmock.h>
  4. namespace NGTest {
  5. namespace NDetail {
  6. [[nodiscard]] bool MatchOrUpdateGolden(std::string_view actualContent, const TString& goldenFilename);
  7. }
  8. /**
  9. * Matches a string or std::vector<char> equal to the specified file content.
  10. * The file must be brought to the test using macro DATA in ya.make.
  11. *
  12. * The matcher is able to update the file by the actual content during
  13. * the special test run with the argument '--test-param GTEST_UPDATE_GOLDEN=1'.
  14. * Any change in such files should be added to VCS manually.
  15. *
  16. * The matcher should not be used for a binary data. Use it for a content whose
  17. * diff will be visual during a code review: text, config, image.
  18. *
  19. * Example:
  20. * ```
  21. * TEST(Suite, Name) {
  22. * std::string data = RenderSomeTextData();
  23. * EXPECT_THAT(data, NGTest::GoldenFileEq(SRC_("golden/data.txt")));
  24. * }
  25. * ```
  26. */
  27. MATCHER_P(GoldenFileEq, filename, "")
  28. {
  29. if (!NDetail::MatchOrUpdateGolden(std::string_view(arg.data(), arg.size()), TString(filename))) {
  30. *result_listener
  31. << "\nCall `ya test --test-param GTEST_UPDATE_GOLDEN=1` to update the golden file";
  32. return false;
  33. }
  34. return true;
  35. }
  36. }