string_transform_ut.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "json.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <library/cpp/protobuf/json/proto2json.h>
  4. Y_UNIT_TEST_SUITE(TDoubleEscapeTransform) {
  5. Y_UNIT_TEST(TestEmptyString) {
  6. const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleEscapeTransform();
  7. TString s;
  8. s = "";
  9. transform.Transform(s);
  10. UNIT_ASSERT_EQUAL(s, "");
  11. }
  12. Y_UNIT_TEST(TestAlphabeticString) {
  13. const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleEscapeTransform();
  14. TString s;
  15. s = "abacaba";
  16. transform.Transform(s);
  17. UNIT_ASSERT_EQUAL(s, "abacaba");
  18. }
  19. Y_UNIT_TEST(TestRussianSymbols) {
  20. const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleEscapeTransform();
  21. TString s;
  22. s = "тест";
  23. transform.Transform(s);
  24. UNIT_ASSERT_EQUAL(s, "\\\\321\\\\202\\\\320\\\\265\\\\321\\\\201\\\\321\\\\202");
  25. }
  26. Y_UNIT_TEST(TestEscapeSpecialSymbols) {
  27. const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleEscapeTransform();
  28. TString s;
  29. s = "aba\\ca\"ba";
  30. transform.Transform(s);
  31. UNIT_ASSERT_EQUAL(s, "aba\\\\\\\\ca\\\\\\\"ba");
  32. }
  33. }
  34. Y_UNIT_TEST_SUITE(TDoubleUnescapeTransform) {
  35. Y_UNIT_TEST(TestEmptyString) {
  36. const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleUnescapeTransform();
  37. TString s;
  38. s = "";
  39. transform.Transform(s);
  40. UNIT_ASSERT_EQUAL("", s);
  41. }
  42. Y_UNIT_TEST(TestAlphabeticString) {
  43. const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleUnescapeTransform();
  44. TString s;
  45. s = "abacaba";
  46. transform.Transform(s);
  47. UNIT_ASSERT_EQUAL("abacaba", s);
  48. }
  49. Y_UNIT_TEST(TestRussianSymbols) {
  50. const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleUnescapeTransform();
  51. TString s;
  52. s = "\\\\321\\\\202\\\\320\\\\265\\\\321\\\\201\\\\321\\\\202";
  53. transform.Transform(s);
  54. UNIT_ASSERT_EQUAL("тест", s);
  55. }
  56. Y_UNIT_TEST(TestEscapeSpecialSymbols) {
  57. const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleUnescapeTransform();
  58. TString s;
  59. s = "aba\\\\\\\\ca\\\\\\\"ba";
  60. transform.Transform(s);
  61. UNIT_ASSERT_EQUAL("aba\\ca\"ba", s);
  62. }
  63. Y_UNIT_TEST(TestEscapeSpecialSymbolsDifficultCases) {
  64. const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleUnescapeTransform();
  65. TString s;
  66. s = "\\\\\\\\\\\\\\\\";
  67. transform.Transform(s);
  68. UNIT_ASSERT_EQUAL("\\\\", s);
  69. s = "\\\\\\\\\\\\\\\"";
  70. transform.Transform(s);
  71. UNIT_ASSERT_EQUAL("\\\"", s);
  72. s = "\\\\\\\"\\\\\\\\";
  73. transform.Transform(s);
  74. UNIT_ASSERT_EQUAL("\"\\", s);
  75. s = "\\\\\\\"\\\\\\\"";
  76. transform.Transform(s);
  77. UNIT_ASSERT_EQUAL("\"\"", s);
  78. s = "\\\\\\\\\\\\\\\\\\\\\\\\";
  79. transform.Transform(s);
  80. UNIT_ASSERT_EQUAL("\\\\\\", s);
  81. s = "\\\\\\\\\\\\\\\\\\\\\\\\abacaba\\\\";
  82. transform.Transform(s);
  83. UNIT_ASSERT_EQUAL("\\\\\\abacaba", s);
  84. s = "\\\\\\\\\\\\\\\\\\\\\\\\abacaba\\\"";
  85. transform.Transform(s);
  86. UNIT_ASSERT_EQUAL("\\\\\\abacaba\"", s);
  87. }
  88. }