ZeroEpsilon/spec/ui/event_handler_spec.cr

41 lines
1.2 KiB
Crystal

require "../../src/ui/event_handler"
describe UI::EventHandler do
it "verifies event accumulates and remove callbacks" do
a = UI::EventHandler.new
counter = 0
a.add(UI::EventHandler::Event::KeyPressed) do |cb|
counter += 1
end
a.add(UI::EventHandler::Event::KeyReleased) do |cb|
counter += 10
end
# test having multiple events with different types
counter.should eq(0)
a.handle(UI::EventHandler::Event::KeyPressed.new)
counter.should eq(1)
a.handle(UI::EventHandler::Event::KeyPressed.new)
counter.should eq(2)
a.handle(UI::EventHandler::Event::KeyReleased.new)
counter.should eq(12)
# test 2 callbacks on the same event
a.add(UI::EventHandler::Event::KeyPressed) do |cb|
counter += 1
end
a.handle(UI::EventHandler::Event::KeyPressed.new)
counter.should eq(14)
# test a callback with autoremove (useful for keypressing)
a.add(UI::EventHandler::Event::KeyPressed) do |cb|
counter += 100
a.remove(UI::EventHandler::Event::KeyPressed, cb)
end
a.handle(UI::EventHandler::Event::KeyPressed.new)
counter.should eq(116)
a.handle(UI::EventHandler::Event::KeyPressed.new)
counter.should eq(118)
end
end