github.hscsec.cn/u-root/u-root@v7.0.0+incompatible/cmds/core/tr/tr_test.go (about) 1 // Copyright 2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "bytes" 9 "testing" 10 "unicode" 11 12 "github.com/u-root/u-root/pkg/testutil" 13 ) 14 15 func TestUnescape(t *testing.T) { 16 if _, err := unescape("a\\tb\\nc\\d"); err == nil { 17 t.Errorf("unescape() expected error, got nil") 18 } 19 got, err := unescape("a\\tb\\nc\\\\") 20 if err != nil { 21 t.Fatalf("unescape() error: %v", err) 22 } 23 want := "a\tb\nc\\" 24 if string(got) != want { 25 t.Errorf("unescape() want %q, got %q", want, got) 26 } 27 } 28 29 func TestTR(t *testing.T) { 30 for _, test := range []struct { 31 name string 32 input string 33 output string 34 t *transformer 35 }{ 36 { 37 name: "alnum", 38 input: "0123456789!&?defgh", 39 output: "zzzzzzzzzz!&?zzzzz", 40 t: setToRune(ALNUM, 'z'), 41 }, 42 { 43 name: "alpha", 44 input: "0123456789abcdefgh", 45 output: "0123456789zzzzzzzz", 46 t: setToRune(ALPHA, 'z'), 47 }, 48 { 49 name: "digit", 50 input: "0123456789abcdefgh", 51 output: "zzzzzzzzzzabcdefgh", 52 t: setToRune(DIGIT, 'z'), 53 }, 54 { 55 name: "lower", 56 input: "0123456789abcdEFGH", 57 output: "0123456789zzzzEFGH", 58 t: setToRune(LOWER, 'z'), 59 }, 60 { 61 name: "upper", 62 input: "0123456789abcdEFGH", 63 output: "0123456789abcdzzzz", 64 t: setToRune(UPPER, 'z'), 65 }, 66 { 67 name: "punct", 68 input: "012345*{}[]!.?&()def", 69 output: "012345zzzzzzzzzzzdef", 70 t: setToRune(PUNCT, 'z'), 71 }, 72 { 73 name: "space", 74 input: "0123456789\t\ncdef", 75 output: "0123456789zzcdef", 76 t: setToRune(SPACE, 'z'), 77 }, 78 { 79 name: "graph", 80 input: "\f\t🔫123456789abcdEFG", 81 output: "\f\tzzzzzzzzzzzzzzzzz", 82 t: setToRune(GRAPH, 'z'), 83 }, 84 { 85 name: "lower_to_upper", 86 input: "0123456789abcdEFGH", 87 output: "0123456789ABCDEFGH", 88 t: lowerToUpper(), 89 }, 90 { 91 name: "upper_to_lower", 92 input: "0123456789abcdEFGH", 93 output: "0123456789abcdefgh", 94 t: upperToLower(), 95 }, 96 { 97 name: "runes_to_runes", 98 input: "0123456789abcdEFGH", 99 output: "012x45678yabcdzFGH", 100 t: runesToRunes([]rune("39E"), 'x', 'y', 'z'), 101 }, 102 { 103 name: "runes_to_runes_truncated", 104 input: "0123456789abcdEFGH", 105 output: "012x45678yabcdyFGH", 106 t: runesToRunes([]rune("39E"), 'x', 'y'), 107 }, 108 { 109 name: "delete_alnum", 110 input: "0123456789abcdEFGH", 111 output: "", 112 t: setToRune(ALNUM, unicode.ReplacementChar), 113 }, 114 } { 115 t.Run(test.name, func(t *testing.T) { 116 out := &bytes.Buffer{} 117 test.t.run(bytes.NewBufferString(test.input), out) 118 res := out.String() 119 if test.output != res { 120 t.Errorf("run() want %q, got %q", test.output, res) 121 } 122 }) 123 } 124 } 125 126 func TestMain(m *testing.M) { 127 testutil.Run(m, main) 128 }