github.com/craftyguy/u-root@v1.0.0/cmds/uniq/uniq_test.go (about)

     1  // Copyright 2016 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 TestUniq(t *testing.T) {
    17  	var (
    18  		input1 string = "test\ntest\ngo\ngo\ngo\ncoool\ncoool\ncool\nlegaal\ntest\n"
    19  		input2 string = "u-root\nuniq\nron\nron\nteam\nbinaries\ntest\n\n\n\n\n\n"
    20  		tab           = []struct {
    21  			i string
    22  			o string
    23  			s int
    24  			a []string
    25  		}{
    26  			{input1, "test\ngo\ncoool\ncool\nlegaal\ntest\n", 0, nil},
    27  			{input1, "2\ttest\n3\tgo\n2\tcoool\n1\tcool\n1\tlegaal\n1\ttest\n", 0, []string{"-c"}},
    28  			{input1, "cool\nlegaal\ntest\n", 0, []string{"-u"}},
    29  			{input1, "test\ngo\ncoool\n", 0, []string{"-d"}},
    30  			{input2, "u-root\nuniq\nron\nteam\nbinaries\ntest\n\n", 0, nil},
    31  			{input2, "1\tu-root\n1\tuniq\n2\tron\n1\tteam\n1\tbinaries\n1\ttest\n5\t\n", 0, []string{"-c"}},
    32  			{input2, "u-root\nuniq\nteam\nbinaries\ntest\n", 0, []string{"-u"}},
    33  			{input2, "ron\n\n", 0, []string{"-d"}},
    34  		}
    35  	)
    36  
    37  	tmpDir, err := ioutil.TempDir("", "UniqTest")
    38  	if err != nil {
    39  		t.Fatal("TempDir failed: ", err)
    40  	}
    41  	defer os.RemoveAll(tmpDir)
    42  
    43  	for _, v := range tab {
    44  		c := testutil.Command(t, v.a...)
    45  		c.Stdin = bytes.NewReader([]byte(v.i))
    46  		o, err := c.CombinedOutput()
    47  		if err := testutil.IsExitCode(err, v.s); err != nil {
    48  			t.Error(err)
    49  			continue
    50  		}
    51  		if string(o) != v.o {
    52  			t.Errorf("Uniq %v < %v: want '%v', got '%v'", v.a, v.i, v.o, string(o))
    53  			continue
    54  		}
    55  	}
    56  }
    57  
    58  func TestMain(m *testing.M) {
    59  	testutil.Run(m, main)
    60  }