diff --git a/README.md b/README.md index c1f07f6..927a483 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/assert/error_messages.go b/assert/error_messages.go index 6c17727..be15cab 100644 --- a/assert/error_messages.go +++ b/assert/error_messages.go @@ -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) } diff --git a/assert/string.go b/assert/string.go index d8a342d..1246d46 100644 --- a/assert/string.go +++ b/assert/string.go @@ -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 { diff --git a/assert/string_test.go b/assert/string_test.go index d8dc5ab..613ce1b 100644 --- a/assert/string_test.go +++ b/assert/string_test.go @@ -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 diff --git a/internal/pkg/values/string_value.go b/internal/pkg/values/string_value.go index 2d670df..826f8cc 100644 --- a/internal/pkg/values/string_value.go +++ b/internal/pkg/values/string_value.go @@ -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)