/* Copyright (C) 2013 BMW Group * Author: Manfred Bathelt (manfred.bathelt@bmw.de) * Author: Juergen Gehring (juergen.gehring@bmw.de) * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include using namespace CommonAPI; class VariantTest: public ::testing::Test { protected: typedef Variant BasicVariantType; void SetUp() { fromInt = 5; fromDouble = 12.344; fromString = "123abcsadfaljkawlöfasklöerklöfjasklfjysklfjaskfjsklösdfdko4jdfasdjioögjopefgip3rtgjiprg!"; } void TearDown() { } int fromInt; double fromDouble; std::string fromString; }; TEST_F(VariantTest, HandlesInts) { BasicVariantType myVariant(fromInt); EXPECT_TRUE(myVariant.isType()); EXPECT_FALSE(myVariant.isType()); EXPECT_FALSE(myVariant.isType()); const int myInt = myVariant.get(); EXPECT_EQ(myInt, fromInt); EXPECT_ANY_THROW(myVariant.get()); EXPECT_ANY_THROW(myVariant.get()); } TEST_F(VariantTest, HandlesDoubles) { BasicVariantType myVariant(fromDouble); EXPECT_FALSE(myVariant.isType()); EXPECT_TRUE(myVariant.isType()); EXPECT_FALSE(myVariant.isType()); EXPECT_ANY_THROW(myVariant.get()); const double myDouble = myVariant.get(); EXPECT_EQ(myDouble, fromDouble); EXPECT_ANY_THROW(myVariant.get()); } TEST_F(VariantTest, HandlesStrings) { BasicVariantType myVariant(fromString); EXPECT_FALSE(myVariant.isType()); EXPECT_FALSE(myVariant.isType()); EXPECT_TRUE(myVariant.isType()); EXPECT_ANY_THROW(myVariant.get()); EXPECT_ANY_THROW(myVariant.get()); const std::string myString = myVariant.get(); EXPECT_EQ(myString, fromString); } TEST_F(VariantTest, HandlesStringVectors) { typedef Variant> VectorVariantType; std::vector testVector; for(int i = 0; i < 10; i++) { testVector.push_back(fromString); } VectorVariantType vectorVariant(testVector); const std::vector resultVector = vectorVariant.get>(); EXPECT_EQ(resultVector, testVector); } TEST_F(VariantTest, HandlesAssignment) { Variant myVariant = fromInt; myVariant = fromString; EXPECT_FALSE(myVariant.isType()); EXPECT_FALSE(myVariant.isType()); EXPECT_TRUE(myVariant.isType()); EXPECT_ANY_THROW(myVariant.get()); const std::string myString = myVariant.get(); EXPECT_EQ(myString, fromString); } TEST_F(VariantTest, HandlesVariantConstructionByAssignment) { Variant myVariant = fromInt; EXPECT_TRUE(myVariant.isType()); EXPECT_FALSE(myVariant.isType()); EXPECT_FALSE(myVariant.isType()); const int myInt = myVariant.get(); EXPECT_EQ(myInt, fromInt); } TEST_F(VariantTest, HandlesVariantCopy) { BasicVariantType myVariant(fromInt); Variant myVariantAssigned = myVariant; EXPECT_TRUE(myVariantAssigned.isType()); EXPECT_FALSE(myVariantAssigned.isType()); EXPECT_FALSE(myVariantAssigned.isType()); const int myInt2 = myVariantAssigned.get(); EXPECT_EQ(myInt2, fromInt); Variant myVariantCopied(myVariant); EXPECT_EQ(myVariant, myVariantCopied); EXPECT_TRUE(myVariantCopied.isType()); EXPECT_FALSE(myVariantCopied.isType()); EXPECT_FALSE(myVariantCopied.isType()); const int& myIntCopy = myVariantCopied.get(); EXPECT_EQ(myIntCopy, fromInt); } TEST_F(VariantTest, HandlesVariantsWithinVariants) { typedef Variant VariantInVariantType; BasicVariantType fromInnerVariant(fromInt); VariantInVariantType myOuterVariant(fromInnerVariant); EXPECT_FALSE(myOuterVariant.isType()); EXPECT_FALSE(myOuterVariant.isType()); EXPECT_TRUE(myOuterVariant.isType()); EXPECT_ANY_THROW(myOuterVariant.get()); EXPECT_ANY_THROW(myOuterVariant.get()); const BasicVariantType myInnerVariant = myOuterVariant.get(); EXPECT_EQ(fromInnerVariant, myInnerVariant); } TEST_F(VariantTest, VariantStringArray) { std::vector testVector; testVector.push_back("Test 1"); testVector.push_back("Test 2"); testVector.push_back("Test 3"); testVector.push_back("Test 4"); CommonAPI::Variant>* vectorVariant = new CommonAPI::Variant>(testVector); std::vector readVector = vectorVariant->get >(); EXPECT_EQ(readVector, testVector); delete vectorVariant; } #ifndef WIN32 int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } #endif