aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/scheme/ut_utils
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/scheme/ut_utils
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/scheme/ut_utils')
-rw-r--r--library/cpp/scheme/ut_utils/scheme_ut_utils.cpp44
-rw-r--r--library/cpp/scheme/ut_utils/scheme_ut_utils.h55
-rw-r--r--library/cpp/scheme/ut_utils/ya.make16
3 files changed, 115 insertions, 0 deletions
diff --git a/library/cpp/scheme/ut_utils/scheme_ut_utils.cpp b/library/cpp/scheme/ut_utils/scheme_ut_utils.cpp
new file mode 100644
index 0000000000..0bbdab10e8
--- /dev/null
+++ b/library/cpp/scheme/ut_utils/scheme_ut_utils.cpp
@@ -0,0 +1,44 @@
+#include "scheme_ut_utils.h"
+
+#include <library/cpp/colorizer/colors.h>
+
+#include <util/stream/str.h>
+
+namespace NSc {
+ namespace NUt {
+ NSc::TValue AssertFromJson(TStringBuf val) {
+ try {
+ return TValue::FromJsonThrow(val);
+ } catch (const TSchemeParseException& e) {
+ TStringStream s;
+ NColorizer::TColors colors;
+ s << "\n"
+ << colors.YellowColor() << "Reason:" << colors.OldColor() << "\n"
+ << e.Reason;
+ s << "\n"
+ << colors.YellowColor() << "Where:" << colors.OldColor() << "\n"
+ << val.SubStr(0, e.Offset) << colors.RedColor() << val.SubStr(e.Offset) << colors.OldColor() << "\n";
+ UNIT_FAIL_IMPL("could not parse json", s.Str());
+ return NSc::Null();
+ } catch (const yexception& e) {
+ TStringStream s;
+ s << '\n'
+ << val;
+ UNIT_FAIL_IMPL("could not parse json", s.Str());
+ return NSc::Null();
+ }
+ }
+
+ void AssertScheme(const TValue& expected, const TValue& actual) {
+ UNIT_ASSERT_JSON_EQ_JSON(actual, expected);
+ }
+
+ void AssertSchemeJson(TStringBuf expected, const NSc::TValue& actual) {
+ UNIT_ASSERT_JSON_EQ_JSON(actual, expected);
+ }
+
+ void AssertJsonJson(TStringBuf expected, TStringBuf actual) {
+ UNIT_ASSERT_JSON_EQ_JSON(actual, expected);
+ }
+ }
+}
diff --git a/library/cpp/scheme/ut_utils/scheme_ut_utils.h b/library/cpp/scheme/ut_utils/scheme_ut_utils.h
new file mode 100644
index 0000000000..eb3ea15b2a
--- /dev/null
+++ b/library/cpp/scheme/ut_utils/scheme_ut_utils.h
@@ -0,0 +1,55 @@
+#pragma once
+
+#include <library/cpp/json/json_prettifier.h>
+#include <library/cpp/scheme/scheme.h>
+#include <library/cpp/json/json_value.h>
+#include <library/cpp/json/json_writer.h>
+#include <library/cpp/testing/unittest/registar.h>
+#include <util/string/cast.h>
+
+namespace NSc {
+ namespace NUt {
+ TValue AssertFromJson(TStringBuf json);
+
+ inline TString NormalizeJson(const NSc::TValue& sc) {
+ return sc.ToJson(true);
+ }
+
+ inline TString NormalizeJson(const NJson::TJsonValue& sc) {
+ return NJson::WriteJson(sc, false, true, false);
+ }
+
+ template <class TStr>
+ inline TString NormalizeJson(const TStr& val) {
+ return AssertFromJson(val).ToJson(true);
+ }
+
+#define UNIT_ASSERT_JSON_EQ_JSON_C(A, B, c) \
+ do { \
+ const TString _a = NSc::NUt::NormalizeJson(A); \
+ const TString _b = NSc::NUt::NormalizeJson(B); \
+ if (_a != _b) { \
+ UNIT_FAIL_IMPL( \
+ "json values are different (" #A " != " #B ")", \
+ Sprintf("%s\n!=\n%s\n%s\n%s", _a.data(), _b.data(), \
+ ::NUnitTest::ColoredDiff(NJson::PrettifyJson(_a), NJson::PrettifyJson(_b), " \t\n,:\"{}[]").data(), ToString(c).data())); \
+ } \
+ } while (false)
+
+#define UNIT_ASSERT_JSON_EQ_JSON(A, B) UNIT_ASSERT_JSON_EQ_JSON_C(A, B, "")
+
+ inline TString DumpJson(const TValue& json) {
+ return NJson::CompactifyJson(json.ToJson(true), true, true);
+ }
+
+ // deprecated
+ inline TString DumpJsonVS(const TValue& expected, const TValue& fact) {
+ return DumpJson(expected) + "(expected) != (fact)" + DumpJson(fact);
+ }
+
+ void AssertScheme(const TValue& expected, const TValue& real);
+ void AssertSchemeJson(TStringBuf expected, const TValue& real);
+ void AssertJsonJson(TStringBuf expected, TStringBuf real);
+
+ }
+}
diff --git a/library/cpp/scheme/ut_utils/ya.make b/library/cpp/scheme/ut_utils/ya.make
new file mode 100644
index 0000000000..7661262e1b
--- /dev/null
+++ b/library/cpp/scheme/ut_utils/ya.make
@@ -0,0 +1,16 @@
+LIBRARY()
+
+OWNER(velavokr)
+
+SRCS(
+ scheme_ut_utils.cpp
+)
+
+PEERDIR(
+ library/cpp/colorizer
+ library/cpp/json
+ library/cpp/scheme
+ library/cpp/testing/unittest
+)
+
+END()