yaml_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 yaml
  15. import (
  16. "fmt"
  17. "os"
  18. "testing"
  19. "github.com/astaxie/beego/config"
  20. )
  21. func TestYaml(t *testing.T) {
  22. var (
  23. yamlcontext = `
  24. "appname": beeapi
  25. "httpport": 8080
  26. "mysqlport": 3600
  27. "PI": 3.1415976
  28. "runmode": dev
  29. "autorender": false
  30. "copyrequestbody": true
  31. "PATH": GOPATH
  32. "path1": ${GOPATH}
  33. "path2": ${GOPATH||/home/go}
  34. "empty": ""
  35. `
  36. keyValue = map[string]interface{}{
  37. "appname": "beeapi",
  38. "httpport": 8080,
  39. "mysqlport": int64(3600),
  40. "PI": 3.1415976,
  41. "runmode": "dev",
  42. "autorender": false,
  43. "copyrequestbody": true,
  44. "PATH": "GOPATH",
  45. "path1": os.Getenv("GOPATH"),
  46. "path2": os.Getenv("GOPATH"),
  47. "error": "",
  48. "emptystrings": []string{},
  49. }
  50. )
  51. f, err := os.Create("testyaml.conf")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. _, err = f.WriteString(yamlcontext)
  56. if err != nil {
  57. f.Close()
  58. t.Fatal(err)
  59. }
  60. f.Close()
  61. defer os.Remove("testyaml.conf")
  62. yamlconf, err := config.NewConfig("yaml", "testyaml.conf")
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if yamlconf.String("appname") != "beeapi" {
  67. t.Fatal("appname not equal to beeapi")
  68. }
  69. for k, v := range keyValue {
  70. var (
  71. value interface{}
  72. err error
  73. )
  74. switch v.(type) {
  75. case int:
  76. value, err = yamlconf.Int(k)
  77. case int64:
  78. value, err = yamlconf.Int64(k)
  79. case float64:
  80. value, err = yamlconf.Float(k)
  81. case bool:
  82. value, err = yamlconf.Bool(k)
  83. case []string:
  84. value = yamlconf.Strings(k)
  85. case string:
  86. value = yamlconf.String(k)
  87. default:
  88. value, err = yamlconf.DIY(k)
  89. }
  90. if err != nil {
  91. t.Errorf("get key %q value fatal,%v err %s", k, v, err)
  92. } else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
  93. t.Errorf("get key %q value, want %v got %v .", k, v, value)
  94. }
  95. }
  96. if err = yamlconf.Set("name", "astaxie"); err != nil {
  97. t.Fatal(err)
  98. }
  99. if yamlconf.String("name") != "astaxie" {
  100. t.Fatal("get name error")
  101. }
  102. }