// // Copyright (c) 2016-2022 The Aurora Authors. All rights reserved. // This program is free software. It comes without any warranty, // to the extent permitted by applicable law. You can redistribute // it and/or modify it under the terms of the Unlicense. See LICENSE // file for more details or see below. // // // This is free and unencumbered software released into the public domain. // // Anyone is free to copy, modify, publish, use, compile, sell, or // distribute this software, either in source code form or as a compiled // binary, for any purpose, commercial or non-commercial, and by any // means. // // In jurisdictions that recognize copyright laws, the author or authors // of this software dedicate any and all copyright interest in the // software to the public domain. We make this dedication for the benefit // of the public at large and to the detriment of our heirs and // successors. We intend this dedication to be an overt act of // relinquishment in perpetuity of all present and future rights to this // software under copyright law. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // // For more information, please refer to // package aurora import ( "fmt" "testing" "github.com/stretchr/testify/assert" ) func TestValue_String(t *testing.T) { var au *Aurora // colorized au = New() assert.Equal(t, "x", au.Clear("x").String()) assert.Equal(t, "\033[30;41mx\033[0m", au.Black("x").BgRed().String()) // clear au = New(WithColors(false)) assert.Equal(t, "x", au.Black("x").BgRed().String()) // colorized hyperlink au = New() assert.Equal(t, `]8;;http://example.com`+ `\x]8;;\`, au.Red("x").Hyperlink("http://example.com").String()) // clear hyperlink assert.Equal(t, `]8;;http://example.com\x]8;;\`, au.Clear("x").Hyperlink("http://example.com").String()) } func TestValue_Color(t *testing.T) { // colorized assert.Equal(t, RedFg, New().Red("x").Color()) // clear, also red assert.Equal(t, RedFg, New().Red("x").Color()) } func TestValue_Value(t *testing.T) { // colorized assert.Equal(t, "x", New().Clear("x").Value()) // clear assert.Equal(t, "x", New(WithColors(false)).Black("x").BgRed().Value()) } func TestValue_Reset(t *testing.T) { // colorized var au = New() assert.Equal(t, Value{ cc: au.cc, value: "x", }, au.Red("x").BgBlack().Reset()) // clear au = New(WithColors(false)) assert.Equal(t, Value{ cc: au.cc, value: "x", }, au.Red("x").BgBlack().Reset()) // a hyperlink au = New() assert.Equal(t, Value{ cc: au.cc, value: "x", }, au.Red("x"). BgBlack(). Hyperlink("http://example.com/path", HyperlinkID("10")). Reset()) } func TestValue_Clear(t *testing.T) { // colorized var au = New() assert.Equal(t, Value{ cc: au.cc, value: "x", }, au.Red("x").BgBlack().Clear()) // clear au = New(WithColors(false)) assert.Equal(t, Value{ cc: au.cc, value: "x", }, au.Red("x").BgBlack().Clear()) // a hyperlink au = New() assert.EqualValues(t, Value{ cc: au.cc, value: "x", hyperlink: &hyperlink{ target: "http://example.com/path", params: []HyperlinkParam{{ Key: HyperlinkIDKey, Value: "10", }}, }, }, au.Red("x"). BgBlack(). Hyperlink("http://example.com/path", HyperlinkID("10")). Clear()) } func TestValue_Format(t *testing.T) { // colorized var au = New() assert.Equal(t, "\033[31;44m"+fmt.Sprintf("%+1.3g", 3.14)+"\033[0m", fmt.Sprintf("%+1.3g", au.Red(3.14).BgBlue())) const utf8Verb = "%+1.3世" // verb that fit more then 1 byte assert.Equal(t, "\033[31;44m"+"%!世(float64=+3.14)"+"\033[0m", fmt.Sprintf(utf8Verb, au.Red(3.14).BgBlue())) //nolint // clear au = New(WithColors(false)) assert.Equal(t, fmt.Sprintf("%+1.3g", 3.14), fmt.Sprintf("%+1.3g", au.Red(3.14).BgBlue())) assert.Equal(t, "%!世(float64=+3.14)", fmt.Sprintf(utf8Verb, au.Red(3.14).BgBlue())) //nolint } func TestValue_colors(t *testing.T) { var test = func(name string, v Value, clr Color) { t.Helper() assert.Equal(t, clr, v.Color()) } // colorized var au = New() test("Reset", au.Reset("x"), 0) test("Clear", au.Clear("x"), 0) test("Bold", au.Bold("x"), BoldFm) test("Faint", au.Faint("x"), FaintFm) test("DoublyUnderline", au.DoublyUnderline("x"), DoublyUnderlineFm) test("Fraktur & au.Yellow", Yellow(Fraktur("x")), FrakturFm|YellowFg) test("Italic", au.Italic("x"), ItalicFm) test("Underline", au.Underline("x"), UnderlineFm) test("SlowBlink", au.SlowBlink("x"), SlowBlinkFm) test("RapidBlink", au.RapidBlink("x"), RapidBlinkFm) test("Blink", au.Blink("x"), BlinkFm) test("Reverse", au.Reverse("x"), ReverseFm) test("Inverse", au.Inverse("x"), InverseFm) test("Conceal", au.Conceal("x"), ConcealFm) test("Hidden", au.Hidden("x"), HiddenFm) test("CrossedOut", au.CrossedOut("x"), CrossedOutFm) test("StrikeThrough", au.StrikeThrough("x"), StrikeThroughFm) test("Framed", au.Framed("x"), FramedFm) test("Encircled", au.Encircled("x"), EncircledFm) test("Overlined", au.Overlined("x"), OverlinedFm) test("Black", au.Black("x"), BlackFg) test("Red", au.Red("x"), RedFg) test("Green", au.Green("x"), GreenFg) test("Yellow", au.Yellow("x"), YellowFg) test("Blue", au.Blue("x"), BlueFg) test("Magenta", au.Magenta("x"), MagentaFg) test("Cyan", au.Cyan("x"), CyanFg) test("White", au.White("x"), WhiteFg) test("BrightBlack", au.BrightBlack("x"), BrightFg|BlackFg) test("BrightRed", au.BrightRed("x"), BrightFg|RedFg) test("BrightGreen", au.BrightGreen("x"), BrightFg|GreenFg) test("BrightYellow", au.BrightYellow("x"), BrightFg|YellowFg) test("BrightBlue", au.BrightBlue("x"), BrightFg|BlueFg) test("BrightMagenta", au.BrightMagenta("x"), BrightFg|MagentaFg) test("BrightCyan", au.BrightCyan("x"), BrightFg|CyanFg) test("BrightWhite", au.BrightWhite("x"), BrightFg|WhiteFg) test("Index", au.Index(178, "x"), (Color(178)<