github.com/Thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/util/ui/table_test.go (about)

     1  package ui_test
     2  
     3  import (
     4  	"strings"
     5  
     6  	"code.cloudfoundry.org/cli/util/configv3"
     7  	. "code.cloudfoundry.org/cli/util/ui"
     8  	"code.cloudfoundry.org/cli/util/ui/uifakes"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  )
    13  
    14  var _ = Describe("Table", func() {
    15  	var (
    16  		ui         *UI
    17  		fakeConfig *uifakes.FakeConfig
    18  		out        *Buffer
    19  		errBuff    *Buffer
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeConfig = new(uifakes.FakeConfig)
    24  		fakeConfig.ColorEnabledReturns(configv3.ColorEnabled)
    25  
    26  		var err error
    27  		ui, err = NewUI(fakeConfig)
    28  		Expect(err).NotTo(HaveOccurred())
    29  
    30  		out = NewBuffer()
    31  		ui.Out = out
    32  		ui.OutForInteration = out
    33  		errBuff = NewBuffer()
    34  		ui.Err = errBuff
    35  	})
    36  
    37  	Describe("DisplayKeyValueTable", func() {
    38  		JustBeforeEach(func() {
    39  			ui.DisplayKeyValueTable(" ",
    40  				[][]string{
    41  					{"wut0:", ""},
    42  					{"wut1:", "hi hi"},
    43  					nil,
    44  					[]string{},
    45  					{"wut2:", strings.Repeat("a", 9)},
    46  					{"wut3:", "hi hi " + strings.Repeat("a", 9)},
    47  					{"wut4:", strings.Repeat("a", 15) + " " + strings.Repeat("b", 15)},
    48  				},
    49  				2)
    50  		})
    51  
    52  		Context("in a TTY", func() {
    53  			BeforeEach(func() {
    54  				ui.IsTTY = true
    55  				ui.TerminalWidth = 20
    56  			})
    57  
    58  			It("displays a table with the last column wrapping according to width", func() {
    59  				Expect(out).To(Say(" wut0:  \n"))
    60  				Expect(out).To(Say(" wut1:  hi hi\n"))
    61  				Expect(out).To(Say(" wut2:  %s\n", strings.Repeat("a", 9)))
    62  				Expect(out).To(Say(" wut3:  hi hi\n"))
    63  				Expect(out).To(Say("        %s\n", strings.Repeat("a", 9)))
    64  				Expect(out).To(Say(" wut4:  %s\n", strings.Repeat("a", 15)))
    65  				Expect(out).To(Say("        %s\n", strings.Repeat("b", 15)))
    66  			})
    67  		})
    68  	})
    69  
    70  	Describe("DisplayTableWithHeader", func() {
    71  		It("makes the first row bold", func() {
    72  			ui.DisplayTableWithHeader(" ",
    73  				[][]string{
    74  					{"", "header1", "header2", "header3"},
    75  					{"#0", "data1", "data2", "data3"},
    76  				},
    77  				2)
    78  			Expect(out).To(Say("    \x1b\\[1mheader1\x1b\\[0m")) // Makes sure empty values are not bolded
    79  			Expect(out).To(Say("\x1b\\[1mheader2\x1b\\[0m"))
    80  			Expect(out).To(Say("\x1b\\[1mheader3\x1b\\[0m"))
    81  			Expect(out).To(Say("#0  data1    data2    data3"))
    82  		})
    83  	})
    84  })