Skip to content

Commit

Permalink
implements string assertion has less size than (#33)
Browse files Browse the repository at this point in the history
* implements string assertion has less size than

* update docs
  • Loading branch information
ppapapetrou76 committed Oct 18, 2021
1 parent 6f3b265 commit f3eee5d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -83,11 +83,11 @@ For the following types basic assertions are supported
* [x] DoesNotStartWith
* [x] EndsWith
* [ ] HasLineCount
* [ ] HasSameSizeAs
* [x] HasSameSizeAs
* [ ] HasSizeBetween
* [ ] HasSizeGreaterThan
* [ ] HasSizeGreaterThanOrEqualTo
* [ ] HasSizeLessThan
* [x] HasSizeLessThan
* [ ] HasSizeLessThanOrEqualTo
* [x] IsEqualToIgnoringCase
* [ ] IsEqualToIgnoringNewLines
Expand Down
4 changes: 4 additions & 0 deletions assert/error_messages.go
Expand Up @@ -161,6 +161,10 @@ func shouldHaveSameSizeAs(actual types.Assertable, substr string) string {
return fmt.Sprintf("assertion failed: expected size of [%v] should be same as the size of [%+v], but it isn't", actual.Value(), substr)
}

func shouldHaveLessSizeThan(actual types.Assertable, substr string) string {
return fmt.Sprintf("assertion failed: expected size of [%v] should be less than the size of [%+v], but it isn't", actual.Value(), substr)
}

func shouldHaveType(actual types.Assertable, value interface{}) string {
return fmt.Sprintf("assertion failed: expected value of = %+v, to have type of %T but it hasn't", actual.Value(), value)
}
Expand Down
11 changes: 10 additions & 1 deletion assert/string.go
Expand Up @@ -219,12 +219,21 @@ func (a AssertableString) DoesNotEndWith(substring string) AssertableString {
// HasSameSizeAs asserts if the assertable string has the same size with the given string
// It errors the test if they don't have the same size.
func (a AssertableString) HasSameSizeAs(substring string) AssertableString {
if !(a.actual.Size() == len(substring)) {
if !(a.actual.HasSize(len(substring))) {
a.t.Error(shouldHaveSameSizeAs(a.actual, substring))
}
return a
}

// HasSizeLessThan asserts if the assertable string's length is less than the size of the given string
// It errors the test if they don't have the same size.
func (a AssertableString) HasSizeLessThan(substring string) AssertableString {
if !(a.actual.HasSizeLessThan(len(substring))) {
a.t.Error(shouldHaveLessSizeThan(a.actual, substring))
}
return a
}

// ContainsOnlyDigits asserts if the expected string contains only digits
// It errors the tests if the string has other characters than digits.
func (a AssertableString) ContainsOnlyDigits() AssertableString {
Expand Down
36 changes: 36 additions & 0 deletions assert/string_test.go
Expand Up @@ -645,6 +645,42 @@ func TestAssertableString_HasSameSizeAs(t *testing.T) {
}
}

func TestAssertableString_HasSizeLessThan(t *testing.T) {
tests := []struct {
name string
actual string
substring string
shouldFail bool
}{
{
name: "should fail if it has the same size",
actual: "I'm a happy man",
substring: "I'm a happy orc",
shouldFail: true,
},
{
name: "should fail if it has bigger size",
actual: "I'm a happy big man",
substring: "I'm a happy orc",
shouldFail: true,
},
{
name: "should succeed if it has less size",
actual: "I'm a happy man",
substring: "I'm a happy woman",
shouldFail: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
test := &testing.T{}
ft := NewFluentT(test)
ft.AssertThatString(tt.actual).HasSizeLessThan(tt.substring)
ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail)
})
}
}

func TestAssertableString_ContainsOnlyDigits(t *testing.T) {
tests := []struct {
name string
Expand Down
5 changes: 5 additions & 0 deletions internal/pkg/values/string_value.go
Expand Up @@ -80,6 +80,11 @@ func (s StringValue) HasSize(length int) bool {
return s.Size() == length
}

// HasSizeLessThan returns true if the string has size less than the given value else false.
func (s StringValue) HasSizeLessThan(length int) bool {
return s.Size() < length
}

// Size returns the string size.
func (s StringValue) Size() int {
return len(s.value)
Expand Down

0 comments on commit f3eee5d

Please sign in to comment.