userver: /data/code/userver/libraries/protobuf/tests/json/exceptions_test.cpp Source File
Loading...
Searching...
No Matches
exceptions_test.cpp
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <userver/protobuf/json/exceptions.hpp>
5
6USERVER_NAMESPACE_BEGIN
7
8namespace protobuf::json::tests {
9
10TEST(ExceptionsTest, CtorCorrectlyInitializesObject) {
11 {
12 const std::string path = "field.array[0].item[1]";
13 const std::vector<ParseErrorCode> codes{
14 ParseErrorCode::kUnknownField,
15 ParseErrorCode::kUnknownEnum,
16 ParseErrorCode::kMultipleOneofFields,
17 ParseErrorCode::kInvalidType,
18 ParseErrorCode::kInvalidValue,
19 static_cast<ParseErrorCode>(100)
20 };
21
22 for (const auto& code : codes) {
23 ParseError error(code, path);
24
25 EXPECT_EQ(error.GetErrorInfo().GetCode(), code);
26 EXPECT_EQ(error.GetErrorInfo().GetPath(), path);
27 EXPECT_EQ(std::move(error).GetErrorInfo().GetPath(), path);
28 }
29 }
30
31 {
32 const std::string path = "field.array[0].item[1].map['key']";
33 const std::vector<PrintErrorCode> codes{PrintErrorCode::kInvalidValue, static_cast<PrintErrorCode>(100)};
34
35 for (const auto& code : codes) {
36 PrintError error(code, path);
37
38 EXPECT_EQ(error.GetErrorInfo().GetCode(), code);
39 EXPECT_EQ(error.GetErrorInfo().GetPath(), path);
40 EXPECT_EQ(std::move(error).GetErrorInfo().GetPath(), path);
41 }
42 }
43}
44
45TEST(ExceptionsTest, WhatContainsImportantDetails) {
46 {
47 const std::string path = "field.array[0].item[1]";
48 const ParseError error(ParseErrorCode::kInvalidType, path);
49
50 EXPECT_THAT(error.what(), ::testing::HasSubstr(path));
51 }
52
53 {
54 const std::string path = "field.array[0].item[1].map['key']";
55 const PrintError error(PrintErrorCode::kInvalidValue, path);
56
57 EXPECT_THAT(error.what(), ::testing::HasSubstr(path));
58 }
59}
60
61} // namespace protobuf::json::tests
62
63USERVER_NAMESPACE_END