Skip to content

Commit

Permalink
Merge pull request #6 from mzampetakis/implement-ContainsOnlyDigits
Browse files Browse the repository at this point in the history
ContainsOnlyDigits assertion
  • Loading branch information
ppapapetrou76 authored May 14, 2020
2 parents dc762dd + 15dfc8d commit c845f82
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ For the following types basic assertions are supported
* time.Time (basic assertions)
* moar string assertions
* [ ] ContainsIgnoringCase
* [ ] ContainsOnlyDigits
* [ ] ContainsOnlyOnce
* [ ] ContainsOnlyWhitespaces
* [ ] ContainsWhitespaces
Expand Down
4 changes: 4 additions & 0 deletions assert/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,7 @@ func shouldBeShorter(actual types.Assertable, expected interface{}) string {
func shouldBeLonger(actual types.Assertable, expected interface{}) string {
return fmt.Sprintf("assertion failed: expected value of = %+v, to be longer than %+v", actual.Value(), expected)
}

func shouldContainOnlyDigits(actual types.Assertable) string {
return fmt.Sprintf("assertion failed: expected %+v to have only digits, but it's not", actual.Value())
}
9 changes: 9 additions & 0 deletions assert/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,12 @@ func (a AssertableString) HasSameSizeAs(substring string) AssertableString {
}
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 {
if !(a.actual.HasDigitsOnly()) {
a.t.Error(shouldContainOnlyDigits(a.actual))
}
return a
}
52 changes: 52 additions & 0 deletions assert/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,55 @@ func TestAssertableString_HasSameSizeAs(t *testing.T) {
})
}
}

func TestAssertableString_ContainsOnlyDigits(t *testing.T) {
tests := []struct {
name string
actual string
shouldFail bool
}{
{
name: "should succeed if it only contains digits",
actual: "1234567890",
shouldFail: false,
},
{
name: "should succeed if it only contains one digit",
actual: "4",
shouldFail: false,
},
{
name: "should succeed if it contains huge number",
actual: "18446744073709551616",
shouldFail: false,
},
{
name: "should succeed if it contains huge number and a character",
actual: "a18446744073709551616",
shouldFail: true,
},
{
name: "should fail if contains a letter",
actual: "01e",
shouldFail: true,
},
{
name: "should fail if contains only letters",
actual: "test",
shouldFail: true,
},
{
name: "should fail if contains empty string",
actual: " ",
shouldFail: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
test := &testing.T{}
ft := NewFluentT(test)
ft.AssertThatString(tt.actual).ContainsOnlyDigits()
ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail)
})
}
}
11 changes: 11 additions & 0 deletions internal/pkg/values/string_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package values
import (
"fmt"
"strings"
"unicode"
)

// StringValue value represents a string value
Expand Down Expand Up @@ -98,6 +99,16 @@ func (s StringValue) greaterOrEqual(expected StringValue) bool {
return s.DecoratedValue() >= expected.value
}

// HasDigitsOnly returns true if the string has only digits else false
func (s StringValue) HasDigitsOnly() bool {
for _, c := range s.DecoratedValue() {
if !unicode.IsDigit(c) {
return false
}
}
return true
}

// NewStringValue creates and returns a StringValue struct initialed with the given value
func NewStringValue(value interface{}) StringValue {
switch v := value.(type) {
Expand Down

0 comments on commit c845f82

Please sign in to comment.