Skip to content

Commit

Permalink
Add tests for implicit conversion and exceptions for wrong types.
Browse files Browse the repository at this point in the history
  • Loading branch information
joto committed Jun 26, 2014
1 parent a123269 commit 12c7093
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,59 @@ TEST_CASE( "variant should correctly index types", "[variant]" ) {
REQUIRE(variant_type(float(0.0)).get_type_index() == 0);
}

TEST_CASE( "get with type not in variant type list should throw", "[variant]" ) {
typedef util::variant<int> variant_type;
variant_type var = 5;
REQUIRE(var.get<int>() == 5);
REQUIRE_THROWS(var.get<double>()); // XXX shouldn't this be a compile time error? See https://github.com/mapbox/variant/issues/24
}

TEST_CASE( "get with wrong type (here: double) should throw", "[variant]" ) {
typedef util::variant<int, double> variant_type;
variant_type var = 5;
REQUIRE(var.get<int>() == 5);
REQUIRE_THROWS(var.get<double>());
}

TEST_CASE( "get with wrong type (here: int) should throw", "[variant]" ) {
typedef util::variant<int, double> variant_type;
variant_type var = 5.0;
REQUIRE(var.get<double>() == 5.0);
REQUIRE_THROWS(var.get<int>());
}

TEST_CASE( "implicit conversion", "[variant][implicit conversion]" ) {
typedef util::variant<int> variant_type;
variant_type var(5.0); // converted to int
REQUIRE(var.get<int>() == 5);
REQUIRE_THROWS(var.get<double>());
var = 6.0; // works for operator=, too
REQUIRE(var.get<int>() == 6);
}

TEST_CASE( "implicit conversion to first type in variant type list", "[variant][implicit conversion]" ) {
typedef util::variant<long, char> variant_type;
variant_type var = 5.0; // converted to long
REQUIRE(var.get<long>() == 5);
REQUIRE_THROWS(var.get<char>());
REQUIRE_THROWS(var.get<double>());
}

struct dummy {};

TEST_CASE( "implicit conversion to first type it can convert to even if it doesn't fit", "[variant][implicit conversion]" ) {
typedef util::variant<dummy, unsigned char, long> variant_type;
variant_type var = 500.0; // converted to unsigned char, even if it doesn't fit
REQUIRE(var.get<unsigned char>() == static_cast<unsigned char>(500.0));
REQUIRE_THROWS(var.get<long>());
var = 500; // int converted to unsigned char, even if it doesn't fit
REQUIRE(var.get<unsigned char>() == static_cast<unsigned char>(500));
REQUIRE_THROWS(var.get<long>());
var = 500L; // explicit long is okay
REQUIRE(var.get<long>() == 500L);
REQUIRE_THROWS(var.get<char>());
}

TEST_CASE( "variant value traits", "[variant::detail]" ) {
// Users should not create variants with duplicated types
// however our type indexing should still work
Expand Down

0 comments on commit 12c7093

Please sign in to comment.