lua-ln/spec/ln_spec.lua

107 lines
2.6 KiB
Lua

local ln = require "ln"
describe("ln", function()
describe("exports", function()
it("has Logger", function()
assert.truthy(ln.Logger)
end)
it("has default_logger", function()
assert.truthy(ln.default_logger)
assert.truthy(ln.default_logger.formatter)
assert.truthy(ln.default_logger.filters)
end)
it("has LogfmtFormatter", function()
assert.truthy(ln.LogfmtFormatter)
end)
it("has JSONFormatter", function()
assert.truthy(ln.JSONFormatter)
end)
it("has log", function()
assert.truthy(ln.log)
end)
it("has err", function()
assert.truthy(ln.err)
end)
end)
describe("default logger operations", function()
local lgr = ln.default_logger
local msg
lgr.filters = {
function(message) msg = message end,
}
it("can log", function()
lgr:log {foo = "bar"}
assert.truthy(string.find(msg, 'foo=bar'))
end)
it("can error", function()
lgr:err("vibe check failed", {foo = "baz"})
assert.truthy(string.find(msg, 'foo=baz'))
assert.truthy(string.find(msg, 'vibe check failed'))
end)
it("takes multiple tables", function()
lgr:log({foo = "bar"}, {baz = "boz"})
assert.truthy(string.find(msg, 'foo=bar'))
assert.truthy(string.find(msg, 'baz=boz'))
end)
it("lets you override the formatter", function()
lgr.formatter = ln.JSONFormatter:new()
lgr:log {foo = "bar"}
assert.truthy(string.find(msg, "{"))
end)
end)
describe("logfmt", function()
local fmtr = ln.LogfmtFormatter:new()
it("should return a non-nil message", function()
assert.truthy(fmtr:format({foo = "bar"}))
end)
it("should have spaces when the input does", function()
local msg = fmtr:format {foo = "bar with spaces"}
assert.truthy(string.find(msg, 'foo="bar with spaces"'))
end)
end)
describe("logger options", function()
local msg
local filter_func = function(message) msg = message end
local lgr = ln.Logger:new {
formatter = ln.LogfmtFormatter:new(),
filters = {
filter_func,
}
}
it("sets the formatter to non-nil", function()
assert.truthy(lgr.formatter)
end)
it("respects filters", function()
lgr:log {foo = "bar"}
assert.truthy(string.find(msg, 'foo=bar'))
end)
it("ends if a filter returns non-nil", function()
msg = nil
lgr.filters = {
function(message) return false end,
filter_func,
}
lgr:log {foo = "bar"}
assert.falsy(msg)
end)
end)
end)