...

Source file src/github.com/jedib0t/go-pretty/v6/text/color_test.go

Documentation: github.com/jedib0t/go-pretty/v6/text

     1  package text
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func init() {
    11  	EnableColors()
    12  }
    13  
    14  func TestColor_EnableAndDisable(t *testing.T) {
    15  	defer EnableColors()
    16  
    17  	EnableColors()
    18  	assert.Equal(t, "\x1b[31mtest\x1b[0m", FgRed.Sprint("test"))
    19  
    20  	DisableColors()
    21  	assert.Equal(t, "test", FgRed.Sprint("test"))
    22  
    23  	EnableColors()
    24  	assert.Equal(t, "\x1b[31mtest\x1b[0m", FgRed.Sprint("test"))
    25  }
    26  
    27  func ExampleColor_EscapeSeq() {
    28  	fmt.Printf("Black Background: %#v\n", BgBlack.EscapeSeq())
    29  	fmt.Printf("Black Foreground: %#v\n", FgBlack.EscapeSeq())
    30  
    31  	// Output: Black Background: "\x1b[40m"
    32  	// Black Foreground: "\x1b[30m"
    33  }
    34  
    35  func TestColor_EscapeSeq(t *testing.T) {
    36  	assert.Equal(t, "\x1b[40m", BgBlack.EscapeSeq())
    37  }
    38  
    39  func ExampleColor_HTMLProperty() {
    40  	fmt.Printf("Bold: %#v\n", Bold.HTMLProperty())
    41  	fmt.Printf("Black Background: %#v\n", BgBlack.HTMLProperty())
    42  	fmt.Printf("Black Foreground: %#v\n", FgBlack.HTMLProperty())
    43  
    44  	// Output: Bold: "class=\"bold\""
    45  	// Black Background: "class=\"bg-black\""
    46  	// Black Foreground: "class=\"fg-black\""
    47  }
    48  
    49  func TestColor_HTMLProperty(t *testing.T) {
    50  	assert.Equal(t, "class=\"bold\"", Bold.HTMLProperty())
    51  	assert.Equal(t, "class=\"bg-black\"", BgBlack.HTMLProperty())
    52  	assert.Equal(t, "class=\"fg-black\"", FgBlack.HTMLProperty())
    53  }
    54  
    55  func ExampleColor_Sprint() {
    56  	fmt.Printf("%#v\n", BgBlack.Sprint("Black Background"))
    57  	fmt.Printf("%#v\n", FgBlack.Sprint("Black Foreground"))
    58  
    59  	// Output: "\x1b[40mBlack Background\x1b[0m"
    60  	// "\x1b[30mBlack Foreground\x1b[0m"
    61  }
    62  
    63  func TestColor_Sprint(t *testing.T) {
    64  	assert.Equal(t, "\x1b[31mtest true\x1b[0m", FgRed.Sprint("test ", true))
    65  
    66  	assert.Equal(t, "\x1b[32mtest\x1b[0m\x1b[31mtrue\x1b[0m", FgRed.Sprint("\x1b[32mtest\x1b[0m", true))
    67  	assert.Equal(t, "\x1b[32mtest true\x1b[0m", FgRed.Sprint("\x1b[32mtest ", true))
    68  	assert.Equal(t, "\x1b[32mtest\x1b[0m\x1b[31m \x1b[0m", FgRed.Sprint("\x1b[32mtest\x1b[0m "))
    69  	assert.Equal(t, "\x1b[32mtest\x1b[0m", FgRed.Sprint("\x1b[32mtest\x1b[0m"))
    70  }
    71  
    72  func ExampleColor_Sprintf() {
    73  	fmt.Printf("%#v\n", BgBlack.Sprintf("%s %s", "Black", "Background"))
    74  	fmt.Printf("%#v\n", FgBlack.Sprintf("%s %s", "Black", "Foreground"))
    75  
    76  	// Output: "\x1b[40mBlack Background\x1b[0m"
    77  	// "\x1b[30mBlack Foreground\x1b[0m"
    78  }
    79  
    80  func TestColor_Sprintf(t *testing.T) {
    81  	assert.Equal(t, "\x1b[31mtest true\x1b[0m", FgRed.Sprintf("test %s", "true"))
    82  }
    83  
    84  func ExampleColors_EscapeSeq() {
    85  	fmt.Printf("Black Background: %#v\n", Colors{BgBlack}.EscapeSeq())
    86  	fmt.Printf("Black Foreground: %#v\n", Colors{FgBlack}.EscapeSeq())
    87  	fmt.Printf("Black Background, White Foreground: %#v\n", Colors{BgBlack, FgWhite}.EscapeSeq())
    88  	fmt.Printf("Black Foreground, White Background: %#v\n", Colors{FgBlack, BgWhite}.EscapeSeq())
    89  
    90  	// Output: Black Background: "\x1b[40m"
    91  	// Black Foreground: "\x1b[30m"
    92  	// Black Background, White Foreground: "\x1b[40;37m"
    93  	// Black Foreground, White Background: "\x1b[30;47m"
    94  }
    95  
    96  func TestColors_EscapeSeq(t *testing.T) {
    97  	assert.Equal(t, "", Colors{}.EscapeSeq())
    98  	assert.Equal(t, "\x1b[40;37m", Colors{BgBlack, FgWhite}.EscapeSeq())
    99  }
   100  
   101  func ExampleColors_HTMLProperty() {
   102  	fmt.Printf("Black Background: %#v\n", Colors{BgBlack}.HTMLProperty())
   103  	fmt.Printf("Black Foreground: %#v\n", Colors{FgBlack}.HTMLProperty())
   104  	fmt.Printf("Black Background, White Foreground: %#v\n", Colors{BgBlack, FgWhite}.HTMLProperty())
   105  	fmt.Printf("Black Foreground, White Background: %#v\n", Colors{FgBlack, BgWhite}.HTMLProperty())
   106  	fmt.Printf("Bold Italic Underline Red Text: %#v\n", Colors{Bold, Italic, Underline, FgRed}.HTMLProperty())
   107  
   108  	// Output: Black Background: "class=\"bg-black\""
   109  	// Black Foreground: "class=\"fg-black\""
   110  	// Black Background, White Foreground: "class=\"bg-black fg-white\""
   111  	// Black Foreground, White Background: "class=\"bg-white fg-black\""
   112  	// Bold Italic Underline Red Text: "class=\"bold fg-red italic underline\""
   113  }
   114  
   115  func TestColors_HTMLProperty(t *testing.T) {
   116  	assert.Equal(t, "", Colors{}.HTMLProperty())
   117  	assert.Equal(t, "class=\"bg-black fg-white\"", Colors{BgBlack, FgWhite}.HTMLProperty())
   118  	assert.Equal(t, "class=\"bold fg-red\"", Colors{Bold, FgRed}.HTMLProperty())
   119  }
   120  
   121  func ExampleColors_Sprint() {
   122  	fmt.Printf("%#v\n", Colors{BgBlack}.Sprint("Black Background"))
   123  	fmt.Printf("%#v\n", Colors{BgBlack, FgWhite}.Sprint("Black Background, White Foreground"))
   124  	fmt.Printf("%#v\n", Colors{FgBlack}.Sprint("Black Foreground"))
   125  	fmt.Printf("%#v\n", Colors{FgBlack, BgWhite}.Sprint("Black Foreground, White Background"))
   126  
   127  	// Output: "\x1b[40mBlack Background\x1b[0m"
   128  	// "\x1b[40;37mBlack Background, White Foreground\x1b[0m"
   129  	// "\x1b[30mBlack Foreground\x1b[0m"
   130  	// "\x1b[30;47mBlack Foreground, White Background\x1b[0m"
   131  }
   132  
   133  func TestColors_Sprint(t *testing.T) {
   134  	assert.Equal(t, "test true", Colors{}.Sprint("test ", true))
   135  	assert.Equal(t, "\x1b[31mtest true\x1b[0m", Colors{FgRed}.Sprint("test ", true))
   136  
   137  	assert.Equal(t, "\x1b[32mtest\x1b[0m\x1b[31mtrue\x1b[0m", Colors{FgRed}.Sprint("\x1b[32mtest\x1b[0m", true))
   138  	assert.Equal(t, "\x1b[32mtest true\x1b[0m", Colors{FgRed}.Sprint("\x1b[32mtest ", true))
   139  	assert.Equal(t, "\x1b[32mtest\x1b[0m\x1b[31m \x1b[0m", Colors{FgRed}.Sprint("\x1b[32mtest\x1b[0m "))
   140  	assert.Equal(t, "\x1b[32mtest\x1b[0m", Colors{FgRed}.Sprint("\x1b[32mtest\x1b[0m"))
   141  }
   142  
   143  func ExampleColors_Sprintf() {
   144  	fmt.Printf("%#v\n", Colors{BgBlack}.Sprintf("%s %s", "Black", "Background"))
   145  	fmt.Printf("%#v\n", Colors{BgBlack, FgWhite}.Sprintf("%s, %s", "Black Background", "White Foreground"))
   146  	fmt.Printf("%#v\n", Colors{FgBlack}.Sprintf("%s %s", "Black", "Foreground"))
   147  	fmt.Printf("%#v\n", Colors{FgBlack, BgWhite}.Sprintf("%s, %s", "Black Foreground", "White Background"))
   148  
   149  	// Output: "\x1b[40mBlack Background\x1b[0m"
   150  	// "\x1b[40;37mBlack Background, White Foreground\x1b[0m"
   151  	// "\x1b[30mBlack Foreground\x1b[0m"
   152  	// "\x1b[30;47mBlack Foreground, White Background\x1b[0m"
   153  }
   154  
   155  func TestColors_Sprintf(t *testing.T) {
   156  	assert.Equal(t, "test true", Colors{}.Sprintf("test %s", "true"))
   157  	assert.Equal(t, "\x1b[31mtest true\x1b[0m", Colors{FgRed}.Sprintf("test %s", "true"))
   158  }
   159  

View as plain text