github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/writer_test.go (about) 1 package internal_test 2 3 import ( 4 . "github.com/onsi/ginkgo" 5 . "github.com/onsi/gomega" 6 7 "github.com/onsi/ginkgo/internal" 8 "github.com/onsi/gomega/gbytes" 9 ) 10 11 var _ = Describe("Writer", func() { 12 var writer *internal.Writer 13 var out *gbytes.Buffer 14 15 BeforeEach(func() { 16 out = gbytes.NewBuffer() 17 writer = internal.NewWriter(out) 18 }) 19 20 Context("when configured to WriterModeStreamAndBuffer (the default setting)", func() { 21 It("should stream directly to the passed in writer", func() { 22 writer.Write([]byte("foo")) 23 Ω(out).Should(gbytes.Say("foo")) 24 }) 25 26 It("does also stores the bytes", func() { 27 writer.Write([]byte("foo")) 28 Ω(out).Should(gbytes.Say("foo")) 29 Ω(string(writer.Bytes())).Should(Equal("foo")) 30 }) 31 }) 32 33 Context("when configured to WriterModeBufferOnly", func() { 34 BeforeEach(func() { 35 writer.SetMode(internal.WriterModeBufferOnly) 36 }) 37 38 It("should not write to the passed in writer", func() { 39 writer.Write([]byte("foo")) 40 Ω(out).ShouldNot(gbytes.Say("foo")) 41 }) 42 43 Describe("Bytes()", func() { 44 BeforeEach(func() { 45 writer.Write([]byte("foo")) 46 }) 47 48 It("returns all that's been written so far", func() { 49 Ω(writer.Bytes()).Should(Equal([]byte("foo"))) 50 }) 51 52 It("clears when told to truncate", func() { 53 writer.Truncate() 54 Ω(writer.Bytes()).Should(BeEmpty()) 55 writer.Write([]byte("bar")) 56 Ω(writer.Bytes()).Should(Equal([]byte("bar"))) 57 }) 58 }) 59 }) 60 61 Describe("Teeing to additional writers", func() { 62 var tee1, tee2 *gbytes.Buffer 63 BeforeEach(func() { 64 tee1 = gbytes.NewBuffer() 65 tee2 = gbytes.NewBuffer() 66 }) 67 68 Context("when told to tee to additional writers", func() { 69 BeforeEach(func() { 70 writer.TeeTo(tee1) 71 writer.TeeTo(tee2) 72 }) 73 74 It("tees to all registered tee writers", func() { 75 writer.Print("hello") 76 Ω(string(tee1.Contents())).Should(Equal("hello")) 77 Ω(string(tee2.Contents())).Should(Equal("hello")) 78 }) 79 80 Context("when told to clear tee writers", func() { 81 BeforeEach(func() { 82 writer.ClearTeeWriters() 83 }) 84 85 It("stops teeing to said writers", func() { 86 writer.Print("goodbye") 87 Ω(tee1.Contents()).Should(BeEmpty()) 88 Ω(tee2.Contents()).Should(BeEmpty()) 89 }) 90 }) 91 92 }) 93 }) 94 95 Describe("Convenience print methods", func() { 96 It("can Print", func() { 97 writer.Print("foo", "baz", " ", "bizzle") 98 Ω(string(out.Contents())).Should(Equal("foobaz bizzle")) 99 }) 100 101 It("can Println", func() { 102 writer.Println("foo", "baz", " ", "bizzle") 103 Ω(string(out.Contents())).Should(Equal("foo baz bizzle\n")) 104 }) 105 106 It("can Printf", func() { 107 writer.Printf("%s%d - %s\n", "foo", 17, "bar") 108 Ω(string(out.Contents())).Should(Equal("foo17 - bar\n")) 109 }) 110 }) 111 })