From 7befa3600183c5c6defd93a484dd1902eaa98c0c Mon Sep 17 00:00:00 2001 From: Jim Ursetto Date: Sun, 1 May 2022 20:50:48 -0500 Subject: [PATCH 1/2] Expand test cases for Boolean --- stringy_test.go | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/stringy_test.go b/stringy_test.go index 208cacb..3ba9652 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -29,33 +29,42 @@ func TestInput_EmptyNoMatchBetween(t *testing.T) { } } -func TestInput_Boolean(t *testing.T) { - str := New("on") - val := str.Boolean() - if !val { - t.Errorf("Expected: to be true but got: %v", val) +func TestInput_BooleanTrue(t *testing.T) { + strs := []string{"on", "On", "yes", "YES", "1", "true"} + for _, s := range strs { + t.Run(s, func(t *testing.T) { + if val := New(s).Boolean(); !val { + t.Errorf("Expected: to be true but got: %v", val) + } + }) } } -func TestInput_BooleanOff(t *testing.T) { - str := New("off") - val := str.Boolean() - if val { - t.Errorf("Expected: to be false but got: %v", val) +func TestInput_BooleanFalse(t *testing.T) { + strs := []string{"off", "Off", "no", "NO", "0", "false"} + for _, s := range strs { + t.Run(s, func(t *testing.T) { + if val := New(s).Boolean(); val { + t.Errorf("Expected: to be false but got: %v", val) + } + }) } } func TestInput_BooleanError(t *testing.T) { - defer func() { - if err := recover(); err == nil { - t.Errorf("Error expected") - } - }() - str := New("invalid") - val := str.Boolean() - if val { - t.Errorf("Expected: to be false but got: %v", val) + strs := []string{"invalid", "-1", ""} + for _, s := range strs { + t.Run(s, func(t *testing.T) { + defer func() { + if err := recover(); err == nil { + t.Errorf("Error expected") + } + }() + val := New(s).Boolean() + t.Errorf("Expected: to panic but got: %v", val) + }) } + } func TestInput_CamelCase(t *testing.T) { From 0bfb740fcd17684f8b40f915928cfc621d159798 Mon Sep 17 00:00:00 2001 From: Jim Ursetto Date: Sun, 1 May 2022 20:52:35 -0500 Subject: [PATCH 2/2] Properly test for boolean "true" "true" was treated as an invalid boolean. All strings in the slice of valid boolean values need to be lowercase, since the input is lowercased before comparison. --- message.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/message.go b/message.go index f21f798..73ea6a5 100644 --- a/message.go +++ b/message.go @@ -18,4 +18,4 @@ const ( var False = []string{"off", "no", "0", "false"} // True is slice of array for true logical representation in string -var True = []string{"on", "yes", "1", "True"} +var True = []string{"on", "yes", "1", "true"}