diff --git a/emitter.tl-dev-1.rockspec b/emitter.tl-dev-1.rockspec index 963d561..8bcc6d3 100644 --- a/emitter.tl-dev-1.rockspec +++ b/emitter.tl-dev-1.rockspec @@ -26,6 +26,7 @@ dependencies = { } test_dependencies = { "busted", + "busted-tl", } build_dependencies = { "tl >= 0.24.1", @@ -34,6 +35,9 @@ build_dependencies = { } test = { type = "busted", + flags = { + "--loaders=teal", + }, } build = { type = "builtin", diff --git a/spec/emitter_spec.lua b/spec/emitter_spec.tl similarity index 55% rename from spec/emitter_spec.lua rename to spec/emitter_spec.tl index 7e3f98d..8db9a56 100644 --- a/spec/emitter_spec.lua +++ b/spec/emitter_spec.tl @@ -1,12 +1,21 @@ +global describe: function(string, function) +global it: function(string, function) + local Emitter = require("emitter") -local Greet = {} -function Greet.new(name) +local record Greet is Emitter.Event + name: string +end + +function Greet.new(name: string): Greet return { type = Greet, name = name } end -local Depart = {} -function Depart.new(name) +local record Depart is Emitter.Event + name: string +end + +function Depart.new(name: string): Depart return { type = Depart, name = name } end @@ -19,150 +28,151 @@ describe("Emitter", function() describe("subscribe", function() it("emits in order", function() - local messages = {} + local messages: {string} = {} local emitter = Emitter.new() - emitter:on(Greet, function(greet) + emitter:on(Greet, function(greet: Greet) table.insert(messages, greet.name .. " 1") end) - emitter:on(Greet, function(greet) + emitter:on(Greet, function(greet: Greet) table.insert(messages, greet.name .. " 2") end) - emitter:on(Depart, function(depart) + emitter:on(Depart, function(depart: Depart) table.insert(messages, "Depart: " .. depart.name) end) emitter:emit(Greet.new("Hi")) - assert.equals(2, #messages) - assert.equals("Hi 1", messages[1]) - assert.equals("Hi 2", messages[2]) + assert(2 == #messages) + assert("Hi 1" == messages[1]) + assert("Hi 2" == messages[2]) emitter:emit(Depart.new("Bye")) - assert.equals(3, #messages) - assert.equals("Depart: Bye", messages[3]) + assert(3 == #messages) + assert("Depart: Bye" == messages[3]) end) it("can prepend events", function() - local messages = {} + local messages: {string} = {} local emitter = Emitter.new() - emitter:on(Greet, function(greet) + emitter:on(Greet, function(greet: Greet) table.insert(messages, greet.name .. " 1") end, { position = "first" }) - emitter:on(Greet, function(greet) + emitter:on(Greet, function(greet: Greet) table.insert(messages, greet.name .. " 2") end, { position = "first" }) emitter:emit(Greet.new("Hi")) - assert.equals(2, #messages) - assert.equals("Hi 2", messages[1]) - assert.equals("Hi 1", messages[2]) + assert(2 == #messages) + assert("Hi 2" == messages[1]) + assert("Hi 1" == messages[2]) end) end) describe("unsubscribe", function() it("emits once", function() - local messages = {} + local messages: {string} = {} local emitter = Emitter.new() - emitter:once(Greet, function(greet) + emitter:once(Greet, function(greet: Greet) table.insert(messages, greet.name .. " 1") end) - emitter:once(Greet, function(greet) + emitter:once(Greet, function(greet: Greet) table.insert(messages, greet.name .. " 2") end) emitter:emit(Greet.new("Hi")) - assert.equals(2, #messages) - assert.equals("Hi 1", messages[1]) - assert.equals("Hi 2", messages[2]) + assert(2 == #messages) + assert("Hi 1" == messages[1]) + assert("Hi 2" == messages[2]) emitter:emit(Greet.new("Hi")) - assert.equals(2, #messages) + assert(2 == #messages) end) it("can unsubscribe by receiver", function() - local messages = {} + local messages: {string} = {} local emitter = Emitter.new() - local listener = function(greet) + local listener = function(greet: Greet) table.insert(messages, greet.name) end emitter:on(Greet, listener) emitter:emit(Greet.new("Hi")) - assert.equals(1, #messages) - assert.equals("Hi", messages[1]) + assert(1 == #messages) + assert("Hi" == messages[1]) emitter:off(Greet, listener) emitter:emit(Greet.new("Hi")) - assert.equals(1, #messages) + assert(1 == #messages) end) it("can unsubscribe by id", function() - local messages = {} + local messages: {string} = {} local emitter = Emitter.new() - local listener = function(greet) + local listener = function(greet: Greet) table.insert(messages, greet.name) end emitter:on(Greet, listener, { id = "id" }) emitter:emit(Greet.new("Hi")) - assert.equals(1, #messages) - assert.equals("Hi", messages[1]) + assert(1 == #messages) + assert("Hi" == messages[1]) emitter:off(Greet, "id") emitter:emit(Greet.new("Hi")) - assert.equals(1, #messages) + assert(1 == #messages) end) it("can unsubscribe multiple by id", function() - local messages = {} + local messages: {string} = {} local emitter = Emitter.new() - local listener = function(greet) + local listener = function(greet: Greet) table.insert(messages, greet.name) end emitter:on(Greet, listener, { id = "id" }) emitter:on(Greet, listener, { id = "id" }) emitter:emit(Greet.new("Hi")) - assert.equals(2, #messages) - assert.equals("Hi", messages[1]) - assert.equals("Hi", messages[2]) + assert(2 == #messages) + assert("Hi" == messages[1]) + assert("Hi" == messages[2]) emitter:off(Greet, "id") emitter:emit(Greet.new("Hi")) - assert.equals(2, #messages) + assert(2 == #messages) end) end) describe("forwards events", function() it("forwards events to an emitter", function() - local messages = {} - local forwarded = {} + local messages: {string} = {} + local forwarded: {string} = {} local emitter = Emitter.new() local forward = Emitter.new() - emitter:on(Greet, function(greet) + emitter:on(Greet, function(greet: Greet) table.insert(messages, greet.name) end) - forward:on(Greet, function(greet) + forward:on(Greet, function(greet: Greet) table.insert(forwarded, greet.name) end) emitter:startForwarding(forward) emitter:emit(Greet.new("Hi")) - assert.equals(1, #messages) - assert.equals("Hi", messages[1]) - assert.equals(1, #forwarded) - assert.equals("Hi", forwarded[1]) + assert(1 == #messages) + assert("Hi" == messages[1]) + assert(1 == #forwarded) + assert("Hi" == forwarded[1]) forward:emit(Greet.new("Hi")) - assert.equals(1, #messages) - assert.equals(2, #forwarded) + assert(1 == #messages) + assert(2 == #forwarded) emitter:stopForwarding(forward) emitter:emit(Greet.new("Hello")) - assert.equals(2, #messages) - assert.equals("Hello", messages[2]) - assert.equals(2, #forwarded) + assert(2 == #messages) + assert("Hello" == messages[2]) + assert(2 == #forwarded) end) end) end) +