github.com/jlowellwofford/u-root@v1.0.0/cmds/wc/wc_test.go (about) 1 // Copyright 2016-2017 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 "io/ioutil" 10 "os" 11 "testing" 12 13 "github.com/u-root/u-root/pkg/testutil" 14 ) 15 16 func TestWc(t *testing.T) { 17 var tab = []struct { 18 i string 19 o string 20 s int 21 a []string 22 }{ 23 {"simple test count words", "4\n", 0, []string{"-w"}}, // don't fail more 24 {"lines\nlines\n", "2\n", 0, []string{"-l"}}, 25 {"count chars\n", "12\n", 0, []string{"-c"}}, 26 } 27 28 tmpDir, err := ioutil.TempDir("", "TestWc") 29 if err != nil { 30 t.Fatal("TempDir failed: ", err) 31 } 32 defer os.RemoveAll(tmpDir) 33 34 for _, v := range tab { 35 c := testutil.Command(t, v.a...) 36 c.Stdin = bytes.NewReader([]byte(v.i)) 37 o, err := c.CombinedOutput() 38 if err := testutil.IsExitCode(err, v.s); err != nil { 39 t.Error(err) 40 continue 41 } 42 if string(o) != v.o { 43 t.Errorf("Wc %v < %v: want '%v', got '%v'", v.a, v.i, v.o, string(o)) 44 continue 45 } 46 } 47 } 48 49 func TestMain(m *testing.M) { 50 testutil.Run(m, main) 51 }