...

Source file src/github.com/onsi/ginkgo/v2/formatter/formatter_test.go

Documentation: github.com/onsi/ginkgo/v2/formatter

     1  package formatter_test
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	. "github.com/onsi/ginkgo/v2"
     8  	"github.com/onsi/ginkgo/v2/formatter"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("Formatter", func() {
    13  	var colorMode formatter.ColorMode
    14  	var f formatter.Formatter
    15  
    16  	BeforeEach(func() {
    17  		colorMode = formatter.ColorModeTerminal
    18  		os.Unsetenv("GINKGO_CLI_COLOR_RED")
    19  		os.Unsetenv("GINKGO_CLI_COLOR_ORANGE")
    20  		os.Unsetenv("GINKGO_CLI_COLOR_CORAL")
    21  		os.Unsetenv("GINKGO_CLI_COLOR_MAGENTA")
    22  		os.Unsetenv("GINKGO_CLI_COLOR_GREEN")
    23  		os.Unsetenv("GINKGO_CLI_COLOR_DARK_GREEN")
    24  		os.Unsetenv("GINKGO_CLI_COLOR_YELLOW")
    25  		os.Unsetenv("GINKGO_CLI_COLOR_LIGHT_YELLOW")
    26  		os.Unsetenv("GINKGO_CLI_COLOR_CYAN")
    27  		os.Unsetenv("GINKGO_CLI_COLOR_LIGHT_GRAY")
    28  		os.Unsetenv("GINKGO_CLI_COLOR_BLUE")
    29  	})
    30  
    31  	JustBeforeEach(func() {
    32  		f = formatter.New(colorMode)
    33  	})
    34  
    35  	Context("with ColorModeNone", func() {
    36  		BeforeEach(func() {
    37  			colorMode = formatter.ColorModeNone
    38  		})
    39  
    40  		It("strips out color information", func() {
    41  			Ω(f.F("{{green}}{{bold}}hi there{{/}}")).Should(Equal("hi there"))
    42  		})
    43  	})
    44  
    45  	Context("with ColorModeTerminal", func() {
    46  		BeforeEach(func() {
    47  			colorMode = formatter.ColorModeTerminal
    48  		})
    49  
    50  		It("renders the color information using terminal escape codes", func() {
    51  			Ω(f.F("{{green}}{{bold}}hi there{{/}}")).Should(Equal("\x1b[38;5;10m\x1b[1mhi there\x1b[0m"))
    52  		})
    53  	})
    54  
    55  	Context("with ColorModePassthrough", func() {
    56  		BeforeEach(func() {
    57  			colorMode = formatter.ColorModePassthrough
    58  		})
    59  
    60  		It("leaves the color information as is, allowing us to test statements more easily", func() {
    61  			Ω(f.F("{{green}}{{bold}}hi there{{/}}")).Should(Equal("{{green}}{{bold}}hi there{{/}}"))
    62  		})
    63  	})
    64  
    65  	DescribeTable("with environment overrides",
    66  		func(envVars map[string]string, input, expected string) {
    67  			for envVar, value := range envVars {
    68  				os.Setenv(envVar, value)
    69  			}
    70  			f := formatter.New(colorMode)
    71  			Ω(f.F(input)).Should(Equal(expected))
    72  			for envVar := range envVars {
    73  				os.Unsetenv(envVar)
    74  			}
    75  		},
    76  
    77  		Entry("uses default for too low codes", map[string]string{
    78  			"GINKGO_CLI_COLOR_RED": "-1",
    79  		}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),
    80  
    81  		Entry("uses default for too high codes", map[string]string{
    82  			"GINKGO_CLI_COLOR_RED": "256",
    83  		}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),
    84  
    85  		Entry("supports literal alias for 8bit color", map[string]string{
    86  			"GINKGO_CLI_COLOR_RED": "red",
    87  		}, "{{red}}hi there{{/}}", "\x1b[38;5;1mhi there\x1b[0m"),
    88  
    89  		Entry("supports number alias for 8bit color", map[string]string{
    90  			"GINKGO_CLI_COLOR_RED": "1",
    91  		}, "{{red}}hi there{{/}}", "\x1b[38;5;1mhi there\x1b[0m"),
    92  
    93  		Entry("supports 16bit colors (bright)", map[string]string{
    94  			"GINKGO_CLI_COLOR_RED": "9",
    95  		}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),
    96  
    97  		Entry("supports 16bit color literal aliases (bright)", map[string]string{
    98  			"GINKGO_CLI_COLOR_RED": "bright-red",
    99  		}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),
   100  
   101  		Entry("supports extended 256 colors", map[string]string{
   102  			"GINKGO_CLI_COLOR_RED": "16",
   103  		}, "{{red}}hi there{{/}}", "\x1b[38;5;16mhi there\x1b[0m"),
   104  	)
   105  
   106  	Describe("NewWithNoColorBool", func() {
   107  		Context("when the noColor bool is true", func() {
   108  			It("strips out color information", func() {
   109  				f = formatter.NewWithNoColorBool(true)
   110  				Ω(f.F("{{green}}{{bold}}hi there{{/}}")).Should(Equal("hi there"))
   111  			})
   112  		})
   113  
   114  		Context("when the noColor bool is false", func() {
   115  			It("renders the color information using terminal escape codes", func() {
   116  				f = formatter.NewWithNoColorBool(false)
   117  				Ω(f.F("{{green}}{{bold}}hi there{{/}}")).Should(Equal("\x1b[38;5;10m\x1b[1mhi there\x1b[0m"))
   118  			})
   119  		})
   120  	})
   121  
   122  	Describe("F", func() {
   123  		It("transforms the color information and sprintfs", func() {
   124  			Ω(f.F("{{green}}hi there {{cyan}}%d {{yellow}}%s{{/}}", 3, "wise men")).Should(Equal("\x1b[38;5;10mhi there \x1b[38;5;14m3 \x1b[38;5;11mwise men\x1b[0m"))
   125  		})
   126  
   127  		It("avoids sprintf if there are no additional arguments", func() {
   128  			Ω(f.F("{{green}}hi there {{cyan}}%d {{yellow}}%s{{/}}")).Should(Equal("\x1b[38;5;10mhi there \x1b[38;5;14m%d \x1b[38;5;11m%s\x1b[0m"))
   129  		})
   130  	})
   131  
   132  	Describe("Fi", func() {
   133  		It("transforms the color information, sprintfs, and applies an indentation", func() {
   134  			Ω(f.Fi(2, "{{green}}hi there\n{{cyan}}%d {{yellow}}%s{{/}}", 3, "wise men")).Should(Equal(
   135  				"    \x1b[38;5;10mhi there\n    \x1b[38;5;14m3 \x1b[38;5;11mwise men\x1b[0m",
   136  			))
   137  		})
   138  	})
   139  
   140  	DescribeTable("Fiw",
   141  		func(indentation int, maxWidth int, input string, expected ...string) {
   142  			Ω(f.Fiw(uint(indentation), uint(maxWidth), input)).Should(Equal(strings.Join(expected, "\n")))
   143  		},
   144  		Entry("basic case", 0, 0, "a really long string is fine", "a really long string is fine"),
   145  		Entry("indentation is accounted for in width",
   146  			1, 10,
   147  			"1234 678",
   148  			"  1234 678",
   149  		),
   150  		Entry("indentation is accounted for in width",
   151  			1, 10,
   152  			"1234 6789",
   153  			"  1234",
   154  			"  6789",
   155  		),
   156  		Entry("when there is a nice long sentence",
   157  			0, 10,
   158  			"12 456 890 1234 5",
   159  			"12 456 890",
   160  			"1234 5",
   161  		),
   162  		Entry("when a word in a sentence intersects the boundary",
   163  			0, 10,
   164  			"12 456 8901 123 45",
   165  			"12 456",
   166  			"8901 123",
   167  			"45",
   168  		),
   169  		Entry("when a word in a sentence is just too long",
   170  			0, 10,
   171  			"12 12345678901 12 12345 678901 12345678901",
   172  			"12",
   173  			"12345678901",
   174  			"12 12345",
   175  			"678901",
   176  			"12345678901",
   177  		),
   178  	)
   179  
   180  	Describe("CycleJoin", func() {
   181  		It("combines elements, cycling through styles as it goes", func() {
   182  			Ω(f.CycleJoin([]string{"a", "b", "c"}, "|", []string{"{{red}}", "{{green}}"})).Should(Equal(
   183  				"\x1b[38;5;9ma|\x1b[38;5;10mb|\x1b[38;5;9mc\x1b[0m",
   184  			))
   185  		})
   186  	})
   187  })
   188  

View as plain text