From 4fb3d84ad3920c7a11d5f444ffc5c1e6c53aa147 Mon Sep 17 00:00:00 2001 From: Andrew Austin Date: Sun, 23 Aug 2015 17:24:46 -0400 Subject: [PATCH] color: add Equals() method to compare two colors --- color.go | 25 +++++++++++++++++++++++++ color_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/color.go b/color.go index c4a10c3..f715d62 100644 --- a/color.go +++ b/color.go @@ -259,6 +259,31 @@ func (c *Color) isNoColorSet() bool { return NoColor } +// Equals returns a boolean value indicating whether two colors are equal. +func (c *Color) Equals(c2 *Color) bool { + if len(c.params) != len(c2.params) { + return false + } + + for _, attr := range c.params { + if !c2.attrExists(attr) { + return false + } + } + + return true +} + +func (c *Color) attrExists(a Attribute) bool { + for _, attr := range c.params { + if attr == a { + return true + } + } + + return false +} + func boolPtr(v bool) *bool { return &v } diff --git a/color_test.go b/color_test.go index e4a538c..053b561 100644 --- a/color_test.go +++ b/color_test.go @@ -48,6 +48,40 @@ func TestColor(t *testing.T) { } } +func TestColorEquals(t *testing.T) { + fgblack1 := New(FgBlack) + fgblack2 := New(FgBlack) + bgblack := New(BgBlack) + fgbgblack := New(FgBlack, BgBlack) + fgblackbgred := New(FgBlack, BgRed) + fgred := New(FgRed) + bgred := New(BgRed) + + if !fgblack1.Equals(fgblack2) { + t.Error("Two black colors are not equal") + } + + if fgblack1.Equals(bgblack) { + t.Error("Fg and bg black colors are equal") + } + + if fgblack1.Equals(fgbgblack) { + t.Error("Fg black equals fg/bg black color") + } + + if fgblack1.Equals(fgred) { + t.Error("Fg black equals Fg red") + } + + if fgblack1.Equals(bgred) { + t.Error("Fg black equals Bg red") + } + + if fgblack1.Equals(fgblackbgred) { + t.Error("Fg black equals fg black bg red") + } +} + func TestNoColor(t *testing.T) { rb := new(bytes.Buffer) Output = rb