diff --git a/color.go b/color.go index ada7604e..d3906bfb 100644 --- a/color.go +++ b/color.go @@ -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 { diff --git a/color_test.go b/color_test.go index 103d6cbb..e22af6b3 100644 --- a/color_test.go +++ b/color_test.go @@ -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