1package ui_test
2
3import (
4	"bytes"
5
6	boshlog "github.com/cloudfoundry/bosh-utils/logger"
7	. "github.com/onsi/ginkgo"
8	. "github.com/onsi/gomega"
9
10	. "github.com/cloudfoundry/bosh-cli/ui"
11	fakeui "github.com/cloudfoundry/bosh-cli/ui/fakes"
12	. "github.com/cloudfoundry/bosh-cli/ui/table"
13)
14
15var _ = Describe("IndentingUI", func() {
16	var (
17		uiOut, uiErr *bytes.Buffer
18		parentFakeUI *fakeui.FakeUI
19		parentUI     UI
20		ui           UI
21	)
22
23	BeforeEach(func() {
24		uiOut = bytes.NewBufferString("")
25		uiErr = bytes.NewBufferString("")
26
27		logger := boshlog.NewLogger(boshlog.LevelNone)
28		parentUI = NewWriterUI(uiOut, uiErr, logger)
29		parentFakeUI = &fakeui.FakeUI{}
30	})
31
32	JustBeforeEach(func() {
33		ui = NewIndentingUI(parentUI)
34	})
35
36	Describe("ErrorLinef", func() {
37		It("delegates to the parent UI with an indent", func() {
38			ui.ErrorLinef("fake-error-line")
39			Expect(uiErr.String()).To(ContainSubstring("  fake-error-line\n"))
40			Expect(uiOut.String()).To(BeEmpty())
41		})
42	})
43
44	Describe("PrintLinef", func() {
45		It("delegates to the parent UI with an indent", func() {
46			ui.PrintLinef("fake-line")
47			Expect(uiOut.String()).To(ContainSubstring("  fake-line\n"))
48			Expect(uiErr.String()).To(BeEmpty())
49		})
50	})
51
52	Describe("BeginLinef", func() {
53		It("delegates to the parent UI with an indent", func() {
54			ui.BeginLinef("fake-start")
55			Expect(uiOut.String()).To(ContainSubstring("  fake-start"))
56			Expect(uiErr.String()).To(BeEmpty())
57		})
58	})
59
60	Describe("EndLinef", func() {
61		It("delegates to the parent UI", func() {
62			ui.EndLinef("fake-end")
63			Expect(uiOut.String()).To(ContainSubstring("fake-end\n"))
64			Expect(uiErr.String()).To(BeEmpty())
65		})
66	})
67
68	Describe("PrintBlock", func() {
69		BeforeEach(func() {
70			parentUI = parentFakeUI
71		})
72
73		It("delegates to the parent UI", func() {
74			ui.PrintBlock([]byte("block"))
75			Expect(parentFakeUI.Blocks).To(Equal([]string{"block"}))
76		})
77	})
78
79	Describe("PrintErrorBlock", func() {
80		BeforeEach(func() {
81			parentUI = parentFakeUI
82		})
83
84		It("delegates to the parent UI", func() {
85			ui.PrintBlock([]byte("block"))
86			Expect(parentFakeUI.Blocks).To(Equal([]string{"block"}))
87		})
88	})
89
90	Describe("PrintTable", func() {
91		BeforeEach(func() {
92			parentUI = parentFakeUI
93		})
94
95		It("delegates to the parent UI", func() {
96			table := Table{
97				Content: "things",
98				Header:  []Header{NewHeader("header1")},
99			}
100
101			ui.PrintTable(table)
102
103			Expect(parentFakeUI.Table).To(Equal(table))
104		})
105	})
106
107	Describe("PrintTableFiltered", func() {
108		BeforeEach(func() {
109			parentUI = parentFakeUI
110		})
111
112		It("delegates to the parent UI", func() {
113			table := Table{
114				Content: "things",
115				Header:  []Header{NewHeader("header1")},
116			}
117			filteredHeader := []Header{}
118
119			ui.PrintTableFiltered(table, filteredHeader)
120
121			Expect(parentFakeUI.Table).To(Equal(table))
122		})
123	})
124
125	Describe("IsInteractive", func() {
126		BeforeEach(func() {
127			parentUI = parentFakeUI
128		})
129
130		It("delegates to the parent UI", func() {
131			parentFakeUI.Interactive = true
132			Expect(ui.IsInteractive()).To(BeTrue())
133
134			parentFakeUI.Interactive = false
135			Expect(ui.IsInteractive()).To(BeFalse())
136		})
137	})
138
139	Describe("Flush", func() {
140		BeforeEach(func() {
141			parentUI = parentFakeUI
142		})
143
144		It("delegates to the parent UI", func() {
145			ui.Flush()
146			Expect(parentFakeUI.Flushed).To(BeTrue())
147		})
148	})
149})
150