pkg/types: add tests for args
authorStefan Junker <mail@stefanjunker.de>
Thu, 17 Mar 2016 13:09:54 +0000 (14:09 +0100)
committerStefan Junker <mail@stefanjunker.de>
Mon, 21 Mar 2016 19:40:39 +0000 (20:40 +0100)
types/args.go
types/args_test.go [new file with mode: 0644]
types/types_suite_test.go [new file with mode: 0644]

index c4e3c55..3b667b0 100644 (file)
@@ -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/types/args_test.go b/types/args_test.go
new file mode 100644 (file)
index 0000000..123548c
--- /dev/null
@@ -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/types/types_suite_test.go b/types/types_suite_test.go
new file mode 100644 (file)
index 0000000..b026169
--- /dev/null
@@ -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")
+}