File: ComponentTest.h

package info (click to toggle)
0ad 0.27.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 173,296 kB
  • sloc: cpp: 194,003; javascript: 19,098; ansic: 15,066; python: 6,328; sh: 1,699; perl: 1,575; java: 533; xml: 482; php: 192; makefile: 99
file content (250 lines) | stat: -rw-r--r-- 6,860 bytes parent folder | download | duplicates (3)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* Copyright (C) 2023 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_COMPONENTTEST
#define INCLUDED_COMPONENTTEST

#include "lib/self_test.h"

#include "maths/Vector3D.h"
#include "ps/XML/Xeromyces.h"
#include "simulation2/MessageTypes.h"
#include "simulation2/system/Component.h"
#include "simulation2/components/ICmpTerrain.h"
#include "simulation2/serialization/DebugSerializer.h"
#include "simulation2/serialization/HashSerializer.h"
#include "simulation2/serialization/StdSerializer.h"
#include "simulation2/serialization/StdDeserializer.h"

#include <iostream>

/**
 * @file
 * Various common features for component test cases.
 */

/**
 * Class to test a single component.
 * - Create an instance of this class
 * - Use AddMock to add mock components that the tested component relies on
 * - Use Add to add the test component itself, and it returns a component pointer
 * - Call methods on the component pointer
 * - Use Roundtrip to test the consistency of serialization
 */
class ComponentTestHelper
{
	CSimContext m_Context;
	CComponentManager m_ComponentManager;
	CParamNode m_Param;
	IComponent* m_Cmp;
	EComponentTypeId m_Cid;
	bool m_isSystemEntityInit = false;

public:
	ComponentTestHelper(ScriptContext& scriptContext) :
		m_Context(), m_ComponentManager(m_Context, scriptContext), m_Cmp(NULL)
	{
		m_ComponentManager.LoadComponentTypes();
	}

	const ScriptInterface& GetScriptInterface()
	{
		return m_ComponentManager.GetScriptInterface();
	}

	CSimContext& GetSimContext()
	{
		return m_Context;
	}

	/**
	 * Call this once to initialise the test helper with a component.
	 */
	template<typename T>
	T* Add(EComponentTypeId cid, const std::string& xml, entity_id_t ent = 10)
	{
		TS_ASSERT(m_Cmp == NULL);

		CEntityHandle handle;
		if (ent == SYSTEM_ENTITY)
		{
			if (!m_isSystemEntityInit)
			{
				m_ComponentManager.InitSystemEntity();
				m_isSystemEntityInit = true;
			}
			handle = m_ComponentManager.GetSystemEntity();
			m_Context.SetSystemEntity(handle);
		}
		else
			handle = m_ComponentManager.LookupEntityHandle(ent, true);

		m_Cid = cid;
		TS_ASSERT_EQUALS(CParamNode::LoadXMLString(m_Param, ("<test>" + xml + "</test>").c_str()), PSRETURN_OK);
		TS_ASSERT(m_ComponentManager.AddComponent(handle, m_Cid, m_Param.GetChild("test")));
		m_Cmp = m_ComponentManager.QueryInterface(ent, T::GetInterfaceId());
		TS_ASSERT(m_Cmp != NULL);
		return static_cast<T*> (m_Cmp);
	}

	void AddMock(entity_id_t ent, EInterfaceId iid, IComponent& component)
	{
		CEntityHandle handle;
		if (ent == SYSTEM_ENTITY)
		{
			if (!m_isSystemEntityInit)
			{
				m_ComponentManager.InitSystemEntity();
				m_isSystemEntityInit = true;
			}
			handle = m_ComponentManager.GetSystemEntity();
			m_Context.SetSystemEntity(handle);
		}
		else
			handle = m_ComponentManager.LookupEntityHandle(ent, true);

		m_ComponentManager.AddMockComponent(handle, iid, component);
	}

	void HandleMessage(IComponent* cmp, const CMessage& msg, bool global)
	{
		cmp->HandleMessage(msg, global);
	}

