github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/core/strings/strings_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 "fmt" 10 "io/ioutil" 11 "os" 12 "testing" 13 14 "github.com/u-root/u-root/pkg/testutil" 15 ) 16 17 type test struct { 18 name string 19 flags []string 20 in string 21 out string 22 } 23 24 var stringsTests = []test{ 25 { 26 "empty", 27 []string{}, "", "", 28 }, 29 { 30 "sequences are too short", 31 []string{}, "\n\na\nab\n\nabc\nabc\xff\n01\n", "", 32 }, 33 { 34 "entire string is too short", 35 []string{}, "abc", "", 36 }, 37 { 38 "entire string just fits perfectly", 39 []string{}, "abcd", "abcd\n", 40 }, 41 { 42 "entire string is printable", 43 []string{}, "abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz\n", 44 }, 45 { 46 "terminating newline", 47 []string{}, "abcdefghijklmnopqrstuvwxyz\n", "abcdefghijklmnopqrstuvwxyz\n", 48 }, 49 { 50 "mix of printable and non-printable sequences", 51 []string{}, "\n\na123456\nab\n\nabc\nabcde\xff\n01\n", "a123456\nabcde\n", 52 }, 53 { 54 "spaces are printable", 55 []string{}, " abcdefghijklm nopqrstuvwxyz ", " abcdefghijklm nopqrstuvwxyz \n", 56 }, 57 { 58 "shorter value of n", 59 []string{"--n", "1"}, "\n\na\nab\n\nabc\nabc\xff\n01\n", "a\nab\nabc\nabc\n01\n", 60 }, 61 { 62 "larger value of n", 63 []string{"--n", "6"}, "\n\na123456\nab\n\nabc\nabcde\xff\n01\n", "a123456\n", 64 }, 65 } 66 67 // strings < in > out 68 func TestSortWithPipes(t *testing.T) { 69 tmpDir, err := ioutil.TempDir("", "strings") 70 if err != nil { 71 t.Fatal(err) 72 } 73 defer os.RemoveAll(tmpDir) 74 75 // Table-driven testing 76 for _, tt := range stringsTests { 77 t.Run(fmt.Sprintf("%v", tt.name), func(t *testing.T) { 78 cmd := testutil.Command(t, tt.flags...) 79 cmd.Stdin = bytes.NewReader([]byte(tt.in)) 80 out, err := cmd.CombinedOutput() 81 if err != nil { 82 t.Errorf("strings(%#v): %v", tt.in, err) 83 } 84 if string(out) != tt.out { 85 t.Errorf("strings(%#v) = %#v; want %#v", tt.in, 86 string(out), tt.out) 87 } 88 }) 89 } 90 } 91 92 func TestMain(m *testing.M) { 93 testutil.Run(m, main) 94 }