Advertisement
mrmamongo

test.cpp

Dec 11th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. // Copyright 2020 BoryaBes <[email protected]>
  2.  
  3. #include <gtest/gtest.h>
  4.  
  5. #include <header.hpp>
  6. #include <sstream>
  7.  
  8. TEST(table, print_table) {
  9.   std::stringstream json_stream;
  10.   json_stream << R"(
  11. {
  12.  "items": [
  13.    {
  14.      "name": "Ivanov Petr",
  15.      "group": "1",
  16.      "avg": "4.25",
  17.      "debt": null
  18.    },
  19.    {
  20.      "name": "Sidorov Ivan",
  21.      "group": 31,
  22.      "avg": 4,
  23.      "debt": "C++"
  24.    },
  25.    {
  26.      "name": "Pertov Nikita",
  27.      "group": "IU8-31",
  28.      "avg": 3.33,
  29.      "debt": [
  30.        "C++",
  31.        "Linux",
  32.        "Network"
  33.      ]
  34.    }
  35.  ],
  36.  "_meta": {
  37.    "count": 3
  38.  }
  39. }
  40. )";
  41.   auto students = parse_json(json_stream);
  42.   std::stringstream printed_table;
  43.   printed_table << students;
  44.   std::string ref_string = R"(| name                | group     | avg   | debt      |
  45. |---------------------|-----------|-------|-----------|
  46. | Ivanov Petr         | 1         | 4.25  | null      |
  47. |---------------------|-----------|-------|-----------|
  48. | Sidorov Ivan        | 31        | 4     | C++       |
  49. |---------------------|-----------|-------|-----------|
  50. | Pertov Nikita       | IU8-31    | 3.33  | 3    items|
  51. |---------------------|-----------|-------|-----------|
  52. )";
  53.   EXPECT_EQ(ref_string, printed_table.str());
  54. }
  55.  
  56. TEST(exception, ItemsIsNotArray) {
  57.   std::stringstream json_stream;
  58.  
  59.   std::string exception_String;
  60.   json_stream << R"(
  61.  {
  62.    "items":
  63.    {
  64.      "name": "Ivanov Petr",
  65.          "group": "1",
  66.          "avg": "4.25",
  67.          "debt": null
  68.    },
  69. "_meta": {
  70. "count": 3
  71. }
  72.  }
  73. )";
  74.   try {
  75.     auto students = parse_json(json_stream);
  76.   } catch (std::runtime_error &e) {
  77.     exception_String = e.what();
  78.   }
  79.   EXPECT_EQ(exception_String, "Items most be array type");
  80. }
  81. TEST(exception, InvalidCount) {
  82.   std::stringstream json_stream;
  83.   std::string exception_String;
  84.   json_stream << R"(
  85.  {
  86.    "items": [
  87.      {
  88.        "name": "Ivanov Petr",
  89.            "group": "1",
  90.            "avg": "4.25",
  91.            "debt": null
  92.      },
  93.      {
  94.        "name": "Sidorov Ivan",
  95.            "group": 31,
  96.            "avg": 4,
  97.            "debt": "C++"
  98.      },
  99.      {
  100.        "name": "Pertov Nikita",
  101.            "group": "IU8-31",
  102.            "avg": 3.33,
  103.            "debt": [
  104.        "C++",
  105.            "Linux",
  106.            "Network"
  107.        ]
  108.      }
  109.      ],
  110.      "_meta": {
  111.      "count" : 11
  112.    }
  113.  })";
  114.   try {
  115.     auto students = parse_json(json_stream);
  116.   } catch (std::runtime_error &e) {
  117.     exception_String = e.what();
  118.   }
  119.   EXPECT_EQ(exception_String, "meta: count and items size mismatch");
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement