Debian Codegraph @

keyfile/keyfile_test.go

raw
1 package keyfile
2
3 import (
4 "reflect"
5 "strings"
6 "testing"
7 )
8
9 const testParser = `
10 # This right here is a test file.
11 [Header 1]
12 key=value
13 # This line is used to test extraneous white space.
14 cat = dog
15 [Header 2]
16 # This line tests for extra equal signs.
17 man = bear = pig
18 `
19
20 func TestParser(t *testing.T) {
21 kf, err := New(strings.NewReader(testParser))
22 if err != nil {
23 t.Error(err)
24 }
25 t.Log(kf)
26
27 s := kf.Value("Header 1", "key")
28 if s != "value" {
29 t.Error("basic usage")
30 }
31 s = kf.Value("Header 1", "cat")
32 if s != "dog" {
33 t.Error("whitespace")
34 }
35 s = kf.Value("Header 2", "man")
36 if s != "bear = pig" {
37 t.Error("equal signs")
38 }
39
40 // Groups() and Keys() should always lead to valid values.
41 for _, group := range kf.Groups() {
42 if !kf.GroupExists(group) {
43 t.Errorf("GroupExists == false for group='%v'", group)
44 }
45
46 for _, key := range kf.Keys(group) {
47 if !kf.KeyExists(group, key) {
48 t.Errorf("KeyExists == false for group='%v' key='%v'", group, key)
49 }
50 }
51 }
52 }
53
54 const testInvalid = `
55 # This example will have an invalid line.
56 [Header 1]
57 key=value
58 hello world!
59 `
60
61 func TestInvalid(t *testing.T) {
62 _, err := New(strings.NewReader(testInvalid))
63 if err != ErrInvalid {
64 t.Fail()
65 }
66 }
67
68 const testList = `
69 # This tests that the formatting functions work.
70 [Header 1]
71 list=man;bear;pig;
72 list2=man\;bear;pig\r;
73 list3=man;bear;pig
74 `
75
76 func TestList(t *testing.T) {
77 kf, err := New(strings.NewReader(testList))
78 if err != nil {
79 t.Error(err)
80 }
81
82 expect := []string{"man", "bear", "pig"}
83 actual, err := kf.ValueList("Header 1", "list")
84 if err != nil {
85 t.Error(err)
86 }
87 t.Log(expect)
88 t.Log(actual)
89 if !reflect.DeepEqual(actual, expect) {
90 t.Fail()
91 }
92
93 expect = []string{"man;bear", "pig\\r"}
94 actual, err = kf.ValueList("Header 1", "list2")
95 if err != nil {
96 t.Error(err)
97 }
98 t.Log(expect)
99 t.Log(actual)
100 if !reflect.DeepEqual(actual, expect) {
101 t.Fail()
102 }
103
104 expect = []string{"man", "bear", "pig"}
105 actual, err = kf.ValueList("Header 1", "list3")
106 if err != nil {
107 t.Error(err)
108 }
109 t.Log(expect)
110 t.Log(actual)
111 if !reflect.DeepEqual(actual, expect) {
112 t.Fail()
113 }
114 }
115
116 func TestExists(t *testing.T) {
117 kf, err := New(strings.NewReader(""))
118 if err != nil {
119 t.Error(err)
120 }
121
122 if !kf.GroupExists("") {
123 t.Fail()
124 }
125
126 if kf.GroupExists("group") {
127 t.Fail()
128 }
129
130 if kf.KeyExists("", "") {
131 t.Fail()
132 }
133
134 if kf.KeyExists("group", "") {
135 t.Fail()
136 }
137
138 if kf.KeyExists("", "key") {
139 t.Fail()
140 }
141
142 if kf.KeyExists("group", "key") {
143 t.Fail()
144 }
145 }
146
147 const testListUnexpectedEndOfString = `
148 list=asdf;asdf;\`
149
150 func TestListUnexpectedEndOfString(t *testing.T) {
151 kf, err := New(strings.NewReader(testListUnexpectedEndOfString))
152 if err != nil {
153 t.FailNow()
154 }
155
156 _, err = kf.ValueList("", "list")
157 if err != ErrUnexpectedEndOfString {
158 t.Fail()
159 }
160 }