github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/builtin_fn_styled_test.go (about) 1 package eval_test 2 3 import ( 4 "testing" 5 6 "github.com/markusbkk/elvish/pkg/eval" 7 . "github.com/markusbkk/elvish/pkg/eval/evaltest" 8 ) 9 10 func TestStyledSegment(t *testing.T) { 11 Test(t, 12 That("print (styled (styled-segment abc &fg-color=cyan) bold)"). 13 Prints("\033[1;36mabc\033[m"), 14 That("print (styled (styled-segment (styled-segment abc &fg-color=magenta) &dim=$true) cyan)"). 15 Prints("\033[2;36mabc\033[m"), 16 That("print (styled (styled-segment abc &inverse=$true) inverse)"). 17 Prints("\033[7mabc\033[m"), 18 That("print (styled (styled-segment abc) toggle-inverse)"). 19 Prints("\033[7mabc\033[m"), 20 That("print (styled (styled-segment abc &inverse=$true) no-inverse)"). 21 Prints("abc"), 22 That("print (styled (styled-segment abc &inverse=$true) toggle-inverse)"). 23 Prints("abc"), 24 25 That("styled-segment []").Throws(ErrorWithMessage( 26 "argument to styled-segment must be a string or a styled segment")), 27 That("styled-segment text &foo=bar"). 28 Throws(ErrorWithMessage("unrecognized option 'foo'")), 29 ) 30 } 31 32 func TestStyled(t *testing.T) { 33 Test(t, 34 // Transform string 35 That("print (styled abc bold)").Prints("\033[1mabc\033[m"), 36 That("print (styled abc red cyan)").Prints("\033[36mabc\033[m"), 37 That("print (styled abc bg-green)").Prints("\033[42mabc\033[m"), 38 That("print (styled abc no-dim)").Prints("abc"), 39 40 // Transform already styled text 41 That("print (styled (styled abc red) blue)"). 42 Prints("\033[34mabc\033[m"), 43 That("print (styled (styled abc italic) red)"). 44 Prints("\033[3;31mabc\033[m"), 45 That("print (styled (styled abc inverse) inverse)"). 46 Prints("\033[7mabc\033[m"), 47 That("print (styled (styled abc inverse) no-inverse)").Prints("abc"), 48 That("print (styled (styled abc inverse) toggle-inverse)").Prints("abc"), 49 That("print (styled (styled abc inverse) toggle-inverse toggle-inverse)").Prints("\033[7mabc\033[m"), 50 51 // Function as transformer 52 That("print (styled abc {|s| put $s })").Prints("abc"), 53 That("print (styled abc {|s| styled-segment $s &bold=$true &italic=$false })").Prints("\033[1mabc\033[m"), 54 That("print (styled abc italic {|s| styled-segment $s &bold=$true &italic=$false })").Prints("\033[1mabc\033[m"), 55 56 That("styled abc {|_| fail bad }").Throws(eval.FailError{"bad"}), 57 That("styled abc {|_| put a b }").Throws(ErrorWithMessage( 58 "styling function must return a single segment; got 2 values")), 59 That("styled abc {|_| put [] }").Throws(ErrorWithMessage( 60 "styling function must return a segment; got list")), 61 62 // Bad usage 63 That("styled abc hopefully-never-exists").Throws(ErrorWithMessage( 64 "hopefully-never-exists is not a valid style transformer")), 65 That("styled []").Throws(ErrorWithMessage( 66 "expected string, styled segment or styled text; got list")), 67 That("styled abc []").Throws(ErrorWithMessage( 68 "need string or callable; got list")), 69 ) 70 } 71 72 func TestStyled_DoesNotModifyArgument(t *testing.T) { 73 Test(t, 74 That("var x = (styled text); var y = (styled $x red); put $x[0][fg-color]"). 75 Puts("default"), 76 That("var x = (styled-segment text); var y = (styled $x red); put $x[fg-color]"). 77 Puts("default"), 78 ) 79 } 80 81 func TestStyledConcat(t *testing.T) { 82 Test(t, 83 // string+segment 84 That("print abc(styled-segment abc &fg-color=red)").Prints("abc\033[31mabc\033[m"), 85 // segment+string 86 That("print (styled-segment abc &fg-color=red)abc").Prints("\033[31mabc\033[mabc"), 87 // segment+segment 88 That("print (styled-segment abc &bg-color=red)(styled-segment abc &fg-color=red)").Prints("\033[41mabc\033[m\033[31mabc\033[m"), 89 // segment+text 90 That("print (styled-segment abc &underlined=$true)(styled abc bright-cyan)").Prints("\033[4mabc\033[m\033[96mabc\033[m"), 91 // segment+num 92 That("print (num 99.0)(styled-segment abc &blink)").Prints("99.0\033[5mabc\033[m"), 93 That("print (num 66)(styled-segment abc &blink)").Prints("66\033[5mabc\033[m"), 94 That("print (num 3/2)(styled-segment abc &blink)").Prints("3/2\033[5mabc\033[m"), 95 // num+segment 96 That("print (styled-segment abc &blink)(float64 88)").Prints("\033[5mabc\033[m88.0"), 97 That("print (styled-segment abc &blink)(num 44/3)").Prints("\033[5mabc\033[m44/3"), 98 That("print (styled-segment abc &blink)(num 42)").Prints("\033[5mabc\033[m42"), 99 // string+text 100 That("print abc(styled abc blink)").Prints("abc\033[5mabc\033[m"), 101 // text+string 102 That("print (styled abc blink)abc").Prints("\033[5mabc\033[mabc"), 103 // number+text 104 That("print (float64 13)(styled abc blink)").Prints("13.0\033[5mabc\033[m"), 105 That("print (num 13)(styled abc blink)").Prints("13\033[5mabc\033[m"), 106 That("print (num 4/3)(styled abc blink)").Prints("4/3\033[5mabc\033[m"), 107 // text+number 108 That("print (styled abc blink)(float64 127)").Prints("\033[5mabc\033[m127.0"), 109 That("print (styled abc blink)(num 13)").Prints("\033[5mabc\033[m13"), 110 That("print (styled abc blink)(num 3/4)").Prints("\033[5mabc\033[m3/4"), 111 // text+segment 112 That("print (styled abc inverse)(styled-segment abc &bg-color=white)").Prints("\033[7mabc\033[m\033[47mabc\033[m"), 113 // text+text 114 That("print (styled abc bold)(styled abc dim)").Prints("\033[1mabc\033[m\033[2mabc\033[m"), 115 ) 116 } 117 118 func TestStyledIndexing(t *testing.T) { 119 Test(t, 120 That("put (styled-segment abc &italic=$true &fg-color=red)[bold]").Puts(false), 121 That("put (styled-segment abc &italic=$true &fg-color=red)[italic]").Puts(true), 122 That("put (styled-segment abc &italic=$true &fg-color=red)[fg-color]").Puts("red"), 123 ) 124 125 Test(t, 126 That("put (styled abc red)[0][bold]").Puts(false), 127 That("put (styled abc red)[0][bg-color]").Puts("default"), 128 That("var t = (styled-segment abc &underlined=$true)(styled abc bright-cyan); put $t[1][fg-color]").Puts("bright-cyan"), 129 That("var t = (styled-segment abc &underlined=$true)(styled abc bright-cyan); put $t[1][underlined]").Puts(false), 130 ) 131 }