forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter_test.go
95 lines (78 loc) · 2.68 KB
/
writer_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
package scribe_test
import (
"bytes"
"testing"
"github.com/paketo-buildpacks/packit/scribe"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testWriter(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect
context("Writer", func() {
var (
buffer *bytes.Buffer
writer scribe.Writer
)
it.Before(func() {
buffer = bytes.NewBuffer(nil)
writer = scribe.NewWriter(buffer)
})
context("Write", func() {
it("prints to the writer", func() {
_, err := writer.Write([]byte("some-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("some-text"))
})
context("when the writer has a color", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.BlueColor))
})
it("prints to the writer with the correct color codes", func() {
_, err := writer.Write([]byte("some-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("\x1b[0;38;5;4msome-text\x1b[0m"))
})
})
context("when the writer has an indent", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithIndent(2))
})
it("prints to the writer with the correct indentation", func() {
_, err := writer.Write([]byte("some-text\nother-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal(" some-text\n other-text"))
})
})
context("when the writer has a return prefix", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.RedColor), scribe.WithIndent(2))
})
it("prints to the writer with the correct indentation", func() {
_, err := writer.Write([]byte("\rsome-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("\r\x1b[0;38;5;1m some-text\x1b[0m"))
})
})
context("when the writer has a newline suffix", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.RedColor), scribe.WithIndent(2))
})
it("prints to the writer with the correct indentation", func() {
_, err := writer.Write([]byte("some-text\n"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("\x1b[0;38;5;1m some-text\x1b[0m\n"))
})
})
context("when the input has a percent symbol", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.MagentaColor))
})
it("prints to the writer with the correct indentation", func() {
_, err := writer.Write([]byte("some-%"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("\x1b[0;38;5;5msome-%\x1b[0m"))
})
})
})
})
}