2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-07-17 08:22:52 +00:00
|
|
|
package yaml
|
2013-08-21 16:07:33 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-12 06:32:39 +00:00
|
|
|
"fmt"
|
2013-08-21 16:07:33 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2014-05-15 06:18:47 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/config"
|
2013-08-21 16:07:33 +00:00
|
|
|
)
|
|
|
|
|
2016-01-27 12:46:30 +00:00
|
|
|
func TestYaml(t *testing.T) {
|
|
|
|
|
|
|
|
var (
|
|
|
|
yamlcontext = `
|
2013-08-22 05:46:22 +00:00
|
|
|
"appname": beeapi
|
|
|
|
"httpport": 8080
|
|
|
|
"mysqlport": 3600
|
|
|
|
"PI": 3.1415976
|
|
|
|
"runmode": dev
|
|
|
|
"autorender": false
|
2013-08-21 16:07:33 +00:00
|
|
|
"copyrequestbody": true
|
2016-03-14 11:22:00 +00:00
|
|
|
"PATH": GOPATH
|
2016-03-29 13:47:33 +00:00
|
|
|
"path1": ${GOPATH}
|
|
|
|
"path2": ${GOPATH||/home/go}
|
2016-01-27 12:46:30 +00:00
|
|
|
"empty": ""
|
2013-08-21 16:07:33 +00:00
|
|
|
`
|
2016-03-12 06:32:39 +00:00
|
|
|
|
|
|
|
keyValue = map[string]interface{}{
|
|
|
|
"appname": "beeapi",
|
|
|
|
"httpport": 8080,
|
|
|
|
"mysqlport": int64(3600),
|
|
|
|
"PI": 3.1415976,
|
|
|
|
"runmode": "dev",
|
|
|
|
"autorender": false,
|
|
|
|
"copyrequestbody": true,
|
2016-03-14 11:22:00 +00:00
|
|
|
"PATH": "GOPATH",
|
|
|
|
"path1": os.Getenv("GOPATH"),
|
|
|
|
"path2": os.Getenv("GOPATH"),
|
2016-03-12 06:32:39 +00:00
|
|
|
"error": "",
|
|
|
|
"emptystrings": []string{},
|
|
|
|
}
|
2016-01-27 12:46:30 +00:00
|
|
|
)
|
2013-08-21 16:07:33 +00:00
|
|
|
f, err := os.Create("testyaml.conf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(yamlcontext)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
defer os.Remove("testyaml.conf")
|
2014-05-15 06:18:47 +00:00
|
|
|
yamlconf, err := config.NewConfig("yaml", "testyaml.conf")
|
2013-08-21 16:07:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-27 12:46:30 +00:00
|
|
|
|
2013-08-21 16:07:33 +00:00
|
|
|
if yamlconf.String("appname") != "beeapi" {
|
|
|
|
t.Fatal("appname not equal to beeapi")
|
|
|
|
}
|
2016-03-12 06:32:39 +00:00
|
|
|
|
|
|
|
for k, v := range keyValue {
|
|
|
|
|
|
|
|
var (
|
|
|
|
value interface{}
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch v.(type) {
|
|
|
|
case int:
|
|
|
|
value, err = yamlconf.Int(k)
|
|
|
|
case int64:
|
|
|
|
value, err = yamlconf.Int64(k)
|
|
|
|
case float64:
|
|
|
|
value, err = yamlconf.Float(k)
|
|
|
|
case bool:
|
|
|
|
value, err = yamlconf.Bool(k)
|
|
|
|
case []string:
|
|
|
|
value = yamlconf.Strings(k)
|
|
|
|
case string:
|
|
|
|
value = yamlconf.String(k)
|
|
|
|
default:
|
|
|
|
value, err = yamlconf.DIY(k)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("get key %q value fatal,%v err %s", k, v, err)
|
|
|
|
} else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
|
|
|
|
t.Errorf("get key %q value, want %v got %v .", k, v, value)
|
|
|
|
}
|
|
|
|
|
2013-08-21 16:07:33 +00:00
|
|
|
}
|
2016-03-12 06:32:39 +00:00
|
|
|
|
2013-08-21 16:07:33 +00:00
|
|
|
if err = yamlconf.Set("name", "astaxie"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if yamlconf.String("name") != "astaxie" {
|
|
|
|
t.Fatal("get name error")
|
|
|
|
}
|
2016-01-27 12:46:30 +00:00
|
|
|
|
2013-08-21 16:07:33 +00:00
|
|
|
}
|