aboutsummaryrefslogtreecommitdiff
path: root/src/test/ScriptTest.cpp
blob: c35c3e26c8d868514700f44e4e5a04e1cabfc744 (plain)
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
#include <gtest/gtest.h>

// stupid hack to allow access to private/protected members under test
#define private public
#define protected public

#include <crepe/ComponentManager.h>
#include <crepe/api/BehaviorScript.h>
#include <crepe/api/Event.h>
#include <crepe/api/EventManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Script.h>
#include <crepe/api/Vector2.h>
#include <crepe/system/ScriptSystem.h>

using namespace std;
using namespace crepe;
using namespace testing;

class MyEvent : public Event {};

class ScriptTest : public Test {
public:
	ComponentManager component_manager{};
	ScriptSystem system{component_manager};
	EventManager & evmgr = EventManager::get_instance();

	class MyScript : public Script {
		// NOTE: default (private) visibility of init and update shouldn't cause
		// issues!
		void init() {
			this->init_count++;

			subscribe<MyEvent>([this](const MyEvent &) {
				this->event_count++;
				return true;
			});

			// init should never be called more than once
			EXPECT_LE(this->init_count, 1);
		}
		void update() { this->update_count++; }

	public:
		unsigned init_count = 0;
		unsigned update_count = 0;
		unsigned event_count = 0;
	};

	BehaviorScript * behaviorscript_ref = nullptr;
	MyScript * script_ref = nullptr;

	void SetUp() override {
		auto & mgr = this->component_manager;
		GameObject entity = mgr.new_object("name");
		BehaviorScript & component = entity.add_component<BehaviorScript>();

		this->behaviorscript_ref = &component;
		EXPECT_EQ(this->behaviorscript_ref->script.get(), nullptr);
		component.set_script<MyScript>();
		ASSERT_NE(this->behaviorscript_ref->script.get(), nullptr);

		this->script_ref = (MyScript *) this->behaviorscript_ref->script.get();
		ASSERT_NE(this->script_ref, nullptr);

		// sanity
		ASSERT_EQ(script_ref->init_count, 0);
		ASSERT_EQ(script_ref->update_count, 0);
		ASSERT_EQ(script_ref->event_count, 0);
	}
};

TEST_F(ScriptTest, Default) {
	EXPECT_EQ(0, this->script_ref->init_count);
	EXPECT_EQ(0, this->script_ref->update_count);
	EXPECT_EQ(0, this->script_ref->event_count);
}

TEST_F(ScriptTest, UpdateOnce) {
	this->system.update();
	EXPECT_EQ(1, this->script_ref->init_count);
	EXPECT_EQ(1, this->script_ref->update_count);
	EXPECT_EQ(0, this->script_ref->event_count);
}

TEST_F(ScriptTest, UpdateInactive) {
	this->behaviorscript_ref->active = false;
	this->system.update();
	EXPECT_EQ(0, this->script_ref->init_count);
	EXPECT_EQ(0, this->script_ref->update_count);
	EXPECT_EQ(0, this->script_ref->event_count);

	this->behaviorscript_ref->active = true;
	this->system.update();
	EXPECT_EQ(1, this->script_ref->init_count);
	EXPECT_EQ(1, this->script_ref->update_count);
	EXPECT_EQ(0, this->script_ref->event_count);
}

TEST_F(ScriptTest, EventInactive) {
	this->system.update();
	this->behaviorscript_ref->active = false;
	EXPECT_EQ(1, this->script_ref->init_count);
	EXPECT_EQ(1, this->script_ref->update_count);
	EXPECT_EQ(0, this->script_ref->event_count);

	this->evmgr.trigger_event<MyEvent>();
	EXPECT_EQ(1, this->script_ref->init_count);
	EXPECT_EQ(1, this->script_ref->update_count);
	EXPECT_EQ(0, this->script_ref->event_count);

	this->behaviorscript_ref->active = true;
	this->evmgr.trigger_event<MyEvent>();
	EXPECT_EQ(1, this->script_ref->init_count);
	EXPECT_EQ(1, this->script_ref->update_count);
	EXPECT_EQ(1, this->script_ref->event_count);
}

TEST_F(ScriptTest, ListScripts) {
	size_t script_count = 0;
	for (auto & _ : this->system.get_scripts()) {
		script_count++;
	}
	ASSERT_EQ(1, script_count);
}