From ee6f00610f53fda83740ee408ae75a1c4429dd06 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 8 Sep 2015 22:07:40 +0200 Subject: [PATCH] Moved tests to Ginkgo --- bool_test.go | 41 +++++++ config_test.go | 274 ------------------------------------------ errors_test.go | 56 +++++++++ float_test.go | 44 +++++++ general_test.go | 83 +++++++++++++ int_test.go | 54 +++++++++ os-args_test.go | 40 ++++++ rconfig_suite_test.go | 13 ++ slice_test.go | 51 ++++++++ sub-struct_test.go | 36 ++++++ uint_test.go | 59 +++++++++ 11 files changed, 477 insertions(+), 274 deletions(-) create mode 100644 bool_test.go delete mode 100644 config_test.go create mode 100644 errors_test.go create mode 100644 float_test.go create mode 100644 general_test.go create mode 100644 int_test.go create mode 100644 os-args_test.go create mode 100644 rconfig_suite_test.go create mode 100644 slice_test.go create mode 100644 sub-struct_test.go create mode 100644 uint_test.go diff --git a/bool_test.go b/bool_test.go new file mode 100644 index 0000000..123f821 --- /dev/null +++ b/bool_test.go @@ -0,0 +1,41 @@ +package rconfig + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Testing bool parsing", func() { + type t struct { + Test1 bool `default:"true"` + Test2 bool `default:"false" flag:"test2"` + Test3 bool `default:"true" flag:"test3,t"` + Test4 bool `flag:"test4"` + } + + var ( + err error + args []string + cfg t + ) + + BeforeEach(func() { + cfg = t{} + args = []string{ + "--test2", + "-t", + } + }) + + JustBeforeEach(func() { + err = parse(&cfg, args) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.Test1).To(Equal(true)) + Expect(cfg.Test2).To(Equal(true)) + Expect(cfg.Test3).To(Equal(true)) + Expect(cfg.Test4).To(Equal(false)) + }) +}) diff --git a/config_test.go b/config_test.go deleted file mode 100644 index 84830a8..0000000 --- a/config_test.go +++ /dev/null @@ -1,274 +0,0 @@ -package rconfig - -import ( - "os" - "testing" -) - -func TestGeneralMechanics(t *testing.T) { - cfg := struct { - Test string `default:"foo" env:"shell" flag:"shell" description:"Test"` - Test2 string `default:"blub" env:"testvar" flag:"testvar,t" description:"Test"` - DefaultFlag string `default:"goo"` - SadFlag string - }{} - - parse(&cfg, []string{ - "--shell=test23", - "-t", "bla", - }) - - if cfg.Test != "test23" { - t.Errorf("Test should be 'test23', is '%s'", cfg.Test) - } - - if cfg.Test2 != "bla" { - t.Errorf("Test2 should be 'bla', is '%s'", cfg.Test2) - } - - if cfg.SadFlag != "" { - t.Errorf("SadFlag should be '', is '%s'", cfg.SadFlag) - } - - if cfg.DefaultFlag != "goo" { - t.Errorf("DefaultFlag should be 'goo', is '%s'", cfg.DefaultFlag) - } - - parse(&cfg, []string{}) - - if cfg.Test != "foo" { - t.Errorf("Test should be 'foo', is '%s'", cfg.Test) - } - - os.Setenv("shell", "test546") - parse(&cfg, []string{}) - - if cfg.Test != "test546" { - t.Errorf("Test should be 'test546', is '%s'", cfg.Test) - } -} - -func TestBool(t *testing.T) { - cfg := struct { - Test1 bool `default:"true"` - Test2 bool `default:"false" flag:"test2"` - Test3 bool `default:"true" flag:"test3,t"` - Test4 bool `flag:"test4"` - }{} - - parse(&cfg, []string{ - "--test2", - "-t", - }) - - if !cfg.Test1 { - t.Errorf("Test1 should be 'true', is '%+v'", cfg.Test1) - } - if !cfg.Test2 { - t.Errorf("Test1 should be 'true', is '%+v'", cfg.Test2) - } - if !cfg.Test3 { - t.Errorf("Test1 should be 'true', is '%+v'", cfg.Test3) - } - if cfg.Test4 { - t.Errorf("Test1 should be 'false', is '%+v'", cfg.Test3) - } -} - -func TestInt(t *testing.T) { - cfg := struct { - Test int `flag:"int"` - TestP int `flag:"intp,i"` - Test8 int8 `flag:"int8"` - Test8P int8 `flag:"int8p,8"` - Test32 int32 `flag:"int32"` - Test32P int32 `flag:"int32p,3"` - Test64 int64 `flag:"int64"` - Test64P int64 `flag:"int64p,6"` - TestDef int8 `default:"66"` - }{} - - parse(&cfg, []string{ - "--int=1", "-i", "2", - "--int8=3", "-8", "4", - "--int32=5", "-3", "6", - "--int64=7", "-6", "8", - }) - - if cfg.Test != 1 || cfg.TestP != 2 || cfg.Test8 != 3 || cfg.Test8P != 4 || cfg.Test32 != 5 || cfg.Test32P != 6 || cfg.Test64 != 7 || cfg.Test64P != 8 { - t.Errorf("One of the int tests failed.") - } - - if cfg.TestDef != 66 { - t.Errorf("TestDef should be '66', is '%d'", cfg.TestDef) - } -} - -func TestUint(t *testing.T) { - cfg := struct { - Test uint `flag:"int"` - TestP uint `flag:"intp,i"` - Test8 uint8 `flag:"int8"` - Test8P uint8 `flag:"int8p,8"` - Test16 uint16 `flag:"int16"` - Test16P uint16 `flag:"int16p,1"` - Test32 uint32 `flag:"int32"` - Test32P uint32 `flag:"int32p,3"` - Test64 uint64 `flag:"int64"` - Test64P uint64 `flag:"int64p,6"` - TestDef uint8 `default:"66"` - }{} - - parse(&cfg, []string{ - "--int=1", "-i", "2", - "--int8=3", "-8", "4", - "--int32=5", "-3", "6", - "--int64=7", "-6", "8", - "--int16=9", "-1", "10", - }) - - if cfg.Test != 1 || cfg.TestP != 2 || cfg.Test8 != 3 || cfg.Test8P != 4 || cfg.Test32 != 5 || cfg.Test32P != 6 || cfg.Test64 != 7 || cfg.Test64P != 8 || cfg.Test16 != 9 || cfg.Test16P != 10 { - t.Errorf("One of the uint tests failed.") - } - - if cfg.TestDef != 66 { - t.Errorf("TestDef should be '66', is '%d'", cfg.TestDef) - } -} - -func TestFloat(t *testing.T) { - cfg := struct { - Test32 float32 `flag:"float32"` - Test32P float32 `flag:"float32p,3"` - Test64 float64 `flag:"float64"` - Test64P float64 `flag:"float64p,6"` - TestDef float32 `default:"66.256"` - }{} - - parse(&cfg, []string{ - "--float32=5.5", "-3", "6.6", - "--float64=7.7", "-6", "8.8", - }) - - if cfg.Test32 != 5.5 || cfg.Test32P != 6.6 || cfg.Test64 != 7.7 || cfg.Test64P != 8.8 { - t.Errorf("One of the int tests failed.") - } - - if cfg.TestDef != 66.256 { - t.Errorf("TestDef should be '66.256', is '%.3f'", cfg.TestDef) - } -} - -func TestSubStruct(t *testing.T) { - cfg := struct { - Test string `default:"blubb"` - Sub struct { - Test string `default:"Hallo"` - } - }{} - - if err := parse(&cfg, []string{}); err != nil { - t.Errorf("Test errored: %s", err) - } - - if cfg.Test != "blubb" { - t.Errorf("Test should be 'blubb', is '%s'", cfg.Test) - } - - if cfg.Sub.Test != "Hallo" { - t.Errorf("Sub.Test should be 'Hallo', is '%s'", cfg.Sub.Test) - } -} - -func TestSlice(t *testing.T) { - cfg := struct { - Int []int `default:"1,2,3" flag:"int"` - String []string `default:"a,b,c" flag:"string"` - IntP []int `default:"1,2,3" flag:"intp,i"` - StringP []string `default:"a,b,c" flag:"stringp,s"` - }{} - - if err := parse(&cfg, []string{ - "--int=4,5", "-s", "hallo,welt", - }); err != nil { - t.Errorf("Test errored: %s", err) - } - - if len(cfg.Int) != 2 || cfg.Int[0] != 4 || cfg.Int[1] != 5 { - t.Errorf("Int should be '4,5', is '%+v'", cfg.Int) - } - - if len(cfg.String) != 3 || cfg.String[0] != "a" || cfg.String[1] != "b" { - t.Errorf("String should be 'a,b,c', is '%+v'", cfg.String) - } - - if len(cfg.StringP) != 2 || cfg.StringP[0] != "hallo" || cfg.StringP[1] != "welt" { - t.Errorf("StringP should be 'hallo,welt', is '%+v'", cfg.StringP) - } -} - -func TestErrors(t *testing.T) { - if err := parse(&struct { - A int `default:"a"` - }{}, []string{}); err == nil { - t.Errorf("Test should have errored") - } - - if err := parse(&struct { - A float32 `default:"a"` - }{}, []string{}); err == nil { - t.Errorf("Test should have errored") - } - - if err := parse(&struct { - A uint `default:"a"` - }{}, []string{}); err == nil { - t.Errorf("Test should have errored") - } - - if err := parse(&struct { - B struct { - A uint `default:"a"` - } - }{}, []string{}); err == nil { - t.Errorf("Test should have errored") - } - - if err := parse(&struct { - A []int `default:"a,bn"` - }{}, []string{}); err == nil { - t.Errorf("Test should have errored") - } -} - -func TestOSArgs(t *testing.T) { - os.Args = []string{"--a=bar"} - - cfg := struct { - A string `default:"a" flag:"a"` - }{} - - Parse(&cfg) - - if cfg.A != "bar" { - t.Errorf("A should be 'bar', is '%s'", cfg.A) - } -} - -func TestNonPointer(t *testing.T) { - cfg := struct { - A string `default:"a"` - }{} - - if err := parse(cfg, []string{}); err == nil { - t.Errorf("Test should have errored") - } -} - -func TestOtherType(t *testing.T) { - cfg := "test" - - if err := parse(&cfg, []string{}); err == nil { - t.Errorf("Test should have errored") - } -} diff --git a/errors_test.go b/errors_test.go new file mode 100644 index 0000000..46db039 --- /dev/null +++ b/errors_test.go @@ -0,0 +1,56 @@ +package rconfig + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Testing errors", func() { + + It("should not accept string as int", func() { + Expect(parse(&struct { + A int `default:"a"` + }{}, []string{})).To(HaveOccurred()) + }) + + It("should not accept string as float", func() { + Expect(parse(&struct { + A float32 `default:"a"` + }{}, []string{})).To(HaveOccurred()) + }) + + It("should not accept string as uint", func() { + Expect(parse(&struct { + A uint `default:"a"` + }{}, []string{})).To(HaveOccurred()) + }) + + It("should not accept string as uint in sub-struct", func() { + Expect(parse(&struct { + B struct { + A uint `default:"a"` + } + }{}, []string{})).To(HaveOccurred()) + }) + + It("should not accept string slice as int slice", func() { + Expect(parse(&struct { + A []int `default:"a,bn"` + }{}, []string{})).To(HaveOccurred()) + }) + + It("should not accept variables not being pointers", func() { + cfg := struct { + A string `default:"a"` + }{} + + Expect(parse(cfg, []string{})).To(HaveOccurred()) + }) + + It("should not accept variables not being pointers to structs", func() { + cfg := "test" + + Expect(parse(cfg, []string{})).To(HaveOccurred()) + }) + +}) diff --git a/float_test.go b/float_test.go new file mode 100644 index 0000000..4ec8a1e --- /dev/null +++ b/float_test.go @@ -0,0 +1,44 @@ +package rconfig + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Testing float parsing", func() { + type t struct { + Test32 float32 `flag:"float32"` + Test32P float32 `flag:"float32p,3"` + Test64 float64 `flag:"float64"` + Test64P float64 `flag:"float64p,6"` + TestDef float32 `default:"66.256"` + } + + var ( + err error + args []string + cfg t + ) + + BeforeEach(func() { + cfg = t{} + args = []string{ + "--float32=5.5", "-3", "6.6", + "--float64=7.7", "-6", "8.8", + } + }) + + JustBeforeEach(func() { + err = parse(&cfg, args) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.Test32).To(Equal(float32(5.5))) + Expect(cfg.Test32P).To(Equal(float32(6.6))) + Expect(cfg.Test64).To(Equal(float64(7.7))) + Expect(cfg.Test64P).To(Equal(float64(8.8))) + + Expect(cfg.TestDef).To(Equal(float32(66.256))) + }) +}) diff --git a/general_test.go b/general_test.go new file mode 100644 index 0000000..03e4cec --- /dev/null +++ b/general_test.go @@ -0,0 +1,83 @@ +package rconfig + +import ( + "os" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Testing general parsing", func() { + type t struct { + Test string `default:"foo" env:"shell" flag:"shell" description:"Test"` + Test2 string `default:"blub" env:"testvar" flag:"testvar,t" description:"Test"` + DefaultFlag string `default:"goo"` + SadFlag string + } + + var ( + err error + args []string + cfg t + ) + + Context("with defined arguments", func() { + BeforeEach(func() { + cfg = t{} + args = []string{ + "--shell=test23", + "-t", "bla", + } + }) + + JustBeforeEach(func() { + err = parse(&cfg, args) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have parsed the expected values", func() { + Expect(cfg.Test).To(Equal("test23")) + Expect(cfg.Test2).To(Equal("bla")) + Expect(cfg.SadFlag).To(Equal("")) + Expect(cfg.DefaultFlag).To(Equal("goo")) + }) + }) + + Context("with no arguments", func() { + BeforeEach(func() { + cfg = t{} + args = []string{} + }) + + JustBeforeEach(func() { + err = parse(&cfg, args) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have used the default value", func() { + Expect(cfg.Test).To(Equal("foo")) + }) + }) + + Context("with no arguments and set env", func() { + BeforeEach(func() { + cfg = t{} + args = []string{} + os.Setenv("shell", "test546") + }) + + AfterEach(func() { + os.Unsetenv("shell") + }) + + JustBeforeEach(func() { + err = parse(&cfg, args) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have used the value from env", func() { + Expect(cfg.Test).To(Equal("test546")) + }) + }) + +}) diff --git a/int_test.go b/int_test.go new file mode 100644 index 0000000..2cc0022 --- /dev/null +++ b/int_test.go @@ -0,0 +1,54 @@ +package rconfig + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Testing int parsing", func() { + type t struct { + Test int `flag:"int"` + TestP int `flag:"intp,i"` + Test8 int8 `flag:"int8"` + Test8P int8 `flag:"int8p,8"` + Test32 int32 `flag:"int32"` + Test32P int32 `flag:"int32p,3"` + Test64 int64 `flag:"int64"` + Test64P int64 `flag:"int64p,6"` + TestDef int8 `default:"66"` + } + + var ( + err error + args []string + cfg t + ) + + BeforeEach(func() { + cfg = t{} + args = []string{ + "--int=1", "-i", "2", + "--int8=3", "-8", "4", + "--int32=5", "-3", "6", + "--int64=7", "-6", "8", + } + }) + + JustBeforeEach(func() { + err = parse(&cfg, args) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.Test).To(Equal(1)) + Expect(cfg.TestP).To(Equal(2)) + Expect(cfg.Test8).To(Equal(int8(3))) + Expect(cfg.Test8P).To(Equal(int8(4))) + Expect(cfg.Test32).To(Equal(int32(5))) + Expect(cfg.Test32P).To(Equal(int32(6))) + Expect(cfg.Test64).To(Equal(int64(7))) + Expect(cfg.Test64P).To(Equal(int64(8))) + + Expect(cfg.TestDef).To(Equal(int8(66))) + }) +}) diff --git a/os-args_test.go b/os-args_test.go new file mode 100644 index 0000000..eacee71 --- /dev/null +++ b/os-args_test.go @@ -0,0 +1,40 @@ +package rconfig_test + +import ( + "os" + + . "github.com/Luzifer/rconfig" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Testing os.Args", func() { + type t struct { + A string `default:"a" flag:"a"` + } + + var ( + err error + cfg t + ) + + JustBeforeEach(func() { + err = Parse(&cfg) + }) + + Context("With only valid arguments", func() { + + BeforeEach(func() { + cfg = t{} + os.Args = []string{"--a=bar"} + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.A).To(Equal("bar")) + }) + + }) + +}) diff --git a/rconfig_suite_test.go b/rconfig_suite_test.go new file mode 100644 index 0000000..72c9ce4 --- /dev/null +++ b/rconfig_suite_test.go @@ -0,0 +1,13 @@ +package rconfig_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "testing" +) + +func TestRconfig(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Rconfig Suite") +} diff --git a/slice_test.go b/slice_test.go new file mode 100644 index 0000000..7d9524e --- /dev/null +++ b/slice_test.go @@ -0,0 +1,51 @@ +package rconfig + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Testing slices", func() { + type t struct { + Int []int `default:"1,2,3" flag:"int"` + String []string `default:"a,b,c" flag:"string"` + IntP []int `default:"1,2,3" flag:"intp,i"` + StringP []string `default:"a,b,c" flag:"stringp,s"` + } + + var ( + err error + args []string + cfg t + ) + + BeforeEach(func() { + cfg = t{} + args = []string{ + "--int=4,5", "-s", "hallo,welt", + } + }) + + JustBeforeEach(func() { + err = parse(&cfg, args) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values for int-slice", func() { + Expect(len(cfg.Int)).To(Equal(2)) + Expect(cfg.Int).To(Equal([]int{4, 5})) + Expect(cfg.Int).NotTo(Equal([]int{5, 4})) + }) + It("should have the expected values for int-shorthand-slice", func() { + Expect(len(cfg.IntP)).To(Equal(3)) + Expect(cfg.IntP).To(Equal([]int{1, 2, 3})) + }) + It("should have the expected values for string-slice", func() { + Expect(len(cfg.String)).To(Equal(3)) + Expect(cfg.String).To(Equal([]string{"a", "b", "c"})) + }) + It("should have the expected values for string-shorthand-slice", func() { + Expect(len(cfg.StringP)).To(Equal(2)) + Expect(cfg.StringP).To(Equal([]string{"hallo", "welt"})) + }) +}) diff --git a/sub-struct_test.go b/sub-struct_test.go new file mode 100644 index 0000000..cfbfbc2 --- /dev/null +++ b/sub-struct_test.go @@ -0,0 +1,36 @@ +package rconfig + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Testing sub-structs", func() { + type t struct { + Test string `default:"blubb"` + Sub struct { + Test string `default:"Hallo"` + } + } + + var ( + err error + args []string + cfg t + ) + + BeforeEach(func() { + cfg = t{} + args = []string{} + }) + + JustBeforeEach(func() { + err = parse(&cfg, args) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.Test).To(Equal("blubb")) + Expect(cfg.Sub.Test).To(Equal("Hallo")) + }) +}) diff --git a/uint_test.go b/uint_test.go new file mode 100644 index 0000000..886db1d --- /dev/null +++ b/uint_test.go @@ -0,0 +1,59 @@ +package rconfig + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Testing uint parsing", func() { + type t struct { + Test uint `flag:"int"` + TestP uint `flag:"intp,i"` + Test8 uint8 `flag:"int8"` + Test8P uint8 `flag:"int8p,8"` + Test16 uint16 `flag:"int16"` + Test16P uint16 `flag:"int16p,1"` + Test32 uint32 `flag:"int32"` + Test32P uint32 `flag:"int32p,3"` + Test64 uint64 `flag:"int64"` + Test64P uint64 `flag:"int64p,6"` + TestDef uint8 `default:"66"` + } + + var ( + err error + args []string + cfg t + ) + + BeforeEach(func() { + cfg = t{} + args = []string{ + "--int=1", "-i", "2", + "--int8=3", "-8", "4", + "--int32=5", "-3", "6", + "--int64=7", "-6", "8", + "--int16=9", "-1", "10", + } + }) + + JustBeforeEach(func() { + err = parse(&cfg, args) + }) + + It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should have the expected values", func() { + Expect(cfg.Test).To(Equal(uint(1))) + Expect(cfg.TestP).To(Equal(uint(2))) + Expect(cfg.Test8).To(Equal(uint8(3))) + Expect(cfg.Test8P).To(Equal(uint8(4))) + Expect(cfg.Test32).To(Equal(uint32(5))) + Expect(cfg.Test32P).To(Equal(uint32(6))) + Expect(cfg.Test64).To(Equal(uint64(7))) + Expect(cfg.Test64P).To(Equal(uint64(8))) + Expect(cfg.Test16).To(Equal(uint16(9))) + Expect(cfg.Test16P).To(Equal(uint16(10))) + + Expect(cfg.TestDef).To(Equal(uint8(66))) + }) +})