-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): add tests for Rgb and Color
- Loading branch information
Showing
9 changed files
with
212 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |