-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_test.go
More file actions
64 lines (53 loc) · 1.68 KB
/
Copy pathdecoder_test.go
File metadata and controls
64 lines (53 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package yamlpath_test
import (
"io"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"rodusek.dev/pkg/yamlpath"
"rodusek.dev/pkg/yamlpath/internal/yamlconv"
"rodusek.dev/pkg/yamlpath/internal/yamltest"
)
func TestDecoder_Decode_DecodesSequenceAndReturnsEOF(t *testing.T) {
type object struct {
Name string `yaml:"name"`
Age int `yaml:"age"`
}
collection := yamlpath.Collection{
yamltest.MustParseNode(`{"name": "Alice", "age": 30}`),
yamltest.MustParseNode(`{"name": "Bob", "age": 25}`),
}
sut := collection.Decoder()
var got object
if err := sut.Decode(&got); err != nil {
t.Fatalf("Decoder.Decoder() = error %v, want nil", err)
}
if want := (object{Name: "Alice", Age: 30}); !cmp.Equal(got, want) {
t.Errorf("Decoder.Decoder() = %v, want %v", got, want)
}
if err := sut.Decode(&got); err != nil {
t.Fatalf("Decoder.Decoder() = error %v, want nil", err)
}
if want := (object{Name: "Bob", Age: 25}); !cmp.Equal(got, want) {
t.Errorf("Decoder.Decoder() = %v, want %v", got, want)
}
// End of sequence
err := sut.Decode(&got)
if got, want := err, io.EOF; !cmp.Equal(got, want, cmpopts.EquateErrors()) {
t.Errorf("Decoder.Decoder() = error %v, want %v", got, want)
}
}
func TestDecoder_Decode_BadDecodingReturnsError(t *testing.T) {
collection := yamlpath.Collection{
yamlconv.String("hello"),
}
sut := collection.Decoder()
var got int
err := sut.Decode(&got)
if got, want := err, cmpopts.AnyError; !cmp.Equal(got, want, cmpopts.EquateErrors()) {
t.Fatalf("Decoder.Decoder() = error %v, want %v", got, want)
}
if got, want := got, 0; !cmp.Equal(got, want) {
t.Errorf("Decoder.Decoder() = %v, want %v", got, want)
}
}