Skip to content

Commit

Permalink
Merge pull request #16 from ppapapetrou76/add-string-lower-upper-case…
Browse files Browse the repository at this point in the history
…-assertions

Adds lower/upper case string assertions
  • Loading branch information
ppapapetrou76 authored Feb 28, 2021
2 parents ac46f11 + 1bbfe8c commit f4b6fe5
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions assert/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
18 changes: 18 additions & 0 deletions assert/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
53 changes: 53 additions & 0 deletions assert/string_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assert

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -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)
})
}
}
10 changes: 10 additions & 0 deletions internal/pkg/values/string_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f4b6fe5

Please sign in to comment.