summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/ginkgo/internal/writer/writer_test.go
blob: 3e1d17c6d50b738b08dd1d9e03aea4ac6bfeb3bf (plain)
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
package writer_test

import (
	"github.com/onsi/gomega/gbytes"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/ginkgo/internal/writer"
	. "github.com/onsi/gomega"
)

var _ = Describe("Writer", func() {
	var writer *Writer
	var out *gbytes.Buffer

	BeforeEach(func() {
		out = gbytes.NewBuffer()
		writer = New(out)
	})

	It("should stream directly to the outbuffer by default", func() {
		writer.Write([]byte("foo"))
		Ω(out).Should(gbytes.Say("foo"))
	})

	It("should not emit the header when asked to DumpOutWitHeader", func() {
		writer.Write([]byte("foo"))
		writer.DumpOutWithHeader("my header")
		Ω(out).ShouldNot(gbytes.Say("my header"))
		Ω(out).Should(gbytes.Say("foo"))
	})

	Context("when told not to stream", func() {
		BeforeEach(func() {
			writer.SetStream(false)
		})

		It("should only write to the buffer when told to DumpOut", func() {
			writer.Write([]byte("foo"))
			Ω(out).ShouldNot(gbytes.Say("foo"))
			writer.DumpOut()
			Ω(out).Should(gbytes.Say("foo"))
		})

		It("should truncate the internal buffer when told to truncate", func() {
			writer.Write([]byte("foo"))
			writer.Truncate()
			writer.DumpOut()
			Ω(out).ShouldNot(gbytes.Say("foo"))

			writer.Write([]byte("bar"))
			writer.DumpOut()
			Ω(out).Should(gbytes.Say("bar"))
		})

		Describe("emitting a header", func() {
			Context("when the buffer has content", func() {
				It("should emit the header followed by the content", func() {
					writer.Write([]byte("foo"))
					writer.DumpOutWithHeader("my header")

					Ω(out).Should(gbytes.Say("my header"))
					Ω(out).Should(gbytes.Say("foo"))
				})
			})

			Context("when the buffer has no content", func() {
				It("should not emit the header", func() {
					writer.DumpOutWithHeader("my header")

					Ω(out).ShouldNot(gbytes.Say("my header"))
				})
			})
		})
	})
})