rippled
Loading...
Searching...
No Matches
SemanticVersion_test.cpp
1//------------------------------------------------------------------------------
2/*
3This file is part of rippled: https://github.com/ripple/rippled
4Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6Permission to use, copy, modify, and/or distribute this software for any
7purpose with or without fee is hereby granted, provided that the above
8copyright notice and this permission notice appear in all copies.
9
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19#include <xrpl/beast/core/SemanticVersion.h>
20#include <xrpl/beast/unit_test.h>
21namespace beast {
22
24{
26
27public:
28 void
29 checkPass(std::string const& input, bool shouldPass = true)
30 {
32
33 if (shouldPass)
34 {
35 BEAST_EXPECT(v.parse(input));
36 BEAST_EXPECT(v.print() == input);
37 }
38 else
39 {
40 BEAST_EXPECT(!v.parse(input));
41 }
42 }
43
44 void
45 checkFail(std::string const& input)
46 {
47 checkPass(input, false);
48 }
49
50 // check input and input with appended metadata
51 void
52 checkMeta(std::string const& input, bool shouldPass)
53 {
54 checkPass(input, shouldPass);
55
56 checkPass(input + "+a", shouldPass);
57 checkPass(input + "+1", shouldPass);
58 checkPass(input + "+a.b", shouldPass);
59 checkPass(input + "+ab.cd", shouldPass);
60
61 checkFail(input + "!");
62 checkFail(input + "+");
63 checkFail(input + "++");
64 checkFail(input + "+!");
65 checkFail(input + "+.");
66 checkFail(input + "+a.!");
67 }
68
69 void
71 {
72 checkMeta(input, false);
73 }
74
75 // check input, input with appended release data,
76 // input with appended metadata, and input with both
77 // appended release data and appended metadata
78 //
79 void
80 checkRelease(std::string const& input, bool shouldPass = true)
81 {
82 checkMeta(input, shouldPass);
83
84 checkMeta(input + "-1", shouldPass);
85 checkMeta(input + "-a", shouldPass);
86 checkMeta(input + "-a1", shouldPass);
87 checkMeta(input + "-a1.b1", shouldPass);
88 checkMeta(input + "-ab.cd", shouldPass);
89 checkMeta(input + "--", shouldPass);
90
91 checkMetaFail(input + "+");
92 checkMetaFail(input + "!");
93 checkMetaFail(input + "-");
94 checkMetaFail(input + "-!");
95 checkMetaFail(input + "-.");
96 checkMetaFail(input + "-a.!");
97 checkMetaFail(input + "-0.a");
98 }
99
100 // Checks the major.minor.version string alone and with all
101 // possible combinations of release identifiers and metadata.
102 //
103 void
104 check(std::string const& input, bool shouldPass = true)
105 {
106 checkRelease(input, shouldPass);
107 }
108
109 void
110 negcheck(std::string const& input)
111 {
112 check(input, false);
113 }
114
115 void
117 {
118 testcase("parsing");
119
120 check("0.0.0");
121 check("1.2.3");
122 check("2147483647.2147483647.2147483647"); // max int
123
124 // negative values
125 negcheck("-1.2.3");
126 negcheck("1.-2.3");
127 negcheck("1.2.-3");
128
129 // missing parts
130 negcheck("");
131 negcheck("1");
132 negcheck("1.");
133 negcheck("1.2");
134 negcheck("1.2.");
135 negcheck(".2.3");
136
137 // whitespace
138 negcheck(" 1.2.3");
139 negcheck("1 .2.3");
140 negcheck("1.2 .3");
141 negcheck("1.2.3 ");
142
143 // leading zeroes
144 negcheck("01.2.3");
145 negcheck("1.02.3");
146 negcheck("1.2.03");
147 }
148
149 static identifier_list
151 {
152 return identifier_list();
153 }
154
155 static identifier_list
156 ids(std::string const& s1)
157 {
159 v.push_back(s1);
160 return v;
161 }
162
163 static identifier_list
164 ids(std::string const& s1, std::string const& s2)
165 {
167 v.push_back(s1);
168 v.push_back(s2);
169 return v;
170 }
171
172 static identifier_list
173 ids(std::string const& s1, std::string const& s2, std::string const& s3)
174 {
176 v.push_back(s1);
177 v.push_back(s2);
178 v.push_back(s3);
179 return v;
180 }
181
182 // Checks the decomposition of the input into appropriate values
183 void
185 std::string const& input,
186 int majorVersion,
187 int minorVersion,
188 int patchVersion,
189 identifier_list const& preReleaseIdentifiers = identifier_list(),
190 identifier_list const& metaData = identifier_list())
191 {
193
194 BEAST_EXPECT(v.parse(input));
195
196 BEAST_EXPECT(v.majorVersion == majorVersion);
197 BEAST_EXPECT(v.minorVersion == minorVersion);
198 BEAST_EXPECT(v.patchVersion == patchVersion);
199
200 BEAST_EXPECT(v.preReleaseIdentifiers == preReleaseIdentifiers);
201 BEAST_EXPECT(v.metaData == metaData);
202 }
203
204 void
206 {
207 testcase("values");
208
209 checkValues("0.1.2", 0, 1, 2);
210 checkValues("1.2.3", 1, 2, 3);
211 checkValues("1.2.3-rc1", 1, 2, 3, ids("rc1"));
212 checkValues("1.2.3-rc1.debug", 1, 2, 3, ids("rc1", "debug"));
213 checkValues("1.2.3-rc1.debug.asm", 1, 2, 3, ids("rc1", "debug", "asm"));
214 checkValues("1.2.3+full", 1, 2, 3, ids(), ids("full"));
215 checkValues("1.2.3+full.prod", 1, 2, 3, ids(), ids("full", "prod"));
217 "1.2.3+full.prod.x86", 1, 2, 3, ids(), ids("full", "prod", "x86"));
219 "1.2.3-rc1.debug.asm+full.prod.x86",
220 1,
221 2,
222 3,
223 ids("rc1", "debug", "asm"),
224 ids("full", "prod", "x86"));
225 }
226
227 // makes sure the left version is less than the right
228 void
230 {
231 SemanticVersion left;
232 SemanticVersion right;
233
234 BEAST_EXPECT(left.parse(lhs));
235 BEAST_EXPECT(right.parse(rhs));
236
237 BEAST_EXPECT(compare(left, left) == 0);
238 BEAST_EXPECT(compare(right, right) == 0);
239 BEAST_EXPECT(compare(left, right) < 0);
240 BEAST_EXPECT(compare(right, left) > 0);
241
242 BEAST_EXPECT(left < right);
243 BEAST_EXPECT(right > left);
244 BEAST_EXPECT(left == left);
245 BEAST_EXPECT(right == right);
246 }
247
248 void
249 checkLess(std::string const& lhs, std::string const& rhs)
250 {
251 checkLessInternal(lhs, rhs);
252 checkLessInternal(lhs + "+meta", rhs);
253 checkLessInternal(lhs, rhs + "+meta");
254 checkLessInternal(lhs + "+meta", rhs + "+meta");
255 }
256
257 void
259 {
260 testcase("comparisons");
261
262 checkLess("1.0.0-alpha", "1.0.0-alpha.1");
263 checkLess("1.0.0-alpha.1", "1.0.0-alpha.beta");
264 checkLess("1.0.0-alpha.beta", "1.0.0-beta");
265 checkLess("1.0.0-beta", "1.0.0-beta.2");
266 checkLess("1.0.0-beta.2", "1.0.0-beta.11");
267 checkLess("1.0.0-beta.11", "1.0.0-rc.1");
268 checkLess("1.0.0-rc.1", "1.0.0");
269 checkLess("0.9.9", "1.0.0");
270 }
271
272 void
273 run() override
274 {
275 testParse();
276 testValues();
277 testCompare();
278 }
279};
280
281BEAST_DEFINE_TESTSUITE(SemanticVersion, beast_core, beast);
282} // namespace beast
static identifier_list ids()
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.
bool parse(std::string const &input)
Parse a semantic version string.
identifier_list preReleaseIdentifiers
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:53
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:153
int compare(SemanticVersion const &lhs, SemanticVersion const &rhs)
Compare two SemanticVersions against each other.
T push_back(T... args)