2010-07-15 8 views
5

C'è un modo per passare un tipo e una stringa ad un test parametrizzato usando il test di google.Passando un typename e una stringa al test parametrizzato usando google test

vorrei fare:

template <typename T> 
class RawTypesTest : public ::testing::TestWithParam<const char * type> { 
protected: 
    virtual void SetUp() { 
     message = type; 
    } 
}; 

TEST_P(RawTypesTest, Foo) { 
    ASSERT_STREQ(message, type); 
    ParamType * data = ..; 
    ... 
} 

Grazie in anticipo

+0

è 'TestParam' una classe GTEST? –

+0

scusa, avrebbe dovuto essere TestWithParam – adk

risposta

9

Valore test con parametri non funzionano per il passaggio di informazioni sul tipo; puoi farlo solo con test parametrici digitati o di tipo. In entrambi i casi dovrai raggruppare il tuo tipo e le stringhe in strutture speciali. Ecco come farlo con type-parameterized tests:

template <typename T> class RawTypesTest : public testing::Test { 
public: 
    virtual void SetUp() { 
    this->message_ = TypeParam::kStringValue; 
    } 

protected: 
    const char* const message_; 
} 

TYPED_TEST_CASE_P(RawTypesTest); 

TYPED_TEST_P(RawTypesTest, DoesFoo) { 
    ASSERT_STREQ(message, TypeParam::kStringValue); 
    TypeParam::Type* data = ...; 
} 

TYPED_TEST_P(RawTypesTest, DoesBar) { ... } 

REGISTER_TYPED_TEST_CASE_P(FooTest, DoesFoo, DoesBar); 

E ora si deve definire le strutture dei parametri e istanziare i test per loro:

struct TypeAndString1 { 
    typedef Type1 Type; 
    static const char* kStringValue = "my string 1"; 
}; 
const char* TypeAndString1::kStringValue; 

struct TypeAndString2 { 
    typedef Type1 Type; 
    static const char* kStringValue = "my string 2"; 
}; 
const char* TypeAndString2::kStringValue; 

typedef testing::Types<TypeAndString1, TypeAndString2> MyTypes; 
INSTANTIATE_TYPED_TEST_CASE_P(OneAndTwo, RawTypeTest, MyTypes); 

è possibile utilizzare una macro per semplificare la definizione del parametro tipi:

#define MY_PARAM_TYPE(name, type, string) \ 
    struct name { \ 
    typedef type Type; \ 
    static const char kStringValue = string; \ 
    }; \ 
    const char* name::kStringValue 

Poi definizioni di struct parametri diventano molto più breve:

MY_PARAM_TYPE(TypeAndString1, Type1, "my string 1"); 
MY_PARAM_TYPE(TypeAndString2, Type2, "my string 2"); 

Questo è piuttosto complicato ma non esiste un modo semplice per farlo. Il mio miglior consiglio è di provare a ridimensionare i test per evitare di richiedere sia informazioni di tipo che di valore. Ma se devi, ecco la strada.

0

Non ho eseguito ricerche sulla famiglia di macro TYPED_TEST_P internamente, ma trovo che utilizzarli siano più complicati per l'obiettivo. Si potrebbe ottenere lo stesso semplicemente codificando il parametro come parte del tipo.

#define TSLP(name, value) \ 
struct Tslp##name \ 
{ \ 
    std::string operator()() \ 
    { \ 
     return value; \ 
    } \ 
} 

TSLP(Empty, ""); 
TSLP(Foo, "foo"); 
template<class Type, class Param> 
class ParamTextFixture : public Type { 
    static std::string message() { 
     return Param()(); 
    } 
} 

allora tutto quello che dovete fare è nei tipi di sostituire la classe con il ParamTextFixture

typedef ::testing::Types< 
    ParamTextFixture<MyClass, TslpEmpty> 
    , ParamTextFixture<MyClass, TslpFoo> > 
... 
Problemi correlati