test: add more tests for the internal flag package
All checks were successful
REUSE Compliance Check / check (push) Successful in 6s
All checks were successful
REUSE Compliance Check / check (push) Successful in 6s
This commit is contained in:
parent
0ad02e0af4
commit
74f32aab53
2 changed files with 113 additions and 0 deletions
56
internal/flag/intslice_test.go
Normal file
56
internal/flag/intslice_test.go
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
package flag_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIntSliceValue(t *testing.T) {
|
||||||
|
flagset := flag.NewFlagSet("test", flag.ExitOnError)
|
||||||
|
intSliceVal := internalFlag.NewIntSliceValue()
|
||||||
|
|
||||||
|
if !intSliceVal.Empty() {
|
||||||
|
t.Fatalf("The initialised IntSliceValue is not empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
flagset.Var(&intSliceVal, "int-value", "Integer value")
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"--int-value", "0",
|
||||||
|
"--int-value", "1",
|
||||||
|
"--int-value", "2",
|
||||||
|
"--int-value", "3",
|
||||||
|
"--int-value", "4",
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := flagset.Parse(args); err != nil {
|
||||||
|
t.Fatalf("Received an error parsing the flag: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
wantLength := 5
|
||||||
|
if !intSliceVal.ExpectedLength(wantLength) {
|
||||||
|
t.Fatalf(
|
||||||
|
"Error: intSliceVal.ExpectedLength(%d) == false: actual length is %d",
|
||||||
|
wantLength,
|
||||||
|
len(intSliceVal),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := "0, 1, 2, 3, 4"
|
||||||
|
got := intSliceVal.String()
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf(
|
||||||
|
"Unexpected result after parsing IntSliceValue: want %s, got %s",
|
||||||
|
want,
|
||||||
|
got,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
t.Logf(
|
||||||
|
"Expected result after parsing IntSliceValue: got %s",
|
||||||
|
got,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
57
internal/flag/stringslice_test.go
Normal file
57
internal/flag/stringslice_test.go
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
package flag_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStringSliceValue(t *testing.T) {
|
||||||
|
flagset := flag.NewFlagSet("test", flag.ExitOnError)
|
||||||
|
stringSliceVal := internalFlag.NewStringSliceValue()
|
||||||
|
|
||||||
|
if !stringSliceVal.Empty() {
|
||||||
|
t.Fatalf("The initialised StringSliceValue is not empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
flagset.Var(&stringSliceVal, "colour", "String value")
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"--colour", "orange",
|
||||||
|
"--colour", "blue",
|
||||||
|
"--colour", "magenta",
|
||||||
|
"--colour", "red",
|
||||||
|
"--colour", "green",
|
||||||
|
"--colour", "silver",
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := flagset.Parse(args); err != nil {
|
||||||
|
t.Fatalf("Received an error parsing the flag: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
wantLength := 6
|
||||||
|
if !stringSliceVal.ExpectedLength(wantLength) {
|
||||||
|
t.Fatalf(
|
||||||
|
"Error: intSliceVal.ExpectedLength(%d) == false: actual length is %d",
|
||||||
|
wantLength,
|
||||||
|
len(stringSliceVal),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := "orange, blue, magenta, red, green, silver"
|
||||||
|
got := stringSliceVal.String()
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf(
|
||||||
|
"Unexpected result after parsing StringSliceValue: want %s, got %s",
|
||||||
|
want,
|
||||||
|
got,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
t.Logf(
|
||||||
|
"Expected result after parsing StringSliceValue: got %s",
|
||||||
|
got,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue