github.com/cloudfoundry/cli@v7.1.0+incompatible/util/ui/ui_for_app_test.go (about) 1 package ui_test 2 3 import ( 4 "code.cloudfoundry.org/cli/util/configv3" 5 . "code.cloudfoundry.org/cli/util/ui" 6 "code.cloudfoundry.org/cli/util/ui/uifakes" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/gbytes" 10 ) 11 12 var _ = Describe("UI", func() { 13 var ( 14 ui *UI 15 fakeConfig *uifakes.FakeConfig 16 out *Buffer 17 ) 18 19 BeforeEach(func() { 20 fakeConfig = new(uifakes.FakeConfig) 21 fakeConfig.ColorEnabledReturns(configv3.ColorEnabled) 22 23 var err error 24 ui, err = NewUI(fakeConfig) 25 Expect(err).NotTo(HaveOccurred()) 26 27 out = NewBuffer() 28 ui.Out = out 29 ui.Err = NewBuffer() 30 }) 31 32 Describe("DisplayInstancesTableForApp", func() { 33 Context("in english", func() { 34 It("displays a table with red coloring for down and crashed", func() { 35 ui.DisplayInstancesTableForApp([][]string{ 36 {"", "header1", "header2", "header3"}, 37 {"#0", "starting", "val1", "val2"}, 38 {"#1", "down", "val1", "val2"}, 39 {"#2", "crashed", "val1", "val2"}, 40 }) 41 42 Expect(ui.Out).To(Say("\x1b\\[1mheader1\x1b\\[0m\\s+\x1b\\[1mheader2\x1b\\[0m\\s+\x1b\\[1mheader3\x1b\\[0m")) // Makes sure empty values are not bolded 43 Expect(ui.Out).To(Say(`#0\s+starting\s+val1\s+val2`)) 44 Expect(ui.Out).To(Say("#1\\s+\x1b\\[31;1mdown\x1b\\[0m\\s+val1\\s+val2")) 45 Expect(ui.Out).To(Say("#2\\s+\x1b\\[31;1mcrashed\x1b\\[0m\\s+val1\\s+val2")) 46 }) 47 }) 48 49 Context("in a non-english language", func() { 50 BeforeEach(func() { 51 fakeConfig.LocaleReturns("fr-FR") 52 53 var err error 54 ui, err = NewUI(fakeConfig) 55 Expect(err).NotTo(HaveOccurred()) 56 ui.Out = NewBuffer() 57 ui.Err = NewBuffer() 58 }) 59 60 It("displays a table with red coloring for down and crashed", func() { 61 ui.DisplayInstancesTableForApp([][]string{ 62 {"", "header1", "header2", "header3"}, 63 {"#0", ui.TranslateText("starting"), "val1", "val2"}, 64 {"#1", ui.TranslateText("down"), "val1", "val2"}, 65 {"#2", ui.TranslateText("crashed"), "val1", "val2"}, 66 }) 67 68 Expect(ui.Out).To(Say("\x1b\\[1mheader1\x1b\\[0m\\s+\x1b\\[1mheader2\x1b\\[0m\\s+\x1b\\[1mheader3\x1b\\[0m")) // Makes sure empty values are not bolded 69 Expect(ui.Out).To(Say(`#0\s+%s\s+val1\s+val2`, ui.TranslateText("starting"))) 70 Expect(ui.Out).To(Say("#1\\s+\x1b\\[31;1m%s\x1b\\[0m\\s+val1\\s+val2", ui.TranslateText("down"))) 71 Expect(ui.Out).To(Say("#2\\s+\x1b\\[31;1m%s\x1b\\[0m\\s+val1\\s+val2", ui.TranslateText("crashed"))) 72 }) 73 }) 74 }) 75 76 Describe("DisplayKeyValueTableForApp", func() { 77 When("the app is running properly", func() { 78 BeforeEach(func() { 79 ui.DisplayKeyValueTableForApp([][]string{ 80 {"name:", "dora"}, 81 {"requested state:", "started"}, 82 {"instances:", "1/1"}, 83 }) 84 }) 85 86 It("displays a table with the no change in coloring", func() { 87 Expect(ui.Out).To(Say("name: dora\n")) 88 Expect(ui.Out).To(Say("requested state: started\n")) 89 Expect(ui.Out).To(Say("instances: 1/1\n")) 90 }) 91 }) 92 93 When("the app is stopped and has 0 instances", func() { 94 Context("in english", func() { 95 BeforeEach(func() { 96 ui.DisplayKeyValueTableForApp([][]string{ 97 {"name:", "dora"}, 98 {"requested state:", "stopped"}, 99 {"instances:", "0/1"}, 100 }) 101 }) 102 103 It("displays a table with the no change in coloring", func() { 104 Expect(ui.Out).To(Say("name: dora\n")) 105 Expect(ui.Out).To(Say("requested state: stopped\n")) 106 Expect(ui.Out).To(Say("instances: 0/1\n")) 107 }) 108 }) 109 110 Context("in a non-english language", func() { 111 BeforeEach(func() { 112 fakeConfig.LocaleReturns("fr-FR") 113 114 var err error 115 ui, err = NewUI(fakeConfig) 116 Expect(err).NotTo(HaveOccurred()) 117 ui.Out = NewBuffer() 118 ui.Err = NewBuffer() 119 120 ui.DisplayKeyValueTableForApp([][]string{ 121 {"name:", "dora"}, 122 {"requested state:", ui.TranslateText("stopped")}, 123 {"instances:", "0/1"}, 124 }) 125 }) 126 127 It("displays a table with the no change in coloring", func() { 128 Expect(ui.Out).To(Say("name: dora\n")) 129 Expect(ui.Out).To(Say("requested state: %s\n", ui.TranslateText("stopped"))) 130 Expect(ui.Out).To(Say("instances: 0/1\n")) 131 }) 132 }) 133 }) 134 135 When("the app is not stopped and has 0 instances", func() { 136 Context("in english", func() { 137 BeforeEach(func() { 138 ui.DisplayKeyValueTableForApp([][]string{ 139 {"name:", "dora"}, 140 {"requested state:", "running"}, 141 {"instances:", "0/1"}, 142 }) 143 }) 144 145 It("displays a table with requested state and instances in red", func() { 146 Expect(ui.Out).To(Say("name: dora\n")) 147 Expect(ui.Out).To(Say("requested state: \x1b\\[31;1mrunning\x1b\\[0m\n")) 148 Expect(ui.Out).To(Say("instances: \x1b\\[31;1m0/1\x1b\\[0m\n")) 149 }) 150 }) 151 152 Context("in a non-english language", func() { 153 BeforeEach(func() { 154 fakeConfig.LocaleReturns("fr-FR") 155 156 var err error 157 ui, err = NewUI(fakeConfig) 158 Expect(err).NotTo(HaveOccurred()) 159 ui.Out = NewBuffer() 160 ui.Err = NewBuffer() 161 162 ui.DisplayKeyValueTableForApp([][]string{ 163 {"name:", "dora"}, 164 {"requested state:", ui.TranslateText("running")}, 165 {"instances:", "0/1"}, 166 }) 167 }) 168 169 It("displays a table with requested state and instances in red", func() { 170 Expect(ui.Out).To(Say("name: dora\n")) 171 Expect(ui.Out).To(Say("requested state: \x1b\\[31;1m%s\x1b\\[0m\n", ui.TranslateText("running"))) 172 Expect(ui.Out).To(Say("instances: \x1b\\[31;1m0/1\x1b\\[0m\n")) 173 }) 174 }) 175 }) 176 }) 177 })