github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/builtin_fn_styled_test.go (about) 1 package eval_test 2 3 import ( 4 "testing" 5 6 . "src.elv.sh/pkg/eval/evaltest" 7 ) 8 9 func TestStyledString(t *testing.T) { 10 Test(t, 11 That("print (styled abc hopefully-never-exists)").Throws(ErrorWithMessage( 12 "hopefully-never-exists is not a valid style transformer")), 13 14 That("print (styled abc bold)").Prints("\033[1mabc\033[m"), 15 That("print (styled abc red cyan)").Prints("\033[36mabc\033[m"), 16 That("print (styled abc bg-green)").Prints("\033[42mabc\033[m"), 17 That("print (styled abc no-dim)").Prints("abc"), 18 ) 19 } 20 21 func TestStyledSegment(t *testing.T) { 22 Test(t, 23 That("print (styled (styled-segment abc &fg-color=cyan) bold)"). 24 Prints("\033[1;36mabc\033[m"), 25 That("print (styled (styled-segment (styled-segment abc &fg-color=magenta) &dim=$true) cyan)"). 26 Prints("\033[2;36mabc\033[m"), 27 That("print (styled (styled-segment abc &inverse=$true) inverse)"). 28 Prints("\033[7mabc\033[m"), 29 That("print (styled (styled-segment abc) toggle-inverse)"). 30 Prints("\033[7mabc\033[m"), 31 That("print (styled (styled-segment abc &inverse=$true) no-inverse)"). 32 Prints("abc"), 33 That("print (styled (styled-segment abc &inverse=$true) toggle-inverse)"). 34 Prints("abc"), 35 ) 36 } 37 38 func TestStyledText(t *testing.T) { 39 Test(t, 40 That("print (styled (styled abc red) blue)"). 41 Prints("\033[34mabc\033[m"), 42 That("print (styled (styled abc italic) red)"). 43 Prints("\033[3;31mabc\033[m"), 44 That("print (styled (styled abc inverse) inverse)"). 45 Prints("\033[7mabc\033[m"), 46 That("print (styled (styled abc inverse) no-inverse)").Prints("abc"), 47 That("print (styled (styled abc inverse) toggle-inverse)").Prints("abc"), 48 That("print (styled (styled abc inverse) toggle-inverse toggle-inverse)").Prints("\033[7mabc\033[m"), 49 ) 50 } 51 52 func TestStyled_DoesNotModifyArgument(t *testing.T) { 53 Test(t, 54 That("x = (styled text); _ = (styled $x red); put $x[0][fg-color]"). 55 Puts("default"), 56 That("x = (styled-segment text); _ = (styled $x red); put $x[fg-color]"). 57 Puts("default"), 58 ) 59 } 60 61 func TestStyledConcat(t *testing.T) { 62 Test(t, 63 // string+segment 64 That("print abc(styled-segment abc &fg-color=red)").Prints("abc\033[31mabc\033[m"), 65 // segment+string 66 That("print (styled-segment abc &fg-color=red)abc").Prints("\033[31mabc\033[mabc"), 67 // segment+segment 68 That("print (styled-segment abc &bg-color=red)(styled-segment abc &fg-color=red)").Prints("\033[41mabc\033[m\033[31mabc\033[m"), 69 // segment+text 70 That("print (styled-segment abc &underlined=$true)(styled abc bright-cyan)").Prints("\033[4mabc\033[m\033[96mabc\033[m"), 71 ) 72 73 Test(t, 74 // string+text 75 That("print abc(styled abc blink)").Prints("abc\033[5mabc\033[m"), 76 // text+string 77 That("print (styled abc blink)abc").Prints("\033[5mabc\033[mabc"), 78 // number+text 79 That("print (num 13)(styled abc blink)").Prints("13\033[5mabc\033[m"), 80 // text+number 81 That("print (styled abc blink)(num 13)").Prints("\033[5mabc\033[m13"), 82 // text+segment 83 That("print (styled abc inverse)(styled-segment abc &bg-color=white)").Prints("\033[7mabc\033[m\033[47mabc\033[m"), 84 // text+text 85 That("print (styled abc bold)(styled abc dim)").Prints("\033[1mabc\033[m\033[2mabc\033[m"), 86 ) 87 } 88 89 func TestFunctionalStyleStylings(t *testing.T) { 90 // lambda 91 Test(t, 92 That("print (styled abc [s]{ put $s })").Prints("abc"), 93 That("print (styled abc [s]{ styled-segment $s &bold=$true &italic=$false })").Prints("\033[1mabc\033[m"), 94 That("print (styled abc italic [s]{ styled-segment $s &bold=$true &italic=$false })").Prints("\033[1mabc\033[m"), 95 ) 96 97 // fn 98 Test(t, 99 That("fn f [s]{ put $s }; print (styled abc $f~)").Prints("abc"), 100 That("fn f [s]{ styled-segment $s &bold=$true &italic=$false }; print (styled abc $f~)").Prints("\033[1mabc\033[m"), 101 That("fn f [s]{ styled-segment $s &bold=$true &italic=$false }; print (styled abc italic $f~)").Prints("\033[1mabc\033[m"), 102 ) 103 104 // var 105 Test(t, 106 That("f = [s]{ put $s }; print (styled abc $f)").Prints("abc"), 107 That("f = [s]{ styled-segment $s &bold=$true &italic=$false }; print (styled abc $f)").Prints("\033[1mabc\033[m"), 108 That("f = [s]{ styled-segment $s &bold=$true &italic=$false }; print (styled abc italic $f)").Prints("\033[1mabc\033[m"), 109 ) 110 } 111 112 func TestStyledIndexing(t *testing.T) { 113 Test(t, 114 That("put (styled-segment abc &italic=$true &fg-color=red)[bold]").Puts(false), 115 That("put (styled-segment abc &italic=$true &fg-color=red)[italic]").Puts(true), 116 That("put (styled-segment abc &italic=$true &fg-color=red)[fg-color]").Puts("red"), 117 ) 118 119 Test(t, 120 That("put (styled abc red)[0][bold]").Puts(false), 121 That("put (styled abc red)[0][bg-color]").Puts("default"), 122 That("t = (styled-segment abc &underlined=$true)(styled abc bright-cyan); put $t[1][fg-color]").Puts("bright-cyan"), 123 That("t = (styled-segment abc &underlined=$true)(styled abc bright-cyan); put $t[1][underlined]").Puts(false), 124 ) 125 }