github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestUnescape(t *testing.T) {
    14  	if _, err := unescape("a\\tb\\nc\\d"); err == nil {
    15  		t.Errorf("unescape() expected error, got nil")
    16  	}
    17  	got, err := unescape("a\\tb\\nc\\\\")
    18  	if err != nil {
    19  		t.Fatalf("unescape() error: %v", err)
    20  	}
    21  	want := "a\tb\nc\\"
    22  	if string(got) != want {
    23  		t.Errorf("unescape() want %q, got %q", want, got)
    24  	}
    25  }
    26  
    27  func TestTR(t *testing.T) {
    28  	for _, test := range []struct {
    29  		name         string
    30  		args         []string
    31  		input        string
    32  		wantOutput   string
    33  		del          bool
    34  		wantNewError bool
    35  	}{
    36  		{
    37  			name:       "alnum",
    38  			args:       []string{"[:alnum:]", "z"},
    39  			input:      "0123456789!&?defgh",
    40  			wantOutput: "zzzzzzzzzz!&?zzzzz",
    41  		},
    42  		{
    43  			name:       "alpha",
    44  			args:       []string{"[:alpha:]", "z"},
    45  			input:      "0123456789abcdefgh",
    46  			wantOutput: "0123456789zzzzzzzz",
    47  		},
    48  		{
    49  			name:       "digit",
    50  			args:       []string{"[:digit:]", "z"},
    51  			input:      "0123456789abcdefgh",
    52  			wantOutput: "zzzzzzzzzzabcdefgh",
    53  		},
    54  		{
    55  			name:       "lower",
    56  			args:       []string{"[:lower:]", "z"},
    57  			input:      "0123456789abcdEFGH",
    58  			wantOutput: "0123456789zzzzEFGH",
    59  		},
    60  		{
    61  			name:       "upper",
    62  			args:       []string{"[:upper:]", "z"},
    63  			input:      "0123456789abcdEFGH",
    64  			wantOutput: "0123456789abcdzzzz",
    65  		},
    66  		{
    67  			name:       "punct",
    68  			args:       []string{"[:punct:]", "z"},
    69  			input:      "012345*{}[]!.?&()def",
    70  			wantOutput: "012345zzzzzzzzzzzdef",
    71  		},
    72  		{
    73  			name:       "space",
    74  			args:       []string{"[:space:]", "z"},
    75  			input:      "0123456789\t\ncdef",
    76  			wantOutput: "0123456789zzcdef",
    77  		},
    78  		{
    79  			name:       "graph",
    80  			args:       []string{"[:graph:]", "z"},
    81  			input:      "\f\t🔫123456789abcdEFG",
    82  			wantOutput: "\f\tzzzzzzzzzzzzzzzzz",
    83  		},
    84  		{
    85  			name:       "lower_to_upper",
    86  			args:       []string{"[:lower:]", "[:upper:]"},
    87  			input:      "0123456789abcdEFGH",
    88  			wantOutput: "0123456789ABCDEFGH",
    89  		},
    90  		{
    91  			name:       "upper_to_lower",
    92  			args:       []string{"[:upper:]", "[:lower:]"},
    93  			input:      "0123456789abcdEFGH",
    94  			wantOutput: "0123456789abcdefgh",
    95  		},
    96  		{
    97  			name:       "runes_to_runes",
    98  			args:       []string{"39E", "xyz"},
    99  			input:      "0123456789abcdEFGH",
   100  			wantOutput: "012x45678yabcdzFGH",
   101  		},
   102  		{
   103  			name:       "runes_to_runes_truncated",
   104  			args:       []string{"39E", "xy"},
   105  			input:      "0123456789abcdEFGH",
   106  			wantOutput: "012x45678yabcdyFGH",
   107  		},
   108  		{
   109  			name:       "delete_alnum",
   110  			args:       []string{"[:alnum:]", "\uFFFD"},
   111  			input:      "0123456789abcdEFGH",
   112  			wantOutput: "",
   113  		},
   114  		{
   115  			name:       "delete_all_flag_true",
   116  			args:       []string{"[:alnum:]"},
   117  			input:      "12345",
   118  			del:        true,
   119  			wantOutput: "",
   120  		},
   121  		{
   122  			name:       "delete_lower_flag_true",
   123  			args:       []string{"[:lower:]"},
   124  			input:      "abcdeABCz",
   125  			del:        true,
   126  			wantOutput: "ABC",
   127  		},
   128  		{
   129  			name:         "no args",
   130  			args:         nil,
   131  			wantNewError: true,
   132  		},
   133  		{
   134  			name:         "del is true",
   135  			args:         []string{"a", "b"},
   136  			del:          true,
   137  			wantNewError: true,
   138  		},
   139  		{
   140  			name:         "more than two args",
   141  			args:         []string{"a", "b", "c"},
   142  			wantNewError: true,
   143  		},
   144  	} {
   145  		t.Run(test.name, func(t *testing.T) {
   146  			in := strings.NewReader(test.input)
   147  			out := &bytes.Buffer{}
   148  			cmd, err := newCommand(in, out, test.args, test.del)
   149  			if test.wantNewError {
   150  				if err == nil {
   151  					t.Fatal("expected error got: nil")
   152  				}
   153  				t.Skip()
   154  			}
   155  
   156  			err = cmd.run()
   157  			if err != nil {
   158  				t.Fatal(err)
   159  			}
   160  			res := out.String()
   161  
   162  			if test.wantOutput != res {
   163  				t.Errorf("run() want %q, got %q", test.wantOutput, res)
   164  			}
   165  		})
   166  	}
   167  }