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