From 128901970874e9939621f6fa0384a56600ee43c9 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 16 Apr 2026 15:18:10 +0200 Subject: add extensions and lib --- src/arg.lua | 12 +----- src/blueprint | 6 ++- src/check | 94 +++++++++++++++++++++++++++++++++++++++++++++++ src/extensions/init.lua | 1 + src/extensions/string.lua | 26 +++++++++++++ src/lib/init.lua | 1 + src/lib/path.lua | 61 ++++++++++++++++++++++++++++++ 7 files changed, 190 insertions(+), 11 deletions(-) create mode 100755 src/check create mode 100644 src/extensions/init.lua create mode 100644 src/extensions/string.lua create mode 100644 src/lib/init.lua create mode 100644 src/lib/path.lua (limited to 'src') diff --git a/src/arg.lua b/src/arg.lua index a0acd9c..1254b71 100644 --- a/src/arg.lua +++ b/src/arg.lua @@ -1,7 +1,7 @@ local function help(opts) - io.write(arg[0]) + io.write(Path:new(arg[0]):name()) io.write(" ") for _, v in ipairs(opts) do @@ -48,14 +48,6 @@ local function help(opts) os.exit() end -function string:flag() - if self:sub(1, 2) ~= "--" then - return nil - end - - return self:sub(3) -end - return function (opts) local args = {} @@ -76,7 +68,7 @@ return function (opts) for _, value in ipairs(arg) do local key = nil if current_flag == nil then - local flag = value:flag() + local flag = value:flag_name() if flag == nil then local option = params[current_index] current_index = current_index + 1 diff --git a/src/blueprint b/src/blueprint index ab0cba1..41b2162 100755 --- a/src/blueprint +++ b/src/blueprint @@ -3,11 +3,15 @@ function setup_package() local str = debug.getinfo(2, "S").source:sub(2) local path = str:match("(.*/)") or "./" - package.path = path .. '?.lua;' + package.path = path .. '?.lua;' .. package.path + package.path = path .. '/?/init.lua;' .. package.path end setup_package() +require('extensions') +require('lib') + local args = require('arg') { { name = "path" }, diff --git a/src/check b/src/check new file mode 100755 index 0000000..99983e2 --- /dev/null +++ b/src/check @@ -0,0 +1,94 @@ +#!/usr/bin/env lua + +function setup_package() + local str = debug.getinfo(2, "S").source:sub(2) + local path = str:match("(.*/)") or "./" + package.path = path .. '?.lua;' .. package.path + package.path = path .. '/?/init.lua;' .. package.path +end + +setup_package() + +require('extensions') +require('lib') + +local function table_length(t) + local count = 0 + + for _ in pairs(t) do + count = count + 1 + end + + return count +end + +local function inspect(root) + return require('inspect')(root, { + newline = ' ', + indent = '', + }) +end + +assert = { + equals = function(expected, actual) + if type(expected) == 'table' and type(expected) == 'table' then + if table_length(expected) ~= table_length(actual) then + error("assertion failed: " .. inspect(expected) .. " != " .. inspect(actual)) + end + + for key, value in pairs(expected) do + assert.equals(value, actual[key]) + end + else + if expected ~= actual then + error("assertion failed: " .. inspect(expected) .. " != " .. inspect(actual)) + end + end + end, +} + +function show_error(file, msg) + io.write('\x1b[2m' .. file .. '\x1b[0m: \x1b[31mfailed\x1b[0m\n') + io.write('\t' .. msg:gsub('\n', '\n\t') .. '\n') +end + +function show_success(file) + io.write('\x1b[2m' .. file .. '\x1b[0m: \x1b[32msuccess\x1b[0m\n') +end + +function show_skip(file) + io.write('\x1b[2m' .. file .. ' -- skipping\x1b[0m\n') +end + +function test_module(module_path) + local status, ret = pcall(dofile, module_path) + + if not status then + show_skip(module_path) + return + end + + local mod = ret + local has_err = false + + if type(mod) ~= 'table' or mod.tests == nil then + show_success(module_path) + return + end + + for name, test in pairs(mod.tests) do + local status, ret = pcall(test) + if not status then + has_err = true + show_error(module_path, ret) + end + end + + if not has_err then + show_success(module_path) + end +end + +for _, a in ipairs(arg) do + test_module(a) +end diff --git a/src/extensions/init.lua b/src/extensions/init.lua new file mode 100644 index 0000000..a4c4f5a --- /dev/null +++ b/src/extensions/init.lua @@ -0,0 +1 @@ +require('extensions.string') diff --git a/src/extensions/string.lua b/src/extensions/string.lua new file mode 100644 index 0000000..247d31a --- /dev/null +++ b/src/extensions/string.lua @@ -0,0 +1,26 @@ +function string:flag_name() + if self:sub(1, 2) ~= "--" then + return nil + end + + return self:sub(3) +end + +function string:split(sep) + splits = {} + + for split in self:gmatch("[^" .. sep .. "]+") do + splits[#splits + 1] = split + end + + return splits +end + +return { + tests = { + function() + local path = 'some/path/here' + assert.equals({ 'some', 'path', 'here' }, path.split("/")) + end, + } +} diff --git a/src/lib/init.lua b/src/lib/init.lua new file mode 100644 index 0000000..4f069d0 --- /dev/null +++ b/src/lib/init.lua @@ -0,0 +1 @@ +require('lib.path') diff --git a/src/lib/path.lua b/src/lib/path.lua new file mode 100644 index 0000000..b0f0fac --- /dev/null +++ b/src/lib/path.lua @@ -0,0 +1,61 @@ +Path = {} + +function Path:new(p) + local path = { segments = p:split('/') } + + setmetatable(path, self) + self.__index = self + + return path +end + +function Path:name() + return self.segments[#self.segments] +end + +function Path:stem() + return self.segments[#self.segments]:match('[^.]+') +end + +function Path:parent() + local path = { segments = { table.unpack(self.segments, 1, #self.segments - 1) } } + + setmetatable(path, self) + self.__index = self + + return path +end + +function Path:__tostring() + return table.concat(self.segments, "/") +end + +function Path:__div(next) + return Path:new(self:__tostring() .. "/" .. next) +end + +return { + tests = { + function() + local path = Path:new("some/path/here") + assert.equals("here", path:name()) + end, + function() + local path = Path:new("some/path/here.txt") + assert.equals("here", path:stem()) + end, + function() + local path = Path:new("some/path/here.txt") + assert.equals("some/path", path:parent():__tostring()) + end, + function() + local path = Path:new("some/path/here.txt") + assert.equals("some/path/here.txt", path:__tostring()) + end, + function() + local path = Path:new("some/path/here") + path = path / 'next.txt' + assert.equals("some/path/here/next.txt", path:__tostring()) + end + } +} -- cgit v1.2.3-70-g09d2