Debian Codegraph @

tests/src/test_stringutils.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include <catch2/catch_all.hpp>
5 #include <catch2/trompeloeil.hpp>
6
7 #include "common/stringutils.h"
8
9 extern template struct trompeloeil::reporter<trompeloeil::specialized>;
10
11 namespace Tests::Utils::String {
12
13 TEST_CASE("StringUtils tests", "[Utils][String]")
14 {
15 SECTION("cleanPrefix")
16 {
17 SECTION("Returns a string without the prefix when the input string "
18 "contains the prefix")
19 {
20 std::string prefix("prefix ");
21 std::string data("prefix text");
22 auto res = ::Utils::String::cleanPrefix(data, prefix);
23
24 REQUIRE(res == "text");
25 }
26
27 SECTION("Returns the input string when the prefix is not a substring of "
28 "the input string")
29 {
30 std::string prefix("prefix");
31 std::string data("some data");
32 auto res = ::Utils::String::cleanPrefix(data, prefix);
33
34 REQUIRE(res == "some data");
35 }
36
37 SECTION("Returns an empty string when the input string equals the prefix")
38 {
39 std::string prefix("prefix");
40 std::string data("prefix");
41 auto res = ::Utils::String::cleanPrefix(data, prefix);
42
43 REQUIRE(res == "");
44 }
45
46 SECTION("Returns the input string when it is shorter than the prefix")
47 {
48 std::string prefix("prefix");
49 std::string data("pref");
50 auto res = ::Utils::String::cleanPrefix(data, prefix);
51
52 REQUIRE(res == "pref");
53 }
54
55 SECTION("Returns the input string when the prefix is empty")
56 {
57 std::string prefix("");
58 std::string data("data");
59 auto res = ::Utils::String::cleanPrefix(data, prefix);
60
61 REQUIRE(res == "data");
62 }
63 }
64
65 SECTION("parseVersion")
66 {
67 SECTION("Returns version when the input string is in 'mayor.minor.patch' "
68 "format")
69 {
70 auto version = ::Utils::String::parseVersion("1.2.3");
71 REQUIRE(version == std::make_tuple(1, 2, 3));
72 }
73
74 SECTION("Returns 0.0.0 version when the input string is not in "
75 "'mayor.minor.patch' format")
76 {
77 auto version = ::Utils::String::parseVersion("not a version");
78 REQUIRE(version == std::make_tuple(0, 0, 0));
79 }
80 }
81
82 SECTION("split")
83 {
84 SECTION("Doesn't include the delimiter character in the results")
85 {
86 auto result = ::Utils::String::split(" ", ' ');
87 REQUIRE(result.empty());
88 }
89
90 SECTION("Returns the original string when it doesn't contains the "
91 "delimiter character")
92 {
93 auto result = ::Utils::String::split("one", ' ');
94 REQUIRE(result.size() == 1);
95 REQUIRE(result[0] == "one");
96 }
97
98 SECTION("Split a string into substrings")
99 {
100 auto result = ::Utils::String::split("one two three ", ' ');
101 REQUIRE(result.size() == 3);
102 REQUIRE(result[0] == "one");
103 REQUIRE(result[1] == "two");
104 REQUIRE(result[2] == "three");
105 }
106 }
107
108 SECTION("parseKernelProcVersion")
109 {
110 SECTION("Provides kernel version when data is using...")
111 {
112 SECTION("Semver format")
113 {
114 std::string const data("Linux version 1.2.3_other_info ...");
115 auto output = ::Utils::String::parseKernelProcVersion(data);
116 REQUIRE(output.has_value());
117 REQUIRE(*output == "1.2.3");
118 }
119
120 SECTION("Invalid semver format...")
121 {
122 SECTION("Missing patch version (see #254)")
123 {
124 std::string const data("Linux version 1.2_other_info ...");
125 auto output = ::Utils::String::parseKernelProcVersion(data);
126 REQUIRE(output.has_value());
127 REQUIRE(*output == "1.2.0");
128 }
129
130 SECTION("With additional version numbers after patch version")
131 {
132 std::string const data("Linux version 1.2.3.4_other_info ...");
133 auto output = ::Utils::String::parseKernelProcVersion(data);
134 REQUIRE(output.has_value());
135 REQUIRE(*output == "1.2.3");
136 }
137 }
138 }
139
140 SECTION("Returns no value when the string follow an unknown format")
141 {
142 std::string const data("Other format");
143 auto output = ::Utils::String::parseKernelProcVersion(data);
144 REQUIRE_FALSE(output.has_value());
145 }
146 }
147 }
148
149 } // namespace Tests::Utils::String