Skip to content

Commit

Permalink
chore(tests): add tests for Rgb and Color
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvirtin committed Jul 2, 2024
1 parent 6eef36d commit b1b1282
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 28 deletions.
4 changes: 4 additions & 0 deletions lua/vgit/core/Color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ local Object = require('vgit.core.Object')
local Color = Object:extend()

function Color:constructor(spec)
if not spec then error('spec is required') end
if not spec.name then error('spec.name is required') end
if not spec.attribute then error('spec.attribute is required') end

return {
spec = spec,
rgb = nil,
Expand Down
13 changes: 13 additions & 0 deletions lua/vgit/core/event.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
local loop = require('vgit.core.loop')

local event = {
type = {
BufRead = 'BufRead',
BufEnter = 'BufEnter',
BufDelete = 'BufDelete',
WinEnter = 'WinEnter',
BufWinEnter = 'BufWinEnter',
BufWinLeave = 'BufWinLeave',
ColorScheme = 'ColorScheme',
CursorHold = 'CursorHold',
CursorMoved = 'CursorMoved',
InsertEnter = 'InsertEnter',
QuitPre = 'QuitPre'
},
group = vim.api.nvim_create_augroup('VGitGroup', { clear = true }),
}

Expand Down
15 changes: 0 additions & 15 deletions lua/vgit/core/event_type.lua

This file was deleted.

3 changes: 1 addition & 2 deletions lua/vgit/core/highlight.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local event = require('vgit.core.event')
local hls_setting = require('vgit.settings.hls')
local event_type = require('vgit.core.event_type')

local highlight = {}

Expand Down Expand Up @@ -39,7 +38,7 @@ function highlight.register_module(dependency)
end

function highlight.register_events()
event.on(event_type.ColorScheme, function()
event.on(event.type.ColorScheme, function()
hls_setting:for_each(function(hl, color)
highlight.define(hl, color)
end)
Expand Down
12 changes: 6 additions & 6 deletions lua/vgit/features/buffer/LiveBlame.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local loop = require('vgit.core.loop')
local event = require('vgit.core.event')
local Object = require('vgit.core.Object')
local Window = require('vgit.core.Window')
local console = require('vgit.core.console')
local Namespace = require('vgit.core.Namespace')
local event_type = require('vgit.core.event_type')
local git_buffer_store = require('vgit.git.git_buffer_store')
local live_blame_setting = require('vgit.settings.live_blame')

Expand Down Expand Up @@ -86,19 +86,19 @@ end
function LiveBlame:register_events()
git_buffer_store.attach('attach', function(git_buffer)
git_buffer
:on(event_type.BufEnter, function()
:on(event.type.BufEnter, function()
self:reset()
end)
:on(event_type.WinEnter, function()
:on(event.type.WinEnter, function()
self:reset()
end)
:on(event_type.CursorMoved, function()
:on(event.type.CursorMoved, function()
self:reset()
end)
:on(event_type.InsertEnter, function()
:on(event.type.InsertEnter, function()
self:reset()
end)
:on(event_type.CursorHold, function()
:on(event.type.CursorHold, function()
self:render()
end)
end)
Expand Down
3 changes: 1 addition & 2 deletions lua/vgit/git/git_buffer_store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local event = require('vgit.core.event')
local Watcher = require('vgit.core.Watcher')
local git_repo = require('vgit.git.git_repo')
local GitBuffer = require('vgit.git.GitBuffer')
local event_type = require('vgit.core.event_type')

local buffers = {}
local event_handlers = {
Expand All @@ -27,7 +26,7 @@ git_buffer_store.register_events = loop.coroutine(function()

is_registerd = true

event.on(event_type.BufRead, function()
event.on(event.type.BufRead, function()
git_buffer_store.collect()
end)

Expand Down
6 changes: 3 additions & 3 deletions lua/vgit/ui/screen_manager.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local loop = require('vgit.core.loop')
local event = require('vgit.core.event')
local keymap = require('vgit.core.keymap')
local event_type = require('vgit.core.event_type')
local scene_setting = require('vgit.settings.scene')
local DiffScreen = require('vgit.features.screens.DiffScreen')
local HistoryScreen = require('vgit.features.screens.HistoryScreen')
Expand Down Expand Up @@ -111,11 +111,11 @@ function screen_manager.show(screen_name, ...)
if success then
screen_manager.active_screen = screen
screen.scene
:on(event_type.BufWinLeave, function()
:on(event.type.BufWinLeave, function()
loop.free_textlock()
if screen_manager.has_active_screen() then return screen_manager.destroy_active_screen() end
end)
:on(event_type.QuitPre, function()
:on(event.type.QuitPre, function()
if screen_manager.has_active_screen() then return screen_manager.destroy_active_screen() end
end)
end
Expand Down
99 changes: 99 additions & 0 deletions tests/unit/core/Color_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
local Color = require('vgit.core.Color')

describe('Color:', function()
describe('constructor', function()
it('should initialize Color object with spec', function()
local spec = { name = 'Normal', attribute = 'fg' }
local color = Color(spec)

assert.is_equal(color.spec, spec)
assert.is_nil(color.rgb)
assert.is_nil(color.hex)
end)

it('should throw an error if spec is nil', function()
assert.has_error(function() Color(nil) end, 'spec is required')
end)

it('should throw an error if spec.name is nil', function()
assert.has_error(function() Color({ attribute = 'fg' }) end, 'spec.name is required')
end)

it('should throw an error if spec.attribute is nil', function()
assert.has_error(function() Color({ name = 'Normal' }) end, 'spec.attribute is required')
end)
end)

describe('to_hex', function()
it('should return cached hex value if already computed', function()
local spec = { name = 'Normal', attribute = 'fg' }
local color = Color(spec)
color.hex = '#ff0000'

assert.is_equal(color:to_hex(), '#ff0000')
end)

it('should return hex value based on spec', function()
local spec = { name = 'Normal', attribute = 'fg' }
vim.api.nvim_set_hl(0, 'Normal', { foreground = 16711680 })
local color = Color(spec)

assert.is_equal(color:to_hex(), '#ff0000')
end)
end)

describe('to_rgb', function()
it('should return Rgb object based on hex value', function()
local spec = { name = 'Normal', attribute = 'fg' }
vim.api.nvim_set_hl(0, 'Normal', { foreground = 16711680 })
local color = Color(spec)
local rgb = color:to_rgb()

assert.is_equal(rgb.hex, '#ff0000')
end)
end)

describe('get', function()
it('should return the RGB value as a hex string', function()
local spec = { name = 'Normal', attribute = 'fg' }
vim.api.nvim_set_hl(0, 'Normal', { foreground = 16711680 })
local color = Color(spec)

assert.is_equal(color:get(), '#ff0000')
end)
end)

describe('lighten', function()
it('should lighten the color by the given percentage', function()
vim.api.nvim_set_hl(0, 'Normal', { foreground = 16711680 })
local initial_color = Color({ name = 'Normal', attribute = 'fg' })
local color = Color({ name = 'Normal', attribute = 'fg' })

color:lighten(50)

local initial_rgb = initial_color:to_rgb()
local rgb = color:to_rgb()

assert.is_true(rgb.r > initial_rgb.r)
assert.is_true(rgb.g == 0)
assert.is_true(rgb.b == 0)
end)
end)

describe('darken', function()
it('should darken the color by the given percentage', function()
vim.api.nvim_set_hl(0, 'Normal', { foreground = 16711680 })
local initial_color = Color({ name = 'Normal', attribute = 'fg' })
local color = Color({ name = 'Normal', attribute = 'fg' })

color:darken(50)

local initial_rgb = initial_color:to_rgb()
local rgb = color:to_rgb()

assert.is_true(rgb.r < initial_rgb.r)
assert.is_true(rgb.g < initial_rgb.g)
assert.is_true(rgb.b < initial_rgb.b)
end)
end)
end)
85 changes: 85 additions & 0 deletions tests/unit/core/Rgb_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
local Rgb = require('vgit.core.Rgb')

describe('Rgb:', function()
describe('constructor', function()
it('should initialize Rgb object with hex and RGB values', function()
local hex = '#ff0000'
local rgb = Rgb(hex)

assert.is_equal(rgb.hex, hex)
assert.is_equal(rgb.r, 255)
assert.is_equal(rgb.g, 0)
assert.is_equal(rgb.b, 0)
end)

it('should handle nil hex input gracefully', function()
local rgb = Rgb(nil)

assert.is_nil(rgb.hex)
assert.is_nil(rgb.r)
assert.is_nil(rgb.g)
assert.is_nil(rgb.b)
end)
end)

describe('scale_up', function()
it('should scale up RGB values by a percentage', function()
local rgb = Rgb('#800000')
rgb:scale_up(50)

assert.is_equal(rgb.r, 192)
assert.is_equal(rgb.g, 0)
assert.is_equal(rgb.b, 0)
end)

it('should handle nil hex input gracefully', function()
local rgb = Rgb(nil)
rgb:scale_up(50)

assert.is_nil(rgb.r)
assert.is_nil(rgb.g)
assert.is_nil(rgb.b)
end)
end)

describe('scale_down()', function()
it('should scale down RGB values by a percentage', function()
local rgb = Rgb('#ff0000')
rgb:scale_down(50)

assert.is_equal(rgb.r, 127)
assert.is_equal(rgb.g, 1)
assert.is_equal(rgb.b, 1)
end)

it('should handle nil hex input gracefully', function()
local rgb = Rgb(nil)
rgb:scale_down(50)

assert.is_nil(rgb.r)
assert.is_nil(rgb.g)
assert.is_nil(rgb.b)
end)
end)

describe('get()', function()
it('should return the hex representation of the RGB color', function()
local rgb = Rgb('#ff0000')
assert.is_equal(rgb:get(), '#ff0000')
end)

it('should return "NONE" if hex is nil', function()
local rgb = Rgb(nil)
assert.is_equal(rgb:get(), 'NONE')
end)

it('should cap RGB values at 255', function()
local rgb = Rgb('#ff0000')
rgb.r = 300
rgb.g = 300
rgb.b = 300

assert.is_equal(rgb:get(), '#ffffff')
end)
end)
end)

0 comments on commit b1b1282

Please sign in to comment.