From 1bbfe8c444e4a443734eb669cc3a8ea3cd4ea462 Mon Sep 17 00:00:00 2001 From: Patroklos Papapetrou Date: Sun, 28 Feb 2021 17:01:15 +0200 Subject: [PATCH] Adds lower/upper case string assertions --- README.md | 8 ++--- assert/error.go | 8 +++++ assert/string.go | 18 ++++++++++ assert/string_test.go | 53 +++++++++++++++++++++++++++++ internal/pkg/values/string_value.go | 10 ++++++ 5 files changed, 93 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d0c713a..d9cd894 100644 --- a/README.md +++ b/README.md @@ -89,11 +89,11 @@ For the following types basic assertions are supported * [ ] HasSizeGreaterThanOrEqualTo * [ ] HasSizeLessThan * [ ] HasSizeLessThanOrEqualTo - * [ ] IsEqualToIgnoringCase + * [x] IsEqualToIgnoringCase * [ ] IsEqualToIgnoringNewLines * [x] IsEqualToIgnoringWhitespace - * [ ] IsLowerCase - * [ ] IsNotEqualToIgnoringCase + * [x] IsLowerCase + * [x] IsNotEqualToIgnoringCase * [x] IsNotEqualToIgnoringWhitespace * [ ] IsSubstringOf - * [ ] IsUpperCase + * [x] IsUpperCase diff --git a/assert/error.go b/assert/error.go index b240a90..3312d43 100644 --- a/assert/error.go +++ b/assert/error.go @@ -160,3 +160,11 @@ func shouldBeLonger(actual types.Assertable, expected interface{}) string { func shouldContainOnlyDigits(actual types.Assertable) string { return fmt.Sprintf("assertion failed: expected %+v to have only digits, but it's not", actual.Value()) } + +func shouldBeLowerCase(actual types.Assertable) string { + return fmt.Sprintf("assertion failed: expected %+v to be lower case, but it's not", actual.Value()) +} + +func shouldBeUpperCase(actual types.Assertable) string { + return fmt.Sprintf("assertion failed: expected %+v to be upper case, but it's not", actual.Value()) +} diff --git a/assert/string.go b/assert/string.go index d5637b5..0c0b297 100644 --- a/assert/string.go +++ b/assert/string.go @@ -72,6 +72,24 @@ func (a AssertableString) IsEmpty() AssertableString { return a } +// IsLowerCase asserts if the expected string is lower case +// It errors the tests if the string is not lower case. +func (a AssertableString) IsLowerCase() AssertableString { + if !a.actual.IsLowerCase() { + a.t.Error(shouldBeLowerCase(a.actual)) + } + return a +} + +// IsUpperCase asserts if the expected string is upper case +// It errors the tests if the string is not upper case. +func (a AssertableString) IsUpperCase() AssertableString { + if !a.actual.IsUpperCase() { + a.t.Error(shouldBeUpperCase(a.actual)) + } + return a +} + // IsNotEmpty asserts if the expected string is not empty // It errors the tests if the string is empty. func (a AssertableString) IsNotEmpty() AssertableString { diff --git a/assert/string_test.go b/assert/string_test.go index 6876903..d2b82e8 100644 --- a/assert/string_test.go +++ b/assert/string_test.go @@ -1,6 +1,7 @@ package assert import ( + "strings" "testing" ) @@ -565,3 +566,55 @@ func TestAssertableString_DoesNotEndWith(t *testing.T) { }) } } + +func TestAssertableString_IsLowerCase(t *testing.T) { + tests := []struct { + name string + actual string + shouldFail bool + }{ + { + name: "should assert string that is not lower case", + actual: "My name is Bond", + shouldFail: true, + }, + { + name: "should assert string that is lower case", + actual: "my name is bond", + shouldFail: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + test := &testing.T{} + ThatString(test, tt.actual).IsLowerCase() + ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail) + }) + } +} + +func TestAssertableString_IsUpperCase(t *testing.T) { + tests := []struct { + name string + actual string + shouldFail bool + }{ + { + name: "should assert string that is not upper case", + actual: "My name is Bond", + shouldFail: true, + }, + { + name: "should assert string that is upper case", + actual: strings.ToUpper("my name is bond"), + shouldFail: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + test := &testing.T{} + ThatString(test, tt.actual).IsUpperCase() + ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail) + }) + } +} diff --git a/internal/pkg/values/string_value.go b/internal/pkg/values/string_value.go index 71bb4ff..6538490 100644 --- a/internal/pkg/values/string_value.go +++ b/internal/pkg/values/string_value.go @@ -134,6 +134,16 @@ func (s StringValue) HasDigitsOnly() bool { return true } +// IsLowerCase returns true if the string is in lower case. +func (s StringValue) IsLowerCase() bool { + return s.IsEqualTo(strings.ToLower(s.value)) +} + +// IsUpperCase returns true if the string is in upper case. +func (s StringValue) IsUpperCase() bool { + return s.IsEqualTo(strings.ToUpper(s.value)) +} + // NewStringValue creates and returns a StringValue struct initialed with the given value. func NewStringValue(value interface{}) StringValue { switch v := value.(type) {