Skip to content

Commit

Permalink
Add Len to FluentSlice, FluentMap, FluentString (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Feb 6, 2024
1 parent d816578 commit 18c484f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
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
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
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
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
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
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
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

0 comments on commit 18c484f

Please sign in to comment.