sort-cli: init

This commit is contained in:
2024-01-18 21:50:27 +03:00
parent 0b7b36223f
commit 40182d4a46
5 changed files with 301 additions and 0 deletions

57
sort-cli/main_test.go Normal file
View File

@ -0,0 +1,57 @@
package main
import (
"flag"
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)
func TestFlags(t *testing.T) {
// We manipulate the Args to set them up for the testcases
// after this test we restore the initial args
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
cases := []struct {
Name string
Args []string
ExpectedExit int
ExpectedOutput string
}{
{"No flags",
[]string{"testdata/first"},
0,
"alabama barcelona\nbarcelona california\ncalifornia denver\ncalifornia denver\nамур брянск\nбелгород волгоград\nволгоград геленджик",
},
{"Reverse",
[]string{"-r", "testdata/first"},
0,
"волгоград геленджик\nбелгород волгоград\nамур брянск\ncalifornia denver\ncalifornia denver\nbarcelona california\nalabama barcelona",
},
{"Unique",
[]string{"-u", "testdata/first"},
0,
"alabama barcelona\nbarcelona california\ncalifornia denver\nамур брянск\nбелгород волгоград\nволгоград геленджик",
},
{"Column 2",
[]string{"-k=2", "testdata/first"},
0,
"alabama barcelona\nbarcelona california\ncalifornia denver\ncalifornia denver\nамур брянск\nбелгород волгоград\nволгоград геленджик",
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
// this call is required because otherwise flags panics, if args are set between flag.Parse calls
flag.CommandLine = flag.NewFlagSet(tc.Name, flag.ExitOnError)
// we need a value to set Args[0] to, cause flag begins parsing at Args[1]
os.Args = append([]string{tc.Name}, tc.Args...)
assert.Equal(t, tc.ExpectedOutput, strings.Trim(run(), "\n"))
})
}
}