github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/terminal/table_test.go (about)

     1  package terminal_test
     2  
     3  import (
     4  	. "github.com/cloudfoundry/cli/cf/terminal"
     5  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
     6  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("Table", func() {
    12  	var (
    13  		ui    *testterm.FakeUI
    14  		table Table
    15  	)
    16  
    17  	BeforeEach(func() {
    18  		ui = &testterm.FakeUI{}
    19  		table = NewTable(ui, []string{"watashi", "no", "atama!"})
    20  	})
    21  
    22  	It("prints the header", func() {
    23  		table.Print()
    24  		Expect(ui.Outputs).To(ContainSubstrings(
    25  			[]string{"watashi", "no", "atama!"},
    26  		))
    27  	})
    28  
    29  	It("prints format string literals as strings", func() {
    30  		table.Add("cloak %s", "and", "dagger")
    31  		table.Print()
    32  
    33  		Expect(ui.Outputs).To(ContainSubstrings(
    34  			[]string{"cloak %s", "and", "dagger"},
    35  		))
    36  	})
    37  
    38  	It("prints all the rows you give it", func() {
    39  		table.Add("something", "and", "nothing")
    40  		table.Print()
    41  		Expect(ui.Outputs).To(ContainSubstrings(
    42  			[]string{"something", "and", "nothing"},
    43  		))
    44  	})
    45  
    46  	Describe("adding rows to be printed later", func() {
    47  		It("prints them when you call Print()", func() {
    48  			table.Add("a", "b", "c")
    49  			table.Add("passed", "to", "print")
    50  			table.Print()
    51  
    52  			Expect(ui.Outputs).To(ContainSubstrings(
    53  				[]string{"a", "b", "c"},
    54  			))
    55  		})
    56  
    57  		It("flushes previously added rows and then outputs passed rows", func() {
    58  			table.Add("a", "b", "c")
    59  			table.Add("passed", "to", "print")
    60  			table.Print()
    61  
    62  			Expect(ui.Outputs).To(ContainSubstrings(
    63  				[]string{"watashi", "no", "atama!"},
    64  				[]string{"a", "b", "c"},
    65  				[]string{"passed", "to", "print"},
    66  			))
    67  		})
    68  
    69  		It("flushes the buffer of rows when you call print", func() {
    70  			table.Add("a", "b", "c")
    71  			table.Add("passed", "to", "print")
    72  			table.Print()
    73  			ui.ClearOutputs()
    74  
    75  			table.Print()
    76  			Expect(ui.Outputs).To(BeEmpty())
    77  		})
    78  	})
    79  
    80  	Describe("aligning columns", func() {
    81  		It("aligns rows to the header when the header is longest", func() {
    82  			table.Add("a", "b", "c")
    83  			table.Print()
    84  
    85  			Expect(ui.Outputs).To(ContainSubstrings(
    86  				[]string{"watashi   no   atama!"},
    87  				[]string{"a         b    c"},
    88  			))
    89  		})
    90  
    91  		It("aligns rows to the longest row provided", func() {
    92  			table.Add("x", "y", "z")
    93  			table.Add("something", "something", "darkside")
    94  			table.Print()
    95  
    96  			Expect(ui.Outputs).To(ContainSubstrings(
    97  				[]string{"watashi     no          atama!"},
    98  				[]string{"x           y           z"},
    99  				[]string{"something   something   darkside"},
   100  			))
   101  		})
   102  
   103  		It("aligns rows to the longest row provided when there are multibyte characters present", func() {
   104  			table.Add("x", "ÿ", "z")
   105  			table.Add("something", "something", "darkside")
   106  			table.Print()
   107  
   108  			Expect(ui.Outputs).To(ContainSubstrings(
   109  				[]string{"watashi     no          atama!"},
   110  				[]string{"x           ÿ           z"},
   111  				[]string{"something   something   darkside"},
   112  			))
   113  		})
   114  	})
   115  })