github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/terminal/colors/colors_test.go (about)

     1  package colors_test
     2  
     3  import (
     4  	"os"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  
     9  	"github.com/cloudfoundry-incubator/ltc/terminal/colors"
    10  )
    11  
    12  var _ = Describe("colors", func() {
    13  	Context("when $TERM is set", func() {
    14  		var previousTerm string
    15  
    16  		BeforeEach(func() {
    17  			previousTerm = os.Getenv("TERM")
    18  			Expect(os.Setenv("TERM", "xterm")).To(Succeed())
    19  		})
    20  
    21  		AfterEach(func() {
    22  			Expect(os.Setenv("TERM", previousTerm)).To(Succeed())
    23  		})
    24  
    25  		itShouldNotColorizeWhitespace := func(colorizer func(text string) string) {
    26  			It("returns a string without color codes when only whitespace is passed in", func() {
    27  				Expect(colorizer("  ")).To(Equal("  "))
    28  				Expect(colorizer("\n")).To(Equal("\n"))
    29  				Expect(colorizer("\t")).To(Equal("\t"))
    30  				Expect(colorizer("\r")).To(Equal("\r"))
    31  			})
    32  		}
    33  
    34  		Describe("Red", func() {
    35  			It("adds the red color code", func() {
    36  				Expect(colors.Red("ERROR NOT GOOD")).To(Equal("\x1b[91mERROR NOT GOOD\x1b[0m"))
    37  			})
    38  
    39  			itShouldNotColorizeWhitespace(colors.Red)
    40  		})
    41  
    42  		Describe("Green", func() {
    43  			It("adds the green color code", func() {
    44  				Expect(colors.Green("TOO GOOD")).To(Equal("\x1b[32mTOO GOOD\x1b[0m"))
    45  			})
    46  
    47  			itShouldNotColorizeWhitespace(colors.Green)
    48  		})
    49  
    50  		Describe("Cyan", func() {
    51  			It("adds the cyan color code", func() {
    52  				Expect(colors.Cyan("INFO")).To(Equal("\x1b[36mINFO\x1b[0m"))
    53  			})
    54  
    55  			itShouldNotColorizeWhitespace(colors.Cyan)
    56  		})
    57  
    58  		Describe("Yellow", func() {
    59  			It("adds the yellow color code", func() {
    60  				Expect(colors.Yellow("INFO")).To(Equal("\x1b[33mINFO\x1b[0m"))
    61  			})
    62  
    63  			itShouldNotColorizeWhitespace(colors.Yellow)
    64  		})
    65  
    66  		Describe("Gray", func() {
    67  			It("adds the gray color code", func() {
    68  				Expect(colors.Gray("INFO")).To(Equal("\x1b[90mINFO\x1b[0m"))
    69  			})
    70  
    71  			itShouldNotColorizeWhitespace(colors.Gray)
    72  		})
    73  
    74  		Describe("Bold", func() {
    75  			It("adds the bold color code", func() {
    76  				Expect(colors.Bold("Bold")).To(Equal("\x1b[1mBold\x1b[0m"))
    77  			})
    78  
    79  			itShouldNotColorizeWhitespace(colors.Bold)
    80  		})
    81  
    82  		Describe("PurpleUnderline", func() {
    83  			It("adds the purple underlined color code", func() {
    84  				Expect(colors.PurpleUnderline("PURPLE UNDERLINE")).To(Equal("\x1b[35;4mPURPLE UNDERLINE\x1b[0m"))
    85  			})
    86  
    87  			itShouldNotColorizeWhitespace(colors.PurpleUnderline)
    88  		})
    89  
    90  		Describe("NoColor", func() {
    91  			It("adds no color code", func() {
    92  				Expect(colors.NoColor("None")).To(Equal("\x1b[0mNone\x1b[0m"))
    93  			})
    94  
    95  			itShouldNotColorizeWhitespace(colors.NoColor)
    96  		})
    97  	})
    98  
    99  	Context("when $TERM is not set", func() {
   100  		var previousTerm string
   101  
   102  		BeforeEach(func() {
   103  			previousTerm = os.Getenv("TERM")
   104  			Expect(os.Unsetenv("TERM")).To(Succeed())
   105  		})
   106  
   107  		AfterEach(func() {
   108  			Expect(os.Setenv("TERM", previousTerm)).To(Succeed())
   109  		})
   110  
   111  		Describe("Red", func() {
   112  			It("adds the red color code", func() {
   113  				Expect(colors.Red("ERROR NOT GOOD")).To(Equal("ERROR NOT GOOD"))
   114  			})
   115  		})
   116  
   117  		Describe("Green", func() {
   118  			It("adds the green color code", func() {
   119  				Expect(colors.Green("TOO GOOD")).To(Equal("TOO GOOD"))
   120  			})
   121  		})
   122  
   123  		Describe("Cyan", func() {
   124  			It("adds the cyan color code", func() {
   125  				Expect(colors.Cyan("INFO")).To(Equal("INFO"))
   126  			})
   127  		})
   128  
   129  		Describe("Yellow", func() {
   130  			It("adds the yellow color code", func() {
   131  				Expect(colors.Yellow("INFO")).To(Equal("INFO"))
   132  			})
   133  		})
   134  
   135  		Describe("Gray", func() {
   136  			It("adds the gray color code", func() {
   137  				Expect(colors.Gray("INFO")).To(Equal("INFO"))
   138  			})
   139  		})
   140  
   141  		Describe("Bold", func() {
   142  			It("adds the bold color code", func() {
   143  				Expect(colors.Bold("Bold")).To(Equal("Bold"))
   144  			})
   145  		})
   146  
   147  		Describe("PurpleUnderline", func() {
   148  			It("adds the purple underlined color code", func() {
   149  				Expect(colors.PurpleUnderline("PURPLE UNDERLINE")).To(Equal("PURPLE UNDERLINE"))
   150  			})
   151  		})
   152  
   153  		Describe("NoColor", func() {
   154  			It("adds no color code", func() {
   155  				Expect(colors.NoColor("None")).To(Equal("None"))
   156  			})
   157  		})
   158  	})
   159  })