ini_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain 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,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package config
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "strings"
  20. "testing"
  21. )
  22. func TestIni(t *testing.T) {
  23. var (
  24. inicontext = `
  25. ;comment one
  26. #comment two
  27. appname = beeapi
  28. httpport = 8080
  29. mysqlport = 3600
  30. PI = 3.1415976
  31. runmode = "dev"
  32. autorender = false
  33. copyrequestbody = true
  34. session= on
  35. cookieon= off
  36. newreg = OFF
  37. needlogin = ON
  38. enableSession = Y
  39. enableCookie = N
  40. flag = 1
  41. path1 = ${GOPATH}
  42. path2 = ${GOPATH||/home/go}
  43. [demo]
  44. key1="asta"
  45. key2 = "xie"
  46. CaseInsensitive = true
  47. peers = one;two;three
  48. password = ${GOPATH}
  49. `
  50. keyValue = map[string]interface{}{
  51. "appname": "beeapi",
  52. "httpport": 8080,
  53. "mysqlport": int64(3600),
  54. "pi": 3.1415976,
  55. "runmode": "dev",
  56. "autorender": false,
  57. "copyrequestbody": true,
  58. "session": true,
  59. "cookieon": false,
  60. "newreg": false,
  61. "needlogin": true,
  62. "enableSession": true,
  63. "enableCookie": false,
  64. "flag": true,
  65. "path1": os.Getenv("GOPATH"),
  66. "path2": os.Getenv("GOPATH"),
  67. "demo::key1": "asta",
  68. "demo::key2": "xie",
  69. "demo::CaseInsensitive": true,
  70. "demo::peers": []string{"one", "two", "three"},
  71. "demo::password": os.Getenv("GOPATH"),
  72. "null": "",
  73. "demo2::key1": "",
  74. "error": "",
  75. "emptystrings": []string{},
  76. }
  77. )
  78. f, err := os.Create("testini.conf")
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. _, err = f.WriteString(inicontext)
  83. if err != nil {
  84. f.Close()
  85. t.Fatal(err)
  86. }
  87. f.Close()
  88. defer os.Remove("testini.conf")
  89. iniconf, err := NewConfig("ini", "testini.conf")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. for k, v := range keyValue {
  94. var err error
  95. var value interface{}
  96. switch v.(type) {
  97. case int:
  98. value, err = iniconf.Int(k)
  99. case int64:
  100. value, err = iniconf.Int64(k)
  101. case float64:
  102. value, err = iniconf.Float(k)
  103. case bool:
  104. value, err = iniconf.Bool(k)
  105. case []string:
  106. value = iniconf.Strings(k)
  107. case string:
  108. value = iniconf.String(k)
  109. default:
  110. value, err = iniconf.DIY(k)
  111. }
  112. if err != nil {
  113. t.Fatalf("get key %q value fail,err %s", k, err)
  114. } else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
  115. t.Fatalf("get key %q value, want %v got %v .", k, v, value)
  116. }
  117. }
  118. if err = iniconf.Set("name", "astaxie"); err != nil {
  119. t.Fatal(err)
  120. }
  121. if iniconf.String("name") != "astaxie" {
  122. t.Fatal("get name error")
  123. }
  124. }
  125. func TestIniSave(t *testing.T) {
  126. const (
  127. inicontext = `
  128. app = app
  129. ;comment one
  130. #comment two
  131. # comment three
  132. appname = beeapi
  133. httpport = 8080
  134. # DB Info
  135. # enable db
  136. [dbinfo]
  137. # db type name
  138. # suport mysql,sqlserver
  139. name = mysql
  140. `
  141. saveResult = `
  142. app=app
  143. #comment one
  144. #comment two
  145. # comment three
  146. appname=beeapi
  147. httpport=8080
  148. # DB Info
  149. # enable db
  150. [dbinfo]
  151. # db type name
  152. # suport mysql,sqlserver
  153. name=mysql
  154. `
  155. )
  156. cfg, err := NewConfigData("ini", []byte(inicontext))
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. name := "newIniConfig.ini"
  161. if err := cfg.SaveConfigFile(name); err != nil {
  162. t.Fatal(err)
  163. }
  164. defer os.Remove(name)
  165. if data, err := ioutil.ReadFile(name); err != nil {
  166. t.Fatal(err)
  167. } else {
  168. cfgData := string(data)
  169. datas := strings.Split(saveResult, "\n")
  170. for _, line := range datas {
  171. if strings.Contains(cfgData, line+"\n") == false {
  172. t.Fatalf("different after save ini config file. need contains %q", line)
  173. }
  174. }
  175. }
  176. }