1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
4#include <userver/protobuf/json/exceptions.hpp>
8namespace protobuf::json::tests {
10TEST(ExceptionsTest, CtorCorrectlyInitializesObject) {
12 const std::vector<std::pair<ParseErrorCode, std::string>> errors{
13 {ParseErrorCode::kUnknownField,
"hello world"},
14 {ParseErrorCode::kUnknownEnum,
""},
15 {ParseErrorCode::kMultipleOneofFields,
"additional description"},
16 {ParseErrorCode::kInvalidType,
""},
17 {ParseErrorCode::kInvalidValue,
""},
18 {
static_cast<ParseErrorCode>(100),
""}
20 const std::string path =
"field.array[0].item[1]";
22 for (
const auto& e : errors) {
23 ParseError error(e.first, path, e.second);
25 EXPECT_EQ(error.GetErrorInfo().GetCode(), e.first);
26 EXPECT_EQ(error.GetErrorInfo().GetPath(), path);
27 EXPECT_EQ(std::move(error).GetErrorInfo().GetPath(), path);
32 const std::vector<std::pair<PrintErrorCode, std::string>>
33 errors{{PrintErrorCode::kInvalidValue,
""}, {
static_cast<PrintErrorCode>(100),
"hello world"}};
34 const std::string path =
"field.array[0].item[1].map['key']";
36 for (
const auto& e : errors) {
37 PrintError error(e.first, path, e.second);
39 EXPECT_EQ(error.GetErrorInfo().GetCode(), e.first);
40 EXPECT_EQ(error.GetErrorInfo().GetPath(), path);
41 EXPECT_EQ(std::move(error).GetErrorInfo().GetPath(), path);
46TEST(ExceptionsTest, WhatContainsImportantDetails) {
48 const std::string path =
"field.array[0].item[1]";
49 const std::string additional_desc =
"hello world";
50 const ParseError error(ParseErrorCode::kInvalidType, path, additional_desc);
52 EXPECT_THAT(error.what(), ::testing::HasSubstr(path));
53 EXPECT_THAT(error.what(), ::testing::HasSubstr(additional_desc));
57 const std::string path =
"field.array[0].item[1].map['key']";
58 const std::string additional_desc =
"hello world";
59 const PrintError error(PrintErrorCode::kInvalidValue, path, additional_desc);
61 EXPECT_THAT(error.what(), ::testing::HasSubstr(path));
62 EXPECT_THAT(error.what(), ::testing::HasSubstr(additional_desc));