~vtuson/scopecreator/twitter-template

« back to all changes in this revision

Viewing changes to src/go/src/github.com/kylelemons/go-gypsy/yaml/config_test.go

  • Committer: Victor Palau
  • Date: 2015-03-11 14:24:42 UTC
  • Revision ID: vtuson@gmail.com-20150311142442-f2pxp111c8ynv232
public release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Google, Inc.  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
 
 
15
package yaml
 
16
 
 
17
import (
 
18
        "testing"
 
19
)
 
20
 
 
21
var dummyConfigFile = `
 
22
mapping:
 
23
  key1: value1
 
24
  key2: value2
 
25
  key3: 5
 
26
  key4: true
 
27
  key5: false
 
28
list:
 
29
  - item1
 
30
  - item2
 
31
config:
 
32
  server:
 
33
    - www.google.com
 
34
    - www.cnn.com
 
35
    - www.example.com
 
36
  admin:
 
37
    - username: god
 
38
      password: z3u5
 
39
    - username: lowly
 
40
      password: f!r3m3
 
41
`
 
42
 
 
43
var configGetTests = []struct {
 
44
        Spec string
 
45
        Want string
 
46
        Err  string
 
47
}{
 
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`},
 
62
}
 
63
 
 
64
func TestGet(t *testing.T) {
 
65
        config := Config(dummyConfigFile)
 
66
 
 
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)
 
71
                }
 
72
 
 
73
                switch err {
 
74
                case nil:
 
75
                        got = ""
 
76
                default:
 
77
                        got = err.Error()
 
78
                }
 
79
                if want := test.Err; got != want {
 
80
                        t.Errorf("Get(%q) error %#q, want %#q", test.Spec, got, want)
 
81
                }
 
82
        }
 
83
 
 
84
        i, err := config.GetInt("mapping.key3")
 
85
        if err != nil || i != 5 {
 
86
                t.Errorf("GetInt mapping.key3 wrong")
 
87
        }
 
88
 
 
89
        b, err := config.GetBool("mapping.key4")
 
90
        if err != nil || b != true {
 
91
                t.Errorf("GetBool mapping.key4 wrong")
 
92
        }
 
93
 
 
94
        b, err = config.GetBool("mapping.key5")
 
95
        if err != nil || b != false {
 
96
                t.Errorf("GetBool mapping.key5 wrong")
 
97
        }
 
98
 
 
99
}