builder_ut.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "builder.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. static void TestEquals(const TString& expected, const TString& actual) {
  4. UNIT_ASSERT_VALUES_EQUAL(expected, actual);
  5. }
  6. struct TClassWithStreamOperator {
  7. ui32 Id;
  8. TString Name;
  9. TClassWithStreamOperator(ui32 id, const TString& name)
  10. : Id(id)
  11. , Name(name)
  12. {
  13. }
  14. };
  15. IOutputStream& operator<<(IOutputStream& out, const TClassWithStreamOperator& value) {
  16. return out << value.Id << " " << value.Name;
  17. }
  18. Y_UNIT_TEST_SUITE(TStringBuilderTest) {
  19. Y_UNIT_TEST(TestStringBuilder) {
  20. TestEquals("", TStringBuilder());
  21. TestEquals("a", TStringBuilder() << "a");
  22. TestEquals("a1", TStringBuilder() << "a" << 1);
  23. TestEquals("value: 123 name", TStringBuilder() << "value: " << TClassWithStreamOperator(123, "name"));
  24. }
  25. Y_UNIT_TEST(TestStringBuilderOut) {
  26. TString s;
  27. TStringOutput out(s);
  28. TStringBuilder sb;
  29. sb << "a";
  30. out << sb;
  31. TestEquals("a", s);
  32. }
  33. Y_UNIT_TEST(TestStringBuilderRValue) {
  34. struct TRValueAcceptTester {
  35. static bool IsRValue(const TString&) {
  36. return false;
  37. }
  38. static bool IsRValue(TString&&) {
  39. return true;
  40. }
  41. };
  42. UNIT_ASSERT(TRValueAcceptTester::IsRValue(TStringBuilder() << "a" << 1));
  43. TStringBuilder b;
  44. UNIT_ASSERT(!TRValueAcceptTester::IsRValue(b << "a" << 1));
  45. TStringBuilder b2;
  46. UNIT_ASSERT(!TRValueAcceptTester::IsRValue(b2 << "a" << 1 << TStringBuilder() << "a"));
  47. UNIT_ASSERT_VALUES_EQUAL("a1a", b2);
  48. UNIT_ASSERT(TRValueAcceptTester::IsRValue(TStringBuilder() << b2));
  49. UNIT_ASSERT_VALUES_EQUAL("a1a", TStringBuilder() << b2);
  50. }
  51. }