From f7dc4934b04c062b1ce8aad09725a30707255e69 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 11 Jan 2019 23:56:54 +0300 Subject: Improve standard version API --- tests/standard-version/driver.cxx | 117 ++++++++++++++++++++++++++++------ tests/standard-version/testscript | 130 ++++++++++++++++++++++++++++++++------ 2 files changed, 206 insertions(+), 41 deletions(-) (limited to 'tests/standard-version') diff --git a/tests/standard-version/driver.cxx b/tests/standard-version/driver.cxx index 7ad5b00..f20eb28 100644 --- a/tests/standard-version/driver.cxx +++ b/tests/standard-version/driver.cxx @@ -7,6 +7,7 @@ #ifndef __cpp_lib_modules #include // ios::failbit, ios::badbit #include +#include // uint*_t #include #include // invalid_argument #endif @@ -19,16 +20,19 @@ import std.core; import std.io; #endif import butl.utility; // operator<<(ostream,exception), eof() +import butl.optional; import butl.standard_version; #else #include +#include #include #endif using namespace std; using namespace butl; -// Create standard version from string, and also test another ctors. +// Return the standard version created from a string, and also perform some +// resulting version tests (check other constructors, invariants, etc.). // static standard_version version (const string& s, @@ -37,6 +41,8 @@ version (const string& s, { standard_version r (s, f); + // Test the other constructors. + // try { standard_version v (r.epoch, @@ -76,6 +82,8 @@ version (const string& s, assert (r == v); } + // Test using the resulting version with shortcut operators. + // if (!r.stub ()) { auto max_ver = [&v] (char c) -> string @@ -102,6 +110,29 @@ version (const string& s, assert (c1 == c2); } } + + // Check some invariants for the resulting version. + // + // Stub is not a final (pre-)release nor snapshot. + // + assert (!r.stub () || !(r.final () || r.snapshot ())); + + // Earliest is a final alpha. + // + assert (!r.earliest () || (r.final () && r.alpha ())); + + // Final is a release or a pre-release but not a snapshot. + // + assert (r.final () == + (r.release () || (r.pre_release () && !r.snapshot ()))); + + // Snapshot is a pre-release. + // + assert (!r.snapshot () || r.pre_release ()); + + // Pre-release is either alpha or beta. + // + assert (r.pre_release ().has_value () == (r.alpha () || r.beta ())); } catch (const invalid_argument& e) { @@ -114,18 +145,24 @@ version (const string& s, // Usages: // -// argv[0] -a -// argv[0] -b -// argv[0] -c -// argv[0] -r -// argv[0] -s +// argv[0] (-rl|-pr|-al|-bt|-st|-el|-sn|-fn) +// argv[0] -cm +// argv[0] -cr +// argv[0] -sf // argv[0] // -// -a output 'y' for alpha-version, 'n' otherwise -// -b output 'y' for beta-version, 'n' otherwise -// -c output 0 if versions are equal, -1 if the first one is less, 1 otherwise -// -r create version constraints from stdin lines, and print them to stdout -// -s output 'y' if version satisfies constraint, 'n' otherwise +// -rl output 'y' for release, 'n' otherwise +// -pr output DDD version part for pre-release, '-' otherwise +// -al output alpha version number for alpha-version, '-' otherwise +// -bt output beta version number for beta-version, '-' otherwise +// -st output 'y' for stub, 'n' otherwise +// -el output 'y' for earliest, 'n' otherwise +// -sn output 'y' for snapshot, 'n' otherwise +// -fn output 'y' for final, 'n' otherwise +// +// -cm output 0 if versions are equal, -1 if the first one is less, 1 otherwise +// -cr create version constraints from stdin lines, and print them to stdout +// -sf output 'y' if version satisfies constraint, 'n' otherwise // // If no options are specified, then create versions from stdin lines, and // print them to stdout. @@ -134,6 +171,8 @@ int main (int argc, char* argv[]) try { + using butl::optional; + cin.exceptions (ios::badbit); cout.exceptions (ios::failbit | ios::badbit); @@ -141,28 +180,66 @@ try { string o (argv[1]); - if (o == "-a") + if (o == "-rl") + { + assert (argc == 3); + cout << (version (argv[2]).release () ? 'y' : 'n') << endl; + } + else if (o == "-pr") { assert (argc == 3); - char r (version (argv[2]).alpha () ? 'y' : 'n'); - cout << r << endl; + if (optional n = version (argv[2]).pre_release ()) + cout << *n << endl; + else + cout << '-' << endl; } - else if (o == "-b") + else if (o == "-al") { assert (argc == 3); - char r (version (argv[2]).beta () ? 'y' : 'n'); - cout << r << endl; + if (optional n = version (argv[2]).alpha ()) + cout << *n << endl; + else + cout << '-' << endl; + } + else if (o == "-bt") + { + assert (argc == 3); + + if (optional n = version (argv[2]).beta ()) + cout << *n << endl; + else + cout << '-' << endl; + } + else if (o == "-st") + { + assert (argc == 3); + cout << (version (argv[2]).stub () ? 'y' : 'n') << endl; + } + else if (o == "-el") + { + assert (argc == 3); + cout << (version (argv[2]).earliest () ? 'y' : 'n') << endl; + } + else if (o == "-sn") + { + assert (argc == 3); + cout << (version (argv[2]).snapshot () ? 'y' : 'n') << endl; + } + else if (o == "-fn") + { + assert (argc == 3); + cout << (version (argv[2]).final () ? 'y' : 'n') << endl; } - else if (o == "-c") + else if (o == "-cm") { assert (argc == 4); int r (version (argv[2]).compare (version (argv[3]))); cout << r << endl; } - else if (o == "-r") + else if (o == "-cr") { assert (argc == 2); @@ -170,7 +247,7 @@ try while (getline (cin, s)) cout << standard_version_constraint (s) << endl; } - else if (o == "-s") + else if (o == "-sf") { assert (argc == 4); diff --git a/tests/standard-version/testscript b/tests/standard-version/testscript index 5b1e8d4..2efec3c 100644 --- a/tests/standard-version/testscript +++ b/tests/standard-version/testscript @@ -168,42 +168,130 @@ } } +: release +: +{ + test.options += -rl + + $* '1.2.3' >'y' : release + $* '1.2.3-b.1' >'n' : beta-final + $* '1.2.3-a.1' >'n' : alpha-final + $* '1.2.3-a.0.1' >'n' : alpha-snapshot + $* '1.2.3-b.0.1' >'n' : beta-snapshot + $* '1.2.3-' >'n' : earliest + $* '0' >'n' : stub +} + +: pre-release +: +{ + test.options += -pr + + $* '1.2.3' >'-' : release + $* '1.2.3-b.1' >'501' : beta-final + $* '1.2.3-a.1' >'1' : alpha-final + $* '1.2.3-a.0.1' >'0' : alpha-snapshot + $* '1.2.3-b.0.1' >'500' : beta-snapshot + $* '1.2.3-' >'0' : earliest + $* '0' >'-' : stub +} + : alpha : { - test.options += -a - - $* '1.2.3' >n: non-prerelease - $* '1.2.3-b.1' >n: beta - $* '1.2.3-a.1' >y: final - $* '1.2.3-a.0.1' >y: snapshot - $* '1.2.3-' >y: earliest - $* '0' >n: stub + test.options += -al + + $* '1.2.3' >'-' : release + $* '1.2.3-b.1' >'-' : beta-final + $* '1.2.3-a.1' >'1' : final + $* '1.2.3-a.0.1' >'0' : snapshot + $* '1.2.3-b.0.1' >'-' : beta-snapshot + $* '1.2.3-' >'0' : earliest + $* '0' >'-' : stub } : beta : { - test.options += -b - - $* '1.2.3' >n: non-prerelease - $* '1.2.3-a.1' >n: alpha - $* '1.2.3-b.1' >y: final - $* '1.2.3-b.0.1' >y: snapshot - $* '1.2.3-' >n: earliest - $* '0+1' >n: stub + test.options += -bt + + $* '1.2.3' >'-' : release + $* '1.2.3-a.1' >'-' : alpha + $* '1.2.3-b.1' >'1' : final + $* '1.2.3-a.0.1' >'-' : alpha-snapshot + $* '1.2.3-b.0.1' >'0' : snapshot + $* '1.2.3-' >'-' : earliest + $* '0+1' >'-' : stub +} + +: stub +: +{ + test.options += -st + + $* '1.2.3' >'n' : release + $* '1.2.3-b.1' >'n' : beta-final + $* '1.2.3-a.1' >'n' : alpha-final + $* '1.2.3-a.0.1' >'n' : alpha-snapshot + $* '1.2.3-b.0.1' >'n' : beta-snapshot + $* '1.2.3-' >'n' : earliest + $* '0' >'y' : stub + $* '0+1' >'y' : stub-1 +} + +: earliest +: +{ + test.options += -el + + $* '1.2.3' >'n' : release + $* '1.2.3-b.1' >'n' : beta-final + $* '1.2.3-a.1' >'n' : alpha-final + $* '1.2.3-a.0.1' >'n' : alpha-snapshot + $* '1.2.3-b.0.1' >'n' : beta-snapshot + $* '1.2.3-' >'y' : earliest + $* '0+2' >'n' : stub +} + +: snapshot +: +{ + test.options += -sn + + $* '1.2.3' >'n' : release + $* '1.2.3-b.1' >'n' : beta-final + $* '1.2.3-a.1' >'n' : alpha-final + $* '1.2.3-a.0.1' >'y' : alpha + $* '1.2.3-b.0.1' >'y' : beta + $* '1.2.3-a.1.z' >'y' : latest + $* '1.2.3-' >'n' : earliest + $* '0' >'n' : stub +} + +: final +: +{ + test.options += -fn + + $* '1.2.3' >'y' : release + $* '1.2.3-b.1' >'y' : beta-final + $* '1.2.3-a.1' >'y' : alpha-final + $* '1.2.3-a.0.1' >'n' : alpha + $* '1.2.3-b.0.1' >'n' : beta + $* '1.2.3-' >'y' : earliest + $* '0' >'n' : stub } : compare : { - test.options += -c + test.options += -cm : epoch : { - $* '+4-1.2.3' '+4-1.2.3' >'0' : equal - $* '1.2.4' '+4-1.2.3' >'-1': less + $* '+4-1.2.3' '+4-1.2.3' >'0' : equal + $* '1.2.4' '+4-1.2.3' >'-1' : less } : non-prerelease @@ -233,7 +321,7 @@ : constraints : { - test.options += -r + test.options += -cr : range : @@ -381,7 +469,7 @@ : satisfaction : { - test.options += -s + test.options += -sf : comparison : -- cgit v1.1