diff --git a/README.md b/README.md index 927a483..7ec8fd2 100644 --- a/README.md +++ b/README.md @@ -84,11 +84,11 @@ For the following types basic assertions are supported * [x] EndsWith * [ ] HasLineCount * [x] HasSameSizeAs - * [ ] HasSizeBetween - * [ ] HasSizeGreaterThan - * [ ] HasSizeGreaterThanOrEqualTo + * [X] HasSizeBetween + * [x] HasSizeGreaterThan + * [x] HasSizeGreaterThanOrEqualTo * [x] HasSizeLessThan - * [ ] HasSizeLessThanOrEqualTo + * [x] HasSizeLessThanOrEqualTo * [x] IsEqualToIgnoringCase * [ ] IsEqualToIgnoringNewLines * [x] IsEqualToIgnoringWhitespace diff --git a/assert/error_messages.go b/assert/error_messages.go index f1f5231..235c7e8 100644 --- a/assert/error_messages.go +++ b/assert/error_messages.go @@ -161,10 +161,26 @@ 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 shouldHaveSizeBetween(actual types.Assertable, shortString, longString string) string { + return fmt.Sprintf("assertion failed: expected size of [%v] should be greater then the size of [%+v] and less than the size of [%+v], but it isn't", actual.Value(), shortString, longString) +} + 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 shouldHaveLessSizeThanOrEqual(actual types.Assertable, substr string) string { + return fmt.Sprintf("assertion failed: expected size of [%v] should be less than or equal to the size of [%+v], but it isn't", actual.Value(), substr) +} + +func shouldHaveGreaterSizeThan(actual types.Assertable, substr string) string { + return fmt.Sprintf("assertion failed: expected size of [%v] should be greater than the size of [%+v], but it isn't", actual.Value(), substr) +} + +func shouldHaveGreaterSizeThanOrEqual(actual types.Assertable, substr string) string { + return fmt.Sprintf("assertion failed: expected size of [%v] should be greater than or equalto 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 7403bfc..d423385 100644 --- a/assert/string.go +++ b/assert/string.go @@ -245,6 +245,36 @@ func (a AssertableString) HasSameSizeAs(substring string) AssertableString { return a } +// HasSizeBetween asserts if the assertable string has bigger size of the first given string and less size than the second given string +// It errors the test if the assertable string has the same or less size than the first string or greater than or the same size to the second string. +func (a AssertableString) HasSizeBetween(shortString, longString string) AssertableString { + a.t.Helper() + if a.actual.HasSizeLessThanOrEqual(len(shortString)) || a.actual.HasSizeGreaterThanOrEqual(len(longString)) { + a.t.Error(shouldHaveSizeBetween(a.actual, shortString, longString)) + } + return a +} + +// HasSizeGreaterThan asserts if the assertable string has bigger size of the given string +// It errors the test if the assertable string has the less or equal size to the given one. +func (a AssertableString) HasSizeGreaterThan(substring string) AssertableString { + a.t.Helper() + if !(a.actual.HasSizeGreaterThan(len(substring))) { + a.t.Error(shouldHaveGreaterSizeThan(a.actual, substring)) + } + return a +} + +// HasSizeGreaterThanOrEqualTo asserts if the assertable string has bigger os the same size of the given string +// It errors the test if the assertable string has the less size to the given one. +func (a AssertableString) HasSizeGreaterThanOrEqualTo(substring string) AssertableString { + a.t.Helper() + if !(a.actual.HasSizeGreaterThanOrEqual(len(substring))) { + a.t.Error(shouldHaveGreaterSizeThanOrEqual(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 { @@ -255,6 +285,16 @@ func (a AssertableString) HasSizeLessThan(substring string) AssertableString { return a } +// HasSizeLessThanOrEqualTo asserts if the assertable string's length is less than or equal to the size of the given string +// It errors the test if they don't have the same size. +func (a AssertableString) HasSizeLessThanOrEqualTo(substring string) AssertableString { + a.t.Helper() + if !(a.actual.HasSizeLessThanOrEqual(len(substring))) { + a.t.Error(shouldHaveLessSizeThanOrEqual(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 613ce1b..ca8617b 100644 --- a/assert/string_test.go +++ b/assert/string_test.go @@ -645,6 +645,138 @@ func TestAssertableString_HasSameSizeAs(t *testing.T) { } } +func TestAssertableString_HasSizeBetween(t *testing.T) { + tests := []struct { + name string + actual string + shortString string + longString string + shouldFail bool + }{ + { + name: "should fail if it has greater size than the longString", + actual: "I'm not a happy big man", + shortString: "I'm a happy orc", + longString: "I'm not a happy orc", + shouldFail: true, + }, + { + name: "should fail if it has the same size as the longString", + actual: "I'm a happy big man", + shortString: "I'm a happy orc", + longString: "I'm not a happy orc", + shouldFail: true, + }, + { + name: "should fail if it has less size than the shortString", + actual: "I'm a orc", + shortString: "I'm a happy orc", + longString: "I'm not a happy orc", + shouldFail: true, + }, + { + name: "should fail if it has the same size as the shortString", + actual: "I'm a happy man", + shortString: "I'm a happy orc", + longString: "I'm not a happy orc", + shouldFail: true, + }, + { + name: "should succeed if it has size between shortString and longString", + actual: "I'm a happy big man", + shortString: "I'm a happy orc", + longString: "I'm not a happy big orc", + shouldFail: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + test := &testing.T{} + ft := NewFluentT(test) + ft.AssertThatString(tt.actual).HasSizeBetween(tt.shortString, tt.longString) + ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail) + }) + } +} + +func TestAssertableString_HasSizeGreaterThan(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 less size", + actual: "I'm a happy man", + substring: "I'm a happy woman", + shouldFail: true, + }, + { + name: "should succeed if it has bigger size", + actual: "I'm a happy big man", + substring: "I'm a happy orc", + shouldFail: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + test := &testing.T{} + ft := NewFluentT(test) + ft.AssertThatString(tt.actual).HasSizeGreaterThan(tt.substring) + ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail) + }) + } +} + +func TestAssertableString_HasSizeGreaterThanOrEqual(t *testing.T) { + tests := []struct { + name string + actual string + substring string + shouldFail bool + }{ + { + name: "should fail if it has less size", + actual: "I'm a happy man", + substring: "I'm a happy woman", + shouldFail: true, + }, + { + name: "should succeed if it has bigger size", + actual: "I'm a happy big man", + substring: "I'm a happy orc", + shouldFail: false, + }, + { + name: "should succeed if it has the same size", + actual: "I'm a happy man", + substring: "I'm a happy orc", + shouldFail: false, + }, + { + name: "should succeed if it has the same size and empty", + actual: "", + substring: "", + shouldFail: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + test := &testing.T{} + ft := NewFluentT(test) + ft.AssertThatString(tt.actual).HasSizeGreaterThanOrEqualTo(tt.substring) + ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail) + }) + } +} + func TestAssertableString_HasSizeLessThan(t *testing.T) { tests := []struct { name string @@ -681,6 +813,48 @@ func TestAssertableString_HasSizeLessThan(t *testing.T) { } } +func TestAssertableString_HasSizeLessThanOrEqualTo(t *testing.T) { + tests := []struct { + name string + actual string + substring string + shouldFail bool + }{ + { + 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, + }, + { + name: "should succeed if it has the same size", + actual: "I'm a happy man", + substring: "I'm a happy orc", + shouldFail: false, + }, + { + name: "should succeed if it has the same size and empty", + actual: "", + substring: "", + shouldFail: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + test := &testing.T{} + ft := NewFluentT(test) + ft.AssertThatString(tt.actual).HasSizeLessThanOrEqualTo(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 826f8cc..03dbee6 100644 --- a/internal/pkg/values/string_value.go +++ b/internal/pkg/values/string_value.go @@ -80,11 +80,26 @@ func (s StringValue) HasSize(length int) bool { return s.Size() == length } +// HasSizeGreaterThan returns true if the string has size greater than the given value else false. +func (s StringValue) HasSizeGreaterThan(length int) bool { + return s.Size() > length +} + +// HasSizeGreaterThanOrEqual returns true if the string has size greater than the given value else false. +func (s StringValue) HasSizeGreaterThanOrEqual(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 } +// HasSizeLessThanOrEqual returns true if the string has size less than the given value else false. +func (s StringValue) HasSizeLessThanOrEqual(length int) bool { + return s.Size() <= length +} + // Size returns the string size. func (s StringValue) Size() int { return len(s.value)