tstring_test.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <library/cpp/yaml/as/tstring.h>
  3. #include <contrib/libs/yaml-cpp/include/yaml-cpp/yaml.h>
  4. #include <array>
  5. Y_UNIT_TEST_SUITE(YamlTstringTest) {
  6. Y_UNIT_TEST(As) {
  7. const auto* key1 = "key1";
  8. const auto* key2 = "key2";
  9. const TString string = "string";
  10. const TString anotherString = "another string";
  11. YAML::Node map;
  12. map[key1] = std::string(string);
  13. UNIT_ASSERT_VALUES_EQUAL(map[key1].as<TString>(), string);
  14. UNIT_ASSERT_VALUES_EQUAL(map[key1].as<TString>(anotherString), string);
  15. UNIT_ASSERT_VALUES_EQUAL(map[key2].as<TString>(anotherString), anotherString);
  16. UNIT_ASSERT_EXCEPTION(map[key1].as<int>(), YAML::BadConversion);
  17. UNIT_ASSERT_EXCEPTION(map[key2].as<int>(), YAML::BadConversion);
  18. UNIT_ASSERT_EXCEPTION(map[key2].as<TString>(), YAML::BadConversion);
  19. }
  20. Y_UNIT_TEST(Convert) {
  21. const TString string("string");
  22. auto node = YAML::Node(std::string(string));
  23. UNIT_ASSERT_VALUES_EQUAL(node.Scalar(), YAML::convert<TString>::encode(string).Scalar());
  24. TString buffer;
  25. YAML::convert<TString>::decode(node, buffer);
  26. UNIT_ASSERT_VALUES_EQUAL(string, buffer);
  27. }
  28. Y_UNIT_TEST(NullCharactersInString) {
  29. const std::array<char, 3> characters = {{'a', '\0', 'b'}};
  30. const std::string testString(characters.data(), characters.size());
  31. const TString refString(characters.data(), characters.size());
  32. const std::string key = "key";
  33. YAML::Node node;
  34. node[key] = testString;
  35. UNIT_ASSERT_VALUES_EQUAL(node[key].as<TString>(), refString);
  36. UNIT_ASSERT_VALUES_EQUAL(node[key].as<TString>(TString("fallback")), refString);
  37. auto stringNode = YAML::Node(testString);
  38. UNIT_ASSERT_VALUES_EQUAL(stringNode.Scalar(), YAML::convert<TString>::encode(refString).Scalar());
  39. TString buffer;
  40. YAML::convert<TString>::decode(stringNode, buffer);
  41. UNIT_ASSERT_VALUES_EQUAL(refString, buffer);
  42. }
  43. }