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

Add HasSize assertions #43

Merged
merged 1 commit into from Dec 21, 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
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions assert/error_messages.go
Expand Up @@ -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)
}
Expand Down
40 changes: 40 additions & 0 deletions assert/string.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
174 changes: 174 additions & 0 deletions assert/string_test.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions internal/pkg/values/string_value.go
Expand Up @@ -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)
Expand Down