-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hook-up cost tracking via vmtemplate service
- Loading branch information
1 parent
97308e2
commit 8db7f04
Showing
13 changed files
with
478 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type TimeUnit = string | ||
|
||
const ( | ||
TimeUnitSeconds TimeUnit = "seconds" | ||
TimeUnitMinutes TimeUnit = "minutes" | ||
TimeUnitHours TimeUnit = "hours" | ||
) | ||
|
||
func ParseTimeUnit(s string) (TimeUnit, error) { | ||
lower := strings.ToLower(s) | ||
switch lower { | ||
case "seconds", "second", "sec", "s": | ||
return TimeUnitSeconds, nil | ||
case "minutes", "minute", "min", "m": | ||
return TimeUnitMinutes, nil | ||
case "hours", "hour", "h": | ||
return TimeUnitHours, nil | ||
default: | ||
return TimeUnitSeconds, fmt.Errorf("%s is not a valid time unit", s) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestParseTimeUnit(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input []string | ||
want TimeUnit | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "ok second", | ||
input: []string{"seconds", "second", "sec", "s", "SECONDS", "SECOND", "SEC", "S"}, | ||
want: TimeUnitSeconds, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "ok minute", | ||
input: []string{"minutes", "minute", "min", "m", "MINUTES", "MINUTE", "MIN", "M"}, | ||
want: TimeUnitMinutes, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "ok hour", | ||
input: []string{"hours", "hour", "h", "HOURS", "HOUR", "H"}, | ||
want: TimeUnitHours, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "nok", | ||
input: []string{"", " ", "idk"}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
for _, input := range tt.input { | ||
t.Run(fmt.Sprintf("%s %s", tt.name, input), func(t *testing.T) { | ||
actual, err := ParseTimeUnit(input) | ||
if tt.wantErr { | ||
require.Errorf(t, err, "error expected ParseTimeUnit(%v)", input) | ||
} else { | ||
require.NoErrorf(t, err, "no error expected ParseTimeUnit(%v)", input) | ||
assert.Equalf(t, tt.want, actual, "ParseTimeUnit(%v)", input) | ||
} | ||
}) | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.