From 8916a7ea5ff794ee720a57c748e43c4fa385bcbd Mon Sep 17 00:00:00 2001 From: Stefan Junker Date: Thu, 17 Mar 2016 14:09:54 +0100 Subject: [PATCH] pkg/types: add tests for args --- pkg/types/args.go | 4 +- pkg/types/args_test.go | 92 +++++++++++++++++++++++++++++++++++ pkg/types/types_suite_test.go | 13 +++++ test | 2 +- 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 pkg/types/args_test.go create mode 100644 pkg/types/types_suite_test.go diff --git a/pkg/types/args.go b/pkg/types/args.go index c4e3c55b..3b667b0f 100644 --- a/pkg/types/args.go +++ b/pkg/types/args.go @@ -47,9 +47,9 @@ type CommonArgs struct { 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 -func getKeyField(keyString string, v reflect.Value) reflect.Value { +func GetKeyField(keyString string, v reflect.Value) reflect.Value { return v.Elem().FieldByName(keyString) } diff --git a/pkg/types/args_test.go b/pkg/types/args_test.go new file mode 100644 index 00000000..123548cc --- /dev/null +++ b/pkg/types/args_test.go @@ -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()) + }) + }) +}) diff --git a/pkg/types/types_suite_test.go b/pkg/types/types_suite_test.go new file mode 100644 index 00000000..b026169c --- /dev/null +++ b/pkg/types/types_suite_test.go @@ -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") +} diff --git a/test b/test index 6364ec54..a333086f 100755 --- a/test +++ b/test @@ -11,7 +11,7 @@ set -e 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" # user has not provided PKG override