aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/durations/durations_test.go
blob: 12008a7134df45ff2df389a2c8afa37551035c91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package durations

import (
	"testing"
	"time"
)

func TestNewDurationFromArg(t *testing.T) {
	tests := []struct {
		name    string
		arg     string
		wantErr bool
		want    time.Duration
	}{
		{
			name: "valid GO duration - seconds",
			arg:  "30s",
			want: 30 * time.Second,
		},
		{
			name: "valid GO duration - minutes",
			arg:  "2m",
			want: 2 * time.Minute,
		},
		{
			name: "number - fallback to seconds",
			arg:  "30",
			want: 30 * time.Second,
		},
		{
			name:    "invalid duration",
			arg:     "twenty seconds",
			wantErr: true,
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			actual, err := NewDurationFromArg(test.arg)
			if test.wantErr && err == nil {
				t.Error("error was expected")
			}
			if !test.wantErr && err != nil {
				t.Error("error was not expected")
			}

			if test.want != actual {
				t.Errorf("expected '%v' got '%v'", test.want, actual)
			}
		})
	}
}