lua-ln/spec/ln_spec.lua

107 lines
2.6 KiB
Lua
Raw Normal View History

2019-12-25 13:32:59 +00:00
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)
2019-12-25 18:53:37 +00:00
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
2019-12-25 18:53:37 +00:00
local msg
lgr.filters = {
function(message) msg = message end,
}
it("can log", function()
lgr:log {foo = "bar"}
2019-12-25 18:53:37 +00:00
assert.truthy(string.find(msg, 'foo=bar'))
end)
it("can error", function()
2019-12-25 18:53:37 +00:00
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"})
2019-12-25 18:53:37 +00:00
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"}
2019-12-25 18:53:37 +00:00
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)
2019-12-25 18:53:37 +00:00
describe("logger options", function()
2019-12-25 18:53:37 +00:00
local msg
local filter_func = function(message) msg = message end
local lgr = ln.Logger:new {
2019-12-25 18:53:37 +00:00
formatter = ln.LogfmtFormatter:new(),
filters = {
filter_func,
}
}
it("sets the formatter to non-nil", function()
assert.truthy(lgr.formatter)
end)
2019-12-25 18:53:37 +00:00
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)
2019-12-25 13:32:59 +00:00
end)