...

Source file src/github.com/logrusorgru/aurora/v3/value_test.go

Documentation: github.com/logrusorgru/aurora/v3

     1  //
     2  // Copyright (c) 2016-2020 The Aurora Authors. All rights reserved.
     3  // This program is free software. It comes without any warranty,
     4  // to the extent permitted by applicable law. You can redistribute
     5  // it and/or modify it under the terms of the Unlicense. See LICENSE
     6  // file for more details or see below.
     7  //
     8  
     9  //
    10  // This is free and unencumbered software released into the public domain.
    11  //
    12  // Anyone is free to copy, modify, publish, use, compile, sell, or
    13  // distribute this software, either in source code form or as a compiled
    14  // binary, for any purpose, commercial or non-commercial, and by any
    15  // means.
    16  //
    17  // In jurisdictions that recognize copyright laws, the author or authors
    18  // of this software dedicate any and all copyright interest in the
    19  // software to the public domain. We make this dedication for the benefit
    20  // of the public at large and to the detriment of our heirs and
    21  // successors. We intend this dedication to be an overt act of
    22  // relinquishment in perpetuity of all present and future rights to this
    23  // software under copyright law.
    24  //
    25  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    26  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    27  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    28  // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    29  // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    30  // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    31  // OTHER DEALINGS IN THE SOFTWARE.
    32  //
    33  // For more information, please refer to <http://unlicense.org/>
    34  //
    35  
    36  package aurora
    37  
    38  import (
    39  	"fmt"
    40  	"testing"
    41  )
    42  
    43  func TestValue_String(t *testing.T) {
    44  	var v Value
    45  	// colorized
    46  	v = value{"x", 0, 0}
    47  	if x := v.String(); x != "x" {
    48  		t.Errorf("(value).String: want %q, got %q", "x", x)
    49  	}
    50  	v = value{"x", BlackFg, RedBg}
    51  	want := "\033[0;30mx\033[0;41m"
    52  	if got := v.String(); want != got {
    53  		t.Errorf("(value).String: want %q, got %q", want, got)
    54  	}
    55  	// clear
    56  	v = valueClear{"x"}
    57  	if x := v.String(); x != "x" {
    58  		t.Errorf("(value).String: want %q, got %q", "x", x)
    59  	}
    60  
    61  }
    62  
    63  func TestValue_Color(t *testing.T) {
    64  	// colorized
    65  	if (value{"", RedFg, 0}).Color() != RedFg {
    66  		t.Error("wrong color")
    67  	}
    68  	// clear
    69  	if (valueClear{0}).Color() != 0 {
    70  		t.Error("wrong color")
    71  	}
    72  }
    73  
    74  func TestValue_Value(t *testing.T) {
    75  	// colorized
    76  	if (value{"x", RedFg, BlueBg}).Value() != "x" {
    77  		t.Error("wrong value")
    78  	}
    79  	// clear
    80  	if (valueClear{"x"}).Value() != "x" {
    81  		t.Error("wrong value")
    82  	}
    83  }
    84  
    85  func TestValue_Bleach(t *testing.T) {
    86  	// colorized
    87  	if (value{"x", RedFg, BlueBg}).Bleach() != (value{value: "x"}) {
    88  		t.Error("wrong bleached")
    89  	}
    90  	// clear
    91  	if (valueClear{"x"}).Bleach() != (valueClear{"x"}) {
    92  		t.Error("wrong bleached")
    93  	}
    94  }
    95  
    96  func TestValue_Format(t *testing.T) {
    97  	var v Value
    98  	var want, got string
    99  	//
   100  	// colorized
   101  	//
   102  	v = value{3.14, RedFg, BlueBg}
   103  	got = fmt.Sprintf("%+1.3g", v)
   104  	want = "\033[0;31m" + fmt.Sprintf("%+1.3g", 3.14) + "\033[0;44m"
   105  	if want != got {
   106  		t.Errorf("Format: want %q, got %q", want, got)
   107  	}
   108  	//
   109  	var utf8Verb = "%+1.3δΈ–" // verb that fit more then 1 byte
   110  	got = fmt.Sprintf(utf8Verb, v)
   111  	want = "\033[0;31m" + fmt.Sprintf(utf8Verb, 3.14) + "\033[0;44m"
   112  	if want != got {
   113  		t.Errorf("Format: want %q, got %q", want, got)
   114  	}
   115  	//
   116  	// clear
   117  	//
   118  	v = valueClear{3.14}
   119  	got = fmt.Sprintf("%+1.3g", v)
   120  	want = fmt.Sprintf("%+1.3g", 3.14)
   121  	if want != got {
   122  		t.Errorf("Format: want %q, got %q", want, got)
   123  	}
   124  	//
   125  	got = fmt.Sprintf(utf8Verb, v)
   126  	want = fmt.Sprintf(utf8Verb, 3.14)
   127  	if want != got {
   128  		t.Errorf("Format: want %q, got %q", want, got)
   129  	}
   130  }
   131  
   132  func Test_tail(t *testing.T) {
   133  	// colorized
   134  	if (value{"x", 0, BlueBg}).tail() != BlueBg {
   135  		t.Error("wrong tail color")
   136  	}
   137  	// clear
   138  	if (valueClear{"x"}).tail() != 0 {
   139  		t.Error("wrong tail color")
   140  	}
   141  }
   142  
   143  func Test_setTail(t *testing.T) {
   144  	// colorized
   145  	if (value{"x", 0, 0}).setTail(RedFg) != (value{"x", 0, RedFg}) {
   146  		t.Error("wrong setTail behavior")
   147  	}
   148  	// clear
   149  	if (valueClear{"x"}).setTail(RedFg) != (valueClear{"x"}) {
   150  		t.Error("wrong setTail behavior")
   151  	}
   152  }
   153  
   154  func TestValue_colors(t *testing.T) {
   155  	test := func(name string, v Value, clr Color) {
   156  		t.Helper()
   157  		if c := v.Color(); c != clr {
   158  			t.Errorf("wrong color for %s: want %d, got %d", name, clr, c)
   159  		}
   160  	}
   161  	// colorized
   162  	test("Reset", Reset("x"), 0)
   163  	test("Bold", Bold("x"), BoldFm)
   164  	test("Faint", Faint("x"), FaintFm)
   165  	test("DoublyUnderline", DoublyUnderline("x"), DoublyUnderlineFm)
   166  	test("Fraktur & Yellow", Yellow(Fraktur("x")), FrakturFm|YellowFg)
   167  	test("Italic", Italic("x"), ItalicFm)
   168  	test("Underline", Underline("x"), UnderlineFm)
   169  	test("SlowBlink", SlowBlink("x"), SlowBlinkFm)
   170  	test("RapidBlink", RapidBlink("x"), RapidBlinkFm)
   171  	test("Blink", Blink("x"), BlinkFm)
   172  	test("Reverse", Reverse("x"), ReverseFm)
   173  	test("Inverse", Inverse("x"), InverseFm)
   174  	test("Conceal", Conceal("x"), ConcealFm)
   175  	test("Hidden", Hidden("x"), HiddenFm)
   176  	test("CrossedOut", CrossedOut("x"), CrossedOutFm)
   177  	test("StrikeThrough", StrikeThrough("x"), StrikeThroughFm)
   178  	test("Framed", Framed("x"), FramedFm)
   179  	test("Encircled", Encircled("x"), EncircledFm)
   180  	test("Overlined", Overlined("x"), OverlinedFm)
   181  	test("Black", Black("x"), BlackFg)
   182  	test("Red", Red("x"), RedFg)
   183  	test("Green", Green("x"), GreenFg)
   184  	test("Yellow", Yellow("x"), YellowFg)
   185  	test("Brown", Brown("x"), BrownFg)
   186  	test("Blue", Blue("x"), BlueFg)
   187  	test("Magenta", Magenta("x"), MagentaFg)
   188  	test("Cyan", Cyan("x"), CyanFg)
   189  	test("White", White("x"), WhiteFg)
   190  	test("BrightBlack", BrightBlack("x"), BrightFg|BlackFg)
   191  	test("BrightRed", BrightRed("x"), BrightFg|RedFg)
   192  	test("BrightGreen", BrightGreen("x"), BrightFg|GreenFg)
   193  	test("BrightYellow", BrightYellow("x"), BrightFg|YellowFg)
   194  	test("BrightBlue", BrightBlue("x"), BrightFg|BlueFg)
   195  	test("BrightMagenta", BrightMagenta("x"), BrightFg|MagentaFg)
   196  	test("BrightCyan", BrightCyan("x"), BrightFg|CyanFg)
   197  	test("BrightWhite", BrightWhite("x"), BrightFg|WhiteFg)
   198  	test("Index", Index(178, "x"), (Color(178)<<shiftFg)|flagFg)
   199  	test("Gray", Gray(14, "x"), (Color(14+232)<<shiftFg)|flagFg)
   200  	test("BgBlack", BgBlack("x"), BlackBg)
   201  	test("BgRed", BgRed("x"), RedBg)
   202  	test("BgGreen", BgGreen("x"), GreenBg)
   203  	test("BgYellow", BgYellow("x"), YellowBg)
   204  	test("BgBrown", BgBrown("x"), BrownBg)
   205  	test("BgBlue", BgBlue("x"), BlueBg)
   206  	test("BgMagenta", BgMagenta("x"), MagentaBg)
   207  	test("BgCyan", BgCyan("x"), CyanBg)
   208  	test("BgWhite", BgWhite("x"), WhiteBg)
   209  	test("BgBrightBlack", BgBrightBlack("x"), BrightBg|BlackBg)
   210  	test("BgBrightRed", BgBrightRed("x"), BrightBg|RedBg)
   211  	test("BgBrightGreen", BgBrightGreen("x"), BrightBg|GreenBg)
   212  	test("BgBrightYellow", BgBrightYellow("x"), BrightBg|YellowBg)
   213  	test("BgBrightBlue", BgBrightBlue("x"), BrightBg|BlueBg)
   214  	test("BgBrightMagenta", BgBrightMagenta("x"), BrightBg|MagentaBg)
   215  	test("BgBrightCyan", BgBrightCyan("x"), BrightBg|CyanBg)
   216  	test("BgBrightWhite", BgBrightWhite("x"), BrightBg|WhiteBg)
   217  	test("BgIndex", BgIndex(187, "x"), Color(187)<<shiftBg|flagBg)
   218  	test("BgGray", BgGray(15, "x"), Color(232+15)<<shiftBg|flagBg)
   219  	test("Colorize", Colorize("x", RedFg|BlueBg|BrightBg|BoldFm),
   220  		RedFg|BlueBg|BrightBg|BoldFm)
   221  	// clear
   222  	test("Reset", valueClear{"x"}.Reset(), 0)
   223  	test("Bold", valueClear{"x"}.Bold(), 0)
   224  	test("Faint", valueClear{"x"}.Faint(), 0)
   225  	test("DoublyUnderline", valueClear{"x"}.DoublyUnderline(), 0)
   226  	test("Fraktur & Yellow", valueClear{"x"}.Fraktur(), 0)
   227  	test("Italic", valueClear{"x"}.Italic(), 0)
   228  	test("Underline", valueClear{"x"}.Underline(), 0)
   229  	test("SlowBlink", valueClear{"x"}.SlowBlink(), 0)
   230  	test("RapidBlink", valueClear{"x"}.RapidBlink(), 0)
   231  	test("Blink", valueClear{"x"}.Blink(), 0)
   232  	test("Reverse", valueClear{"x"}.Reverse(), 0)
   233  	test("Inverse", valueClear{"x"}.Inverse(), 0)
   234  	test("Conceal", valueClear{"x"}.Conceal(), 0)
   235  	test("Hidden", valueClear{"x"}.Hidden(), 0)
   236  	test("CrossedOut", valueClear{"x"}.CrossedOut(), 0)
   237  	test("StrikeThrough", valueClear{"x"}.StrikeThrough(), 0)
   238  	test("Framed", valueClear{"x"}.Framed(), 0)
   239  	test("Encircled", valueClear{"x"}.Encircled(), 0)
   240  	test("Overlined", valueClear{"x"}.Overlined(), 0)
   241  	test("Black", valueClear{"x"}.Black(), 0)
   242  	test("Red", valueClear{"x"}.Red(), 0)
   243  	test("Green", valueClear{"x"}.Green(), 0)
   244  	test("Yellow", valueClear{"x"}.Yellow(), 0)
   245  	test("Brown", valueClear{"x"}.Brown(), 0)
   246  	test("Blue", valueClear{"x"}.Blue(), 0)
   247  	test("Magenta", valueClear{"x"}.Magenta(), 0)
   248  	test("Cyan", valueClear{"x"}.Cyan(), 0)
   249  	test("White", valueClear{"x"}.White(), 0)
   250  	test("BrightBlack", valueClear{"x"}.BrightBlack(), 0)
   251  	test("BrightRed", valueClear{"x"}.BrightRed(), 0)
   252  	test("BrightGreen", valueClear{"x"}.BrightGreen(), 0)
   253  	test("BrightYellow", valueClear{"x"}.BrightYellow(), 0)
   254  	test("BrightBlue", valueClear{"x"}.BrightBlue(), 0)
   255  	test("BrightMagenta", valueClear{"x"}.BrightMagenta(), 0)
   256  	test("BrightCyan", valueClear{"x"}.BrightCyan(), 0)
   257  	test("BrightWhite", valueClear{"x"}.BrightWhite(), 0)
   258  	test("Index", valueClear{"x"}.Index(178), 0)
   259  	test("Gray", valueClear{"x"}.Gray(14), 0)
   260  	test("BgBlack", valueClear{"x"}.BgBlack(), 0)
   261  	test("BgRed", valueClear{"x"}.BgRed(), 0)
   262  	test("BgGreen", valueClear{"x"}.BgGreen(), 0)
   263  	test("BgYellow", valueClear{"x"}.BgYellow(), 0)
   264  	test("BgBrown", valueClear{"x"}.BgBrown(), 0)
   265  	test("BgBlue", valueClear{"x"}.BgBlue(), 0)
   266  	test("BgMagenta", valueClear{"x"}.BgMagenta(), 0)
   267  	test("BgCyan", valueClear{"x"}.BgCyan(), 0)
   268  	test("BgWhite", valueClear{"x"}.BgWhite(), 0)
   269  	test("BgBrightBlack", valueClear{"x"}.BgBrightBlack(), 0)
   270  	test("BgBrightRed", valueClear{"x"}.BgBrightRed(), 0)
   271  	test("BgBrightGreen", valueClear{"x"}.BgBrightGreen(), 0)
   272  	test("BgBrightYellow", valueClear{"x"}.BgBrightYellow(), 0)
   273  	test("BgBrightBlue", valueClear{"x"}.BgBrightBlue(), 0)
   274  	test("BgBrightMagenta", valueClear{"x"}.BgBrightMagenta(), 0)
   275  	test("BgBrightCyan", valueClear{"x"}.BgBrightCyan(), 0)
   276  	test("BgBrightWhite", valueClear{"x"}.BgBrightWhite(), 0)
   277  	test("BgIndex", valueClear{"x"}.BgIndex(187), 0)
   278  	test("BgGray", valueClear{"x"}.BgGray(15), 0)
   279  	test("Colorize", valueClear{"x"}.Colorize(RedFg|BlueBg|BrightBg|BoldFm), 0)
   280  	// change
   281  	test("Reset", Bold("x").Reset(), 0)
   282  	test("Bold", Faint("x").Bold(), BoldFm)
   283  	test("Faint", Bold("x").Faint(), FaintFm)
   284  	test("DoublyUnderline", Reset("x").DoublyUnderline(), DoublyUnderlineFm)
   285  	test("Fraktur & Yellow", Reset("x").Yellow().Fraktur(), FrakturFm|YellowFg)
   286  	test("Italic", Reset("x").Italic(), ItalicFm)
   287  	test("Underline", Reset("x").Underline(), UnderlineFm)
   288  	test("SlowBlink", RapidBlink("x").SlowBlink(), SlowBlinkFm)
   289  	test("RapidBlink", SlowBlink("x").RapidBlink(), RapidBlinkFm)
   290  	test("Blink", Reset("x").Blink(), BlinkFm)
   291  	test("Reverse", Reset("x").Reverse(), ReverseFm)
   292  	test("Inverse", Reset("x").Inverse(), InverseFm)
   293  	test("Conceal", Reset("x").Conceal(), ConcealFm)
   294  	test("Hidden", Reset("x").Hidden(), HiddenFm)
   295  	test("CrossedOut", Reset("x").CrossedOut(), CrossedOutFm)
   296  	test("StrikeThrough", Reset("x").StrikeThrough(), StrikeThroughFm)
   297  	test("Framed", Reset("x").Framed(), FramedFm)
   298  	test("Encircled", Reset("x").Encircled(), EncircledFm)
   299  	test("Overlined", Reset("x").Overlined(), OverlinedFm)
   300  	test("Black", Reset("x").Black(), BlackFg)
   301  	test("Red", Reset("x").Red(), RedFg)
   302  	test("Green", Reset("x").Green(), GreenFg)
   303  	test("Yellow", Reset("x").Yellow(), YellowFg)
   304  	test("Brown", Reset("x").Brown(), BrownFg)
   305  	test("Blue", Reset("x").Blue(), BlueFg)
   306  	test("Magenta", Reset("x").Magenta(), MagentaFg)
   307  	test("Cyan", Reset("x").Cyan(), CyanFg)
   308  	test("White", Reset("x").White(), WhiteFg)
   309  	test("BrightBlack", Reset("x").BrightBlack(), BrightFg|BlackFg)
   310  	test("BrightRed", Reset("x").BrightRed(), BrightFg|RedFg)
   311  	test("BrightGreen", Reset("x").BrightGreen(), BrightFg|GreenFg)
   312  	test("BrightYellow", Reset("x").BrightYellow(), BrightFg|YellowFg)
   313  	test("BrightBlue", Reset("x").BrightBlue(), BrightFg|BlueFg)
   314  	test("BrightMagenta", Reset("x").BrightMagenta(), BrightFg|MagentaFg)
   315  	test("BrightCyan", Reset("x").BrightCyan(), BrightFg|CyanFg)
   316  	test("BrightWhite", Reset("x").BrightWhite(), BrightFg|WhiteFg)
   317  	test("Index", Reset("x").Index(178), (Color(178)<<shiftFg)|flagFg)
   318  	test("Gray", Reset("x").Gray(14), (Color(14+232)<<shiftFg)|flagFg)
   319  	test("BgBlack", Reset("x").BgBlack(), BlackBg)
   320  	test("BgRed", Reset("x").BgRed(), RedBg)
   321  	test("BgGreen", Reset("x").BgGreen(), GreenBg)
   322  	test("BgYellow", Reset("x").BgYellow(), YellowBg)
   323  	test("BgBrown", Reset("x").BgBrown(), BrownBg)
   324  	test("BgBlue", Reset("x").BgBlue(), BlueBg)
   325  	test("BgMagenta", Reset("x").BgMagenta(), MagentaBg)
   326  	test("BgCyan", Reset("x").BgCyan(), CyanBg)
   327  	test("BgWhite", Reset("x").BgWhite(), WhiteBg)
   328  	test("BgBrightBlack", Reset("x").BgBrightBlack(), BrightBg|BlackBg)
   329  	test("BgBrightRed", Reset("x").BgBrightRed(), BrightBg|RedBg)
   330  	test("BgBrightGreen", Reset("x").BgBrightGreen(), BrightBg|GreenBg)
   331  	test("BgBrightYellow", Reset("x").BgBrightYellow(), BrightBg|YellowBg)
   332  	test("BgBrightBlue", Reset("x").BgBrightBlue(), BrightBg|BlueBg)
   333  	test("BgBrightMagenta", Reset("x").BgBrightMagenta(), BrightBg|MagentaBg)
   334  	test("BgBrightCyan", Reset("x").BgBrightCyan(), BrightBg|CyanBg)
   335  	test("BgBrightWhite", Reset("x").BgBrightWhite(), BrightBg|WhiteBg)
   336  	test("BgIndex", Reset("x").BgIndex(187), Color(187)<<shiftBg|flagBg)
   337  	test("BgGray", Reset("x").BgGray(15), Color(232+15)<<shiftBg|flagBg)
   338  	test("Colorize", Reset("x").Colorize(RedFg|BlueBg|BrightBg|BoldFm),
   339  		RedFg|BlueBg|BrightBg|BoldFm)
   340  	// overflow
   341  	test("Gray", Reset("x").Gray(151), Color(232+23)<<shiftFg|flagFg)
   342  	test("BgGray", Reset("x").BgGray(115), Color(232+23)<<shiftBg|flagBg)
   343  }
   344  

View as plain text