diff options
Diffstat (limited to 'src/test/ScriptTest.cpp')
-rw-r--r-- | src/test/ScriptTest.cpp | 109 |
1 files changed, 57 insertions, 52 deletions
diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp index c35c3e2..73009a7 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -23,7 +23,7 @@ class ScriptTest : public Test { public: ComponentManager component_manager{}; ScriptSystem system{component_manager}; - EventManager & evmgr = EventManager::get_instance(); + EventManager & event_manager = EventManager::get_instance(); class MyScript : public Script { // NOTE: default (private) visibility of init and update shouldn't cause @@ -47,79 +47,84 @@ public: unsigned event_count = 0; }; - BehaviorScript * behaviorscript_ref = nullptr; - MyScript * script_ref = nullptr; + OptionalRef<BehaviorScript> behaviorscript; + OptionalRef<MyScript> script; 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); + this->behaviorscript = component; + ASSERT_TRUE(this->behaviorscript); + EXPECT_EQ(component.script.get(), nullptr); component.set_script<MyScript>(); - ASSERT_NE(this->behaviorscript_ref->script.get(), nullptr); + ASSERT_NE(component.script.get(), nullptr); - this->script_ref = (MyScript *) this->behaviorscript_ref->script.get(); - ASSERT_NE(this->script_ref, nullptr); + this->script = *(MyScript *) component.script.get(); + ASSERT_TRUE(this->script); // sanity - ASSERT_EQ(script_ref->init_count, 0); - ASSERT_EQ(script_ref->update_count, 0); - ASSERT_EQ(script_ref->event_count, 0); + MyScript & script = this->script; + ASSERT_EQ(script.init_count, 0); + ASSERT_EQ(script.update_count, 0); + ASSERT_EQ(script.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); + MyScript & script = this->script; + EXPECT_EQ(0, script.init_count); + EXPECT_EQ(0, script.update_count); + EXPECT_EQ(0, script.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); + MyScript & script = this->script; + + system.update(); + EXPECT_EQ(1, script.init_count); + EXPECT_EQ(1, script.update_count); + EXPECT_EQ(0, script.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); + BehaviorScript & behaviorscript = this->behaviorscript; + MyScript & script = this->script; + + behaviorscript.active = false; + system.update(); + EXPECT_EQ(0, script.init_count); + EXPECT_EQ(0, script.update_count); + EXPECT_EQ(0, script.event_count); + + behaviorscript.active = true; + system.update(); + EXPECT_EQ(1, script.init_count); + EXPECT_EQ(1, script.update_count); + EXPECT_EQ(0, script.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); + BehaviorScript & behaviorscript = this->behaviorscript; + MyScript & script = this->script; + EventManager & evmgr = this->event_manager; + + system.update(); + behaviorscript.active = false; + EXPECT_EQ(1, script.init_count); + EXPECT_EQ(1, script.update_count); + EXPECT_EQ(0, script.event_count); + + evmgr.trigger_event<MyEvent>(); + EXPECT_EQ(1, script.init_count); + EXPECT_EQ(1, script.update_count); + EXPECT_EQ(0, script.event_count); + + behaviorscript.active = true; + evmgr.trigger_event<MyEvent>(); + EXPECT_EQ(1, script.init_count); + EXPECT_EQ(1, script.update_count); + EXPECT_EQ(1, script.event_count); } -TEST_F(ScriptTest, ListScripts) { - size_t script_count = 0; - for (auto & _ : this->system.get_scripts()) { - script_count++; - } - ASSERT_EQ(1, script_count); -} |