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

     1  package terminal_test
     2  
     3  import (
     4  	. "github.com/cloudfoundry/cli/cf/terminal"
     5  
     6  	io_helpers "github.com/cloudfoundry/cli/testhelpers/io"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("TeePrinter", func() {
    12  	var (
    13  		output  []string
    14  		printer *TeePrinter
    15  	)
    16  
    17  	Describe(".Print", func() {
    18  		var bucket *[]string
    19  
    20  		BeforeEach(func() {
    21  			bucket = &[]string{}
    22  
    23  			output = io_helpers.CaptureOutput(func() {
    24  				printer = NewTeePrinter()
    25  				printer.SetOutputBucket(bucket)
    26  				printer.Print("Hello ")
    27  				printer.Print("Mom!")
    28  			})
    29  		})
    30  
    31  		It("should delegate to fmt.Print", func() {
    32  			Expect(output[0]).To(Equal("Hello Mom!"))
    33  		})
    34  
    35  		It("should save the output to the slice", func() {
    36  			Expect((*bucket)[0]).To(Equal("Hello "))
    37  			Expect((*bucket)[1]).To(Equal("Mom!"))
    38  		})
    39  
    40  		It("should decolorize text", func() {
    41  			bucket = &[]string{}
    42  			io_helpers.CaptureOutput(func() {
    43  				printer = NewTeePrinter()
    44  				printer.SetOutputBucket(bucket)
    45  				printer.Print("hi " + EntityNameColor("foo"))
    46  			})
    47  
    48  			Expect((*bucket)[0]).To(Equal("hi foo"))
    49  		})
    50  	})
    51  
    52  	Describe(".Printf", func() {
    53  		var bucket *[]string
    54  
    55  		BeforeEach(func() {
    56  			bucket = &[]string{}
    57  
    58  			output = io_helpers.CaptureOutput(func() {
    59  				printer = NewTeePrinter()
    60  				printer.SetOutputBucket(bucket)
    61  				printer.Printf("Hello %s", "everybody")
    62  			})
    63  		})
    64  
    65  		It("should delegate to fmt.Printf", func() {
    66  			Expect(output[0]).To(Equal("Hello everybody"))
    67  		})
    68  
    69  		It("should save the output to the slice", func() {
    70  			Expect((*bucket)[0]).To(Equal("Hello everybody"))
    71  		})
    72  
    73  		It("should decolorize text", func() {
    74  			bucket = &[]string{}
    75  			io_helpers.CaptureOutput(func() {
    76  				printer = NewTeePrinter()
    77  				printer.SetOutputBucket(bucket)
    78  				printer.Printf("hi %s", EntityNameColor("foo"))
    79  			})
    80  
    81  			Expect((*bucket)[0]).To(Equal("hi foo"))
    82  		})
    83  	})
    84  
    85  	Describe(".Println", func() {
    86  		var bucket *[]string
    87  		BeforeEach(func() {
    88  			bucket = &[]string{}
    89  
    90  			output = io_helpers.CaptureOutput(func() {
    91  				printer = NewTeePrinter()
    92  				printer.SetOutputBucket(bucket)
    93  				printer.Println("Hello ", "everybody")
    94  			})
    95  		})
    96  
    97  		It("should delegate to fmt.Printf", func() {
    98  			Expect(output[0]).To(Equal("Hello everybody"))
    99  		})
   100  
   101  		It("should save the output to the slice", func() {
   102  			Expect((*bucket)[0]).To(Equal("Hello everybody"))
   103  		})
   104  
   105  		It("should decolorize text", func() {
   106  			bucket = &[]string{}
   107  			io_helpers.CaptureOutput(func() {
   108  				printer = NewTeePrinter()
   109  				printer.SetOutputBucket(bucket)
   110  				printer.Println("hi " + EntityNameColor("foo"))
   111  			})
   112  
   113  			Expect((*bucket)[0]).To(Equal("hi foo"))
   114  		})
   115  	})
   116  
   117  	Describe(".ForcePrintf", func() {
   118  		var bucket *[]string
   119  
   120  		BeforeEach(func() {
   121  			bucket = &[]string{}
   122  
   123  			output = io_helpers.CaptureOutput(func() {
   124  				printer = NewTeePrinter()
   125  				printer.SetOutputBucket(bucket)
   126  				printer.ForcePrintf("Hello %s", "everybody")
   127  			})
   128  		})
   129  
   130  		It("should delegate to fmt.Printf", func() {
   131  			Expect(output[0]).To(Equal("Hello everybody"))
   132  		})
   133  
   134  		It("should save the output to the slice", func() {
   135  			Expect((*bucket)[0]).To(Equal("Hello everybody"))
   136  		})
   137  
   138  		It("should decolorize text", func() {
   139  			bucket = &[]string{}
   140  			io_helpers.CaptureOutput(func() {
   141  				printer = NewTeePrinter()
   142  				printer.SetOutputBucket(bucket)
   143  				printer.Printf("hi %s", EntityNameColor("foo"))
   144  			})
   145  
   146  			Expect((*bucket)[0]).To(Equal("hi foo"))
   147  		})
   148  	})
   149  
   150  	Describe(".ForcePrintln", func() {
   151  		var bucket *[]string
   152  
   153  		BeforeEach(func() {
   154  			bucket = &[]string{}
   155  
   156  			output = io_helpers.CaptureOutput(func() {
   157  				printer = NewTeePrinter()
   158  				printer.SetOutputBucket(bucket)
   159  				printer.ForcePrintln("Hello ", "everybody")
   160  			})
   161  		})
   162  
   163  		It("should delegate to fmt.Printf", func() {
   164  			Expect(output[0]).To(Equal("Hello everybody"))
   165  		})
   166  
   167  		It("should save the output to the slice", func() {
   168  			Expect((*bucket)[0]).To(Equal("Hello everybody"))
   169  		})
   170  
   171  		It("should decolorize text", func() {
   172  			bucket = &[]string{}
   173  			io_helpers.CaptureOutput(func() {
   174  				printer = NewTeePrinter()
   175  				printer.SetOutputBucket(bucket)
   176  				printer.Println("hi " + EntityNameColor("foo"))
   177  			})
   178  
   179  			Expect((*bucket)[0]).To(Equal("hi foo"))
   180  		})
   181  	})
   182  
   183  	Describe(".SetOutputBucket", func() {
   184  		var bucket *[]string
   185  
   186  		output = io_helpers.CaptureOutput(func() {
   187  			bucket = &[]string{}
   188  			printer = NewTeePrinter()
   189  			printer.SetOutputBucket(bucket)
   190  			printer.ForcePrintf("Hello %s", "everybody")
   191  		})
   192  
   193  		It("sets the []string used to save the output", func() {
   194  			Expect((*bucket)[0]).To(Equal("Hello everybody"))
   195  		})
   196  
   197  		It("disables the output saving when set to nil", func() {
   198  			printer.SetOutputBucket(nil)
   199  			Expect((*bucket)[0]).To(Equal("Hello everybody"))
   200  		})
   201  	})
   202  
   203  	Describe("Pausing Output", func() {
   204  		var bucket *[]string
   205  
   206  		BeforeEach(func() {
   207  			bucket = &[]string{}
   208  
   209  			output = io_helpers.CaptureOutput(func() {
   210  				printer = NewTeePrinter()
   211  				printer.SetOutputBucket(bucket)
   212  				printer.DisableTerminalOutput(true)
   213  				printer.Print("Hello")
   214  				printer.Println("Mom!")
   215  				printer.Printf("Dad!")
   216  				printer.ForcePrint("Forced Hello")
   217  				printer.ForcePrintln("Forced Mom")
   218  				printer.ForcePrintf("Forced Dad")
   219  			})
   220  		})
   221  
   222  		It("should print only forced terminal output", func() {
   223  			Expect(output).To(Equal([]string{"Forced HelloForced Mom", "Forced Dad"}))
   224  		})
   225  
   226  		It("should still capture all output", func() {
   227  			Expect(*bucket).To(Equal([]string{"Hello", "Mom!", "Dad!", "Forced Hello", "Forced Mom", "Forced Dad"}))
   228  		})
   229  
   230  		Describe(".ResumeOutput", func() {
   231  			var bucket *[]string
   232  			BeforeEach(func() {
   233  				bucket = &[]string{}
   234  
   235  				output = io_helpers.CaptureOutput(func() {
   236  					printer.SetOutputBucket(bucket)
   237  					printer.DisableTerminalOutput(false)
   238  					printer.Print("Hello")
   239  					printer.Println("Mom!")
   240  					printer.Printf("Dad!")
   241  					printer.Println("Grandpa!")
   242  					printer.ForcePrint("ForcePrint")
   243  					printer.ForcePrintln("ForcePrintln")
   244  					printer.ForcePrintf("ForcePrintf")
   245  				})
   246  			})
   247  
   248  			It("should print all output", func() {
   249  				Expect(output).To(Equal([]string{"HelloMom!", "Dad!Grandpa!", "ForcePrintForcePrintln", "ForcePrintf"}))
   250  			})
   251  
   252  			It("should capture all output", func() {
   253  				Expect(*bucket).To(Equal([]string{"Hello", "Mom!", "Dad!", "Grandpa!", "ForcePrint", "ForcePrintln", "ForcePrintf"}))
   254  			})
   255  		})
   256  	})
   257  })