Repository: gavv/httpexpect
Pull request #195 (2022-12-08 18:37:25)

Create iter function for object
Issue: https://github.com/gavv/httpexpect/issues/167

This is my first contact with the project and I am a beginner in Go.

I hope I can help :)



coveralls: 
[![Coverage Status](https://coveralls.io/builds/54988976/badge)](https://coveralls.io/builds/54988976)

Coverage increased (+0.02%) to 92.725% when pulling **a2b3e58f35bf25d61bd0fe7ac4860b6a8db4450a on VacariGabriel:feature/167-iter-object** into **e9fbbe48037aced074c4370bc220ff5898332f95 on gavv:master**.

gavv: I've pushed small refactoring of `Array.Iter`: d9fe9eea2093b453e3d962eb9ced84d497a77456

Patch:
diff --git a/_examples/go.mod b/_examples/go.mod
index 5d1ba1b..0227c8c 100644
--- a/_examples/go.mod
+++ b/_examples/go.mod
@@ -5,7 +5,7 @@ go 1.17
 require (
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/fasthttp/websocket v1.5.0
-	github.com/gavv/httpexpect/v2 v2.6.1
+	github.com/gavv/httpexpect/v2 v2.7.0
 	github.com/gin-gonic/gin v1.8.1
 	github.com/gorilla/websocket v1.5.0
 	github.com/kataras/iris/v12 v12.1.8
diff --git a/_examples/go.sum b/_examples/go.sum
index 608e346..668cd26 100644
--- a/_examples/go.sum
+++ b/_examples/go.sum
@@ -47,8 +47,8 @@ github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga
 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
 github.com/gavv/httpexpect v2.0.0+incompatible h1:1X9kcRshkSKEjNJJxX9Y9mQ5BRfbxU5kORdjhlA1yX8=
 github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc=
-github.com/gavv/httpexpect/v2 v2.6.1 h1:vCoP1H++iiAV5VujGKECTLiJLtDNVAzdx9dm49DmSyE=
-github.com/gavv/httpexpect/v2 v2.6.1/go.mod h1:6ffPXr12lflKXEPjyjsbzg9n6dY1FchaG24VTyfAoFo=
+github.com/gavv/httpexpect/v2 v2.7.0 h1:13fBkFn9SzUGsoEc1Ipa5K+ojxsOtJc8QoqhojKqi3s=
+github.com/gavv/httpexpect/v2 v2.7.0/go.mod h1:jIj2f4rLediVaQK7rIH2EcU4W1ovjeSI8D0g85VJe9o=
 github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
 github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
 github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
@@ -384,6 +384,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
 gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
 gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/object.go b/object.go
index 860d69f..6472d6c 100644
--- a/object.go
+++ b/object.go
@@ -757,6 +757,35 @@ func (o *Object) ValueNotEqual(key string, value interface{}) *Object {
 	return o.NotValueEqual(key, value)
 }
 
+// Iter returns a new map of Values attached to object elements.
+//
+// Example:
+//
+// object := NewObject(t, map[string]interface{}{})
+//
+// for key, value := range object.Iter() {
+//	    value.Equal(object.value[key])
+// }
+func (o *Object) Iter() map[string]Value {
+	o.chain.enter("Iter()")
+	defer o.chain.leave()
+
+	if o.chain.failed() {
+		return map[string]Value{}
+	}
+
+	obj := make(map[string]Value)
+
+	for k, v := range o.value {
+		valueChain := o.chain.clone()
+		valueChain.replace("Iter[%q]", k)
+
+		obj[k] = *newValue(valueChain, v)
+	}
+
+	return obj
+}
+
 func (o *Object) containsKey(arg string) bool {
 	for k := range o.value {
 		if k == arg {


Test patch:
diff --git a/object_test.go b/object_test.go
index 2da61cb..5f1ec44 100644
--- a/object_test.go
+++ b/object_test.go
@@ -16,6 +16,7 @@ func TestObjectFailed(t *testing.T) {
 		assert.NotNil(t, value.Keys())
 		assert.NotNil(t, value.Values())
 		assert.NotNil(t, value.Value("foo"))
+		assert.NotNil(t, value.Iter())
 
 		value.Empty()
 		value.NotEmpty()
@@ -130,6 +131,13 @@ func TestObjectGetters(t *testing.T) {
 	assert.Equal(t, nil, value.Value("BAZ").Raw())
 	value.chain.assertFailed(t)
 	value.chain.reset()
+
+	it := value.Iter()
+	assert.Equal(t, 3, len(it))
+	assert.Equal(t, it["foo"].value, value.Value("foo").Raw())
+	assert.Equal(t, it["bar"].value, value.Value("bar").Raw())
+	value.chain.assertOK(t)
+	value.chain.reset()
 }
 
 func TestObjectEmpty(t *testing.T) {