Skip to content

Commit

Permalink
Use busted-tl for teal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Dec 12, 2024
1 parent 71f8315 commit 1f11e0d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 55 deletions.
4 changes: 4 additions & 0 deletions emitter.tl-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = {
}
test_dependencies = {
"busted",
"busted-tl",
}
build_dependencies = {
"tl >= 0.24.1",
Expand All @@ -34,6 +35,9 @@ build_dependencies = {
}
test = {
type = "busted",
flags = {
"--loaders=teal",
},
}
build = {
type = "builtin",
Expand Down
120 changes: 65 additions & 55 deletions spec/emitter_spec.lua → spec/emitter_spec.tl
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)

0 comments on commit 1f11e0d

Please sign in to comment.