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 (
|
|
|
|
"os"
|
2016-01-27 12:46:30 +00:00
|
|
|
"strings"
|
2013-08-21 16:07:33 +00:00
|
|
|
"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-01-27 12:46:30 +00:00
|
|
|
"path": $ENV_GOROOT
|
|
|
|
"PATH": GOROOT
|
|
|
|
"dbinfo":
|
|
|
|
"db": beego
|
|
|
|
"pwd": $ENV_GOROOT
|
|
|
|
"url": localhost
|
|
|
|
"detail":
|
|
|
|
"d1": value1
|
|
|
|
"d2": $ENV_GOROOT
|
|
|
|
"d3": ""
|
|
|
|
"group":
|
|
|
|
"id": 001
|
|
|
|
"name": gp2
|
|
|
|
"empty": ""
|
2013-08-21 16:07:33 +00:00
|
|
|
`
|
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")
|
|
|
|
}
|
|
|
|
if port, err := yamlconf.Int("httpport"); err != nil || port != 8080 {
|
|
|
|
t.Error(port)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if port, err := yamlconf.Int64("mysqlport"); err != nil || port != 3600 {
|
|
|
|
t.Error(port)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if pi, err := yamlconf.Float("PI"); err != nil || pi != 3.1415976 {
|
|
|
|
t.Error(pi)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if yamlconf.String("runmode") != "dev" {
|
|
|
|
t.Fatal("runmode not equal to dev")
|
|
|
|
}
|
|
|
|
if v, err := yamlconf.Bool("autorender"); err != nil || v != false {
|
|
|
|
t.Error(v)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if v, err := yamlconf.Bool("copyrequestbody"); err != nil || v != true {
|
|
|
|
t.Error(v)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
if dbinfo, err := yamlconf.GetSection("dbinfo"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if dbinfo["pwd"] != os.Getenv("GOROOT") {
|
|
|
|
t.Fatal("get pwd error")
|
|
|
|
} else if strings.Contains(dbinfo["detail"], os.Getenv("GOROOT")) == false {
|
|
|
|
t.Fatal("get GOROOT path error")
|
|
|
|
}
|
2013-08-21 16:07:33 +00:00
|
|
|
}
|