From b40ca794fe33a28f1ca4d6aedacc76b706ea8b04 Mon Sep 17 00:00:00 2001 From: vince Date: Wed, 26 Aug 2020 11:06:34 +0800 Subject: Add tests --- plumbing/format/config/common_test.go | 8 ++ plumbing/format/config/option_test.go | 15 ++ plumbing/format/config/section_test.go | 253 +++++++++++++++++++++++++++++++-- 3 files changed, 267 insertions(+), 9 deletions(-) (limited to 'plumbing/format/config') diff --git a/plumbing/format/config/common_test.go b/plumbing/format/config/common_test.go index 365b53f..dca38df 100644 --- a/plumbing/format/config/common_test.go +++ b/plumbing/format/config/common_test.go @@ -64,6 +64,14 @@ func (s *CommonSuite) TestConfig_AddOption(c *C) { c.Assert(obtained, DeepEquals, expected) } +func (s *CommonSuite) TestConfig_HasSection(c *C) { + sect := New(). + AddOption("section1", "sub1", "key1", "value1"). + AddOption("section1", "sub2", "key1", "value1") + c.Assert(sect.HasSection("section1"), Equals, true) + c.Assert(sect.HasSection("section2"), Equals, false) +} + func (s *CommonSuite) TestConfig_RemoveSection(c *C) { sect := New(). AddOption("section1", NoSubsection, "key1", "value1"). diff --git a/plumbing/format/config/option_test.go b/plumbing/format/config/option_test.go index 8588de1..49b4855 100644 --- a/plumbing/format/config/option_test.go +++ b/plumbing/format/config/option_test.go @@ -8,6 +8,21 @@ type OptionSuite struct{} var _ = Suite(&OptionSuite{}) +func (s *OptionSuite) TestOptions_Has(c *C) { + o := Options{ + &Option{"k", "v"}, + &Option{"ok", "v1"}, + &Option{"K", "v2"}, + } + c.Assert(o.Has("k"), Equals, true) + c.Assert(o.Has("K"), Equals, true) + c.Assert(o.Has("ok"), Equals, true) + c.Assert(o.Has("unexistant"), Equals, false) + + o = Options{} + c.Assert(o.Has("k"), Equals, false) +} + func (s *OptionSuite) TestOptions_GetAll(c *C) { o := Options{ &Option{"k", "v"}, diff --git a/plumbing/format/config/section_test.go b/plumbing/format/config/section_test.go index 0290386..c7cc4a9 100644 --- a/plumbing/format/config/section_test.go +++ b/plumbing/format/config/section_test.go @@ -8,6 +8,115 @@ type SectionSuite struct{} var _ = Suite(&SectionSuite{}) +func (s *SectionSuite) TestSections_GoString(c *C) { + sects := Sections{ + &Section{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + }, + }, + &Section{ + Options: []*Option{ + {Key: "key1", Value: "value3"}, + {Key: "key2", Value: "value4"}, + }, + }, + } + + expected := "&config.Section{Name:\"\", Options:&config.Option{Key:\"key1\", Value:\"value1\"}, &config.Option{Key:\"key2\", Value:\"value2\"}, Subsections:}, &config.Section{Name:\"\", Options:&config.Option{Key:\"key1\", Value:\"value3\"}, &config.Option{Key:\"key2\", Value:\"value4\"}, Subsections:}" + c.Assert(sects.GoString(), Equals, expected) +} + +func (s *SectionSuite) TestSubsections_GoString(c *C) { + sects := Subsections{ + &Subsection{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + }, + &Subsection{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + }, + } + + expected := "&config.Subsection{Name:\"\", Options:&config.Option{Key:\"key1\", Value:\"value1\"}, &config.Option{Key:\"key2\", Value:\"value2\"}, &config.Option{Key:\"key1\", Value:\"value3\"}}, &config.Subsection{Name:\"\", Options:&config.Option{Key:\"key1\", Value:\"value1\"}, &config.Option{Key:\"key2\", Value:\"value2\"}, &config.Option{Key:\"key1\", Value:\"value3\"}}" + c.Assert(sects.GoString(), Equals, expected) +} + +func (s *SectionSuite) TestSection_IsName(c *C) { + sect := &Section{ + Name: "name1", + } + + c.Assert(sect.IsName("name1"), Equals, true) + c.Assert(sect.IsName("Name1"), Equals, true) +} + +func (s *SectionSuite) TestSection_Subsection(c *C) { + subSect1 := &Subsection{ + Name: "name1", + Options: Options{ + &Option{Key: "key1", Value: "value1"}, + }, + } + sect := &Section{ + Subsections: Subsections{ + subSect1, + }, + } + + c.Assert(sect.Subsection("name1"), DeepEquals, subSect1) + + subSect2 := &Subsection{ + Name: "name2", + } + c.Assert(sect.Subsection("name2"), DeepEquals, subSect2) +} + +func (s *SectionSuite) TestSection_HasSubsection(c *C) { + sect := &Section{ + Subsections: Subsections{ + &Subsection{ + Name: "name1", + }, + }, + } + + c.Assert(sect.HasSubsection("name1"), Equals, true) + c.Assert(sect.HasSubsection("name2"), Equals, false) +} + +func (s *SectionSuite) TestSection_RemoveSubsection(c *C) { + sect := &Section{ + Subsections: Subsections{ + &Subsection{ + Name: "name1", + }, + &Subsection{ + Name: "name2", + }, + }, + } + + expected := &Section{ + Subsections: Subsections{ + &Subsection{ + Name: "name2", + }, + }, + } + c.Assert(sect.RemoveSubsection("name1"), DeepEquals, expected) + c.Assert(sect.HasSubsection("name1"), Equals, false) + c.Assert(sect.HasSubsection("name2"), Equals, true) +} + func (s *SectionSuite) TestSection_Option(c *C) { sect := &Section{ Options: []*Option{ @@ -21,17 +130,71 @@ func (s *SectionSuite) TestSection_Option(c *C) { c.Assert(sect.Option("key1"), Equals, "value3") } -func (s *SectionSuite) TestSubsection_Option(c *C) { - sect := &Subsection{ +func (s *SectionSuite) TestSection_OptionAll(c *C) { + sect := &Section{ Options: []*Option{ {Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}, {Key: "key1", Value: "value3"}, }, } - c.Assert(sect.Option("otherkey"), Equals, "") - c.Assert(sect.Option("key2"), Equals, "value2") - c.Assert(sect.Option("key1"), Equals, "value3") + c.Assert(sect.OptionAll("otherkey"), DeepEquals, []string{}) + c.Assert(sect.OptionAll("key2"), DeepEquals, []string{"value2"}) + c.Assert(sect.OptionAll("key1"), DeepEquals, []string{"value1", "value3"}) +} + +func (s *SectionSuite) TestSection_HasOption(c *C) { + sect := &Section{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + } + c.Assert(sect.HasOption("otherkey"), Equals, false) + c.Assert(sect.HasOption("key2"), Equals, true) + c.Assert(sect.HasOption("key1"), Equals, true) +} + +func (s *SectionSuite) TestSection_AddOption(c *C) { + sect := &Section{ + Options: []*Option{ + {"key1", "value1"}, + }, + } + sect1 := &Section{ + Options: []*Option{ + {"key1", "value1"}, + {"key2", "value2"}, + }, + } + c.Assert(sect.AddOption("key2", "value2"), DeepEquals, sect1) + + sect2 := &Section{ + Options: []*Option{ + {"key1", "value1"}, + {"key2", "value2"}, + {"key1", "value3"}, + }, + } + c.Assert(sect.AddOption("key1", "value3"), DeepEquals, sect2) +} + +func (s *SectionSuite) TestSection_SetOption(c *C) { + sect := &Section{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + }, + } + + expected := &Section{ + Options: []*Option{ + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value4"}, + }, + } + c.Assert(sect.SetOption("key1", "value4"), DeepEquals, expected) } func (s *SectionSuite) TestSection_RemoveOption(c *C) { @@ -52,7 +215,16 @@ func (s *SectionSuite) TestSection_RemoveOption(c *C) { c.Assert(sect.RemoveOption("key1"), DeepEquals, expected) } -func (s *SectionSuite) TestSubsection_RemoveOption(c *C) { +func (s *SectionSuite) TestSubsection_IsName(c *C) { + sect := &Subsection{ + Name: "name1", + } + + c.Assert(sect.IsName("name1"), Equals, true) + c.Assert(sect.IsName("Name1"), Equals, false) +} + +func (s *SectionSuite) TestSubsection_Option(c *C) { sect := &Subsection{ Options: []*Option{ {Key: "key1", Value: "value1"}, @@ -60,14 +232,59 @@ func (s *SectionSuite) TestSubsection_RemoveOption(c *C) { {Key: "key1", Value: "value3"}, }, } - c.Assert(sect.RemoveOption("otherkey"), DeepEquals, sect) + c.Assert(sect.Option("otherkey"), Equals, "") + c.Assert(sect.Option("key2"), Equals, "value2") + c.Assert(sect.Option("key1"), Equals, "value3") +} - expected := &Subsection{ +func (s *SectionSuite) TestSubsection_OptionAll(c *C) { + sect := &Subsection{ Options: []*Option{ + {Key: "key1", Value: "value1"}, {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, }, } - c.Assert(sect.RemoveOption("key1"), DeepEquals, expected) + c.Assert(sect.OptionAll("otherkey"), DeepEquals, []string{}) + c.Assert(sect.OptionAll("key2"), DeepEquals, []string{"value2"}) + c.Assert(sect.OptionAll("key1"), DeepEquals, []string{"value1", "value3"}) +} + +func (s *SectionSuite) TestSubsection_HasOption(c *C) { + sect := &Subsection{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + } + c.Assert(sect.HasOption("otherkey"), Equals, false) + c.Assert(sect.HasOption("key2"), Equals, true) + c.Assert(sect.HasOption("key1"), Equals, true) +} + +func (s *SectionSuite) TestSubsection_AddOption(c *C) { + sect := &Subsection{ + Options: []*Option{ + {"key1", "value1"}, + }, + } + sect1 := &Subsection{ + Options: []*Option{ + {"key1", "value1"}, + {"key2", "value2"}, + }, + } + c.Assert(sect.AddOption("key2", "value2"), DeepEquals, sect1) + + sect2 := &Subsection{ + Options: []*Option{ + {"key1", "value1"}, + {"key2", "value2"}, + {"key1", "value3"}, + }, + } + c.Assert(sect.AddOption("key1", "value3"), DeepEquals, sect2) } func (s *SectionSuite) TestSubsection_SetOption(c *C) { @@ -88,3 +305,21 @@ func (s *SectionSuite) TestSubsection_SetOption(c *C) { } c.Assert(sect.SetOption("key1", "value1", "value4"), DeepEquals, expected) } + +func (s *SectionSuite) TestSubsection_RemoveOption(c *C) { + sect := &Subsection{ + Options: []*Option{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + {Key: "key1", Value: "value3"}, + }, + } + c.Assert(sect.RemoveOption("otherkey"), DeepEquals, sect) + + expected := &Subsection{ + Options: []*Option{ + {Key: "key2", Value: "value2"}, + }, + } + c.Assert(sect.RemoveOption("key1"), DeepEquals, expected) +} -- cgit