Repository navigation
Expand file tree
/
Copy pathtest_errors.cpp
More file actions
60 lines (50 loc) · 1.76 KB
/
Copy pathtest_errors.cpp
File metadata and controls
60 lines (50 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <string>
#include <vector>
namespace test_errors {
struct Config {
std::string name;
int port;
};
TEST(cli, test_error_invalid_arg_format) {
const char* args[] = {"program", "not-a-flag"};
const auto result = rfl::cli::read<Config>(2, const_cast<char**>(args));
EXPECT_FALSE(result);
}
TEST(cli, test_error_invalid_int) {
const char* args[] = {"program", "--name=test", "--port=abc"};
const auto result = rfl::cli::read<Config>(3, const_cast<char**>(args));
EXPECT_FALSE(result);
}
TEST(cli, test_error_duplicate_key) {
const char* args[] = {"program", "--name=a", "--name=b", "--port=1"};
const auto result = rfl::cli::read<Config>(4, const_cast<char**>(args));
EXPECT_FALSE(result);
}
struct Files {
rfl::Validator<std::string, rfl::Size<rfl::Minimum<5>>> input_file;
std::string output_file;
};
struct Job {
Files files;
std::vector<int> retries;
};
TEST(cli, test_error_details) {
const char* args[] = {"program", "--files.input-file=abcd",
"--retries=1,two"};
const auto result = rfl::cli::read<Job>(3, const_cast<char**>(args));
ASSERT_FALSE(result);
using Path = std::vector<rfl::ErrorDetail::PathSegment>;
const auto details = result.error().details();
ASSERT_EQ(details.size(), 3u) << result.error().what();
EXPECT_EQ(details[0].path, (Path{"files", "input-file"}));
EXPECT_EQ(details[0].message,
"Size validation failed: Value expected to be greater than or "
"equal to 5, but got 4.");
EXPECT_EQ(details[1].path, (Path{"files", "output-file"}));
EXPECT_EQ(details[1].message, "Field named 'output-file' not found.");
EXPECT_EQ(details[2].path, (Path{"retries", size_t{1}}));
}
} // namespace test_errors