forked from barnybug/cli53
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
94 lines (82 loc) · 3.33 KB
/
util_test.go
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
package cli53
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestKeyValuesString(t *testing.T) {
assert.Equal(t, "a=1", KeyValues{"a", 1}.String())
assert.Equal(t, `a=""`, KeyValues{"a", ""}.String())
assert.Equal(t, `a="1"`, KeyValues{"a", "1"}.String())
assert.Equal(t, `a="\""`, KeyValues{"a", `"`}.String())
assert.Equal(t, `a="\\"`, KeyValues{"a", `\`}.String())
}
func mustParse(input string) KeyValues {
result, err := ParseKeyValues(input)
if err != nil {
panic(err)
}
return result
}
func TestParseKeyValuesValid(t *testing.T) {
assert.Equal(t, KeyValues{"a", 1}, mustParse("a=1"))
assert.Equal(t, KeyValues{"a", ""}, mustParse(`a=""`))
assert.Equal(t, KeyValues{"identifier", 1}, mustParse("identifier=1"))
assert.Equal(t, KeyValues{"mixedCase", 1}, mustParse("mixedCase=1"))
assert.Equal(t, KeyValues{"a", "b"}, mustParse(`a="b"`))
assert.Equal(t, KeyValues{"a", `b"c`}, mustParse(`a="b\"c"`))
assert.Equal(t, KeyValues{"a", 1, "b", 2}, mustParse("a=1 b=2"))
assert.Equal(t, KeyValues{"a", 1, "b", 2}, mustParse("a=1 b=2"))
assert.Equal(t, KeyValues{"a", 1, "b", "c"}, mustParse(`a=1 b="c"`))
assert.Equal(t, KeyValues{"a", 1, "b", "c d"}, mustParse(`a=1 b="c d"`))
assert.Equal(t, KeyValues{"a", 1, "b", `c"\d`}, mustParse(`a=1 b="c\"\\d"`))
}
func parsingError(input string) error {
_, err := ParseKeyValues(input)
return err
}
func TestParseKeyValuesErrors(t *testing.T) {
assert.Error(t, parsingError(""))
assert.Error(t, parsingError("a"))
assert.Error(t, parsingError("1"))
assert.Error(t, parsingError("a="))
assert.Error(t, parsingError(`a="`))
assert.Error(t, parsingError(`a="\"`))
assert.Error(t, parsingError(`a=1b=2`))
assert.Error(t, parsingError(`a=x`))
}
func TestQuote(t *testing.T) {
assert.Equal(t, `""`, quote(""))
assert.Equal(t, `"a"`, quote("a"))
assert.Equal(t, `"\"quo\\ted\""`, quote(`"quo\ted"`))
}
func TestSplitValues(t *testing.T) {
assert.Equal(t, []string{}, splitValues(""))
assert.Equal(t, []string{""}, splitValues(`""`))
assert.Equal(t, []string{"abc"}, splitValues(`"abc"`))
assert.Equal(t, []string{"abc", "def"}, splitValues(`"abc" "def"`))
assert.Equal(t, []string{`a "quote" b`}, splitValues(`"a \"quote\" b"`))
}
func TestIsZoneId(t *testing.T) {
assert.True(t, isZoneId("Z1DXU7RZRUQ"))
assert.True(t, isZoneId("Z1DXU7RZRUQP"))
assert.True(t, isZoneId("Z1DXU7RZRUQPI"))
assert.True(t, isZoneId("Z1DXU7RZRUQPIP"))
assert.True(t, isZoneId("/hostedzone/Z1DXU7RZRUQPIP"))
assert.False(t, isZoneId("example.com"))
assert.False(t, isZoneId("example.com."))
assert.False(t, isZoneId("0.1.10.in-addr.arpa."))
}
func TestQualifyName(t *testing.T) {
assert.Equal(t, "example.com.", qualifyName("", "example.com."))
assert.Equal(t, "example.com.", qualifyName("@", "example.com."))
assert.Equal(t, "a.example.com.", qualifyName("a", "example.com."))
assert.Equal(t, "a.", qualifyName("a.", "example.com."))
assert.Equal(t, "a.b.example.com.", qualifyName("a.b", "example.com."))
}
func TestShortenName(t *testing.T) {
assert.Equal(t, "@", shortenName("example.com.", "example.com."))
assert.Equal(t, "a", shortenName("a.example.com.", "example.com."))
assert.Equal(t, "a.", shortenName("a.", "example.com."))
assert.Equal(t, "a.b", shortenName("a.b.example.com.", "example.com."))
assert.Equal(t, "fineexample.com.", shortenName("fineexample.com.", "example.com."))
}