Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 18c484f

Browse files
authored
Add Len to FluentSlice, FluentMap, FluentString (#124)
1 parent d816578 commit 18c484f

7 files changed

+70
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

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

10+
### Added
11+
12+
- Add `Len` assertion for `string`, `[]T`, `map[K]V` types.
13+
1014
## [1.0.0](https://github.com/fluentassert/verify/releases/tag/v1.0.0) - 2023-04-05
1115

1216
This release contains breaking changes.

map.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (x FluentMap[K, V]) NotContainPair(k K, v V, opts ...cmp.Option) FailureMes
9090
return FailureMessage(fmt.Sprintf("contains the pair\ngot: %+v\nkey: %+v\nvalue: %+v", x.Got, k, v))
9191
}
9292

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

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

113-
// None tests if all of the slice's item does not meet the predicate criteria.
113+
// None tests if none of the map's pairs meets the predicate criteria.
114114
func (x FluentMap[K, V]) None(predicate func(K, V) bool) FailureMessage {
115115
for k, v := range x.Got {
116116
if predicate(k, v) {
@@ -119,3 +119,12 @@ func (x FluentMap[K, V]) None(predicate func(K, V) bool) FailureMessage {
119119
}
120120
return ""
121121
}
122+
123+
// Len tests the length of the map.
124+
func (x FluentMap[K, V]) Len(want int) FailureMessage {
125+
gotLen := len(x.Got)
126+
if gotLen != want {
127+
return FailureMessage(fmt.Sprintf("has different length\ngot: %+v\nlen: %v\nwant: %v", x.Got, gotLen, want))
128+
}
129+
return ""
130+
}

map_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,15 @@ func TestMap(t *testing.T) {
134134
assertFailed(t, got, "a pair meets the predicate criteria")
135135
})
136136
})
137+
138+
t.Run("Len", func(t *testing.T) {
139+
t.Run("Passed", func(t *testing.T) {
140+
got := verify.Map(dict).Len(len(dict))
141+
assertPassed(t, got)
142+
})
143+
t.Run("Failed", func(t *testing.T) {
144+
got := verify.Map(dict).Len(10)
145+
assertFailed(t, got, "has different length")
146+
})
147+
})
137148
}

slice.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (x FluentSlice[T]) NotContain(item T, opts ...cmp.Option) FailureMessage {
103103
return ""
104104
}
105105

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

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

126-
// None tests if all of the slice's item does not meet the predicate criteria.
126+
// None tests if none of the slice's items meets the predicate criteria.
127127
func (x FluentSlice[T]) None(predicate func(T) bool) FailureMessage {
128128
for _, v := range x.Got {
129129
if predicate(v) {
@@ -132,3 +132,12 @@ func (x FluentSlice[T]) None(predicate func(T) bool) FailureMessage {
132132
}
133133
return ""
134134
}
135+
136+
// Len tests the length of the slice.
137+
func (x FluentSlice[T]) Len(want int) FailureMessage {
138+
gotLen := len(x.Got)
139+
if gotLen != want {
140+
return FailureMessage(fmt.Sprintf("has different length\ngot: %+v\nlen: %v\nwant: %v", x.Got, gotLen, want))
141+
}
142+
return ""
143+
}

slice_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,15 @@ func TestSlice(t *testing.T) {
126126
assertFailed(t, got, "an item meets the predicate criteria")
127127
})
128128
})
129+
130+
t.Run("Len", func(t *testing.T) {
131+
t.Run("Passed", func(t *testing.T) {
132+
got := verify.Slice(list).Len(len(list))
133+
assertPassed(t, got)
134+
})
135+
t.Run("Failed", func(t *testing.T) {
136+
got := verify.Slice(list).Len(10)
137+
assertFailed(t, got, "has different length")
138+
})
139+
})
129140
}

string.go

+9
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,12 @@ func (x FluentString[T]) NotMatchRegex(regex *regexp.Regexp) FailureMessage {
116116
}
117117
return FailureMessage(fmt.Sprintf("the string value matches the regular expression\ngot: %q\nregex: %s", x.Got, regex.String()))
118118
}
119+
120+
// Len tests the length of the string.
121+
func (x FluentString[T]) Len(want int) FailureMessage {
122+
gotLen := len(x.Got)
123+
if gotLen != want {
124+
return FailureMessage(fmt.Sprintf("has different length\ngot: %+v\nlen: %v\nwant: %v", x.Got, gotLen, want))
125+
}
126+
return ""
127+
}

string_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ func TestString(t *testing.T) {
140140
})
141141
})
142142

143+
t.Run("Len", func(t *testing.T) {
144+
t.Run("Passed", func(t *testing.T) {
145+
got := verify.String("abc").Len(3)
146+
assertPassed(t, got)
147+
})
148+
t.Run("Failed", func(t *testing.T) {
149+
got := verify.String("abc").Len(10)
150+
assertFailed(t, got, "has different length")
151+
})
152+
})
153+
143154
t.Run("has assertions from Ordered, Obj, Any", func(t *testing.T) {
144155
want := "text"
145156
got := verify.String(want).FluentOrdered.FluentObj.FluentAny.Got // type embedding done properly

0 commit comments

Comments
 (0)