azakharov93

Tests for JSON parsing

Sep 18th, 2021 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.34 KB | None | 0 0
  1. //
  2. // Created by azakharov on 9/18/2021.
  3. //
  4.  
  5. #include <gtest/gtest.h>
  6.  
  7. #include "../src/sprint_10/json.h"
  8.  
  9. using namespace std::string_literals;
  10. using namespace json;
  11.  
  12. json::Document LoadJSON(const std::string& s) {
  13.     std::istringstream strm(s);
  14.     return json::Load(strm);
  15. }
  16.  
  17. std::string Print(const Node& node) {
  18.     std::ostringstream out;
  19.     Print(Document{node}, out);
  20.     return out.str();
  21. }
  22.  
  23. TEST(JsonParsing, ParsingEmptyInput) {
  24.     EXPECT_THROW(auto value = LoadJSON(""s).GetRoot(), json::ParsingError);
  25. }
  26.  
  27. TEST(JsonParsing, NullNode) {
  28.     Node node;
  29.     EXPECT_TRUE(node.IsNull()) << "Default constructor should create null node";
  30.     EXPECT_EQ(Print(node), "null"s) << "Print() method for null node does not work properly";
  31.  
  32.     Node node1{nullptr};
  33.     EXPECT_TRUE(node1.IsNull()) << "Constructor with {nullptr} should create null node";
  34.  
  35.     const Node node2 = LoadJSON("null"s).GetRoot();
  36.     EXPECT_TRUE(node2.IsNull()) << "Expected null node  with null constructor";
  37.  
  38.     EXPECT_EQ(node, node2) << "operator== for null node does not work properly";
  39. }
  40.  
  41. TEST(JsonParsing, NullNodeThrowsException) {
  42.     const std::vector<std::string> inputs{"n"s, "nu"s, "nul"s, "nul1"s, "nullqwe"s, "nul\\t"s};
  43.  
  44.     for (const auto& input : inputs)
  45.         EXPECT_THROW(LoadJSON(input), json::ParsingError) << "Should throw for incorrect input for " << input;
  46. }
  47.  
  48. TEST(JsonParsing, BooleanNode) {
  49.     Node true_node{true};
  50.     EXPECT_TRUE(true_node.IsBool());
  51.     EXPECT_TRUE(true_node.AsBool());
  52.  
  53.     Node false_node{false};
  54.     EXPECT_TRUE(false_node.IsBool());
  55.     EXPECT_FALSE(false_node.AsBool());
  56.  
  57.     EXPECT_EQ(Print(true_node), "true"s);
  58.     EXPECT_EQ(Print(false_node), "false"s);
  59.  
  60.     EXPECT_EQ(LoadJSON("true"s).GetRoot(), true_node);
  61.     EXPECT_EQ(LoadJSON("false"s).GetRoot(), false_node);
  62. }
  63.  
  64. TEST(JsonParsing, BooleanNodeThrowsException) {
  65.     const std::vector<std::string> inputs{"t"s, "tr"s,  "tru"s,  "trus"s,  "trueqwe"s,  "tru1e"s,  "tru\\te"s,
  66.                                           "f"s, "fal"s, "fals"s, "falsa"s, "falseqwe"s, "fals1e"s, "fals\\te"s};
  67.  
  68.     for (const auto& input : inputs)
  69.         EXPECT_THROW(LoadJSON(input), json::ParsingError) << "Should throw for incorrect input for " << input;
  70. }
  71.  
  72. TEST(JsonParsing, IntNode) {
  73.     Node int_node{42};
  74.  
  75.     EXPECT_TRUE(int_node.IsInt());
  76.     EXPECT_EQ(int_node.AsInt(), 42);
  77.  
  78.     // целые числа являются подмножеством чисел с плавающей запятой
  79.     EXPECT_TRUE(int_node.IsDouble());
  80.  
  81.     // Когда узел хранит int, можно получить соответствующее ему double-значение
  82.     EXPECT_EQ(int_node.AsDouble(), 42.0);
  83.     EXPECT_FALSE(int_node.IsPureDouble());
  84.  
  85.     EXPECT_EQ(Print(int_node), "42"s);
  86.  
  87.     EXPECT_EQ(LoadJSON("42"s).GetRoot(), int_node);
  88. }
  89.  
  90. TEST(JsonParsing, IntNodeThrowsException) {
  91.     // TODO: maybe consider inputs like: 01, -01, -01qwe, etc.
  92.     std::vector<std::string> input{"qwe"s, "-qwe"s, "0qwe"s, "-0qwe"s};
  93.  
  94.     for (const auto& value : input)
  95.         EXPECT_THROW(auto node = LoadJSON("-qwe").GetRoot(), json::ParsingError);
  96. }
  97.  
  98. TEST(JsonParsing, DoubleNode) {
  99.     Node dbl_node{123.45};
  100.  
  101.     EXPECT_TRUE(dbl_node.IsDouble());
  102.     EXPECT_EQ(dbl_node.AsDouble(), 123.45);
  103.     EXPECT_TRUE(dbl_node.IsPureDouble());  // Значение содержит число с плавающей запятой
  104.     EXPECT_FALSE(dbl_node.IsInt());
  105.  
  106.     EXPECT_EQ(Print(dbl_node), "123.45"s);
  107.  
  108.     EXPECT_EQ(LoadJSON("123.45"s).GetRoot(), dbl_node);
  109.     EXPECT_EQ(LoadJSON("0.25"s).GetRoot().AsDouble(), 0.25);
  110.     EXPECT_EQ(LoadJSON("3e5"s).GetRoot().AsDouble(), 3e5);
  111.     EXPECT_EQ(LoadJSON("1.2e-5"s).GetRoot().AsDouble(), 1.2e-5);
  112.     EXPECT_EQ(LoadJSON("1.2e+5"s).GetRoot().AsDouble(), 1.2e5);
  113.     EXPECT_EQ(LoadJSON("-123456"s).GetRoot().AsInt(), -123456);
  114. }
  115.  
  116. TEST(JsonParsing, DoubleNodeThrowsException) {
  117.     // TODO: maybe consider inputs like: 0.1qwe, 0e10, etc.
  118.     std::vector<std::string> input{"0.qwe"s,  "-0.qwe"s, "12.qwe"s, "-12.qwe"s, "1eqwe"s,
  119.                                    "-1eqwe"s, "1Eqwe"s,  "-1Eqwe"s, "12qwe.3"s};
  120.  
  121.     for (const auto& value : input)
  122.         EXPECT_THROW(auto node = LoadJSON("-qwe").GetRoot(), json::ParsingError);
  123. }
  124.  
  125. TEST(JsonParsing, EmptyStringNodeParsing) {
  126.     auto node = LoadJSON("\"\""s).GetRoot();
  127.  
  128.     EXPECT_TRUE(node.IsString());
  129.     EXPECT_TRUE(node.AsString().empty());
  130. }
  131.  
  132. TEST(JsonParsing, StringNode) {
  133.     Node str_node{" \r\n \" \t\t \\ "s};
  134.  
  135.     EXPECT_TRUE(str_node.IsString());
  136.     EXPECT_EQ(str_node.AsString(), " \r\n \" \t\t \\ "s);
  137.     EXPECT_EQ(Print(str_node), "\" \\r\\n \\\" \\t\\t \\\\ \""s);
  138.  
  139.     EXPECT_EQ(LoadJSON("\" \\r\\n \\\" \\t\\t \\\\ \""s).GetRoot(), str_node);
  140.  
  141.     str_node = Node{"Hello, \"everybody\""s};
  142.     EXPECT_TRUE(str_node.IsString());
  143.     EXPECT_EQ(str_node.AsString(), "Hello, \"everybody\""s);
  144.     EXPECT_EQ(Print(str_node), "\"Hello, \\\"everybody\\\"\""s);
  145.  
  146.     EXPECT_EQ(LoadJSON("\"Hello, \\\"everybody\\\"\"").GetRoot(), str_node);
  147. }
  148.  
  149. TEST(JsonParsing, StringNodeThrowsException) {
  150.     std::vector<std::string> input{"\""s, "\"qwe", "\"qw\\", "\"qw\\o"};
  151.  
  152.     for (const auto& value : input)
  153.         EXPECT_THROW(auto node = LoadJSON("-qwe").GetRoot(), json::ParsingError);
  154. }
  155.  
  156. TEST(JsonParsing, EmptyDictNodeParsing) {
  157.     auto node = LoadJSON("{}"s).GetRoot();
  158.  
  159.     EXPECT_TRUE(node.IsMap());
  160.     EXPECT_TRUE(node.AsMap().empty());
  161. }
  162.  
  163. TEST(JsonParsing, DictNode) {
  164.     Node dict_node{Dict{{"k0"s, nullptr},
  165.                         {"k1"s, true},
  166.                         {"k2"s, false},
  167.                         {"k3"s, 1},
  168.                         {"k4"s, 1.1},
  169.                         {"k5"s, "Hello"s},
  170.                         {"k6"s, Array{1, 1.1}},
  171.                         {"k7"s, Dict{{"k0"s, Array{}}}}}};
  172.  
  173.     EXPECT_TRUE(dict_node.IsMap());
  174.     const Dict& dict = dict_node.AsMap();
  175.  
  176.     EXPECT_EQ(dict.size(), 8);
  177.     EXPECT_EQ(dict.at("k0"s), Node{nullptr});
  178.     EXPECT_EQ(dict.at("k1"s).AsBool(), true);
  179.     EXPECT_EQ(dict.at("k2"s).AsBool(), false);
  180.     EXPECT_EQ(dict.at("k3"s).AsInt(), 1);
  181.     EXPECT_EQ(dict.at("k4"s).AsDouble(), 1.1);
  182.     EXPECT_EQ(dict.at("k5"s).AsString(), "Hello");
  183.  
  184.     EXPECT_TRUE(dict.at("k6"s).IsArray());
  185.     EXPECT_EQ(dict.at("k6"s).AsArray().size(), 2);
  186.     auto lol = dict.at("k6"s).AsArray();
  187.     EXPECT_EQ(dict.at("k6"s).AsArray().at(0).AsInt(), 1);
  188.     EXPECT_EQ(dict.at("k6"s).AsArray().at(1).AsDouble(), 1.1);
  189.  
  190.     EXPECT_TRUE(dict.at("k7"s).IsMap());
  191.     EXPECT_EQ(dict.at("k7"s).AsMap().size(), 1);
  192.     EXPECT_TRUE(dict.at("k7"s).AsMap().at("k0"s).IsArray());
  193.     EXPECT_TRUE(dict.at("k7"s).AsMap().at("k0"s).AsArray().empty());
  194.  
  195.     const std::string expected_print =
  196.         R"({"k0":null, "k1":true, "k2":false, "k3":1, "k4":1.1, "k5":"Hello", "k6":[1, 1.1], "k7":{"k0":[]}})"s;
  197.     auto actual_print = Print(dict_node);
  198.  
  199.     EXPECT_EQ(actual_print, expected_print);
  200.     EXPECT_EQ(LoadJSON(actual_print).GetRoot(), dict_node);
  201. }
  202.  
  203. TEST(JsonParsing, DictNodeThrowsException) {
  204.     std::vector<std::string> inputs {"{"s, "}"s, "{key:1}"s, "{\"key:1"s, "{\"key\",1}"s, "{\"key\"  , 1}"s};
  205.  
  206.     for (const auto& value: inputs)
  207.         EXPECT_THROW(auto node = LoadJSON(value).GetRoot(), json::ParsingError);
  208. }
  209.  
  210. TEST(JsonParsing, ArrayNodeWithAndWithoutSpaces) {
  211.     Node array_node{Array{1, 1.1}};
  212.  
  213.     EXPECT_EQ(LoadJSON("[1,1.1]"s).GetRoot(), array_node) << "Should parse array without spaces between items";
  214.     EXPECT_EQ(LoadJSON("[1, 1.1]"s).GetRoot(), array_node) << "Should parse array without spaces between items";
  215.     EXPECT_EQ(LoadJSON("[1,   1.1]"s).GetRoot(), array_node) << "Should parse array without spaces between items";
  216. }
  217.  
  218. TEST(JsonParsing, EmptyArrayNodeParsing) {
  219.     auto node = LoadJSON("[]"s).GetRoot();
  220.  
  221.     EXPECT_TRUE(node.IsArray());
  222.     EXPECT_TRUE(node.AsArray().empty());
  223. }
  224.  
  225. TEST(JsonParsing, ArrayNode) {
  226.     Node arr_node{
  227.         Array{nullptr, true, false, 1, 1.1, "Hello"s, Array{}, Array{1, 2.1}, Dict{}, Dict{{"key"s, "value"s}}}};
  228.     EXPECT_TRUE(arr_node.IsArray());
  229.  
  230.     const Array& arr = arr_node.AsArray();
  231.     EXPECT_EQ(arr.size(), 10);
  232.  
  233.     EXPECT_EQ(arr.at(0), Node{nullptr});
  234.     EXPECT_EQ(arr.at(1).AsBool(), true);
  235.     EXPECT_EQ(arr.at(2).AsBool(), false);
  236.     EXPECT_EQ(arr.at(3).AsInt(), 1);
  237.     EXPECT_EQ(arr.at(4).AsDouble(), 1.1);
  238.     EXPECT_EQ(arr.at(5).AsString(), "Hello"s);
  239.     EXPECT_EQ(arr.at(6).AsArray(), Array{});
  240.  
  241.     EXPECT_TRUE(arr.at(7).IsArray());
  242.     EXPECT_EQ(arr.at(7).AsArray().at(0), 1);
  243.     EXPECT_EQ(arr.at(7).AsArray().at(1), 2.1);
  244.  
  245.     EXPECT_EQ(arr.at(8).AsMap(), Dict{});
  246.  
  247.     EXPECT_TRUE(arr.at(9).IsMap());
  248.     EXPECT_EQ(arr.at(9).AsMap().at("key"s), "value"s);
  249.  
  250.     const auto expected_print = "[null, true, false, 1, 1.1, \"Hello\", [], [1, 2.1], {}, {\"key\":\"value\"}]"s;
  251.     auto actual_print = Print(arr_node);
  252.  
  253.     EXPECT_EQ(actual_print, expected_print);
  254.     EXPECT_EQ(LoadJSON(actual_print).GetRoot(), arr_node);
  255. }
  256.  
  257. TEST(JsonParsing, ArrayNodeThrowsException) {
  258.     EXPECT_THROW(LoadJSON("["s), json::ParsingError);
  259.     EXPECT_THROW(LoadJSON("]"s), json::ParsingError);
  260.  
  261.     EXPECT_THROW(LoadJSON("[1, 1.1"s), json::ParsingError);
  262.     EXPECT_THROW(LoadJSON("[1, 1.1 1.2]"s), json::ParsingError);
  263.     EXPECT_THROW(LoadJSON("[1  1.1]"s), json::ParsingError);
  264. }
  265.  
Add Comment
Please, Sign In to add comment