section_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Copyright 2014 Unknwon
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package ini_test
  15. import (
  16. "testing"
  17. . "github.com/smartystreets/goconvey/convey"
  18. "gopkg.in/ini.v1"
  19. )
  20. func TestSection_SetBody(t *testing.T) {
  21. Convey("Set body of raw section", t, func() {
  22. f := ini.Empty()
  23. So(f, ShouldNotBeNil)
  24. sec, err := f.NewRawSection("comments", `1111111111111111111000000000000000001110000
  25. 111111111111111111100000000000111000000000`)
  26. So(err, ShouldBeNil)
  27. So(sec, ShouldNotBeNil)
  28. So(sec.Body(), ShouldEqual, `1111111111111111111000000000000000001110000
  29. 111111111111111111100000000000111000000000`)
  30. sec.SetBody("1111111111111111111000000000000000001110000")
  31. So(sec.Body(), ShouldEqual, `1111111111111111111000000000000000001110000`)
  32. Convey("Set for non-raw section", func() {
  33. sec, err := f.NewSection("author")
  34. So(err, ShouldBeNil)
  35. So(sec, ShouldNotBeNil)
  36. So(sec.Body(), ShouldBeEmpty)
  37. sec.SetBody("1111111111111111111000000000000000001110000")
  38. So(sec.Body(), ShouldBeEmpty)
  39. })
  40. })
  41. }
  42. func TestSection_NewKey(t *testing.T) {
  43. Convey("Create a new key", t, func() {
  44. f := ini.Empty()
  45. So(f, ShouldNotBeNil)
  46. k, err := f.Section("").NewKey("NAME", "ini")
  47. So(err, ShouldBeNil)
  48. So(k, ShouldNotBeNil)
  49. So(k.Name(), ShouldEqual, "NAME")
  50. So(k.Value(), ShouldEqual, "ini")
  51. Convey("With duplicated name", func() {
  52. k, err := f.Section("").NewKey("NAME", "ini.v1")
  53. So(err, ShouldBeNil)
  54. So(k, ShouldNotBeNil)
  55. // Overwrite previous existed key
  56. So(k.Value(), ShouldEqual, "ini.v1")
  57. })
  58. Convey("With empty string", func() {
  59. _, err := f.Section("").NewKey("", "")
  60. So(err, ShouldNotBeNil)
  61. })
  62. })
  63. Convey("Create keys with same name and allow shadow", t, func() {
  64. f, err := ini.ShadowLoad([]byte(""))
  65. So(err, ShouldBeNil)
  66. So(f, ShouldNotBeNil)
  67. k, err := f.Section("").NewKey("NAME", "ini")
  68. So(err, ShouldBeNil)
  69. So(k, ShouldNotBeNil)
  70. k, err = f.Section("").NewKey("NAME", "ini.v1")
  71. So(err, ShouldBeNil)
  72. So(k, ShouldNotBeNil)
  73. So(k.ValueWithShadows(), ShouldResemble, []string{"ini", "ini.v1"})
  74. })
  75. }
  76. func TestSection_NewBooleanKey(t *testing.T) {
  77. Convey("Create a new boolean key", t, func() {
  78. f := ini.Empty()
  79. So(f, ShouldNotBeNil)
  80. k, err := f.Section("").NewBooleanKey("start-ssh-server")
  81. So(err, ShouldBeNil)
  82. So(k, ShouldNotBeNil)
  83. So(k.Name(), ShouldEqual, "start-ssh-server")
  84. So(k.Value(), ShouldEqual, "true")
  85. Convey("With empty string", func() {
  86. _, err := f.Section("").NewBooleanKey("")
  87. So(err, ShouldNotBeNil)
  88. })
  89. })
  90. }
  91. func TestSection_GetKey(t *testing.T) {
  92. Convey("Get a key", t, func() {
  93. f := ini.Empty()
  94. So(f, ShouldNotBeNil)
  95. k, err := f.Section("").NewKey("NAME", "ini")
  96. So(err, ShouldBeNil)
  97. So(k, ShouldNotBeNil)
  98. k, err = f.Section("").GetKey("NAME")
  99. So(err, ShouldBeNil)
  100. So(k, ShouldNotBeNil)
  101. So(k.Name(), ShouldEqual, "NAME")
  102. So(k.Value(), ShouldEqual, "ini")
  103. Convey("Key not exists", func() {
  104. _, err := f.Section("").GetKey("404")
  105. So(err, ShouldNotBeNil)
  106. })
  107. Convey("Key exists in parent section", func() {
  108. k, err := f.Section("parent").NewKey("AGE", "18")
  109. So(err, ShouldBeNil)
  110. So(k, ShouldNotBeNil)
  111. k, err = f.Section("parent.child.son").GetKey("AGE")
  112. So(err, ShouldBeNil)
  113. So(k, ShouldNotBeNil)
  114. So(k.Value(), ShouldEqual, "18")
  115. })
  116. })
  117. }
  118. func TestSection_HasKey(t *testing.T) {
  119. Convey("Check if a key exists", t, func() {
  120. f := ini.Empty()
  121. So(f, ShouldNotBeNil)
  122. k, err := f.Section("").NewKey("NAME", "ini")
  123. So(err, ShouldBeNil)
  124. So(k, ShouldNotBeNil)
  125. So(f.Section("").HasKey("NAME"), ShouldBeTrue)
  126. So(f.Section("").Haskey("NAME"), ShouldBeTrue)
  127. So(f.Section("").HasKey("404"), ShouldBeFalse)
  128. So(f.Section("").Haskey("404"), ShouldBeFalse)
  129. })
  130. }
  131. func TestSection_HasValue(t *testing.T) {
  132. Convey("Check if contains a value in any key", t, func() {
  133. f := ini.Empty()
  134. So(f, ShouldNotBeNil)
  135. k, err := f.Section("").NewKey("NAME", "ini")
  136. So(err, ShouldBeNil)
  137. So(k, ShouldNotBeNil)
  138. So(f.Section("").HasValue("ini"), ShouldBeTrue)
  139. So(f.Section("").HasValue("404"), ShouldBeFalse)
  140. })
  141. }
  142. func TestSection_Key(t *testing.T) {
  143. Convey("Get a key", t, func() {
  144. f := ini.Empty()
  145. So(f, ShouldNotBeNil)
  146. k, err := f.Section("").NewKey("NAME", "ini")
  147. So(err, ShouldBeNil)
  148. So(k, ShouldNotBeNil)
  149. k = f.Section("").Key("NAME")
  150. So(k, ShouldNotBeNil)
  151. So(k.Name(), ShouldEqual, "NAME")
  152. So(k.Value(), ShouldEqual, "ini")
  153. Convey("Key not exists", func() {
  154. k := f.Section("").Key("404")
  155. So(k, ShouldNotBeNil)
  156. So(k.Name(), ShouldEqual, "404")
  157. })
  158. Convey("Key exists in parent section", func() {
  159. k, err := f.Section("parent").NewKey("AGE", "18")
  160. So(err, ShouldBeNil)
  161. So(k, ShouldNotBeNil)
  162. k = f.Section("parent.child.son").Key("AGE")
  163. So(k, ShouldNotBeNil)
  164. So(k.Value(), ShouldEqual, "18")
  165. })
  166. })
  167. }
  168. func TestSection_Keys(t *testing.T) {
  169. Convey("Get all keys in a section", t, func() {
  170. f := ini.Empty()
  171. So(f, ShouldNotBeNil)
  172. k, err := f.Section("").NewKey("NAME", "ini")
  173. So(err, ShouldBeNil)
  174. So(k, ShouldNotBeNil)
  175. k, err = f.Section("").NewKey("VERSION", "v1")
  176. So(err, ShouldBeNil)
  177. So(k, ShouldNotBeNil)
  178. k, err = f.Section("").NewKey("IMPORT_PATH", "gopkg.in/ini.v1")
  179. So(err, ShouldBeNil)
  180. So(k, ShouldNotBeNil)
  181. keys := f.Section("").Keys()
  182. names := []string{"NAME", "VERSION", "IMPORT_PATH"}
  183. So(len(keys), ShouldEqual, len(names))
  184. for i, name := range names {
  185. So(keys[i].Name(), ShouldEqual, name)
  186. }
  187. })
  188. }
  189. func TestSection_ParentKeys(t *testing.T) {
  190. Convey("Get all keys of parent sections", t, func() {
  191. f := ini.Empty()
  192. So(f, ShouldNotBeNil)
  193. k, err := f.Section("package").NewKey("NAME", "ini")
  194. So(err, ShouldBeNil)
  195. So(k, ShouldNotBeNil)
  196. k, err = f.Section("package").NewKey("VERSION", "v1")
  197. So(err, ShouldBeNil)
  198. So(k, ShouldNotBeNil)
  199. k, err = f.Section("package").NewKey("IMPORT_PATH", "gopkg.in/ini.v1")
  200. So(err, ShouldBeNil)
  201. So(k, ShouldNotBeNil)
  202. keys := f.Section("package.sub.sub2").ParentKeys()
  203. names := []string{"NAME", "VERSION", "IMPORT_PATH"}
  204. So(len(keys), ShouldEqual, len(names))
  205. for i, name := range names {
  206. So(keys[i].Name(), ShouldEqual, name)
  207. }
  208. })
  209. }
  210. func TestSection_KeyStrings(t *testing.T) {
  211. Convey("Get all key names in a section", t, func() {
  212. f := ini.Empty()
  213. So(f, ShouldNotBeNil)
  214. k, err := f.Section("").NewKey("NAME", "ini")
  215. So(err, ShouldBeNil)
  216. So(k, ShouldNotBeNil)
  217. k, err = f.Section("").NewKey("VERSION", "v1")
  218. So(err, ShouldBeNil)
  219. So(k, ShouldNotBeNil)
  220. k, err = f.Section("").NewKey("IMPORT_PATH", "gopkg.in/ini.v1")
  221. So(err, ShouldBeNil)
  222. So(k, ShouldNotBeNil)
  223. So(f.Section("").KeyStrings(), ShouldResemble, []string{"NAME", "VERSION", "IMPORT_PATH"})
  224. })
  225. }
  226. func TestSection_KeyHash(t *testing.T) {
  227. Convey("Get clone of key hash", t, func() {
  228. f, err := ini.Load([]byte(`
  229. key = one
  230. [log]
  231. name = app
  232. file = a.log
  233. `), []byte(`
  234. key = two
  235. [log]
  236. name = app2
  237. file = b.log
  238. `))
  239. So(err, ShouldBeNil)
  240. So(f, ShouldNotBeNil)
  241. So(f.Section("").Key("key").String(), ShouldEqual, "two")
  242. hash := f.Section("log").KeysHash()
  243. relation := map[string]string{
  244. "name": "app2",
  245. "file": "b.log",
  246. }
  247. for k, v := range hash {
  248. So(v, ShouldEqual, relation[k])
  249. }
  250. })
  251. }
  252. func TestSection_DeleteKey(t *testing.T) {
  253. Convey("Delete a key", t, func() {
  254. f := ini.Empty()
  255. So(f, ShouldNotBeNil)
  256. k, err := f.Section("").NewKey("NAME", "ini")
  257. So(err, ShouldBeNil)
  258. So(k, ShouldNotBeNil)
  259. So(f.Section("").HasKey("NAME"), ShouldBeTrue)
  260. f.Section("").DeleteKey("NAME")
  261. So(f.Section("").HasKey("NAME"), ShouldBeFalse)
  262. })
  263. }