scheme_ut_utils.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <library/cpp/json/json_prettifier.h>
  3. #include <library/cpp/scheme/scheme.h>
  4. #include <library/cpp/json/json_value.h>
  5. #include <library/cpp/json/json_writer.h>
  6. #include <library/cpp/testing/unittest/registar.h>
  7. #include <util/string/cast.h>
  8. namespace NSc {
  9. namespace NUt {
  10. TValue AssertFromJson(TStringBuf json);
  11. inline TString NormalizeJson(const NSc::TValue& sc) {
  12. return sc.ToJson(true);
  13. }
  14. inline TString NormalizeJson(const NJson::TJsonValue& sc) {
  15. return NJson::WriteJson(sc, false, true, false);
  16. }
  17. template <class TStr>
  18. inline TString NormalizeJson(const TStr& val) {
  19. return AssertFromJson(val).ToJson(true);
  20. }
  21. #define UNIT_ASSERT_JSON_EQ_JSON_C(A, B, c) \
  22. do { \
  23. const TString _a = NSc::NUt::NormalizeJson(A); \
  24. const TString _b = NSc::NUt::NormalizeJson(B); \
  25. if (_a != _b) { \
  26. UNIT_FAIL_IMPL( \
  27. "json values are different (" #A " != " #B ")", \
  28. Sprintf("%s\n!=\n%s\n%s\n%s", _a.data(), _b.data(), \
  29. ::NUnitTest::ColoredDiff(NJson::PrettifyJson(_a), NJson::PrettifyJson(_b), " \t\n,:\"{}[]").data(), ToString(c).data())); \
  30. } \
  31. } while (false)
  32. #define UNIT_ASSERT_JSON_EQ_JSON(A, B) UNIT_ASSERT_JSON_EQ_JSON_C(A, B, "")
  33. inline TString DumpJson(const TValue& json) {
  34. return NJson::CompactifyJson(json.ToJson(true), true, true);
  35. }
  36. // deprecated
  37. inline TString DumpJsonVS(const TValue& expected, const TValue& fact) {
  38. return DumpJson(expected) + "(expected) != (fact)" + DumpJson(fact);
  39. }
  40. void AssertScheme(const TValue& expected, const TValue& real);
  41. void AssertSchemeJson(TStringBuf expected, const TValue& real);
  42. void AssertJsonJson(TStringBuf expected, TStringBuf real);
  43. }
  44. }