mirror of
https://github.com/astaxie/beego.git
synced 2025-07-07 02:50:19 +00:00
Merge branch 'astaxie/develop' into iniSaveErrorFix
# Conflicts: # config/ini_test.go
This commit is contained in:
@ -15,12 +15,17 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var inicontext = `
|
||||
func TestIni(t *testing.T) {
|
||||
|
||||
var (
|
||||
inicontext = `
|
||||
;comment one
|
||||
#comment two
|
||||
appname = beeapi
|
||||
@ -30,6 +35,13 @@ PI = 3.1415976
|
||||
runmode = "dev"
|
||||
autorender = false
|
||||
copyrequestbody = true
|
||||
session= on
|
||||
cookieon= off
|
||||
newreg = OFF
|
||||
needlogin = ON
|
||||
enableSession = Y
|
||||
enableCookie = N
|
||||
flag = 1
|
||||
[demo]
|
||||
key1="asta"
|
||||
key2 = "xie"
|
||||
@ -37,7 +49,31 @@ CaseInsensitive = true
|
||||
peers = one;two;three
|
||||
`
|
||||
|
||||
func TestIni(t *testing.T) {
|
||||
keyValue = map[string]interface{}{
|
||||
"appname": "beeapi",
|
||||
"httpport": 8080,
|
||||
"mysqlport": int64(3600),
|
||||
"pi": 3.1415976,
|
||||
"runmode": "dev",
|
||||
"autorender": false,
|
||||
"copyrequestbody": true,
|
||||
"session": true,
|
||||
"cookieon": false,
|
||||
"newreg": false,
|
||||
"needlogin": true,
|
||||
"enableSession": true,
|
||||
"enableCookie": false,
|
||||
"flag": true,
|
||||
"demo::key1": "asta",
|
||||
"demo::key2": "xie",
|
||||
"demo::CaseInsensitive": true,
|
||||
"demo::peers": []string{"one", "two", "three"},
|
||||
"null": "",
|
||||
"demo2::key1": "",
|
||||
"error": "",
|
||||
}
|
||||
)
|
||||
|
||||
f, err := os.Create("testini.conf")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -53,31 +89,31 @@ func TestIni(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if iniconf.String("appname") != "beeapi" {
|
||||
t.Fatal("appname not equal to beeapi")
|
||||
}
|
||||
if port, err := iniconf.Int("httpport"); err != nil || port != 8080 {
|
||||
t.Error(port)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if port, err := iniconf.Int64("mysqlport"); err != nil || port != 3600 {
|
||||
t.Error(port)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pi, err := iniconf.Float("PI"); err != nil || pi != 3.1415976 {
|
||||
t.Error(pi)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if iniconf.String("runmode") != "dev" {
|
||||
t.Fatal("runmode not equal to dev")
|
||||
}
|
||||
if v, err := iniconf.Bool("autorender"); err != nil || v != false {
|
||||
t.Error(v)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v, err := iniconf.Bool("copyrequestbody"); err != nil || v != true {
|
||||
t.Error(v)
|
||||
t.Fatal(err)
|
||||
for k, v := range keyValue {
|
||||
var err error
|
||||
var value interface{}
|
||||
switch v.(type) {
|
||||
case int:
|
||||
value, err = iniconf.Int(k)
|
||||
case int64:
|
||||
value, err = iniconf.Int64(k)
|
||||
case float64:
|
||||
value, err = iniconf.Float(k)
|
||||
case bool:
|
||||
value, err = iniconf.Bool(k)
|
||||
case []string:
|
||||
value = iniconf.Strings(k)
|
||||
case string:
|
||||
value = iniconf.String(k)
|
||||
default:
|
||||
value, err = iniconf.DIY(k)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("get key %q value fail,err %s", k, err)
|
||||
} else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
|
||||
t.Fatalf("get key %q value, want %v got %v .", k, v, value)
|
||||
}
|
||||
|
||||
}
|
||||
if err = iniconf.Set("name", "astaxie"); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -85,21 +121,6 @@ func TestIni(t *testing.T) {
|
||||
if iniconf.String("name") != "astaxie" {
|
||||
t.Fatal("get name error")
|
||||
}
|
||||
if iniconf.String("demo::key1") != "asta" {
|
||||
t.Fatal("get demo.key1 error")
|
||||
}
|
||||
if iniconf.String("demo::key2") != "xie" {
|
||||
t.Fatal("get demo.key2 error")
|
||||
}
|
||||
if v, err := iniconf.Bool("demo::caseinsensitive"); err != nil || v != true {
|
||||
t.Fatal("get demo.caseinsensitive error")
|
||||
}
|
||||
|
||||
if data := iniconf.Strings("demo::peers"); len(data) != 3 {
|
||||
t.Fatal("get strings error", data)
|
||||
} else if data[0] != "one" {
|
||||
t.Fatal("get first params error not equat to one")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -121,7 +142,8 @@ httpport = 8080
|
||||
name = mysql
|
||||
`
|
||||
|
||||
saveResult = `app=app
|
||||
saveResult = `
|
||||
app=app
|
||||
#comment one
|
||||
#comment two
|
||||
# comment three
|
||||
@ -134,7 +156,6 @@ httpport=8080
|
||||
# db type name
|
||||
# suport mysql,sqlserver
|
||||
name=mysql
|
||||
|
||||
`
|
||||
)
|
||||
cfg, err := NewConfigData("ini", []byte(inicontext))
|
||||
@ -149,7 +170,14 @@ name=mysql
|
||||
|
||||
if data, err := ioutil.ReadFile(name); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if string(data) != saveResult {
|
||||
t.Fatal("different after save ini config file.")
|
||||
} else {
|
||||
cfgData := string(data)
|
||||
datas := strings.Split(saveResult, "\n")
|
||||
for _, line := range datas {
|
||||
if strings.Contains(cfgData, line+"\n") == false {
|
||||
t.Fatalf("different after save ini config file. need contains %q", line)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user