-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patht.helpers_test.go
80 lines (69 loc) · 1.72 KB
/
t.helpers_test.go
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
package docxplate
import (
"bytes"
"errors"
"io"
"log"
"testing"
)
// Invalid input tests as valid tests are performed mainly in `t_docx_test.go`
type brokenReadCloser struct {
io.Reader
shouldReadFail bool
shouldCloseFail bool
}
func (brc *brokenReadCloser) Read(p []byte) (n int, err error) {
if brc.shouldReadFail {
return 0, errors.New("broken read error")
}
if brc.Reader == nil {
return 0, io.EOF
}
return brc.Reader.Read(p)
}
func (brc *brokenReadCloser) Close() error {
if brc.shouldCloseFail {
return errors.New("broken close error")
}
return nil
}
func TestReaderBytesInvalidCases(t *testing.T) {
// disable log output for tests
wr := log.Writer()
log.SetOutput(io.Discard)
defer log.SetOutput(wr)
t.Run("nil input", func(t *testing.T) {
var rdr io.ReadCloser = nil
result := readerBytes(rdr)
if result != nil {
t.Fatalf("Expected nil result, got: %v", result)
}
})
t.Run("broken reader", func(t *testing.T) {
rdr := &brokenReadCloser{shouldReadFail: true}
result := readerBytes(rdr)
if result != nil {
t.Fatalf("Expected nil result, got: %v", result)
}
})
t.Run("broken closer", func(t *testing.T) {
data := []byte("test data")
rdr := &brokenReadCloser{Reader: bytes.NewReader(data), shouldCloseFail: true}
result := readerBytes(rdr)
if result != nil {
t.Fatalf("Expected nil result, got: %v", result)
}
})
}
type invalidTestXMLStruct struct {
UnsupportedField complex128
}
func TestStructToXMLBytesError(t *testing.T) {
t.Run("invalid struct", func(t *testing.T) {
invalidStruct := invalidTestXMLStruct{UnsupportedField: complex(1, 2)}
result := structToXMLBytes(invalidStruct)
if result != nil {
t.Fatalf("Expected nil result, got: %v", result)
}
})
}