Skip to content

Commit 1a3f3b7

Browse files
committed
fix(Color): make constructor idempotent
Problem: passing a `Color` instance to the main constructor of `Color` (i.e. `Color.new()` or `Color()`) silently causes a bug and does not throw an error Solution: immediately return the argument back to the caller when passed a `Color` instance, thereby making the constructor idempotent
1 parent 8a311d4 commit 1a3f3b7

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
66

77
## [unreleased]
88

9+
### What's New?
10+
11+
- Added static/class function `Color.is_Color()` for detecting Color instances
12+
13+
### Changes
14+
15+
### Issues Fix
16+
17+
- Made `Color()` constructor idempotent (previously, passing a `Color` inst silently caused a bug)
18+
919
## [v1.1.0] - 23 July 2024
1020

1121
### Highlight Groups

lua/github-theme/lib/color.lua

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ local M = setmetatable(Color --[[@as GhTheme.Color.Static]], {
109109
---@return GhTheme.Color
110110
---@nodiscard
111111
function M.new(color)
112+
if M.is_Color(color) then
113+
return color --[[@as GhTheme.Color]]
114+
end
115+
112116
local ty = type(color)
113117

114118
if ty == 'string' or ty == 'number' then

test/github-theme/color_spec.lua

+29
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ describe('Color', function()
5151
assert.are.same(ex.str, c:to_css())
5252
end)
5353

54+
it('should construct from an integer', function()
55+
local n = 0x12345630
56+
local c = Color.from_hex(n)
57+
assert.are.same(n, c:to_hex(true))
58+
assert.are.same('#12345630', c:to_css(true))
59+
end)
60+
5461
it('should error when given invalid args', function()
5562
assert.does.match.error(function()
5663
---@diagnostic disable-next-line: param-type-mismatch
@@ -99,6 +106,13 @@ describe('Color', function()
99106
assert.are.same(ex.str, c:to_css())
100107
end)
101108

109+
it('should construct from an integer', function()
110+
local n = 0x12345630
111+
local c = Color(n)
112+
assert.are.same(n, c:to_hex(true))
113+
assert.are.same('#12345630', c:to_css(true))
114+
end)
115+
102116
it('should infer from rgba components', function()
103117
local c = Color(Color.from_hex(ex.str):to_rgba())
104118
assert.are.same(ex.hex, c:to_hex())
@@ -116,6 +130,21 @@ describe('Color', function()
116130
-- assert.are.same(ex.hex, c:to_hex())
117131
assert.are.same(ex.str, c:to_css())
118132
end)
133+
134+
it('should be idempotent', function()
135+
local orig = Color(ex.rgba)
136+
orig.alpha = 0.5
137+
local new = Color(orig --[[@as any]])
138+
139+
for _, c in ipairs({ orig, new }) do
140+
for _, k in ipairs({ 'WHITE', 'BLACK', 'BG' }) do
141+
rawset(c, k, c[k])
142+
end
143+
end
144+
145+
assert.are.same(orig, new)
146+
assert.are.same(orig:to_css(true), new:to_css(true))
147+
end)
119148
end)
120149

121150
describe('conversion', function()

0 commit comments

Comments
 (0)