IRSOL
C++ code implementing socket server for interacting with Baumer camera.
test_parser.cpp
Go to the documentation of this file.
3
4#include <catch2/catch_all.hpp>
5
6TEST_CASE("Parsing invalid identifier", "[Protocol][Protocol::Parser]")
7{
8 // Invalid identifiers
9 auto identifier = GENERATE(
10 ""
11 "identifier with spaces",
12 "identifier-with-dashes",
13 "non-alphanumeric<",
14 "??");
15
16 // Valid suffixes (for assignments, queries and commands)
17 auto suffix = GENERATE("=value", "=42", "=3.14", "?", "");
18
19 std::string line = std::string(identifier) + suffix;
20
22 REQUIRE(!msg.has_value());
23}
24
25TEST_CASE("Assignment parsing int", "[Protocol][Protocol::Parser]")
26{
27 auto identifier = GENERATE(
28 "x", "it", "long_identifier", "sequence_identifier[4]", "nested_sequence_identifier[4][423]");
29 auto value = GENERATE(42, 5, 32121);
30
31 std::string input = std::string(identifier) + "=" + std::to_string(value);
32
34 REQUIRE(msg.has_value());
35
36 auto asg = std::get<irsol::protocol::Assignment>(*msg);
37
38 CHECK(asg.identifier == identifier);
39 CHECK(std::get<int>(asg.value) == value);
40}
41
42TEST_CASE("Assignment parsing double", "[Protocol][Protocol::Parser]")
43{
44 auto identifier = GENERATE(
45 "x", "it", "long_identifier", "sequence_identifier[4]", "nested_sequence_identifier[4][423]");
46 auto value = GENERATE(42.0, 0, 5, 32121.4234234);
47
48 std::string input = std::string(identifier) + "=" + std::to_string(value);
49
51 REQUIRE(msg.has_value());
52
53 auto asg = std::get<irsol::protocol::Assignment>(*msg);
54
55 CHECK(asg.identifier == identifier);
56 CHECK(std::get<double>(asg.value) == Catch::Approx(value));
57}
58
59TEST_CASE("Assignment parsing string", "[Protocol][Protocol::Parser]")
60{
61 auto identifier = GENERATE(
62 "x", "it", "long_identifier", "sequence_identifier[4]", "nested_sequence_identifier[4][423]");
63 auto value = GENERATE("c", "longer", "long_string_with_underscores", "5è4?-é");
64
65 std::string input = std::string(identifier) + "=" + value;
66
68 REQUIRE(msg.has_value());
69
70 auto asg = std::get<irsol::protocol::Assignment>(*msg);
71
72 CHECK(asg.identifier == identifier);
73 CHECK(std::get<std::string>(asg.value) == value);
74}
75TEST_CASE("Assignment parsing string (quoted)", "[Protocol][Protocol::Parser]")
76{
77 auto identifier = GENERATE(
78 "x", "it", "long_identifier", "sequence_identifier[4]", "nested_sequence_identifier[4][423]");
79 auto value = GENERATE("c", "longer", "long_string_with_underscores", "5è4?-é");
80 auto prefixSuffixChars = GENERATE("\"\"", "''", "{}", " ");
81
82 std::string input =
83 std::string(identifier) + "=" + prefixSuffixChars[0] + value + prefixSuffixChars[1];
84
86 REQUIRE(msg.has_value());
87
88 auto asg = std::get<irsol::protocol::Assignment>(*msg);
89
90 CHECK(asg.identifier == identifier);
91 CHECK(std::get<std::string>(asg.value) == value);
92}
93
94TEST_CASE("Inquiry parsing", "[Protocol][Protocol::Parser]")
95{
96 auto identifier = GENERATE(
97 "x", "it", "long_identifier", "sequence_identifier[4]", "nested_sequence_identifier[4][423]");
98
99 std::string input = std::string(identifier) + "?";
101 REQUIRE(msg.has_value());
102 auto inq = std::get<irsol::protocol::Inquiry>(*msg);
103 CHECK(inq.identifier == identifier);
104}
105
106TEST_CASE("Command parsing", "[Protocol][Protocol::Parser]")
107{
108 auto identifier = GENERATE("x", "it", "long_identifier");
109
110 std::string input = std::string(identifier);
112 REQUIRE(msg.has_value());
113 auto inq = std::get<irsol::protocol::Command>(*msg);
114 CHECK(inq.identifier == identifier);
115}
static std::optional< InMessage > parse(const std::string &line)
Attempts to parse a single protocol input line into a structured InMessage.
Definition parser.cpp:11
Parses raw protocol input strings into structured messages.
auto prefixSuffixChars
auto asg
auto value
REQUIRE(msg.has_value())
auto msg
std::string input
TEST_CASE("Parsing invalid identifier", "[Protocol][Protocol::Parser]")
CHECK(asg.identifier==identifier)