-
What would be best way to asert more complex structure, for example: type Cursor struct {
ID int `msgpack:"i"`
Value Value `msgpack:"v,omitempty"`
}
type User struct {
ID int `json:"id,omitempty"`
Active bool `json:"active,omitempty"`
Username string `json:"username,omitempty"`
}
type UserEdge struct {
Node *User `json:"node"`
Cursor Cursor `json:"cursor"`
}
type UserConnection struct {
Edges []*UserEdge `json:"edges"`
PageInfo PageInfo `json:"pageInfo"`
TotalCount int `json:"totalCount"`
}
type PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
HasPreviousPage bool `json:"hasPreviousPage"`
StartCursor *Cursor `json:"startCursor"`
EndCursor *Cursor `json:"endCursor"`
} In given example I would like to:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hello, You can use You can also use You can use You can use Perhaps could you give us a literal of what you get with comments telling which fields you want to test, and which ones you want to ignore? I would be happy to demonstrate each case. Something like: PageInfo{
StartCursor: &Cursor{
ID: 123, // ← I want to test this field and ignore others
},
} Max. |
Beta Was this translation helpful? Give feedback.
-
package main_test
import (
"testing"
"github.com/maxatome/go-testdeep/td"
)
var got = &UserConnection{
Edges: []*UserEdge{ // <- I want to test structs at specific indexes in this slice
{
Node: &User{ // <- I want to test fields from this struct
ID: 21474836481,
Username: "user 1",
Active: true,
},
},
{
Node: &User{ // <- I want to test fields from this struct
ID: 21474836482,
Username: "user2",
Active: false,
},
},
},
}
func Test(t *testing.T) {
//
// With JSONPointer + JSON
td.Cmp(t, got,
//the index in slice --v
td.JSONPointer("/edges/1/node",
td.JSON(`
{
"id": 21474836482,
"username": "user2",
// "active" is omitted as false
}`)),
"With JSONPointer + JSON")
//
// JSONPointer to test Username field, so with SuperJSONOf
td.Cmp(t, got,
//the index in slice --v
td.JSONPointer("/edges/1/node",
td.SuperJSONOf(`{"username": "user2"}`)),
"With JSONPointer + SuperJSONOf")
//
// Smuggle now handles slice/array/map indexing as JSONPointer already does
// (available in master branch or future 1.10 release).
td.Cmp(t, got,
td.Smuggle("Edges[1].Node",
// The final expected User
&User{
ID: 21474836482,
Username: "user2",
Active: false,
}),
"With Smuggle")
//
// Smuggle to test Username field
td.Cmp(t, got,
td.Smuggle("Edges[1].Node",
// Only the Username field of User
td.Struct(&User{Username: "user2"}, nil)),
"With Smuggle + Struct")
} Note that Max. |
Beta Was this translation helpful? Give feedback.
Smuggle
can now handle maps/slices in its fields-path form, so for this specific case we can use eitherJSONPointer
orSmuggle
operators: