pkg/types: add tests for args
This commit is contained in:
parent
b91aec9a62
commit
8916a7ea5f
@ -47,9 +47,9 @@ type CommonArgs struct {
|
|||||||
IgnoreUnknown UnmarshallableBool `json:"ignoreunknown,omitempty"`
|
IgnoreUnknown UnmarshallableBool `json:"ignoreunknown,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// getKeyField is a helper function to receive Values
|
// GetKeyField is a helper function to receive Values
|
||||||
// Values that represent a pointer to a struct
|
// Values that represent a pointer to a struct
|
||||||
func getKeyField(keyString string, v reflect.Value) reflect.Value {
|
func GetKeyField(keyString string, v reflect.Value) reflect.Value {
|
||||||
return v.Elem().FieldByName(keyString)
|
return v.Elem().FieldByName(keyString)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
92
pkg/types/args_test.go
Normal file
92
pkg/types/args_test.go
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
package types_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
. "github.com/appc/cni/pkg/types"
|
||||||
|
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/ginkgo/extensions/table"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = Describe("UnmarshallableBool UnmarshalText", func() {
|
||||||
|
DescribeTable("string to bool detection should succeed in all cases",
|
||||||
|
func(inputs []string, expected bool) {
|
||||||
|
for _, s := range inputs {
|
||||||
|
var ub UnmarshallableBool
|
||||||
|
err := ub.UnmarshalText([]byte(s))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(ub).To(Equal(UnmarshallableBool(expected)))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Entry("parse to true", []string{"True", "true", "1"}, true),
|
||||||
|
Entry("parse to false", []string{"False", "false", "0"}, false),
|
||||||
|
)
|
||||||
|
|
||||||
|
Context("When passed an invalid value", func() {
|
||||||
|
It("should result in an error", func() {
|
||||||
|
var ub UnmarshallableBool
|
||||||
|
err := ub.UnmarshalText([]byte("invalid"))
|
||||||
|
Expect(err).To(HaveOccurred())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
var _ = Describe("GetKeyField", func() {
|
||||||
|
type testcontainer struct {
|
||||||
|
Valid string `json:"valid,omitempty"`
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
container = testcontainer{Valid: "valid"}
|
||||||
|
containerInterface = func(i interface{}) interface{} { return i }(&container)
|
||||||
|
containerValue = reflect.ValueOf(containerInterface)
|
||||||
|
)
|
||||||
|
Context("When a valid field is provided", func() {
|
||||||
|
It("should return the correct field", func() {
|
||||||
|
field := GetKeyField("Valid", containerValue)
|
||||||
|
Expect(field.String()).To(Equal("valid"))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
var _ = Describe("LoadArgs", func() {
|
||||||
|
Context("When no arguments are passed", func() {
|
||||||
|
It("LoadArgs should succeed", func() {
|
||||||
|
err := LoadArgs("", struct{}{})
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("When unknown arguments are passed and ignored", func() {
|
||||||
|
It("LoadArgs should succeed", func() {
|
||||||
|
ca := CommonArgs{}
|
||||||
|
err := LoadArgs("IgnoreUnknown=True;Unk=nown", &ca)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("When unknown arguments are passed and not ignored", func() {
|
||||||
|
It("LoadArgs should fail", func() {
|
||||||
|
ca := CommonArgs{}
|
||||||
|
err := LoadArgs("Unk=nown", &ca)
|
||||||
|
Expect(err).To(HaveOccurred())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("When unknown arguments are passed and explicitly not ignored", func() {
|
||||||
|
It("LoadArgs should fail", func() {
|
||||||
|
ca := CommonArgs{}
|
||||||
|
err := LoadArgs("IgnoreUnknown=0, Unk=nown", &ca)
|
||||||
|
Expect(err).To(HaveOccurred())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("When known arguments are passed", func() {
|
||||||
|
It("LoadArgs should succeed", func() {
|
||||||
|
ca := CommonArgs{}
|
||||||
|
err := LoadArgs("IgnoreUnknown=1", &ca)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
13
pkg/types/types_suite_test.go
Normal file
13
pkg/types/types_suite_test.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package types_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTypes(t *testing.T) {
|
||||||
|
RegisterFailHandler(Fail)
|
||||||
|
RunSpecs(t, "Types Suite")
|
||||||
|
}
|
2
test
2
test
@ -11,7 +11,7 @@ set -e
|
|||||||
|
|
||||||
source ./build
|
source ./build
|
||||||
|
|
||||||
TESTABLE="plugins/ipam/dhcp plugins/main/loopback pkg/invoke pkg/ns pkg/skel"
|
TESTABLE="plugins/ipam/dhcp plugins/main/loopback pkg/invoke pkg/ns pkg/skel pkg/types"
|
||||||
FORMATTABLE="$TESTABLE libcni pkg/ip pkg/ns pkg/types pkg/ipam plugins/ipam/host-local plugins/main/bridge plugins/meta/flannel plugins/meta/tuning"
|
FORMATTABLE="$TESTABLE libcni pkg/ip pkg/ns pkg/types pkg/ipam plugins/ipam/host-local plugins/main/bridge plugins/meta/flannel plugins/meta/tuning"
|
||||||
|
|
||||||
# user has not provided PKG override
|
# user has not provided PKG override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user