File: TraitsTest.h

package info (click to toggle)
0ad 0.0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 54,068 kB
  • sloc: cpp: 230,527; ansic: 23,115; python: 13,559; perl: 2,499; sh: 948; xml: 776; makefile: 696; java: 533; ruby: 229; erlang: 53; sql: 21
file content (63 lines) | stat: -rw-r--r-- 1,443 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef __TRAITSTEST_H
#define __TRAITSTEST_H

//
// This example shows how to use TS_ASSERT_EQUALS for your own classes
//
#include <cxxtest/TestSuite.h>
#include <cxxtest/ValueTraits.h>

//
// Define your class with operator==
//
#include <stdio.h>
#include <string.h>

class Pet {
    char _name[128];
public:
    Pet(const char *petName) { strcpy(_name, petName); }

    const char *name() const { return _name; }

    bool operator== (const Pet &other) const {
        return !strcmp(name(), other.name());
    }
};

//
// Instantiate CxxTest::ValueTraits<*your class*>
// Note: Most compilers do not require that you define both
//       ValueTraits<const T> and ValueTraits<T>, but some do.
//
namespace CxxTest {
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits<const Pet> {
    char _asString[256];

public:
    ValueTraits(const Pet &pet) { sprintf(_asString, "Pet(\"%s\")", pet.name()); }
    const char *asString() const { return _asString; }
};

CXXTEST_COPY_CONST_TRAITS(Pet);
}

//
// Here's how it works
//
class TestFunky : public CxxTest::TestSuite {
public:
    void testPets() {
        Pet pet1("dog"), pet2("cat");
        TS_ASSERT_EQUALS(pet1, pet2);
        Pet cat("cat"), gato("cat");
        TS_ASSERT_DIFFERS(cat, gato);
#ifdef _CXXTEST_HAVE_STD
        typedef CXXTEST_STD(string) String;
        TS_ASSERT_EQUALS(String("Hello"), String("World!"));
#endif // _CXXTEST_HAVE_STD
    }
};

#endif // __TRAITSTEST_H