xrpld
Loading...
Searching...
No Matches
SemanticVersion_test.cpp
1#include <xrpl/beast/core/SemanticVersion.h>
2#include <xrpl/beast/unit_test/suite.h>
3
4#include <string>
5
6namespace beast {
7
9{
11
12public:
13 void
14 checkPass(std::string const& input, bool shouldPass = true)
15 {
17
18 if (shouldPass)
19 {
20 BEAST_EXPECT(v.parse(input));
21 BEAST_EXPECT(v.print() == input);
22 }
23 else
24 {
25 BEAST_EXPECT(!v.parse(input));
26 }
27 }
28
29 void
30 checkFail(std::string const& input)
31 {
32 checkPass(input, false);
33 }
34
35 // check input and input with appended metadata
36 void
37 checkMeta(std::string const& input, bool shouldPass)
38 {
39 checkPass(input, shouldPass);
40
41 checkPass(input + "+a", shouldPass);
42 checkPass(input + "+1", shouldPass);
43 checkPass(input + "+a.b", shouldPass);
44 checkPass(input + "+ab.cd", shouldPass);
45
46 checkFail(input + "!");
47 checkFail(input + "+");
48 checkFail(input + "++");
49 checkFail(input + "+!");
50 checkFail(input + "+.");
51 checkFail(input + "+a.!");
52 }
53
54 void
56 {
57 checkMeta(input, false);
58 }
59
60 // check input, input with appended release data,
61 // input with appended metadata, and input with both
62 // appended release data and appended metadata
63 //
64 void
65 checkRelease(std::string const& input, bool shouldPass = true)
66 {
67 checkMeta(input, shouldPass);
68
69 checkMeta(input + "-1", shouldPass);
70 checkMeta(input + "-a", shouldPass);
71 checkMeta(input + "-a1", shouldPass);
72 checkMeta(input + "-a1.b1", shouldPass);
73 checkMeta(input + "-ab.cd", shouldPass);
74 checkMeta(input + "--", shouldPass);
75
76 checkMetaFail(input + "+");
77 checkMetaFail(input + "!");
78 checkMetaFail(input + "-");
79 checkMetaFail(input + "-!");
80 checkMetaFail(input + "-.");
81 checkMetaFail(input + "-a.!");
82 checkMetaFail(input + "-0.a");
83 }
84
85 // Checks the major.minor.version string alone and with all
86 // possible combinations of release identifiers and metadata.
87 //
88 void
89 check(std::string const& input, bool shouldPass = true)
90 {
91 checkRelease(input, shouldPass);
92 }
93
94 void
95 negcheck(std::string const& input)
96 {
97 check(input, false);
98 }
99
100 void
102 {
103 testcase("parsing");
104
105 check("0.0.0");
106 check("1.2.3");
107 check("2147483647.2147483647.2147483647"); // max int
108
109 // negative values
110 negcheck("-1.2.3");
111 negcheck("1.-2.3");
112 negcheck("1.2.-3");
113
114 // missing parts
115 negcheck("");
116 negcheck("1");
117 negcheck("1.");
118 negcheck("1.2");
119 negcheck("1.2.");
120 negcheck(".2.3");
121
122 // whitespace
123 negcheck(" 1.2.3");
124 negcheck("1 .2.3");
125 negcheck("1.2 .3");
126 negcheck("1.2.3 ");
127
128 // leading zeroes
129 negcheck("01.2.3");
130 negcheck("1.02.3");
131 negcheck("1.2.03");
132 }
133
134 static identifier_list
136 {
137 return identifier_list();
138 }
139
140 static identifier_list
141 ids(std::string const& s1)
142 {
144 v.push_back(s1);
145 return v;
146 }
147
148 static identifier_list
149 ids(std::string const& s1, std::string const& s2)
150 {
152 v.push_back(s1);
153 v.push_back(s2);
154 return v;
155 }
156
157 static identifier_list
158 ids(std::string const& s1, std::string const& s2, std::string const& s3)
159 {
161 v.push_back(s1);
162 v.push_back(s2);
163 v.push_back(s3);
164 return v;
165 }
166
167 // Checks the decomposition of the input into appropriate values
168 void
170 std::string const& input,
171 int majorVersion,
172 int minorVersion,
173 int patchVersion,
174 identifier_list const& preReleaseIdentifiers = identifier_list(),
175 identifier_list const& metaData = identifier_list())
176 {
178
179 BEAST_EXPECT(v.parse(input));
180
181 BEAST_EXPECT(v.majorVersion == majorVersion);
182 BEAST_EXPECT(v.minorVersion == minorVersion);
183 BEAST_EXPECT(v.patchVersion == patchVersion);
184
185 BEAST_EXPECT(v.preReleaseIdentifiers == preReleaseIdentifiers);
186 BEAST_EXPECT(v.metaData == metaData);
187 }
188
189 void
191 {
192 testcase("values");
193
194 checkValues("0.1.2", 0, 1, 2);
195 checkValues("1.2.3", 1, 2, 3);
196 checkValues("1.2.3-rc1", 1, 2, 3, ids("rc1"));
197 checkValues("1.2.3-rc1.debug", 1, 2, 3, ids("rc1", "debug"));
198 checkValues("1.2.3-rc1.debug.asm", 1, 2, 3, ids("rc1", "debug", "asm"));
199 checkValues("1.2.3+full", 1, 2, 3, ids(), ids("full"));
200 checkValues("1.2.3+full.prod", 1, 2, 3, ids(), ids("full", "prod"));
201 checkValues("1.2.3+full.prod.x86", 1, 2, 3, ids(), ids("full", "prod", "x86"));
203 "1.2.3-rc1.debug.asm+full.prod.x86",
204 1,
205 2,
206 3,
207 ids("rc1", "debug", "asm"),
208 ids("full", "prod", "x86"));
209 }
210
211 // makes sure the left version is less than the right
212 void
214 {
215 SemanticVersion left;
216 SemanticVersion right;
217
218 BEAST_EXPECT(left.parse(lhs));
219 BEAST_EXPECT(right.parse(rhs));
220
221 BEAST_EXPECT(compare(left, left) == 0);
222 BEAST_EXPECT(compare(right, right) == 0);
223 BEAST_EXPECT(compare(left, right) < 0);
224 BEAST_EXPECT(compare(right, left) > 0);
225
226 BEAST_EXPECT(left < right);
227 BEAST_EXPECT(right > left);
228 BEAST_EXPECT(left == left);
229 BEAST_EXPECT(right == right);
230 }
231
232 void
233 checkLess(std::string const& lhs, std::string const& rhs)
234 {
235 checkLessInternal(lhs, rhs);
236 checkLessInternal(lhs + "+meta", rhs);
237 checkLessInternal(lhs, rhs + "+meta");
238 checkLessInternal(lhs + "+meta", rhs + "+meta");
239 }
240
241 void
243 {
244 testcase("comparisons");
245
246 checkLess("1.0.0-alpha", "1.0.0-alpha.1");
247 checkLess("1.0.0-alpha.1", "1.0.0-alpha.beta");
248 checkLess("1.0.0-alpha.beta", "1.0.0-beta");
249 checkLess("1.0.0-beta", "1.0.0-beta.2");
250 checkLess("1.0.0-beta.2", "1.0.0-beta.11");
251 checkLess("1.0.0-beta.11", "1.0.0-rc.1");
252 checkLess("1.0.0-rc.1", "1.0.0");
253 checkLess("0.9.9", "1.0.0");
254 }
255
256 void
257 run() override
258 {
259 testParse();
260 testValues();
261 testCompare();
262 }
263};
264
266} // namespace beast
SemanticVersion::identifier_list identifier_list
void checkFail(std::string const &input)
void checkPass(std::string const &input, bool shouldPass=true)
void check(std::string const &input, bool shouldPass=true)
void checkMeta(std::string const &input, bool shouldPass)
static identifier_list ids(std::string const &s1, std::string const &s2, std::string const &s3)
void checkLess(std::string const &lhs, std::string const &rhs)
void checkLessInternal(std::string const &lhs, std::string const &rhs)
void checkMetaFail(std::string const &input)
static identifier_list ids(std::string const &s1, std::string const &s2)
void checkRelease(std::string const &input, bool shouldPass=true)
static identifier_list ids(std::string const &s1)
void checkValues(std::string const &input, int majorVersion, int minorVersion, int patchVersion, identifier_list const &preReleaseIdentifiers=identifier_list(), identifier_list const &metaData=identifier_list())
void run() override
Runs the suite.
void negcheck(std::string const &input)
A Semantic Version number.
identifier_list preReleaseIdentifiers
bool parse(std::string_view input)
Parse a semantic version string.
std::string print() const
Produce a string from semantic version components.
std::vector< std::string > identifier_list
identifier_list metaData
A testsuite class.
Definition suite.h:50
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
int compare(SemanticVersion const &lhs, SemanticVersion const &rhs)
Compare two SemanticVersions against each other.
BEAST_DEFINE_TESTSUITE(aged_set, beast, beast)
T push_back(T... args)