github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/readline/read_test.go (about) 1 package readline 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/test/count" 7 ) 8 9 func TestRemoveNonPrintableChars(t *testing.T) { 10 tests := []struct { 11 Slice string 12 Expected string 13 }{ 14 { 15 Slice: "", 16 Expected: "", 17 }, 18 { 19 Slice: "a", 20 Expected: "a", 21 }, 22 { 23 Slice: "abc", 24 Expected: "abc", 25 }, 26 { 27 Slice: "\t", 28 Expected: "\t", 29 }, 30 { 31 Slice: "\ta", 32 Expected: "\ta", 33 }, 34 { 35 Slice: "a\t", 36 Expected: "a\t", 37 }, 38 { 39 Slice: "a\tb", 40 Expected: "a\tb", 41 }, 42 { 43 Slice: "a\tb\tc", 44 Expected: "a\tb\tc", 45 }, 46 { 47 Slice: "a\t\tb\t\tc", 48 Expected: "a\t\tb\t\tc", 49 }, 50 51 // non printable 52 53 { 54 Slice: "\x16", 55 Expected: "", 56 }, 57 { 58 Slice: "\x16a", 59 Expected: "a", 60 }, 61 { 62 Slice: "a\x16", 63 Expected: "a", 64 }, 65 { 66 Slice: "a\x16b", 67 Expected: "ab", 68 }, 69 { 70 Slice: "a\x16b\x16c", 71 Expected: "abc", 72 }, 73 { 74 Slice: "a\x16\x16b\x16\x16c", 75 Expected: "abc", 76 }, 77 78 // unicode 79 80 { 81 Slice: "世界", 82 Expected: "世界", 83 }, 84 { 85 Slice: "\x16世\x16界\x16", 86 Expected: "世界", 87 }, 88 { 89 Slice: "\x16世界\x16世界\x16", 90 Expected: "世界世界", 91 }, 92 { 93 Slice: "\x16\x16世界\x16\x16世界\x16\x16", 94 Expected: "世界世界", 95 }, 96 { 97 Slice: "😀😁😂", 98 Expected: "😀😁😂", 99 }, 100 { 101 Slice: "\x16😀\x16😁\x16😂", 102 Expected: "😀😁😂", 103 }, 104 { 105 Slice: "\x16😀😁😂\x16😀😁😂\x16", 106 Expected: "😀😁😂😀😁😂", 107 }, 108 { 109 Slice: "\x16\x16😀😁😂\x16\x16😀😁😂\x16\x16", 110 Expected: "😀😁😂😀😁😂", 111 }, 112 } 113 114 count.Tests(t, len(tests)) 115 116 for i, test := range tests { 117 s := []byte(test.Slice) 118 actual := string(s[:removeNonPrintableChars(s)]) 119 120 if test.Expected != actual { 121 t.Errorf("Expected does not match actual in test %d", i) 122 t.Logf(" Slice: '%s'", test.Slice) 123 t.Logf(" Expected: '%s'", test.Expected) 124 t.Logf(" Actual: '%s'", actual) 125 t.Logf(" s bytes: '%v'", []byte(test.Slice)) 126 t.Logf(" e bytes: '%v'", []byte(test.Expected)) 127 t.Logf(" a bytes: '%v'", []byte(actual)) 128 } 129 } 130 }