IRSOL
C++ code implementing socket server for interacting with Baumer camera.
test_utils.cpp
Go to the documentation of this file.
2
3#include <catch2/catch_all.hpp>
4#include <string>
5
6TEST_CASE("fromString<int>", "[Protocol][Protocol::Utils]")
7{
8 auto value = GENERATE(0, 1, 42, 31232423);
9
10 auto resValue = irsol::protocol::utils::fromString<int>(std::to_string(value));
11 REQUIRE(value == resValue);
12}
13
14TEST_CASE("fromString<int> with invalid arg", "[Protocol][Protocol::Utils]")
15{
16 auto value =
17 GENERATE("", " ", "-", "42.", "42.0", "42i", "42.i", ".42", "i42", "42..", "invalid");
18
19 REQUIRE_THROWS_AS(irsol::protocol::utils::fromString<int>(value), std::invalid_argument);
20}
21
22TEST_CASE("fromString<double>", "[Protocol][Protocol::Utils]")
23{
24 auto value = GENERATE(0.0, 1.0, 1.0001, 42.43242, 31232423.123);
25
26 auto resValue = irsol::protocol::utils::fromString<double>(std::to_string(value));
27 REQUIRE(value == Catch::Approx(resValue));
28}
29
30TEST_CASE("fromString<double>->throw", "[Protocol][Protocol::Utils]")
31{
32 auto value = GENERATE("", " ", "-", "42i", "42.i", "i42", "42..", "invalid");
33
34 REQUIRE_THROWS_AS(irsol::protocol::utils::fromString<double>(value), std::invalid_argument);
35}
36
37TEST_CASE("fromString<string>", "[Protocol][Protocol::Utils]")
38{
39 auto value = GENERATE("", "h", "hello", "hello world", "4.s", "s3");
40
41 auto resValue = irsol::protocol::utils::fromString<std::string>(value);
42 REQUIRE(value == resValue);
43}
44
45TEST_CASE("trim", "[Protocol][Protocol::Utils]")
46{
47 auto value = GENERATE(
48 "hello world",
49 " hello world",
50 "hello world ",
51 " hello world\n",
52 " hello world\t",
53 "\n\t \t\t \n\r hello world\n \r \r");
54
55 auto trimmedValue = irsol::protocol::utils::trim(value);
56 REQUIRE(trimmedValue == "hello world");
57}
std::string trim(const std::string &s)
Remove leading and trailing whitespace from a string.
Definition utils.hpp:122
TEST_CASE("fromString<int>", "[Protocol][Protocol::Utils]")
Definition test_utils.cpp:6
Utility functions for protocol string handling and validation in the irsol library.
auto value
REQUIRE(msg.has_value())