Debian Codegraph @

plumbing/format/config/common_test.go

raw
1 package config
2
3 import (
4 "testing"
5
6 . "gopkg.in/check.v1"
7 )
8
9 func Test(t *testing.T) { TestingT(t) }
10
11 type CommonSuite struct{}
12
13 var _ = Suite(&CommonSuite{})
14
15 func (s *CommonSuite) TestConfig_SetOption(c *C) {
16 obtained := New().SetOption("section", NoSubsection, "key1", "value1")
17 expected := &Config{
18 Sections: []*Section{
19 {
20 Name: "section",
21 Options: []*Option{
22 {Key: "key1", Value: "value1"},
23 },
24 },
25 },
26 }
27 c.Assert(obtained, DeepEquals, expected)
28 obtained = obtained.SetOption("section", NoSubsection, "key1", "value1")
29 c.Assert(obtained, DeepEquals, expected)
30
31 obtained = New().SetOption("section", "subsection", "key1", "value1")
32 expected = &Config{
33 Sections: []*Section{
34 {
35 Name: "section",
36 Subsections: []*Subsection{
37 {
38 Name: "subsection",
39 Options: []*Option{
40 {Key: "key1", Value: "value1"},
41 },
42 },
43 },
44 },
45 },
46 }
47 c.Assert(obtained, DeepEquals, expected)
48 obtained = obtained.SetOption("section", "subsection", "key1", "value1")
49 c.Assert(obtained, DeepEquals, expected)
50 }
51
52 func (s *CommonSuite) TestConfig_AddOption(c *C) {
53 obtained := New().AddOption("section", NoSubsection, "key1", "value1")
54 expected := &Config{
55 Sections: []*Section{
56 {
57 Name: "section",
58 Options: []*Option{
59 {Key: "key1", Value: "value1"},
60 },
61 },
62 },
63 }
64 c.Assert(obtained, DeepEquals, expected)
65 }
66
67 func (s *CommonSuite) TestConfig_HasSection(c *C) {
68 sect := New().
69 AddOption("section1", "sub1", "key1", "value1").
70 AddOption("section1", "sub2", "key1", "value1")
71 c.Assert(sect.HasSection("section1"), Equals, true)
72 c.Assert(sect.HasSection("section2"), Equals, false)
73 }
74
75 func (s *CommonSuite) TestConfig_RemoveSection(c *C) {
76 sect := New().
77 AddOption("section1", NoSubsection, "key1", "value1").
78 AddOption("section2", NoSubsection, "key1", "value1")
79 expected := New().
80 AddOption("section1", NoSubsection, "key1", "value1")
81 c.Assert(sect.RemoveSection("other"), DeepEquals, sect)
82 c.Assert(sect.RemoveSection("section2"), DeepEquals, expected)
83 }
84
85 func (s *CommonSuite) TestConfig_RemoveSubsection(c *C) {
86 sect := New().
87 AddOption("section1", "sub1", "key1", "value1").
88 AddOption("section1", "sub2", "key1", "value1")
89 expected := New().
90 AddOption("section1", "sub1", "key1", "value1")
91 c.Assert(sect.RemoveSubsection("section1", "other"), DeepEquals, sect)
92 c.Assert(sect.RemoveSubsection("other", "other"), DeepEquals, sect)
93 c.Assert(sect.RemoveSubsection("section1", "sub2"), DeepEquals, expected)
94 }