github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/ansi/ansi_test.go (about) 1 package ansi 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/lmorg/murex/config" 8 "github.com/lmorg/murex/lang" 9 "github.com/lmorg/murex/lang/types" 10 "github.com/lmorg/murex/test/count" 11 "github.com/lmorg/murex/utils/ansi/codes" 12 ) 13 14 // TestAnsiColoured tests writing colours via the ansi package 15 func TestAnsiColoured(t *testing.T) { 16 count.Tests(t, 1) 17 18 lang.ShellProcess.Config = config.InitConf 19 lang.ShellProcess.Config.Define("shell", "color", config.Properties{ 20 DataType: types.Boolean, 21 Default: true, 22 Description: "test data", 23 }) 24 25 text := "This is only a test" 26 message := "{RED}" + text + "{RESET}" 27 output := ExpandConsts(message) 28 29 if output != codes.FgRed+text+codes.Reset { 30 t.Error("Colourised config: Source string does not match expected output string: " + output) 31 } 32 if output == message { 33 t.Error("Colourised config: Source string has had no variables substituted") 34 } 35 if output == text { 36 t.Error("Colourised config: Source string variables substituted with zero length string") 37 } 38 39 } 40 41 // TestAnsiNoColour tests the color override disables the ansi package 42 func TestAnsiNoColour(t *testing.T) { 43 count.Tests(t, 1) 44 45 lang.ShellProcess.Config = config.InitConf 46 lang.ShellProcess.Config.Define("shell", "color", config.Properties{ 47 DataType: types.Boolean, 48 Default: false, 49 Description: "test data", 50 }) 51 52 text := "This is only a test" 53 message := "{RED}" + text + "{RESET}" 54 output := ExpandConsts(message) 55 56 if output != text { 57 t.Error("No colour override: Source string does not match expected output string: " + output) 58 } 59 } 60 61 func TestChar20Leak(t *testing.T) { 62 tests := []string{ 63 `{ESC}123`, 64 ` {ESC}123`, 65 ` {ESC}123`, 66 "\t{ESC}123", 67 `123{ESC}123`, 68 `{ESC}123`, 69 `123123`, 70 `{ESC}`, 71 } 72 73 lang.ShellProcess.Config = config.InitConf 74 lang.ShellProcess.Config.Define("shell", "color", config.Properties{ 75 DataType: types.Boolean, 76 Default: true, 77 Description: "test data", 78 }) 79 80 for i, test := range tests { 81 expected := strings.ReplaceAll(test, "{ESC}", "\x1b") 82 actual := ExpandConsts(test) 83 if expected != actual { 84 t.Errorf("mismatch in test %d", i) 85 t.Logf(" test: '%s'", test) 86 t.Logf(" expected: %v", []byte(expected)) 87 t.Logf(" actual: %v", []byte(actual)) 88 } 89 } 90 91 count.Tests(t, len(tests)) 92 }