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 Len to FluentSlice, FluentMap, FluentString #124

Merged
merged 2 commits into from
Feb 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/fluentassert/verify/compare/v1.0.0...HEAD)

### Added

- Add `Len` assertion for `string`, `[]T`, `map[K]V` types.

## [1.0.0](https://github.com/fluentassert/verify/releases/tag/v1.0.0) - 2023-04-05

This release contains breaking changes.
Expand Down
15 changes: 12 additions & 3 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (x FluentMap[K, V]) NotContainPair(k K, v V, opts ...cmp.Option) FailureMes
return FailureMessage(fmt.Sprintf("contains the pair\ngot: %+v\nkey: %+v\nvalue: %+v", x.Got, k, v))
}

// Any tests if any of the slice's item meets the predicate criteria.
// Any tests if any of the map's pairs meets the predicate criteria.
func (x FluentMap[K, V]) Any(predicate func(K, V) bool) FailureMessage {
for k, v := range x.Got {
if predicate(k, v) {
Expand All @@ -100,7 +100,7 @@ func (x FluentMap[K, V]) Any(predicate func(K, V) bool) FailureMessage {
return FailureMessage(fmt.Sprintf("none pair does meet the predicate criteria\ngot: %+v", x.Got))
}

// All tests if all of the slice's items meets the predicate criteria.
// All tests if all of the map's pairs meet the predicate criteria.
func (x FluentMap[K, V]) All(predicate func(K, V) bool) FailureMessage {
for k, v := range x.Got {
if !predicate(k, v) {
Expand All @@ -110,7 +110,7 @@ func (x FluentMap[K, V]) All(predicate func(K, V) bool) FailureMessage {
return ""
}

// None tests if all of the slice's item does not meet the predicate criteria.
// None tests if none of the map's pairs meets the predicate criteria.
func (x FluentMap[K, V]) None(predicate func(K, V) bool) FailureMessage {
for k, v := range x.Got {
if predicate(k, v) {
Expand All @@ -119,3 +119,12 @@ func (x FluentMap[K, V]) None(predicate func(K, V) bool) FailureMessage {
}
return ""
}

// Len tests the length of the map.
func (x FluentMap[K, V]) Len(want int) FailureMessage {
gotLen := len(x.Got)
if gotLen != want {
return FailureMessage(fmt.Sprintf("has different length\ngot: %+v\nlen: %v\nwant: %v", x.Got, gotLen, want))
}
return ""
}
11 changes: 11 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,15 @@ func TestMap(t *testing.T) {
assertFailed(t, got, "a pair meets the predicate criteria")
})
})

t.Run("Len", func(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
got := verify.Map(dict).Len(len(dict))
assertPassed(t, got)
})
t.Run("Failed", func(t *testing.T) {
got := verify.Map(dict).Len(10)
assertFailed(t, got, "has different length")
})
})
}
15 changes: 12 additions & 3 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (x FluentSlice[T]) NotContain(item T, opts ...cmp.Option) FailureMessage {
return ""
}

// Any tests if any of the slice's item meets the predicate criteria.
// Any tests if any of the slice's items meets the predicate criteria.
func (x FluentSlice[T]) Any(predicate func(T) bool) FailureMessage {
for _, v := range x.Got {
if predicate(v) {
Expand All @@ -113,7 +113,7 @@ func (x FluentSlice[T]) Any(predicate func(T) bool) FailureMessage {
return FailureMessage(fmt.Sprintf("none item does meet the predicate criteria\ngot: %+v", x.Got))
}

// All tests if all of the slice's items meets the predicate criteria.
// All tests if all of the slice's items meet the predicate criteria.
func (x FluentSlice[T]) All(predicate func(T) bool) FailureMessage {
for _, v := range x.Got {
if !predicate(v) {
Expand All @@ -123,7 +123,7 @@ func (x FluentSlice[T]) All(predicate func(T) bool) FailureMessage {
return ""
}

// None tests if all of the slice's item does not meet the predicate criteria.
// None tests if none of the slice's items meets the predicate criteria.
func (x FluentSlice[T]) None(predicate func(T) bool) FailureMessage {
for _, v := range x.Got {
if predicate(v) {
Expand All @@ -132,3 +132,12 @@ func (x FluentSlice[T]) None(predicate func(T) bool) FailureMessage {
}
return ""
}

// Len tests the length of the slice.
func (x FluentSlice[T]) Len(want int) FailureMessage {
gotLen := len(x.Got)
if gotLen != want {
return FailureMessage(fmt.Sprintf("has different length\ngot: %+v\nlen: %v\nwant: %v", x.Got, gotLen, want))
}
return ""
}
11 changes: 11 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,15 @@ func TestSlice(t *testing.T) {
assertFailed(t, got, "an item meets the predicate criteria")
})
})

t.Run("Len", func(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
got := verify.Slice(list).Len(len(list))
assertPassed(t, got)
})
t.Run("Failed", func(t *testing.T) {
got := verify.Slice(list).Len(10)
assertFailed(t, got, "has different length")
})
})
}
9 changes: 9 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,12 @@ func (x FluentString[T]) NotMatchRegex(regex *regexp.Regexp) FailureMessage {
}
return FailureMessage(fmt.Sprintf("the string value matches the regular expression\ngot: %q\nregex: %s", x.Got, regex.String()))
}

// Len tests the length of the string.
func (x FluentString[T]) Len(want int) FailureMessage {
gotLen := len(x.Got)
if gotLen != want {
return FailureMessage(fmt.Sprintf("has different length\ngot: %+v\nlen: %v\nwant: %v", x.Got, gotLen, want))
}
return ""
}
11 changes: 11 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ func TestString(t *testing.T) {
})
})

t.Run("Len", func(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
got := verify.String("abc").Len(3)
assertPassed(t, got)
})
t.Run("Failed", func(t *testing.T) {
got := verify.String("abc").Len(10)
assertFailed(t, got, "has different length")
})
})

t.Run("has assertions from Ordered, Obj, Any", func(t *testing.T) {
want := "text"
got := verify.String(want).FluentOrdered.FluentObj.FluentAny.Got // type embedding done properly
Expand Down
Loading