CppUTest
MockNamedValue.h
1 /*
2  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the <organization> nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef D_MockNamedValue_h
29 #define D_MockNamedValue_h
30 /*
31  * MockParameterComparator is an interface that needs to be used when creating Comparators.
32  * This is needed when comparing values of non-native type.
33  */
34 
36 {
37 public:
39  virtual ~MockNamedValueComparator() {}
40 
41  virtual bool isEqual(void* object1, void* object2)=0;
42  virtual SimpleString valueToString(void* object)=0;
43 };
44 
46 {
47 public:
48  typedef bool (*isEqualFunction)(void*, void*);
49  typedef SimpleString (*valueToStringFunction)(void*);
50 
51  MockFunctionComparator(isEqualFunction equal, valueToStringFunction valToString)
52  : equal_(equal), valueToString_(valToString) {}
53  virtual ~MockFunctionComparator(){}
54 
55  virtual bool isEqual(void* object1, void* object2){ return equal_(object1, object2); }
56  virtual SimpleString valueToString(void* object) { return valueToString_(object); }
57 private:
58  isEqualFunction equal_;
59  valueToStringFunction valueToString_;
60 };
61 
62 /*
63  * MockNamedValue is the generic value class used. It encapsulates basic types and can use them "as if one"
64  * Also it enables other types by putting object pointers. They can be compared with comparators.
65  *
66  * Basically this class ties together a Name, a Value, a Type, and a Comparator
67  */
68 
70 {
71 public:
72  MockNamedValue(const SimpleString& name);
73  virtual ~MockNamedValue();
74 
75  virtual void setValue(int value);
76  virtual void setValue(double value);
77  virtual void setValue(void* value);
78  virtual void setValue(const char* value);
79  virtual void setObjectPointer(const SimpleString& type, void* objectPtr);
80 
81  virtual void setComparator(MockNamedValueComparator* comparator);
82  virtual void setName(const char* name);
83 
84  virtual bool equals(const MockNamedValue& p) const;
85 
86  virtual SimpleString toString() const;
87 
88  virtual SimpleString getName() const;
89  virtual SimpleString getType() const;
90 
91  virtual int getIntValue() const;
92  virtual double getDoubleValue() const;
93  virtual const char* getStringValue() const;
94  virtual void* getPointerValue() const;
95  virtual void* getObjectPointer() const;
96 private:
97  SimpleString name_;
98  SimpleString type_;
99  union {
100  int intValue_;
101  double doubleValue_;
102  const char* stringValue_;
103  void* pointerValue_;
104  void* objectPointerValue_;
105  } value_;
106  MockNamedValueComparator* comparator_;
107 };
108 
110 {
111 public:
113 
114  SimpleString getName() const;
115  SimpleString getType() const;
116 
117  MockNamedValueListNode* next();
118  MockNamedValue* item();
119 
120  void destroy();
121  void setNext(MockNamedValueListNode* node);
122 private:
123  MockNamedValue* data_;
124  MockNamedValueListNode* next_;
125 };
126 
128 {
129 public:
131 
132  MockNamedValueListNode* begin();
133 
134  void add(MockNamedValue* newValue);
135  void clear();
136 
137  MockNamedValue* getValueByName(const SimpleString& name);
138 
139 private:
140  MockNamedValueListNode* head_;
141 };
142 
143 /*
144  * MockParameterComparatorRepository is a class which stores comparators which can be used for comparing non-native types
145  *
146  */
147 
150 {
152 public:
155 
156  virtual void installComparator(const SimpleString& name, MockNamedValueComparator& comparator);
157  virtual void installComparators(const MockNamedValueComparatorRepository& repository);
158  virtual MockNamedValueComparator* getComparatorForType(const SimpleString& name);
159 
160  void clear();
161 };
162 
163 #endif
Definition: MockNamedValue.h:109
Definition: MockNamedValue.cpp:232
Definition: MockNamedValue.h:45
Definition: SimpleString.h:46
Definition: MockNamedValue.h:35
Definition: MockNamedValue.h:149
Definition: MockNamedValue.h:69
Definition: MockNamedValue.h:127