Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,27 +526,24 @@ func (c *Color) Equals(c2 *Color) bool {
if c == nil || c2 == nil {
return false
}

if len(c.params) != len(c2.params) {
return false
}

counts := make(map[Attribute]int, len(c.params))
for _, attr := range c.params {
if !c2.attrExists(attr) {
return false
}
counts[attr]++
}

return true
}

func (c *Color) attrExists(a Attribute) bool {
for _, attr := range c.params {
if attr == a {
return true
for _, attr := range c2.params {
if counts[attr] == 0 {
return false
}
counts[attr]--
}

return false
return true
}

func boolPtr(v bool) *bool {
Expand Down
14 changes: 14 additions & 0 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ func TestColorEquals(t *testing.T) {
}
}

func TestColorEquals_DuplicateAttributes(t *testing.T) {
ordered := New(FgRed, Bold).Add(FgRed)
reordered := New(Bold, FgRed).Add(FgRed)
differentCounts := New(FgRed, Bold).Add(Bold)

if !ordered.Equals(reordered) {
t.Error("Colors with the same attributes in different orders are not equal")
}

if ordered.Equals(differentCounts) {
t.Error("Colors with different duplicate attribute counts are equal")
}
}

func TestNoColor(t *testing.T) {
rb := new(bytes.Buffer)
Output = rb
Expand Down
Loading