From 0071888a174b7cc9b50e000f85a868ba1e4d63fb Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 25 Jun 2020 19:07:38 -0700 Subject: [PATCH] types/opt: add Bool.EqualBool method Signed-off-by: Brad Fitzpatrick --- types/opt/bool.go | 7 +++++++ types/opt/bool_test.go | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/types/opt/bool.go b/types/opt/bool.go index adfd275c3..3cdf08d27 100644 --- a/types/opt/bool.go +++ b/types/opt/bool.go @@ -29,6 +29,13 @@ func (b Bool) Get() (v bool, ok bool) { return v, err == nil } +// EqualBool reports whether b is equal to v. +// If b is empty or not a valid bool, it reports false. +func (b Bool) EqualBool(v bool) bool { + p, ok := b.Get() + return ok && p == v +} + var ( trueBytes = []byte("true") falseBytes = []byte("false") diff --git a/types/opt/bool_test.go b/types/opt/bool_test.go index ce08556c0..f7bfc910c 100644 --- a/types/opt/bool_test.go +++ b/types/opt/bool_test.go @@ -64,3 +64,26 @@ func TestBool(t *testing.T) { }) } } + +func TestBoolEqualBool(t *testing.T) { + tests := []struct { + b Bool + v bool + want bool + }{ + {"", true, false}, + {"", false, false}, + {"sdflk;", true, false}, + {"sldkf;", false, false}, + {"true", true, true}, + {"true", false, false}, + {"false", true, false}, + {"false", false, true}, + } + for _, tt := range tests { + if got := tt.b.EqualBool(tt.v); got != tt.want { + t.Errorf("(%q).EqualBool(%v) = %v; want %v", string(tt.b), tt.v, got, tt.want) + } + } + +}