1 #ifndef __TRAITSTEST_H
2 #define __TRAITSTEST_H
4 //
5 // This example shows how to use TS_ASSERT_EQUALS for your own classes
6 //
7 #include <cxxtest/TestSuite.h>
8 #include <cxxtest/ValueTraits.h>
10 //
11 // Define your class with operator==
12 //
13 #include <stdio.h>
14 #include <string.h>
16 class Pet
17 {
18 char _name[128];
19 public:
20 Pet( const char *petName ) { strcpy( _name, petName ); }
22 const char *name() const { return _name; }
24 bool operator== ( const Pet &other ) const
25 {
26 return !strcmp( name(), other.name() );
27 }
28 };
30 //
31 // Instantiate CxxTest::ValueTraits<*your class*>
32 // Note: Most compilers do not require that you define both
33 // ValueTraits<const T> and ValueTraits<T>, but some do.
34 //
35 namespace CxxTest
36 {
37 CXXTEST_TEMPLATE_INSTANTIATION
38 class ValueTraits<const Pet>
39 {
40 char _asString[256];
42 public:
43 ValueTraits( const Pet &pet ) { sprintf( _asString, "Pet(\"%s\")", pet.name() ); }
44 const char *asString() const { return _asString; }
45 };
47 CXXTEST_COPY_CONST_TRAITS( Pet );
48 }
50 //
51 // Here's how it works
52 //
53 class TestFunky : public CxxTest::TestSuite
54 {
55 public:
56 void testPets()
57 {
58 Pet pet1("dog"), pet2("cat");
59 TS_ASSERT_EQUALS( pet1, pet2 );
60 Pet cat("cat"), gato("cat");
61 TS_ASSERT_DIFFERS( cat, gato );
62 #ifdef _CXXTEST_HAVE_STD
63 typedef CXXTEST_STD(string) String;
64 TS_ASSERT_EQUALS( String("Hello"), String("World!") );
65 #endif // _CXXTEST_HAVE_STD
66 }
67 };
69 #endif // __TRAITSTEST_H