diff --git a/.golangci.yml b/.golangci.yml index 2f35b19..9316d77 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,20 +5,18 @@ linters: - bodyclose - dogsled - dupl + - err113 - exhaustive - gochecknoinits - gocognit - gocritic - gocyclo - - goerr113 - gofumpt - goimports - - gomnd - goprintffuncname - gosec - - interfacer - - maligned - misspell + - mnd - nakedret - nestif - nlreturn diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..23e12de --- /dev/null +++ b/main_test.go @@ -0,0 +1,81 @@ +package main + +import ( + "testing" + "time" +) + +func TestReadDurationArgs(t *testing.T) { + cases := []struct { + name string + args []string + expectedDuration time.Duration + resultArgs []string + }{ + { + name: "No args", + args: []string{}, + expectedDuration: 0, + resultArgs: []string{}, + }, + { + name: "No durations", + args: []string{"foo", "bar"}, + expectedDuration: 0, + resultArgs: []string{"foo", "bar"}, + }, + { + name: "duration at beginning", + args: []string{"5m", "bar"}, + expectedDuration: time.Minute * 5, + resultArgs: []string{"bar"}, + }, + { + name: "duration in the middle", + args: []string{"foo", "5m", "bar"}, + expectedDuration: 0, + resultArgs: []string{"foo", "5m", "bar"}, + }, + { + name: "duration at end without prefix", + args: []string{"foo", "bar", "5m"}, + expectedDuration: 0, + resultArgs: []string{"foo", "bar", "5m"}, + }, + { + name: "duration at end with prefix", + args: []string{"foo", "for", "5m"}, + expectedDuration: time.Minute * 5, + resultArgs: []string{"foo"}, + }, + } + + for _, c := range cases { + c := c + t.Run(c.name, func(t *testing.T) { + t.Parallel() + + actualArgs, actualDuration := readDurationArgs(c.args) + + if len(actualArgs) != len(c.resultArgs) { + t.Errorf("expected args %v actual %v", c.resultArgs, actualArgs) + } + + for i, expected := range c.resultArgs { + if expected != actualArgs[i] { + t.Errorf("expected arg[%d] %v actual %v", i, expected, actualArgs[i]) + } + } + + if actualDuration == nil { + if c.expectedDuration != 0 { + t.Errorf("expected %v actual %v", c.expectedDuration, actualDuration) + } + } else { + if *actualDuration != c.expectedDuration { + t.Errorf("expected %v actual %v", c.expectedDuration, actualDuration) + } + } + }) + } +}