From 8ad600db039dace64778680622936e5475092a0f Mon Sep 17 00:00:00 2001 From: luxus Date: Fri, 4 Oct 2024 01:05:20 +0200 Subject: [PATCH] forgot the test file :D --- .github/workflows/ci.yml | 11 +++++ tests/colorful_times_spec.lua | 93 +++++++++++++++++++++++++++++++++++ tests/minimal_init.vim | 4 +- 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 41d3e5e..018bad2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,6 +26,17 @@ jobs: with: luaVersion: ${{ matrix['lua-version'] }} + - name: Install LuaRocks + run: | + sudo apt-get update + sudo apt-get install -y luarocks + + - name: Install luacheck + run: luarocks install --local luacheck + + - name: Run luacheck + run: luarocks exec luacheck lua + - name: Set up Neovim uses: rhysd/action-setup-vim@v1 with: diff --git a/tests/colorful_times_spec.lua b/tests/colorful_times_spec.lua index e69de29..90c83e8 100644 --- a/tests/colorful_times_spec.lua +++ b/tests/colorful_times_spec.lua @@ -0,0 +1,93 @@ +-- tests/colorful_times_spec.lua + +local colorful_times = require("colorful-times") + +describe("ColorfulTimes Plugin", function() + -- Test for parsing time strings into minutes since midnight + describe("parse_time", function() + it("parses valid time strings correctly", function() + assert.are.equal(60, colorful_times.parse_time("01:00")) + assert.are.equal(0, colorful_times.parse_time("00:00")) + assert.are.equal(1439, colorful_times.parse_time("23:59")) + end) + + it("returns nil for invalid time strings", function() + assert.is_nil(colorful_times.parse_time("24:00")) + assert.is_nil(colorful_times.parse_time("12:60")) + assert.is_nil(colorful_times.parse_time("invalid")) + end) + end) + + -- Test for handling schedule pre-processing + describe("preprocess_schedule", function() + it("parses valid schedule entries correctly", function() + local schedule = { + { start = "06:00", stop = "18:00", colorscheme = "morning" }, + { start = "18:00", stop = "06:00", colorscheme = "night", background = "dark" }, + } + colorful_times.config.schedule = schedule + colorful_times.preprocess_schedule() + local parsed_schedule = colorful_times.get_parsed_schedule() + + assert.are.equal(2, #parsed_schedule) + assert.are.equal(360, parsed_schedule[1].start_time) -- 06:00 AM + assert.are.equal(1080, parsed_schedule[1].stop_time) -- 06:00 PM + assert.are.equal(1080, parsed_schedule[2].start_time) -- 06:00 PM + assert.are.equal(360 + 1440, parsed_schedule[2].stop_time) -- 06:00 AM next day + end) + end) + + -- Test for system background detection callback handling + describe("get_system_background", function() + it("calls the callback with the correct background value", function() + local function callback(bg) + assert.is_true(bg == "dark" or bg == "light") + end + + colorful_times.get_system_background(callback, "dark") + end) + end) + + -- Test for applying colorscheme + describe("apply_colorscheme", function() + it("sets the correct colorscheme and background", function() + colorful_times.config.default.colorscheme = "default" + colorful_times.config.default.background = "light" + colorful_times.config.enabled = true + + colorful_times.apply_colorscheme() + assert.are.equal("light", vim.o.background) + assert.is_true(pcall(vim.cmd.colorscheme, "default")) + end) + end) + + -- Test for handling the enabled/disabled state + describe("toggle", function() + it("toggles the enabled state correctly", function() + colorful_times.toggle() + assert.is_false(colorful_times.config.enabled) + colorful_times.toggle() + assert.is_true(colorful_times.config.enabled) + end) + end) + + -- Test to verify schedule-based colorscheme changes + describe("get_active_colorscheme", function() + it("selects the correct colorscheme based on schedule", function() + local schedule = { + { start = "00:00", stop = "12:00", colorscheme = "morning" }, + { start = "12:00", stop = "23:59", colorscheme = "afternoon" }, + } + colorful_times.config.schedule = schedule + colorful_times.preprocess_schedule() + + vim.fn = vim.fn or {} + vim.fn.strftime = function() + return "10:00" + end -- Mock current time + + local active_scheme = colorful_times.get_active_colorscheme() + assert.are.equal("morning", active_scheme.colorscheme) + end) + end) +end) diff --git a/tests/minimal_init.vim b/tests/minimal_init.vim index 769e3b1..a55b5c6 100644 --- a/tests/minimal_init.vim +++ b/tests/minimal_init.vim @@ -1,7 +1,7 @@ " tests/minimal_init.vim -set runtimepath+=. -set runtimepath+=~/.local/share/nvim/site/pack/plugins/start/plenary.nvim +set runtimepath^=~/.local/share/nvim/site/pack/plugins/start/plenary.nvim +set runtimepath^=. " Load your plugin runtime plugin/colorful-times.lua