test: a test script and some tests

This adds the start of the test suite that we can add into CI to ensure
we are not pussing a broken build.
This commit is contained in:
Ade Attwood 2022-08-06 10:02:29 +01:00 committed by Ade Attwood
parent fabb652b8d
commit 617f6a98cc
3 changed files with 101 additions and 0 deletions

9
lua/ivy/libivy_test.lua Normal file
View file

@ -0,0 +1,9 @@
local libivy = require "ivy.libivy"
it("should run a simple match", function(t)
local score = libivy.ivy_match("term", "I am a serch term")
if score > 0 then
t.error "Score should not be grater than 0"
end
end)

30
lua/ivy/matcher_test.lua Normal file
View file

@ -0,0 +1,30 @@
local libivy = require "ivy.libivy"
-- Helper function to test a that string `one` has a higher match score than
-- string `two`. If string `one` has a lower score than string `two` a string
-- will be returned that can be used in body of an error. If not then `nil` is
-- returned and all is good.
local match_test = function(term, one, two)
local score_one = libivy.ivy_match(term, one)
local score_two = libivy.ivy_match(term, two)
if score_one < score_two then
return one .. " should be ranked higher than " .. two
end
return nil
end
it("sould match path separator", function(t)
local result = match_test("file", "some/file.lua", "somefile.lua")
if result then
t.error(result)
end
end)
it("sould match the start of a string", function(t)
local result = match_test("file", "file.lua", "somefile.lua")
if result then
t.error(result)
end
end)

62
scripts/test.lua Normal file
View file

@ -0,0 +1,62 @@
package.path = "lua/?.lua;" .. package.path
local global_context = {
current_test_name = "",
total = 0,
pass = 0,
fail = 0,
}
_G.it = function(name, callback)
local context = {
name = name,
error = function(message)
error(message, 2)
end,
}
local time = os.clock() * 1000
local status, err = pcall(callback, context)
local elapsed = (os.clock() * 1000) - time
local prefix = "\x1B[42mPASS"
global_context.total = global_context.total + 1
if status then
global_context.pass = global_context.pass + 1
else
global_context.fail = global_context.fail + 1
prefix = "\x1B[41mFAIL"
end
print(string.format("%s\x1B[0m %s - %s (%.3f ms)", prefix, global_context.current_test_name, name, elapsed))
if err then
print(" " .. err)
end
end
local start_time = os.clock()
for _, name in ipairs(arg) do
-- Turn the file name in to a lau module name that we can require
local module = name:gsub("^lua/", "")
module, _ = module:gsub("/init.lua$", "")
module, _ = module:gsub(".lua$", "")
module = module:gsub("/", ".")
global_context.current_test_name = module:gsub("_test", "")
require(module)
end
print(string.format(
[[
Tests: %d passed, %d total
Time: %.3f seconds]],
global_context.pass,
global_context.total,
(os.clock()) - start_time
))
if global_context.fail > 0 then
os.exit(1, true)
end