github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/formatter/formatter_test.go (about)

     1  package formatter_test
     2  
     3  import (
     4  	"strings"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	"github.com/onsi/ginkgo/formatter"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("Formatter", func() {
    12  	var colorMode formatter.ColorMode
    13  	var f formatter.Formatter
    14  
    15  	BeforeEach(func() {
    16  		colorMode = formatter.ColorModeTerminal
    17  	})
    18  
    19  	JustBeforeEach(func() {
    20  		f = formatter.New(colorMode)
    21  	})
    22  
    23  	Context("with ColorModeNone", func() {
    24  		BeforeEach(func() {
    25  			colorMode = formatter.ColorModeNone
    26  		})
    27  
    28  		It("strips out color information", func() {
    29  			Ω(f.F("{{green}}{{bold}}hi there{{/}}")).Should(Equal("hi there"))
    30  		})
    31  	})
    32  
    33  	Context("with ColorModeTerminal", func() {
    34  		BeforeEach(func() {
    35  			colorMode = formatter.ColorModeTerminal
    36  		})
    37  
    38  		It("renders the color information using terminal escape codes", func() {
    39  			Ω(f.F("{{green}}{{bold}}hi there{{/}}")).Should(Equal("\x1b[38;5;10m\x1b[1mhi there\x1b[0m"))
    40  		})
    41  	})
    42  
    43  	Context("with ColorModePassthrough", func() {
    44  		BeforeEach(func() {
    45  			colorMode = formatter.ColorModePassthrough
    46  		})
    47  
    48  		It("leaves the color information as is, allowing us to test statements more easily", func() {
    49  			Ω(f.F("{{green}}{{bold}}hi there{{/}}")).Should(Equal("{{green}}{{bold}}hi there{{/}}"))
    50  		})
    51  	})
    52  
    53  	Describe("NewWithNoColorBool", func() {
    54  		Context("when the noColor bool is true", func() {
    55  			It("strips out color information", func() {
    56  				f = formatter.NewWithNoColorBool(true)
    57  				Ω(f.F("{{green}}{{bold}}hi there{{/}}")).Should(Equal("hi there"))
    58  			})
    59  		})
    60  
    61  		Context("when the noColor bool is false", func() {
    62  			It("renders the color information using terminal escape codes", func() {
    63  				f = formatter.NewWithNoColorBool(false)
    64  				Ω(f.F("{{green}}{{bold}}hi there{{/}}")).Should(Equal("\x1b[38;5;10m\x1b[1mhi there\x1b[0m"))
    65  			})
    66  		})
    67  	})
    68  
    69  	Describe("F", func() {
    70  		It("transforms the color information and sprintfs", func() {
    71  			Ω(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"))
    72  		})
    73  	})
    74  
    75  	Describe("Fi", func() {
    76  		It("transforms the color information, sprintfs, and applies an indentation", func() {
    77  			Ω(f.Fi(2, "{{green}}hi there\n{{cyan}}%d {{yellow}}%s{{/}}", 3, "wise men")).Should(Equal(
    78  				"    \x1b[38;5;10mhi there\n    \x1b[38;5;14m3 \x1b[38;5;11mwise men\x1b[0m",
    79  			))
    80  		})
    81  	})
    82  
    83  	DescribeTable("Fiw",
    84  		func(indentation int, maxWidth int, input string, expected ...string) {
    85  			Ω(f.Fiw(uint(indentation), uint(maxWidth), input)).Should(Equal(strings.Join(expected, "\n")))
    86  		},
    87  		Entry("basic case", 0, 0, "a really long string is fine", "a really long string is fine"),
    88  		Entry("indentation is accounted for in width",
    89  			1, 10,
    90  			"1234 678",
    91  			"  1234 678",
    92  		),
    93  		Entry("indentation is accounted for in width",
    94  			1, 10,
    95  			"1234 6789",
    96  			"  1234",
    97  			"  6789",
    98  		),
    99  		Entry("when there is a nice long sentence",
   100  			0, 10,
   101  			"12 456 890 1234 5",
   102  			"12 456 890",
   103  			"1234 5",
   104  		),
   105  		Entry("when a word in a sentence intersects the boundary",
   106  			0, 10,
   107  			"12 456 8901 123 45",
   108  			"12 456",
   109  			"8901 123",
   110  			"45",
   111  		),
   112  		Entry("when a word in a sentence is just too long",
   113  			0, 10,
   114  			"12 12345678901 12 12345 678901 12345678901",
   115  			"12",
   116  			"12345678901",
   117  			"12 12345",
   118  			"678901",
   119  			"12345678901",
   120  		),
   121  	)
   122  
   123  	Describe("CycleJoin", func() {
   124  		It("combines elements, cycling through styles as it goes", func() {
   125  			Ω(f.CycleJoin([]string{"a", "b", "c"}, "|", []string{"{{red}}", "{{green}}"})).Should(Equal(
   126  				"\x1b[38;5;9ma|\x1b[38;5;10mb|\x1b[38;5;9mc\x1b[0m",
   127  			))
   128  		})
   129  	})
   130  })