	/**
	 * Checks that the object roundtrips through its serialize/deserialize functions correctly.
	 * Computes the debug output, hash, and binary serialization; then deserializes into a new
	 * system and checks the serialization outputs are unchanged.
	 */
	void Roundtrip(bool verbose = false)
	{
		std::stringstream dbgstr1;
		CDebugSerializer dbg1(GetScriptInterface(), dbgstr1);
		m_Cmp->Serialize(dbg1);

		if (verbose)
			std::cout << "--------\n" << dbgstr1.str() << "--------\n";

		CHashSerializer hash1(GetScriptInterface());
		m_Cmp->Serialize(hash1);

		std::stringstream stdstr1;
		CStdSerializer std1(GetScriptInterface(), stdstr1);
		m_Cmp->Serialize(std1);

		ComponentTestHelper test2(GetScriptInterface().GetContext());
		// (We should never need to add any mock objects etc to test2, since deserialization
		// mustn't depend on other components already existing)

		CEntityHandle ent = test2.m_ComponentManager.LookupEntityHandle(10, true);

		CStdDeserializer stdde2(test2.GetScriptInterface(), stdstr1);

		IComponent* cmp2 = test2.m_ComponentManager.ConstructComponent(ent, m_Cid);
		cmp2->Deserialize(m_Param.GetChild("test"), stdde2);

		TS_ASSERT(stdstr1.peek() == EOF); // Deserialize must read whole stream

		std::stringstream dbgstr2;
		CDebugSerializer dbg2(test2.GetScriptInterface(), dbgstr2);
		cmp2->Serialize(dbg2);

		if (verbose)
			std::cout << "--------\n" << dbgstr2.str() << "--------\n";

		CHashSerializer hash2(test2.GetScriptInterface());
		cmp2->Serialize(hash2);

		std::stringstream stdstr2;
		CStdSerializer std2(test2.GetScriptInterface(), stdstr2);
		cmp2->Serialize(std2);

		TS_ASSERT_EQUALS(dbgstr1.str(), dbgstr2.str());

		TS_ASSERT_EQUALS(hash1.GetHashLength(), hash2.GetHashLength());
		TS_ASSERT_SAME_DATA(hash1.ComputeHash(), hash2.ComputeHash(), hash1.GetHashLength());

		TS_ASSERT_EQUALS(stdstr1.str(), stdstr2.str());

		// TODO: need to extend this so callers can run methods on the cloned component
		// to check that all its data is still correct
	}
};

/**
 * Simple terrain implementation with constant height of 50.
 */
class MockTerrain : public ICmpTerrain
{
public:
	DEFAULT_MOCK_COMPONENT()

	bool IsLoaded() const override
	{
		return true;
	}

	CFixedVector3D CalcNormal(entity_pos_t UNUSED(x), entity_pos_t UNUSED(z)) const override
	{
		return CFixedVector3D(fixed::FromInt(0), fixed::FromInt(1), fixed::FromInt(0));
	}

	CVector3D CalcExactNormal(float UNUSED(x), float UNUSED(z)) const override
	{
		return CVector3D(0.f, 1.f, 0.f);
	}

	entity_pos_t GetGroundLevel(entity_pos_t UNUSED(x), entity_pos_t UNUSED(z)) const override
	{
		return entity_pos_t::FromInt(50);
	}

	float GetExactGroundLevel(float UNUSED(x), float UNUSED(z)) const override
	{
		return 50.f;
	}

	u16 GetTilesPerSide() const override
	{
		return 16;
	}

	u32 GetMapSize() const override
	{
		return GetTilesPerSide() * TERRAIN_TILE_SIZE;
	}

	u16 GetVerticesPerSide() const override
	{
		return 17;
	}

	CTerrain* GetCTerrain() override
	{
		return nullptr;
	}

	void MakeDirty(i32 UNUSED(i0), i32 UNUSED(j0), i32 UNUSED(i1), i32 UNUSED(j1)) override
	{
	}

	void ReloadTerrain(bool UNUSED(ReloadWater)) override
	{
	}
};

#endif // INCLUDED_COMPONENTTEST