Skip to content

Commit feae4e9

Browse files
committed
added json test
1 parent 3884d59 commit feae4e9

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

oop/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module oop
2+
3+
go 1.16
4+
5+
require github.com/stretchr/testify v1.7.0

oop/go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

oop/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ https://code.tutsplus.com/tutorials/lets-go-object-oriented-programming-in-golan
1313
import "fmt"
1414

1515
type Animal struct {
16+
Name string `json:"name"`
1617
lives int
1718
}
1819

@@ -69,7 +70,7 @@ func (*Cat) Meow() string {
6970
return "meow"
7071
}
7172

72-
// overriding:
73+
// Die overriding:
7374
func (c *Cat) Die() string {
7475
c.lives--
7576

oop/main_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestCatJSON(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
b []byte
15+
want Cat
16+
}{
17+
{
18+
b: []byte(`{"name":"Cheshire"}`),
19+
want: Cat{Animal: &Animal{Name: "Cheshire"}},
20+
},
21+
}
22+
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
var got Cat
26+
err := json.Unmarshal(tt.b, &got)
27+
require.NoError(t, err)
28+
assert.Equal(t, tt.want, got)
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)