Repository: mesos Updated Branches: refs/heads/master f5d77f05b -> 957b4f280
Improved unit tests for Version in stout. Switch to table-based test cases, validate that version parsing produces the expected results more precisely. Review: https://reviews.apache.org/r/58706 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/957b4f28 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/957b4f28 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/957b4f28 Branch: refs/heads/master Commit: 957b4f280c36e8ee8e28c404e5aa7cad2583255a Parents: f5d77f0 Author: Neil Conway <[email protected]> Authored: Fri Apr 21 08:49:00 2017 -0700 Committer: Neil Conway <[email protected]> Committed: Wed Apr 26 15:26:49 2017 -0400 ---------------------------------------------------------------------- 3rdparty/stout/tests/version_tests.cpp | 83 +++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/957b4f28/3rdparty/stout/tests/version_tests.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/tests/version_tests.cpp b/3rdparty/stout/tests/version_tests.cpp index 724ed22..925f383 100644 --- a/3rdparty/stout/tests/version_tests.cpp +++ b/3rdparty/stout/tests/version_tests.cpp @@ -10,12 +10,22 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include <map> +#include <string> +#include <utility> +#include <vector> + #include <gtest/gtest.h> +#include <stout/foreach.hpp> #include <stout/gtest.hpp> #include <stout/stringify.hpp> #include <stout/version.hpp> +using std::map; +using std::pair; +using std::string; +using std::vector; // Verify version comparison operations. TEST(VersionTest, Comparison) @@ -37,28 +47,55 @@ TEST(VersionTest, Comparison) } -// Verify version parser. -TEST(VersionTest, Parse) +// Verify that valid version strings are parsed successfully. +TEST(VersionTest, ParseValid) +{ + // Each test case consists of an input value and a corresponding + // expected value: the `Version` that corresponds to the input, and + // the result of `operator<<` for that Version. + + typedef pair<Version, string> ExpectedValue; + + const map<string, ExpectedValue> testCases = { + // Prerelease labels are currently accepted but ignored. + {"1.20.3-rc1", {Version(1, 20, 3), "1.20.3"}}, + {"1.20.3", {Version(1, 20, 3), "1.20.3"}}, + {"1.20", {Version(1, 20, 0), "1.20.0"}}, + {"1", {Version(1, 0, 0), "1.0.0"}} + }; + + foreachpair (const string& input, const ExpectedValue& expected, testCases) { + Try<Version> actual = Version::parse(input); + + ASSERT_SOME(actual) + << "Error parsing input '" << input << "'"; + + EXPECT_EQ(std::get<0>(expected), actual.get()) + << "Incorrect parse of input '" << input << "'"; + EXPECT_EQ(std::get<1>(expected), stringify(actual.get())) + << "Unexpected stringify output for input '" << input << "'"; + } +} + + +// Verify that invalid version strings result in a parse error. +TEST(VersionTest, ParseInvalid) { - Try<Version> version1 = Version::parse("1.20.3"); - Try<Version> version2 = Version::parse("1.20"); - Try<Version> version3 = Version::parse("1"); - - EXPECT_GT(version1.get(), version2.get()); - EXPECT_GT(version2.get(), version3.get()); - EXPECT_EQ(stringify(version2.get()), "1.20.0"); - EXPECT_EQ(stringify(version3.get()), "1.0.0"); - - // Verify that tagged/labeled versions work. - Try<Version> version4 = Version::parse("1.20.3-rc1"); - EXPECT_SOME(version4); - EXPECT_EQ(version4.get(), version1.get()); - EXPECT_EQ(stringify(version4.get()), "1.20.3"); - - EXPECT_ERROR(Version::parse("0.a.b")); - EXPECT_ERROR(Version::parse("")); - EXPECT_ERROR(Version::parse("a")); - EXPECT_ERROR(Version::parse("1.")); - EXPECT_ERROR(Version::parse(".1.2")); - EXPECT_ERROR(Version::parse("0.1.2.3")); + const vector<string> inputs = { + "", + "0.a.b", + "a", + "1.", + ".1.2", + "0.1.2.3", + "-1.1.2" + }; + + foreach (const string& input, inputs) { + Try<Version> parse = Version::parse(input); + + EXPECT_ERROR(parse) + << "Expected error on input '" << input + << "'; got " << parse.get(); + } }
