github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xcolor/xcolor_test.go (about)

     1  package xcolor
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/Aoi-hosizora/ahlib/xtesting"
     6  	"math"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestForceColor(t *testing.T) {
    13  	fmt.Println("\x1b[31mhello world\x1b[0m")
    14  	fmt.Println("\x1b[4;31mhello world\x1b[0m")
    15  	fmt.Println("\x1b[31;103mhello world\x1b[0m")
    16  	fmt.Println("\x1b[4;31;103mhello world\x1b[0m")
    17  	ForceColor()
    18  	fmt.Println("\x1b[31mhello world\x1b[0m")
    19  	fmt.Println("\x1b[4;31mhello world\x1b[0m")
    20  	fmt.Println("\x1b[31;103mhello world\x1b[0m")
    21  	fmt.Println("\x1b[4;31;103mhello world\x1b[0m")
    22  }
    23  
    24  func TestCode(t *testing.T) {
    25  	for _, tc := range []struct {
    26  		give uint8
    27  		want uint8
    28  	}{
    29  		{Bold.Code(), 1},
    30  		{Faint.Code(), 2},
    31  		{Italic.Code(), 3},
    32  		{Underline.Code(), 4},
    33  		{Reverse.Code(), 7},
    34  		{Strikethrough.Code(), 9},
    35  
    36  		{Black.Code(), 30},
    37  		{Red.Code(), 31},
    38  		{Green.Code(), 32},
    39  		{Yellow.Code(), 33},
    40  		{Blue.Code(), 34},
    41  		{Magenta.Code(), 35},
    42  		{Cyan.Code(), 36},
    43  		{White.Code(), 37},
    44  		{Default.Code(), 39},
    45  		{BrightBlack.Code(), 90},
    46  		{BrightRed.Code(), 91},
    47  		{BrightGreen.Code(), 92},
    48  		{BrightYellow.Code(), 93},
    49  		{BrightBlue.Code(), 94},
    50  		{BrightMagenta.Code(), 95},
    51  		{BrightCyan.Code(), 96},
    52  		{BrightWhite.Code(), 97},
    53  
    54  		{BGBlack.Code(), 40},
    55  		{BGRed.Code(), 41},
    56  		{BGGreen.Code(), 42},
    57  		{BGYellow.Code(), 43},
    58  		{BGBlue.Code(), 44},
    59  		{BGMagenta.Code(), 45},
    60  		{BGCyan.Code(), 46},
    61  		{BGWhite.Code(), 47},
    62  		{BGDefault.Code(), 49},
    63  		{BGBrightBlack.Code(), 100},
    64  		{BGBrightRed.Code(), 101},
    65  		{BGBrightGreen.Code(), 102},
    66  		{BGBrightYellow.Code(), 103},
    67  		{BGBrightBlue.Code(), 104},
    68  		{BGBrightMagenta.Code(), 105},
    69  		{BGBrightCyan.Code(), 106},
    70  		{BGBrightWhite.Code(), 107},
    71  	} {
    72  		xtesting.Equal(t, tc.give, tc.want)
    73  	}
    74  }
    75  
    76  func TestString(t *testing.T) {
    77  	for _, tc := range []struct {
    78  		give string
    79  		want string
    80  	}{
    81  		{Bold.String(), "1"},
    82  		{Faint.String(), "2"},
    83  		{Italic.String(), "3"},
    84  		{Underline.String(), "4"},
    85  		{Reverse.String(), "7"},
    86  		{Strikethrough.String(), "9"},
    87  
    88  		{Black.String(), "30"},
    89  		{Red.String(), "31"},
    90  		{Green.String(), "32"},
    91  		{Yellow.String(), "33"},
    92  		{Blue.String(), "34"},
    93  		{Magenta.String(), "35"},
    94  		{Cyan.String(), "36"},
    95  		{White.String(), "37"},
    96  		{Default.String(), "39"},
    97  		{BrightBlack.String(), "90"},
    98  		{BrightRed.String(), "91"},
    99  		{BrightGreen.String(), "92"},
   100  		{BrightYellow.String(), "93"},
   101  		{BrightBlue.String(), "94"},
   102  		{BrightMagenta.String(), "95"},
   103  		{BrightCyan.String(), "96"},
   104  		{BrightWhite.String(), "97"},
   105  
   106  		{BGBlack.String(), "40"},
   107  		{BGRed.String(), "41"},
   108  		{BGGreen.String(), "42"},
   109  		{BGYellow.String(), "43"},
   110  		{BGBlue.String(), "44"},
   111  		{BGMagenta.String(), "45"},
   112  		{BGCyan.String(), "46"},
   113  		{BGWhite.String(), "47"},
   114  		{BGDefault.String(), "49"},
   115  		{BGBrightBlack.String(), "100"},
   116  		{BGBrightRed.String(), "101"},
   117  		{BGBrightGreen.String(), "102"},
   118  		{BGBrightYellow.String(), "103"},
   119  		{BGBrightBlue.String(), "104"},
   120  		{BGBrightMagenta.String(), "105"},
   121  		{BGBrightCyan.String(), "106"},
   122  		{BGBrightWhite.String(), "107"},
   123  	} {
   124  		xtesting.Equal(t, tc.give, tc.want)
   125  	}
   126  
   127  	Style.unexportedXXX(0)
   128  	Color.unexportedXXX(0)
   129  	Background.unexportedXXX(0)
   130  	MixCode.unexportedXXX(nil)
   131  }
   132  
   133  func TestMixCode(t *testing.T) {
   134  	for _, tc := range []struct {
   135  		give []uint8
   136  		want []uint8
   137  	}{
   138  		{MixCode{}.Codes(), []uint8{}},
   139  		{MixCode{Bold.Code()}.Codes(), []uint8{Bold.Code()}},
   140  		{MixCode{Bold.Code(), Bold.Code()}.Codes(), []uint8{Bold.Code(), Bold.Code()}},
   141  		{MixCode{Bold.Code(), Red.Code()}.Codes(), []uint8{Bold.Code(), Red.Code()}},
   142  		{MixCode{Bold.Code(), Red.Code(), BGWhite.Code()}.Codes(), []uint8{Bold.Code(), Red.Code(), BGWhite.Code()}},
   143  	} {
   144  		xtesting.Equal(t, tc.give, tc.want)
   145  	}
   146  
   147  	for _, tc := range []struct {
   148  		give string
   149  		want string
   150  	}{
   151  		{MixCode{}.String(), "0"},
   152  		{MixCode{Bold.Code(), Bold.Code()}.String(), Bold.String() + ";" + Bold.String()},
   153  		{MixCode{Bold.Code(), Red.Code()}.String(), Bold.String() + ";" + Red.String()},
   154  		{MixCode{Bold.Code(), Red.Code(), BGWhite.Code()}.String(), Bold.String() + ";" + Red.String() + ";" + BGWhite.String()},
   155  	} {
   156  		xtesting.Equal(t, tc.give, tc.want)
   157  	}
   158  
   159  	for _, tc := range []struct {
   160  		give MixCode
   161  		want MixCode
   162  	}{
   163  		{Bold.WithStyle(Italic), []uint8{Bold.Code(), Italic.Code()}},
   164  		{Bold.WithColor(Red), []uint8{Bold.Code(), Red.Code()}},
   165  		{Bold.WithBackground(BGWhite), []uint8{Bold.Code(), BGWhite.Code()}},
   166  		{Red.WithStyle(Bold), []uint8{Red.Code(), Bold.Code()}},
   167  		{Red.WithBackground(BGWhite), []uint8{Red.Code(), BGWhite.Code()}},
   168  		{BGWhite.WithStyle(Bold), []uint8{BGWhite.Code(), Bold.Code()}},
   169  		{BGWhite.WithColor(Red), []uint8{BGWhite.Code(), Red.Code()}},
   170  		{MixCode{}.WithStyle(Bold).WithColor(Red).WithBackground(BGWhite), []uint8{Bold.Code(), Red.Code(), BGWhite.Code()}},
   171  	} {
   172  		xtesting.Equal(t, tc.give, tc.want)
   173  	}
   174  }
   175  
   176  func TestPrintEmpty(t *testing.T) {
   177  	xtesting.NotPanic(t, func() {
   178  		Bold.Print()
   179  		Bold.Printf("")
   180  		Bold.Println()
   181  		Bold.Sprint()
   182  		Bold.Sprintf("")
   183  		Bold.Sprintln()
   184  		_, _ = Bold.Fprint(os.Stdout)
   185  		_, _ = Bold.Fprintf(os.Stdout, "")
   186  		_, _ = Bold.Fprintln(os.Stdout)
   187  
   188  		Red.Print()
   189  		Red.Printf("")
   190  		Red.Println()
   191  		Red.Sprint()
   192  		Red.Sprintf("")
   193  		Red.Sprintln()
   194  		_, _ = Red.Fprint(os.Stdout)
   195  		_, _ = Red.Fprintf(os.Stdout, "")
   196  		_, _ = Red.Fprintln(os.Stdout)
   197  
   198  		BGRed.Print()
   199  		BGRed.Printf("")
   200  		BGRed.Println()
   201  		BGRed.Sprint()
   202  		BGRed.Sprintf("")
   203  		BGRed.Sprintln()
   204  		_, _ = BGRed.Fprint(os.Stdout)
   205  		_, _ = BGRed.Fprintf(os.Stdout, "")
   206  		_, _ = BGRed.Fprintln(os.Stdout)
   207  
   208  		Bold.WithStyle(Italic).Print()
   209  		Bold.WithStyle(Italic).Printf("")
   210  		Bold.WithStyle(Italic).Println()
   211  		Bold.WithStyle(Italic).Sprint()
   212  		Bold.WithStyle(Italic).Sprintf("")
   213  		Bold.WithStyle(Italic).Sprintln()
   214  		_, _ = Bold.WithStyle(Italic).Fprint(os.Stdout)
   215  		_, _ = Bold.WithStyle(Italic).Fprintf(os.Stdout, "")
   216  		_, _ = Bold.WithStyle(Italic).Fprintln(os.Stdout)
   217  	})
   218  
   219  	xtesting.NotPanic(t, func() {
   220  		Bold.APrint(5)
   221  		Bold.APrintf(5, "")
   222  		Bold.APrintln(5)
   223  		Bold.ASprint(5)
   224  		Bold.ASprintf(5, "")
   225  		Bold.ASprintln(5)
   226  		_, _ = Bold.AFprint(5, os.Stdout)
   227  		_, _ = Bold.AFprintf(5, os.Stdout, "")
   228  		_, _ = Bold.AFprintln(5, os.Stdout)
   229  
   230  		Red.APrint(-5)
   231  		Red.APrintf(-5, "")
   232  		Red.APrintln(-5)
   233  		Red.ASprint(-5)
   234  		Red.ASprintf(-5, "")
   235  		Red.ASprintln(-5)
   236  		_, _ = Red.AFprint(-5, os.Stdout)
   237  		_, _ = Red.AFprintf(-5, os.Stdout, "")
   238  		_, _ = Red.AFprintln(-5, os.Stdout)
   239  
   240  		BGRed.APrint(5)
   241  		BGRed.APrintf(5, "")
   242  		BGRed.APrintln(5)
   243  		BGRed.ASprint(5)
   244  		BGRed.ASprintf(5, "")
   245  		BGRed.ASprintln(5)
   246  		_, _ = BGRed.AFprint(5, os.Stdout)
   247  		_, _ = BGRed.AFprintf(5, os.Stdout, "")
   248  		_, _ = BGRed.AFprintln(5, os.Stdout)
   249  
   250  		Bold.WithStyle(Italic).APrint(-5)
   251  		Bold.WithStyle(Italic).APrintf(-5, "")
   252  		Bold.WithStyle(Italic).APrintln(-5)
   253  		Bold.WithStyle(Italic).ASprint(-5)
   254  		Bold.WithStyle(Italic).ASprintf(-5, "")
   255  		Bold.WithStyle(Italic).ASprintln(-5)
   256  		_, _ = Bold.WithStyle(Italic).AFprint(-5, os.Stdout)
   257  		_, _ = Bold.WithStyle(Italic).AFprintf(-5, os.Stdout, "")
   258  		_, _ = Bold.WithStyle(Italic).AFprintln(-5, os.Stdout)
   259  	})
   260  }
   261  
   262  func TestPrint(t *testing.T) {
   263  	// style
   264  	fmt.Println(">>> style")
   265  	Bold.Print("bold")
   266  	fmt.Println()
   267  	Faint.Printf("%s", "faint")
   268  	fmt.Println()
   269  	Italic.Println("italic")
   270  	fmt.Print(Underline.Sprint("underline"))
   271  	fmt.Println()
   272  	fmt.Print(Reverse.Sprintf("%s", "reverse"))
   273  	fmt.Println()
   274  	fmt.Print(Strikethrough.Sprintln("strikethrough"))
   275  	_, _ = Bold.Fprint(os.Stdout, "bold")
   276  	_, _ = fmt.Fprintln(os.Stdout)
   277  	_, _ = Faint.Fprintf(os.Stdout, "%s", "faint")
   278  	_, _ = fmt.Fprintln(os.Stdout)
   279  	_, _ = Italic.Fprintln(os.Stdout, "italic")
   280  
   281  	// color
   282  	fmt.Println(">>> color")
   283  	Red.Print("red")
   284  	fmt.Println()
   285  	Green.Printf("%s", "green")
   286  	fmt.Println()
   287  	Yellow.Println("yellow")
   288  	fmt.Print(Blue.Sprint("blue"))
   289  	fmt.Println()
   290  	fmt.Print(Magenta.Sprintf("%s", "magenta"))
   291  	fmt.Println()
   292  	fmt.Print(Cyan.Sprintln("cyan"))
   293  	_, _ = Black.Fprint(os.Stdout, "black")
   294  	_, _ = fmt.Fprintln(os.Stdout)
   295  	_, _ = White.Fprintf(os.Stdout, "%s", "white")
   296  	_, _ = fmt.Fprintln(os.Stdout)
   297  	_, _ = Default.Fprintln(os.Stdout, "default")
   298  	BrightBlack.Println("bright_black")
   299  	BrightRed.Println("bright_red")
   300  	BrightGreen.Println("bright_green")
   301  	BrightYellow.Println("bright_yellow")
   302  	BrightBlue.Println("bright_blue")
   303  	BrightMagenta.Println("bright_magenta")
   304  	BrightCyan.Println("bright_cyan")
   305  	BrightWhite.Println("bright_white")
   306  
   307  	// background
   308  	fmt.Println(">>> background")
   309  	BGRed.Print("bg_red")
   310  	fmt.Println()
   311  	BGGreen.Printf("%s", "bg_green")
   312  	fmt.Println()
   313  	BGYellow.Println("bg_yellow")
   314  	fmt.Print(BGBlue.Sprint("bg_blue"))
   315  	fmt.Println()
   316  	fmt.Print(BGMagenta.Sprintf("%s", "bg_magenta"))
   317  	fmt.Println()
   318  	fmt.Print(BGCyan.Sprintln("bg_cyan"))
   319  	_, _ = BGBlack.Fprint(os.Stdout, "bg_black")
   320  	_, _ = fmt.Fprintln(os.Stdout)
   321  	_, _ = BGWhite.Fprintf(os.Stdout, "%s", "bg_white")
   322  	_, _ = fmt.Fprintln(os.Stdout)
   323  	_, _ = BGDefault.Fprintln(os.Stdout, "bg_default")
   324  	BGBrightBlack.Println("bg_bright_black")
   325  	BGBrightRed.Println("bg_bright_red")
   326  	BGBrightGreen.Println("bg_bright_green")
   327  	BGBrightYellow.Println("bg_bright_yellow")
   328  	BGBrightBlue.Println("bg_bright_blue")
   329  	BGBrightMagenta.Println("bg_bright_magenta")
   330  	BGBrightCyan.Println("bg_bright_cyan")
   331  	BGBrightWhite.Println("bg_bright_white")
   332  
   333  	// mix code
   334  	fmt.Println(">>> mix code")
   335  	Bold.WithStyle(Italic).Print("bold;italic")
   336  	fmt.Println()
   337  	Bold.WithColor(Red).Printf("%s", "bold;red")
   338  	fmt.Println()
   339  	Bold.WithBackground(BGWhite).Println("bold;bg_white")
   340  	fmt.Print(Red.WithStyle(Bold).Sprint("red;bold"))
   341  	fmt.Println()
   342  	fmt.Print(Red.WithBackground(BGWhite).Sprintf("%s", "red;bg_white"))
   343  	fmt.Println()
   344  	fmt.Print(BGWhite.WithStyle(Bold).Sprintln("bg_white;bold"))
   345  	BGWhite.WithColor(Red).Println("bg_white;red")
   346  	Bold.WithColor(Red).WithBackground(BGWhite).Println("bold;red;bg_white")
   347  	_, _ = Bold.WithStyle(Italic).Fprint(os.Stdout, "bold;italic")
   348  	_, _ = fmt.Fprintln(os.Stdout)
   349  	_, _ = Bold.WithColor(Red).Fprintf(os.Stdout, "%s", "bold;red")
   350  	_, _ = fmt.Fprintln(os.Stdout)
   351  	_, _ = Bold.WithBackground(BGWhite).Fprintln(os.Stdout, "bold;bg_white")
   352  }
   353  
   354  func TestNPrint(t *testing.T) {
   355  	for _, alignment := range []int{20, -20} {
   356  		fmt.Println(strings.Repeat("=", int(math.Abs(float64(alignment)))))
   357  
   358  		// style
   359  		fmt.Println(">>> style")
   360  		Bold.APrint(alignment, "bold")
   361  		fmt.Println()
   362  		Faint.APrintf(alignment, "%s", "faint")
   363  		fmt.Println()
   364  		Italic.APrintln(alignment, "italic")
   365  		fmt.Print(Underline.ASprint(alignment, "underline"))
   366  		fmt.Println()
   367  		fmt.Print(Reverse.ASprintf(alignment, "%s", "reverse"))
   368  		fmt.Println()
   369  		fmt.Print(Strikethrough.ASprintln(alignment, "strikethrough"))
   370  		_, _ = Bold.AFprint(alignment, os.Stdout, "bold")
   371  		_, _ = fmt.Fprintln(os.Stdout)
   372  		_, _ = Faint.AFprintf(alignment, os.Stdout, "%s", "faint")
   373  		_, _ = fmt.Fprintln(os.Stdout)
   374  		_, _ = Italic.AFprintln(alignment, os.Stdout, "italic")
   375  
   376  		// color
   377  		fmt.Println(">>> color")
   378  		Red.APrint(alignment, "red")
   379  		fmt.Println()
   380  		Green.APrintf(alignment, "%s", "green")
   381  		fmt.Println()
   382  		Yellow.APrintln(alignment, "yellow")
   383  		fmt.Print(Blue.ASprint(alignment, "blue"))
   384  		fmt.Println()
   385  		fmt.Print(Magenta.ASprintf(alignment, "%s", "magenta"))
   386  		fmt.Println()
   387  		fmt.Print(Cyan.ASprintln(alignment, "cyan"))
   388  		_, _ = Black.AFprint(alignment, os.Stdout, "black")
   389  		_, _ = fmt.Fprintln(os.Stdout)
   390  		_, _ = White.AFprintf(alignment, os.Stdout, "%s", "white")
   391  		_, _ = fmt.Fprintln(os.Stdout)
   392  		_, _ = Default.AFprintln(alignment, os.Stdout, "default")
   393  		BrightBlack.APrintln(alignment, "bright_black")
   394  		BrightRed.APrintln(alignment, "bright_red")
   395  		BrightGreen.APrintln(alignment, "bright_green")
   396  		BrightYellow.APrintln(alignment, "bright_yellow")
   397  		BrightBlue.APrintln(alignment, "bright_blue")
   398  		BrightMagenta.APrintln(alignment, "bright_magenta")
   399  		BrightCyan.APrintln(alignment, "bright_cyan")
   400  		BrightWhite.APrintln(alignment, "bright_white")
   401  
   402  		// background
   403  		fmt.Println(">>> background")
   404  		BGRed.APrint(alignment, "bg_red")
   405  		fmt.Println()
   406  		BGGreen.APrintf(alignment, "%s", "bg_green")
   407  		fmt.Println()
   408  		BGYellow.APrintln(alignment, "bg_yellow")
   409  		fmt.Print(BGBlue.ASprint(alignment, "bg_blue"))
   410  		fmt.Println()
   411  		fmt.Print(BGMagenta.ASprintf(alignment, "%s", "bg_magenta"))
   412  		fmt.Println()
   413  		fmt.Print(BGCyan.ASprintln(alignment, "bg_cyan"))
   414  		_, _ = BGBlack.AFprint(alignment, os.Stdout, "bg_black")
   415  		_, _ = fmt.Fprintln(os.Stdout)
   416  		_, _ = BGWhite.AFprintf(alignment, os.Stdout, "%s", "bg_white")
   417  		_, _ = fmt.Fprintln(os.Stdout)
   418  		_, _ = BGDefault.AFprintln(alignment, os.Stdout, "bg_default")
   419  		BGBrightBlack.APrintln(alignment, "bg_bright_black")
   420  		BGBrightRed.APrintln(alignment, "bg_bright_red")
   421  		BGBrightGreen.APrintln(alignment, "bg_bright_green")
   422  		BGBrightYellow.APrintln(alignment, "bg_bright_yellow")
   423  		BGBrightBlue.APrintln(alignment, "bg_bright_blue")
   424  		BGBrightMagenta.APrintln(alignment, "bg_bright_magenta")
   425  		BGBrightCyan.APrintln(alignment, "bg_bright_cyan")
   426  		BGBrightWhite.APrintln(alignment, "bg_bright_white")
   427  
   428  		// mix code
   429  		fmt.Println(">>> mix code")
   430  		Bold.WithStyle(Italic).APrint(alignment, "bold;italic")
   431  		fmt.Println()
   432  		Bold.WithColor(Red).APrintf(alignment, "%s", "bold;red")
   433  		fmt.Println()
   434  		Bold.WithBackground(BGWhite).APrintln(alignment, "bold;bg_white")
   435  		fmt.Print(Red.WithStyle(Bold).ASprint(alignment, "red;bold"))
   436  		fmt.Println()
   437  		fmt.Print(Red.WithBackground(BGWhite).ASprintf(alignment, "%s", "red;bg_white"))
   438  		fmt.Println()
   439  		fmt.Print(BGWhite.WithStyle(Bold).ASprintln(alignment, "bg_white;bold"))
   440  		BGWhite.WithColor(Red).APrintln(alignment, "bg_white;red")
   441  		Bold.WithColor(Red).WithBackground(BGWhite).APrintln(alignment, "bold;red;bg_white")
   442  		_, _ = Bold.WithStyle(Italic).AFprint(alignment, os.Stdout, "bold;italic")
   443  		_, _ = fmt.Fprintln(os.Stdout)
   444  		_, _ = Bold.WithColor(Red).AFprintf(alignment, os.Stdout, "%s", "bold;red")
   445  		_, _ = fmt.Fprintln(os.Stdout)
   446  		_, _ = Bold.WithBackground(BGWhite).AFprintln(alignment, os.Stdout, "bold;bg_white")
   447  	}
   448  }