Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements string assertion has less size than #33

Merged
merged 2 commits into from Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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