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