...

Source file src/github.com/logrusorgru/aurora/v3/aurora_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  	"testing"
    40  )
    41  
    42  func isClear(v Value) bool {
    43  	return v.Color() == 0 && v.tail() == 0
    44  }
    45  
    46  func isColor(v Value, clr Color) bool {
    47  	return v.Color() == clr
    48  }
    49  
    50  func isTail(v Value, tl Color) bool {
    51  	return v.tail() == tl
    52  }
    53  
    54  func Test_NewAurora(t *testing.T) {
    55  	if a := NewAurora(false); a == nil {
    56  		t.Error("NewAurora(false) returns nil")
    57  	}
    58  	if a := NewAurora(true); a == nil {
    59  		t.Error("NewAurora(true) returns nil")
    60  	}
    61  	if t.Failed() {
    62  		t.FailNow()
    63  	}
    64  }
    65  
    66  func Test_auroraClear_methods(t *testing.T) {
    67  	a := NewAurora(false)
    68  	test := func(mn string, v Value) {
    69  		if !isClear(v) {
    70  			t.Errorf("NewAurora(false).%s is not clear", mn)
    71  		} else if str, ok := v.Value().(string); !ok {
    72  			t.Errorf("NewAurora(false).%s wrong value type", mn)
    73  		} else if str != "x" {
    74  			t.Errorf("NewAurora(false).%s wrong value", mn)
    75  		}
    76  	}
    77  
    78  	test("Reset", a.Reset("x"))
    79  
    80  	test("Bold", a.Bold("x"))
    81  	test("Faint", a.Faint("x"))
    82  	test("DoublyUnderline", a.DoublyUnderline("x"))
    83  	test("Fraktur", a.Fraktur("x"))
    84  	test("Italic", a.Italic("x"))
    85  	test("Underline", a.Underline("x"))
    86  	test("SlowBlink", a.SlowBlink("x"))
    87  	test("RapidBlink", a.RapidBlink("x"))
    88  	test("Blink", a.Blink("x"))
    89  	test("Reverse", a.Reverse("x"))
    90  	test("Inverse", a.Inverse("x"))
    91  	test("Conceal", a.Conceal("x"))
    92  	test("Hidden", a.Hidden("x"))
    93  	test("CrossedOut", a.CrossedOut("x"))
    94  	test("StrikeThrough", a.StrikeThrough("x"))
    95  	test("Framed", a.Framed("x"))
    96  	test("Encircled", a.Encircled("x"))
    97  	test("Overlined", a.Overlined("x"))
    98  
    99  	test("Black", a.Black("x"))
   100  	test("Red", a.Red("x"))
   101  	test("Green", a.Green("x"))
   102  	test("Yellow", a.Yellow("x"))
   103  	test("Brown", a.Brown("x"))
   104  	test("Blue", a.Blue("x"))
   105  	test("Magenta", a.Magenta("x"))
   106  	test("Cyan", a.Cyan("x"))
   107  	test("White", a.White("x"))
   108  	test("BrightBlack", a.BrightBlack("x"))
   109  	test("BrightRed", a.BrightRed("x"))
   110  	test("BrightGreen", a.BrightGreen("x"))
   111  	test("BrightYellow", a.BrightYellow("x"))
   112  	test("BrightBlue", a.BrightBlue("x"))
   113  	test("BrightMagenta", a.BrightMagenta("x"))
   114  	test("BrightCyan", a.BrightCyan("x"))
   115  	test("BrightWhite", a.BrightWhite("x"))
   116  	test("Index", a.Index(178, "x"))
   117  	test("Gray", a.Gray(14, "x"))
   118  
   119  	test("BgBlack", a.BgBlack("x"))
   120  	test("BgRed", a.BgRed("x"))
   121  	test("BgGreen", a.BgGreen("x"))
   122  	test("BgYellow", a.BgYellow("x"))
   123  	test("BgBrown", a.BgBrown("x"))
   124  	test("BgBlue", a.BgBlue("x"))
   125  	test("BgMagenta", a.BgMagenta("x"))
   126  	test("BgCyan", a.BgCyan("x"))
   127  	test("BgWhite", a.BgWhite("x"))
   128  	test("BgBrightBlack", a.BgBrightBlack("x"))
   129  	test("BgBrightRed", a.BgBrightRed("x"))
   130  	test("BgBrightGreen", a.BgBrightGreen("x"))
   131  	test("BgBrightYellow", a.BgBrightYellow("x"))
   132  	test("BgBrightBlue", a.BgBrightBlue("x"))
   133  	test("BgBrightMagenta", a.BgBrightMagenta("x"))
   134  	test("BgBrightCyan", a.BgBrightCyan("x"))
   135  	test("BgBrightWhite", a.BgBrightWhite("x"))
   136  	test("BgIndex", a.BgIndex(187, "x"))
   137  	test("BgGray", a.BgGray(15, "x"))
   138  
   139  	test("Colorize", a.Colorize("x", RedFg|BlueBg|BrightBg|BoldFm))
   140  
   141  }
   142  
   143  func Test_auroraClear_sprintf(t *testing.T) {
   144  	a := NewAurora(false)
   145  	if s := a.Sprintf(a.Black("x: %d"), a.Blue(2)); s != "x: 2" {
   146  		t.Error("NewAurora(false).Sprintf wrong value")
   147  	}
   148  	if s := a.Sprintf("x: %d", a.Blue(2)); s != "x: 2" {
   149  		t.Error("NewAurora(false).Sprintf wrong value")
   150  	}
   151  }
   152  
   153  func Test_aurora_methods(t *testing.T) {
   154  	a := NewAurora(true)
   155  	test := func(mn string, v Value, clr Color) {
   156  		t.Helper()
   157  		if !isColor(v, clr) {
   158  			t.Errorf("NewAurora(true).%s wrong color: %d", mn, v.Color())
   159  		} else if !isTail(v, 0) {
   160  			t.Errorf("NewAurora(true).%s unexpected tail value", mn)
   161  		} else if str, ok := v.Value().(string); !ok {
   162  			t.Errorf("NewAurora(true).%s wrong value type", mn)
   163  		} else if str != "x" {
   164  			t.Errorf("NewAurora(true).%s wrong value", mn)
   165  		}
   166  	}
   167  	test("Reset", a.Reset("x"), 0)
   168  
   169  	test("Bold", a.Bold("x"), BoldFm)
   170  	test("Faint", a.Faint("x"), FaintFm)
   171  	test("DoublyUnderline", a.DoublyUnderline("x"), DoublyUnderlineFm)
   172  	test("Fraktur", a.Fraktur("x"), FrakturFm)
   173  	test("Italic", a.Italic("x"), ItalicFm)
   174  	test("Underline", a.Underline("x"), UnderlineFm)
   175  	test("SlowBlink", a.SlowBlink("x"), SlowBlinkFm)
   176  	test("RapidBlink", a.RapidBlink("x"), RapidBlinkFm)
   177  	test("Blink", a.Blink("x"), BlinkFm)
   178  	test("Reverse", a.Reverse("x"), ReverseFm)
   179  	test("Inverse", a.Inverse("x"), InverseFm)
   180  	test("Conceal", a.Conceal("x"), ConcealFm)
   181  	test("Hidden", a.Hidden("x"), HiddenFm)
   182  	test("CrossedOut", a.CrossedOut("x"), CrossedOutFm)
   183  	test("StrikeThrough", a.StrikeThrough("x"), StrikeThroughFm)
   184  	test("Framed", a.Framed("x"), FramedFm)
   185  	test("Encircled", a.Encircled("x"), EncircledFm)
   186  	test("Overlined", a.Overlined("x"), OverlinedFm)
   187  
   188  	test("Black", a.Black("x"), BlackFg)
   189  	test("Red", a.Red("x"), RedFg)
   190  	test("Green", a.Green("x"), GreenFg)
   191  	test("Yellow", a.Yellow("x"), YellowFg)
   192  	test("Brown", a.Brown("x"), BrownFg)
   193  	test("Blue", a.Blue("x"), BlueFg)
   194  	test("Magenta", a.Magenta("x"), MagentaFg)
   195  	test("Cyan", a.Cyan("x"), CyanFg)
   196  	test("White", a.White("x"), WhiteFg)
   197  	test("BrightBlack", a.BrightBlack("x"), BrightFg|BlackFg)
   198  	test("BrightRed", a.BrightRed("x"), BrightFg|RedFg)
   199  	test("BrightGreen", a.BrightGreen("x"), BrightFg|GreenFg)
   200  	test("BrightYellow", a.BrightYellow("x"), BrightFg|YellowFg)
   201  	test("BrightBlue", a.BrightBlue("x"), BrightFg|BlueFg)
   202  	test("BrightMagenta", a.BrightMagenta("x"), BrightFg|MagentaFg)
   203  	test("BrightCyan", a.BrightCyan("x"), BrightFg|CyanFg)
   204  	test("BrightWhite", a.BrightWhite("x"), BrightFg|WhiteFg)
   205  	test("Index", a.Index(178, "x"), (Color(178)<<shiftFg)|flagFg)
   206  	test("Gray", a.Gray(14, "x"), (Color(232+14)<<shiftFg)|flagFg)
   207  
   208  	test("BgBlack", a.BgBlack("x"), BlackBg)
   209  	test("BgRed", a.BgRed("x"), RedBg)
   210  	test("BgGreen", a.BgGreen("x"), GreenBg)
   211  	test("BgYellow", a.BgYellow("x"), YellowBg)
   212  	test("BgBrown", a.BgBrown("x"), BrownBg)
   213  	test("BgBlue", a.BgBlue("x"), BlueBg)
   214  	test("BgMagenta", a.BgMagenta("x"), MagentaBg)
   215  	test("BgCyan", a.BgCyan("x"), CyanBg)
   216  	test("BgWhite", a.BgWhite("x"), WhiteBg)
   217  	test("BgBrightBlack", a.BgBrightBlack("x"), BrightBg|BlackBg)
   218  	test("BgBrightRed", a.BgBrightRed("x"), BrightBg|RedBg)
   219  	test("BgBrightGreen", a.BgBrightGreen("x"), BrightBg|GreenBg)
   220  	test("BgBrightYellow", a.BgBrightYellow("x"), BrightBg|YellowBg)
   221  	test("BgBrightBlue", a.BgBrightBlue("x"), BrightBg|BlueBg)
   222  	test("BgBrightMagenta", a.BgBrightMagenta("x"), BrightBg|MagentaBg)
   223  	test("BgBrightCyan", a.BgBrightCyan("x"), BrightBg|CyanBg)
   224  	test("BgBrightWhite", a.BgBrightWhite("x"), BrightBg|WhiteBg)
   225  	test("BgIndex", a.BgIndex(187, "x"), (Color(187)<<shiftBg)|flagBg)
   226  	test("BgGray", a.BgGray(15, "x"), (Color(15+232)<<shiftBg)|flagBg)
   227  
   228  	test("Colorize", a.Colorize("x", RedFg|BlueBg|BrightBg|BoldFm),
   229  		RedFg|BlueBg|BrightBg|BoldFm)
   230  }
   231  
   232  func Test_aurora_Sprintf(t *testing.T) {
   233  	a := NewAurora(true)
   234  	s := a.Sprintf(a.Black("x: %dB"), a.Blue(2))
   235  	if s != "\033[30mx: \033[0;34m2\033[0;30mB\033[0m" {
   236  		t.Errorf("NewAurora(true).Sprintf wrong value: %q", s)
   237  	}
   238  	s = a.Sprintf("x: %dB", a.Blue(2))
   239  	if s != "x: \033[34m2\033[0mB" {
   240  		t.Errorf("NewAurora(true).Sprintf wrong value: %q", s)
   241  	}
   242  }
   243  

View as plain text