-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpenname_test.go
105 lines (83 loc) · 2.28 KB
/
penname_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
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 penname
import (
"fmt"
"io"
"testing"
"github.com/franela/goblin"
. "github.com/onsi/gomega"
)
func TestPenname(t *testing.T) {
g := goblin.Goblin(t)
RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })
g.Describe("Closing", func() {
var p *PenName
g.BeforeEach(func() {
p = &PenName{}
})
g.It("should record when it is closed", func() {
Expect(p.Closed).To(BeFalse())
err := p.Close()
Expect(err).NotTo(HaveOccurred())
Expect(p.Closed).To(BeTrue())
})
g.It("should return an error when told to", func() {
errText := "I wasn't able to close"
p.ReturnError(fmt.Errorf(errText))
err := p.Close()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(errText))
})
g.It("should function as a closer", func() {
err := func(c io.Closer) error {
return c.Close()
}(p)
Expect(err).NotTo(HaveOccurred())
p.ReturnError(fmt.Errorf("Some error"))
err = func(c io.Closer) error {
return c.Close()
}(p)
Expect(err).To(HaveOccurred())
})
g.It("should reset its closed state when told to", func() {
Expect(p.Closed).ToNot(BeTrue())
p.Close()
Expect(p.Closed).To(BeTrue())
p.Reset()
Expect(p.Closed).ToNot(BeTrue())
})
})
g.Describe("Writing", func() {
var p *PenName
g.BeforeEach(func() {
p = &PenName{}
})
g.It("should record what is written to it", func() {
t := "Nothing to see here move along"
n, err := p.Write([]byte(t))
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(len(t)))
Expect(p.Written()).To(Equal([]byte(t)))
})
g.It("should return an error when told to", func() {
errText := "Something went wrong"
p.ReturnError(fmt.Errorf(errText))
_, err := p.Write([]byte("I'm trying to do something"))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(errText))
})
g.It("should function as a writer", func() {
t := "Nothing to see here move along"
fmt.Fprint(p, t)
Expect(p.Written()).To(Equal([]byte(t)))
})
g.It("should reset what was written when told to", func() {
t := "Nothing to see here move along"
n, err := p.Write([]byte(t))
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(len(t)))
Expect(p.Written()).To(Equal([]byte(t)))
p.Reset()
Expect(p.Written()).To(Equal([]byte("")))
})
})
}