-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyml-reader_test.go
More file actions
105 lines (95 loc) · 2.42 KB
/
yml-reader_test.go
File metadata and controls
105 lines (95 loc) · 2.42 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package ymlreader
import (
"context"
"io"
"os"
"testing"
)
func TestUnmarshalFile(t *testing.T) {
tt := []struct {
Name string
Path string
MustError bool
}{
{Name: "Valid path to file", Path: "./examples/simple.yml", MustError: false},
{Name: "Invalid path to file", Path: "./examples/no_exists.yml", MustError: true},
{Name: "Broken file", Path: "./examples/broken.yml", MustError: true},
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
_, err := UnmarshalFile(tc.Path)
if tc.MustError && err == nil {
t.Errorf("failed to unmarshal: %s", err)
}
})
}
}
func TestNewFromFile(t *testing.T) {
tt := []struct {
Name string
Path string
MustError bool
}{
{Name: "Valid path to file", Path: "./examples/simple.yml", MustError: false},
{Name: "Invalid path to file", Path: "./examples/no_exists.yml", MustError: true},
// New has not run unmarshal, so broken file is NOT necessary.
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
_, err := NewFromFile(tc.Path)
if tc.MustError && err == nil {
t.Errorf("failed to unmarshal: %s", err)
}
})
}
}
func TestYMLReader_Close(t *testing.T) {
t.Run("YMLReader is not init", func(t *testing.T) {
err := (*YMLReader)(nil).Close()
if err == nil {
t.Errorf("must be failed: %s", "yml reader: is not initialized")
}
})
t.Run("YMLReader is init", func(t *testing.T) {
y, _ := NewFromFile("./examples/simple.yml")
err := y.Close()
if err != nil {
t.Errorf("failed to clode: %s", err)
}
})
}
func TestNewReader(t *testing.T) {
validReader, _ := os.Open("./examples/simple.yml")
defer validReader.Close()
tt := []struct {
Name string
reader io.ReadCloser
MustError bool
}{
{Name: "Valid reader", reader: validReader, MustError: false},
{Name: "Invalid path to file", reader: nil, MustError: true},
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
_, err := NewFromReader(tc.reader)
if tc.MustError && err == nil {
t.Errorf("failed to unmarshal: %s", err)
}
})
}
}
func TestYMLReader_StartRead(t *testing.T) {
validReader, _ := os.Open("./examples/simple.yml")
defer validReader.Close()
y, _ := NewFromReader(validReader)
err := y.StartRead(context.TODO())
if err != nil {
t.Errorf("failed to read: %s", err)
}
if y.Len() != 1 {
t.Errorf("length of offers must be 1")
}
if y.ReadCount() != 1 {
t.Errorf("read count must be 1")
}
}