From 15dfc8d9b27ee00b042cfa7f39864927cfb4e42a Mon Sep 17 00:00:00 2001 From: mzampetakis Date: Fri, 8 May 2020 22:45:45 +0300 Subject: [PATCH] Adds ContainsOnlyDigits assertion --- README.md | 1 - assert/error.go | 4 +++ assert/string.go | 9 +++++ assert/string_test.go | 52 +++++++++++++++++++++++++++++ internal/pkg/values/string_value.go | 11 ++++++ 5 files changed, 76 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b27b2fd..985088b 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,6 @@ For the following types basic assertions are supported * time.Time (basic assertions) * moar string assertions * [ ] ContainsIgnoringCase - * [ ] ContainsOnlyDigits * [ ] ContainsOnlyOnce * [ ] ContainsOnlyWhitespaces * [ ] ContainsWhitespaces diff --git a/assert/error.go b/assert/error.go index 654543d..33d9a02 100644 --- a/assert/error.go +++ b/assert/error.go @@ -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()) +} diff --git a/assert/string.go b/assert/string.go index e1346ba..bbf0711 100644 --- a/assert/string.go +++ b/assert/string.go @@ -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 +} diff --git a/assert/string_test.go b/assert/string_test.go index 961cb6f..af7a8f9 100644 --- a/assert/string_test.go +++ b/assert/string_test.go @@ -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) + }) + } +} diff --git a/internal/pkg/values/string_value.go b/internal/pkg/values/string_value.go index ea9ff8e..63bb883 100644 --- a/internal/pkg/values/string_value.go +++ b/internal/pkg/values/string_value.go @@ -3,6 +3,7 @@ package values import ( "fmt" "strings" + "unicode" ) // StringValue value represents a string value @@ -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) {