1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package keyvalues_test
import (
gc "gopkg.in/check.v1"
"github.com/juju/utils/keyvalues"
)
type keyValuesSuite struct{}
var _ = gc.Suite(&keyValuesSuite{})
var testCases = []struct {
about string
input []string
allowEmptyVal bool
output map[string]string
error string
}{{
about: "simple test case",
input: []string{"key=value"},
allowEmptyVal: false,
output: map[string]string{"key": "value"},
error: "",
}, {
about: "empty list",
input: []string{},
allowEmptyVal: false,
output: map[string]string{},
error: "",
}, {
about: "nil list",
input: nil,
allowEmptyVal: false,
output: map[string]string{},
error: "",
}, {
about: "invalid format - missing value",
input: []string{"key"},
allowEmptyVal: false,
output: nil,
error: `expected "key=value", got "key"`,
}, {
about: "invalid format - missing value",
input: []string{"key="},
allowEmptyVal: false,
output: nil,
error: `expected "key=value", got "key="`,
}, {
about: "invalid format - missing key",
input: []string{"=value"},
allowEmptyVal: false,
output: nil,
error: `expected "key=value", got "=value"`,
}, {
about: "invalid format",
input: []string{"="},
allowEmptyVal: false,
output: nil,
error: `expected "key=value", got "="`,
}, {
about: "invalid format, allowing empty",
input: []string{"="},
allowEmptyVal: true,
output: nil,
error: `expected "key=value", got "="`,
}, {
about: "duplicate keys",
input: []string{"key=value", "key=value"},
allowEmptyVal: true,
output: nil,
error: `key "key" specified more than once`,
}, {
about: "multiple keys",
input: []string{"key=value", "key2=value", "key3=value"},
allowEmptyVal: true,
output: map[string]string{"key": "value", "key2": "value", "key3": "value"},
error: "",
}, {
about: "empty value",
input: []string{"key="},
allowEmptyVal: true,
output: map[string]string{"key": ""},
error: "",
}, {
about: "whitespace trimmed",
input: []string{"key=value\n", "key2\t=\tvalue2"},
allowEmptyVal: true,
output: map[string]string{"key": "value", "key2": "value2"},
error: "",
}, {
about: "whitespace trimming and duplicate keys",
input: []string{"key =value", "key\t=\tvalue2"},
allowEmptyVal: true,
output: nil,
error: `key "key" specified more than once`,
}, {
about: "whitespace trimming and empty value not allowed",
input: []string{"key= "},
allowEmptyVal: false,
output: nil,
error: `expected "key=value", got "key="`,
}, {
about: "whitespace trimming and empty value",
input: []string{"key= "},
allowEmptyVal: true,
output: map[string]string{"key": ""},
error: "",
}, {
about: "whitespace trimming and missing key",
input: []string{" =value"},
allowEmptyVal: true,
output: nil,
error: `expected "key=value", got "=value"`,
}}
func (keyValuesSuite) TestMapParsing(c *gc.C) {
for i, t := range testCases {
c.Logf("test %d: %s", i, t.about)
result, err := keyvalues.Parse(t.input, t.allowEmptyVal)
c.Check(result, gc.DeepEquals, t.output)
if t.error == "" {
c.Check(err, gc.IsNil)
} else {
c.Check(err, gc.ErrorMatches, t.error)
}
}
}
|