1
// Copyright 2013 Google, Inc. All rights reserved.
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
21
var dummyConfigFile = `
43
var configGetTests = []struct {
48
{"mapping.key1", "value1", ""},
49
{"mapping.key2", "value2", ""},
50
{"list[0]", "item1", ""},
51
{"list[1]", "item2", ""},
52
{"list", "", `yaml: list: type mismatch: "list" is yaml.List, want yaml.Scalar (at "$")`},
53
{"list.0", "", `yaml: .list.0: type mismatch: ".list" is yaml.List, want yaml.Map (at ".0")`},
54
{"config.server[0]", "www.google.com", ""},
55
{"config.server[1]", "www.cnn.com", ""},
56
{"config.server[2]", "www.example.com", ""},
57
{"config.server[3]", "", `yaml: .config.server[3]: ".config.server[3]" not found`},
58
{"config.listen[0]", "", `yaml: .config.listen[0]: ".config.listen" not found`},
59
{"config.admin[0].username", "god", ""},
60
{"config.admin[1].username", "lowly", ""},
61
{"config.admin[2].username", "", `yaml: .config.admin[2].username: ".config.admin[2]" not found`},
64
func TestGet(t *testing.T) {
65
config := Config(dummyConfigFile)
67
for _, test := range configGetTests {
68
got, err := config.Get(test.Spec)
69
if want := test.Want; got != want {
70
t.Errorf("Get(%q) = %q, want %q", test.Spec, got, want)
79
if want := test.Err; got != want {
80
t.Errorf("Get(%q) error %#q, want %#q", test.Spec, got, want)
84
i, err := config.GetInt("mapping.key3")
85
if err != nil || i != 5 {
86
t.Errorf("GetInt mapping.key3 wrong")
89
b, err := config.GetBool("mapping.key4")
90
if err != nil || b != true {
91
t.Errorf("GetBool mapping.key4 wrong")
94
b, err = config.GetBool("mapping.key5")
95
if err != nil || b != false {
96
t.Errorf("GetBool mapping.key5 wrong")