func TestDelayedMessaging(t *testing.T) { dispatcher := testDispatcher{EventDispatcher: &DelayDispatcher{BasicDispatcher: &BasicDispatcher{}}} space := &testMsgSpace{EventDispatcher: &BasicDispatcher{}} dispatcher.SetOwner(space) // TODO: make this generic when Component is defined dispatcher.EventDispatcher.(*DelayDispatcher).Init() rcvr := testDispatcher{EventDispatcher: &BasicDispatcher{}} rcvr.RegisterForEvent("event", dispatcher, func(e EventData) { rcvr.MsgReceived = true }) msg := testEventData{Data: 5} dispatcher.TriggerEvent("event", &msg) test.ExpectEQ(t, true, rcvr.MsgReceived, "message handler shouldn't have triggered") rcvr.MsgReceived = false var delay float32 = 100.0 msg.Delay = delay dispatcher.TriggerEvent("event", &msg) // No time passed test.ExpectEQ(t, false, rcvr.MsgReceived, "message handler shouldn't have triggered") // Too little time passed var tooLittleDt float32 = 1 dtRemaining := delay - tooLittleDt space.TriggerEvent("FrameUpdateEvent", &FrameUpdateEvent{tooLittleDt}) test.ExpectEQ(t, false, rcvr.MsgReceived, "message handler shouldn't have triggered") // Enough time passed enoughDt := dtRemaining space.TriggerEvent("FrameUpdateEvent", &FrameUpdateEvent{enoughDt}) test.ExpectEQ(t, true, rcvr.MsgReceived, "message handler wasn't triggered") }
func TestSerialize_Goc(t *testing.T) { // test setup CoreFactoryFunc = testFactoryFunc data := []byte( `{ "Type": "Goc", "Name": "camera", "Components": [ { "Type": "CameraComponent", "X": 100, "Y": 50 }, { "Type": "ColliderComponent", "Shape": "aabb" } ] }`) holder, _ := support.ReadData(data) goc := Goc{} SerializeInPlace(&goc, holder) test.ExpectEQ(t, "camera", goc.Name, "goc.Name was set correctly") cmp1 := goc.GetComp("serTestComp1").(*serTestComp1) test.ExpectEQ(t, 100, cmp1.X, "cmp1.X was set correctly") test.ExpectEQ(t, 50, cmp1.Y, "cmp1.Y was set correctly") test.ExpectEQ(t, 0, cmp1.Z, "cmp1.Z was set correctly") cmp2 := goc.GetComp("serTestComp2").(*serTestComp2) test.ExpectEQ(t, "aabb", cmp2.Shape, "cmp2.Shape was set correctly") }
func TestMessaging(t *testing.T) { dispatcher := testDispatcher{EventDispatcher: &BasicDispatcher{}} // test dispatching to one listenre rcvr1 := testDispatcher{EventDispatcher: &BasicDispatcher{}} rcvr1.RegisterForEvent("event1", dispatcher, func(e EventData) { rcvr1.MsgData = e.(*testEventData).Data }) msg := testEventData{Data: 5} dispatcher.TriggerEvent("event1", &msg) test.ExpectEQ(t, msg.Data, rcvr1.MsgData, "message handler wasn't triggered") // dispatching to multiple listeners/events rcvr2 := testDispatcher{EventDispatcher: &BasicDispatcher{}} rcvr2.RegisterForEvent("event1", dispatcher, func(e EventData) { rcvr2.MsgData = e.(*testEventData).Data }) rcvr2.RegisterForEvent("event2", dispatcher, func(e EventData) { rcvr2.OtherData = e.(*testEventData).Data }) rcvr1.MsgData = 0 rcvr2.MsgData = 0 dispatcher.TriggerEvent("event1", &msg) test.ExpectEQ(t, msg.Data, rcvr1.MsgData, "message handler wasn't triggered") test.ExpectEQ(t, msg.Data, rcvr2.MsgData, "message handler wasn't triggered") test.ExpectNEQ(t, msg.Data, rcvr2.OtherData, "message handler shouldn't have triggered") dispatcher.TriggerEvent("event2", &msg) test.ExpectEQ(t, msg.Data, rcvr2.OtherData, "message handler wasn't triggered") }
func TestKernel_New(t *testing.T) { core := New() test.ExpectEQ(t, core.State, Running, "Core defaults to running.") test.ExpectEQ(t, 0, core.CurrFrame, "Core starts on frame 0.") // TODO: decide if I should test internals... hmmm test.ExpectEQ(t, core.managers, []Manager(nil), "Core starts with no managers.") }
func TestCore_Spaces(t *testing.T) { core := New() // Space Setup Spc1 := &TestSpace{name: "Spc1"} Spc2 := &TestSpace{name: "Spc2"} core.RegisterSpace(Spc1) core.RegisterSpace(Spc2) // Testing core.StartUp() actOut = nil expOut := []string{ "Spc1 Init()", "Spc2 Init()", } core.StartUp(nil) test.AssertEQ(t, len(expOut), len(actOut), "Length of actOut and expOut aren't equal.") for i, s := range actOut { test.ExpectEQ(t, expOut[i], s, fmt.Sprintf("With test %d: Initialization order was wrong.", i)) } // Testing Core.Run() actOut = nil expOut = []string{ "Spc1 Update()", "Spc2 Update()", } core.State = Stopped core.Run() test.AssertEQ(t, len(expOut), len(actOut), "Length of actOut and expOut aren't equal.") for i, s := range actOut { test.ExpectEQ(t, expOut[i], s, fmt.Sprintf("With test %d: Frame Update order was wrong.", i)) } // Testing core.ShutDown() actOut = nil expOut = []string{ "Spc2 DeInit()", "Spc1 DeInit()", } core.ShutDown() test.AssertEQ(t, len(expOut), len(actOut), "Length of actOut and expOut aren't equal.") for i, s := range actOut { test.ExpectEQ(t, expOut[i], s, fmt.Sprintf("With test %d: DeInitialization order was wrong.", i)) } }
func TestCore_Run(t *testing.T) { core := New() core.State = Rebooting test.ExpectEQ(t, utils.ES_Restart, core.Run(), "The core should have returned a restart value.") test.ExpectEQ(t, 0, core.CurrFrame, "No frame ran.") core.State = Terminated test.ExpectEQ(t, utils.ES_Success, core.Run(), "The core should have return quit value.") test.ExpectEQ(t, 0, core.CurrFrame, "No frame ran.") core.State = Stopped test.ExpectEQ(t, utils.ES_Success, core.Run(), "The core should return quit") test.ExpectEQ(t, 1, core.CurrFrame, "One frame ran.") core.RegisterManager(&frameControlMgr{core: core}) core.StartUp(nil) core.State = Running test.ExpectEQ(t, utils.ES_Success, core.Run(), "The core should return quit") test.ExpectEQ(t, 5, core.CurrFrame, "Core ran until it was stopped.") }
func TestDispatcherUnhooking(t *testing.T) { dispatcher := testDispatcher{EventDispatcher: &BasicDispatcher{}} // test dispatching to all listeners rcvr1 := testDispatcher{EventDispatcher: &BasicDispatcher{}} rcvr2 := testDispatcher{EventDispatcher: &BasicDispatcher{}} rcvr3 := testDispatcher{EventDispatcher: &DelayDispatcher{BasicDispatcher: &BasicDispatcher{}}} rcvr1.RegisterForEvent("event", dispatcher, func(e EventData) { rcvr1.MsgReceived = true }) rcvr2.RegisterForEvent("event", dispatcher, func(e EventData) { rcvr2.MsgReceived = true }) rcvr3.RegisterForEvent("event", dispatcher, func(e EventData) { rcvr3.MsgReceived = true }) dispatcher.TriggerEvent("event", nil) test.ExpectEQ(t, true, rcvr1.MsgReceived, "message handler wasn't triggered - rcvr1 #1") test.ExpectEQ(t, true, rcvr2.MsgReceived, "message handler wasn't triggered - rcvr2 #1") test.ExpectEQ(t, true, rcvr3.MsgReceived, "message handler wasn't triggered - rcvr3 #1") // TODO: make this generic when Component is defined rcvr2.EventDispatcher.(*BasicDispatcher).DeInit() rcvr1.MsgReceived, rcvr2.MsgReceived, rcvr3.MsgReceived = false, false, false dispatcher.TriggerEvent("event", nil) test.ExpectEQ(t, true, rcvr1.MsgReceived, "message handler wasn't triggered - rcvr1 #2") test.ExpectEQ(t, false, rcvr2.MsgReceived, "message handler wasn't unhooked - rcvr2 #2") test.ExpectEQ(t, true, rcvr3.MsgReceived, "message handler wasn't triggered - rcvr3 #2") rcvr3.EventDispatcher.(*DelayDispatcher).DeInit() rcvr1.MsgReceived, rcvr2.MsgReceived, rcvr3.MsgReceived = false, false, false dispatcher.TriggerEvent("event", nil) test.ExpectEQ(t, true, rcvr1.MsgReceived, "message handler wasn't triggered - rcvr1 #3") test.ExpectEQ(t, false, rcvr2.MsgReceived, "message handler wasn't unhooked - rcvr2 #3") test.ExpectEQ(t, false, rcvr3.MsgReceived, "message handler wasn't unhooked - rcvr3 #3") rcvr1.EventDispatcher.(*BasicDispatcher).DeInit() rcvr1.MsgReceived, rcvr2.MsgReceived, rcvr3.MsgReceived = false, false, false dispatcher.TriggerEvent("event", nil) test.ExpectEQ(t, false, rcvr1.MsgReceived, "message handler wasn't unhooked - rcvr1 #4") test.ExpectEQ(t, false, rcvr2.MsgReceived, "message handler wasn't unhooked - rcvr2 #4") test.ExpectEQ(t, false, rcvr3.MsgReceived, "message handler wasn't unhooked - rcvr3 #4") rcvr1.RegisterForEvent("event", dispatcher, func(e EventData) { rcvr1.MsgReceived = true }) rcvr1.MsgReceived, rcvr2.MsgReceived, rcvr3.MsgReceived = false, false, false dispatcher.TriggerEvent("event", nil) test.ExpectEQ(t, true, rcvr1.MsgReceived, "message handler wasn't triggered - rcvr1 #5") test.ExpectEQ(t, false, rcvr2.MsgReceived, "message handler wasn't unhooked - rcvr1 #5") test.ExpectEQ(t, false, rcvr3.MsgReceived, "message handler wasn't unhooked - rcvr1 #5") }
func TestKernelState(t *testing.T) { test.ExpectEQ(t, "Kernel State: Stopped", Stopped.String(), "Stopped.String() failed.") test.ExpectEQ(t, "Kernel State: Terminated", Terminated.String(), "Terminated.String() failed.") test.ExpectEQ(t, "Kernel State: Rebooting", Rebooting.String(), "Rebooting.String() failed.") test.ExpectEQ(t, "Kernel State: Running", Running.String(), "Running.String() failed.") }
func TestCore_Managers(t *testing.T) { core := New() // Test Setup Mgr1 := &TestManager{name: "Mgr1", comp: "TestMgr1InitComponent"} Mgr2 := &TestManager{name: "Mgr2", comp: "TestMgr2InitComponent"} CoreFactoryFunc = func(name string) interface{} { var comp interface{} switch name { default: support.LogFatal("unknown component: %s", name) case "TestMgr1InitComponent": comp = &TestMgr1InitComponent{} case "TestMgr2InitComponent": comp = &TestMgr2InitComponent{} } return comp } core.RegisterManager(Mgr1) core.RegisterManager(Mgr2) cfg := LoadConfig("C:/Users/jmorgan/Sandbox/golang/rsrc/test/json/objects/kernel/TestCore.jrm") // Testing Core.StartUp() actOut = nil expOut := []string{ "Mgr1 StartUp()", "Mgr1 got config", "Mgr2 StartUp()", "Mgr2 got config", } core.StartUp(cfg) test.AssertEQ(t, len(expOut), len(actOut), "Length of actOut and expOut aren't equal. "+fmt.Sprintf("\n\t\tactOut: %v", actOut)) for i, s := range actOut { test.ExpectEQ(t, expOut[i], s, fmt.Sprintf("With test %d: Initialization order was wrong.", i)) } // Testing Core.Run() actOut = nil expOut = []string{ "Mgr1 BeginFrame()", "Mgr2 BeginFrame()", "Mgr2 EndFrame()", "Mgr1 EndFrame()", } core.State = Stopped core.Run() test.AssertEQ(t, len(expOut), len(actOut), "Length of actOut and expOut aren't equal.") for i, s := range actOut { test.ExpectEQ(t, expOut[i], s, fmt.Sprintf("With test %d: Frame Update order was wrong.", i)) } // Testing Core.ShutDown() actOut = nil expOut = []string{ "Mgr2 ShutDown()", "Mgr1 ShutDown()", } core.ShutDown() test.AssertEQ(t, len(expOut), len(actOut), "Length of actOut and expOut aren't equal.") for i, s := range actOut { test.ExpectEQ(t, expOut[i], s, fmt.Sprintf("With test %d: DeInitialization order was wrong.", i)) } }