This repository has been archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproperties_test.go
252 lines (227 loc) · 5.87 KB
/
properties_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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) 2012-2014 The Goproperties Authors.
//
// Permission is hereby granted, free of charge, to any person obtaining nl copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package properties
import (
"bytes"
"io/ioutil"
"math"
"os"
"regexp"
"testing"
"testing/iotest"
)
func mustLoad(s string) Properties {
p := make(Properties)
err := p.Load(bytes.NewReader([]byte(s)))
if err != nil {
panic(err)
}
return p
}
func eqString(l, r string) bool {
return l == r
}
func eqBool(l, r bool) bool {
return l == r
}
func eqUint64(l, r uint64) bool {
return l == r
}
func eqFloat64(l, r float64) bool {
return l == r
}
func eqInt64(l, r int64) bool {
return l == r
}
func TestGeneric(t *testing.T) {
p := mustLoad(source)
if !eqString(p["website"], "http://en.wikipedia.org/") {
t.Fail()
}
if !eqString(p["language"], "English") {
t.Fail()
}
if !eqString(p["message"], "Welcome to Wikipedia!") {
t.Fail()
}
if !eqString(p["unicode"], "Привет, Сова!") {
t.Fail()
}
if !eqString(p["key with spaces"], "This is the value that could be looked up with the key \"key with spaces\".") {
t.Fail()
}
}
func mkTempFile() *os.File {
if f, err := ioutil.TempFile(os.TempDir(), "goproperties"); err != nil {
panic(err)
return nil
} else {
return f
}
}
func TestLoadReal(t *testing.T) {
f := mkTempFile()
defer os.Remove(f.Name())
_, err := Load(f.Name())
if err != nil {
t.FailNow()
}
}
func TestLoadFiction(t *testing.T) {
f, err := ioutil.TempFile(os.TempDir(), "goproperties")
if err != nil {
t.Fatal(err)
}
os.Remove(f.Name())
_, err = Load(f.Name())
if err == nil {
t.FailNow()
}
if matched, _ := regexp.MatchString(".*no such file.*", err.Error()); !matched {
t.Errorf("got %s, want 'file not found'", err)
}
}
func TestLoadTimeoutReader(t *testing.T) {
p := make(Properties)
err := p.Load(iotest.TimeoutReader(bytes.NewReader([]byte(source))))
if err == nil {
t.Fail()
}
}
func testLoadMalformed(t *testing.T, s string) {
p := make(Properties)
err := p.Load(bytes.NewReader([]byte(s)))
if err != ErrMalformedUtf8Encoding {
t.Errorf("got %s, want %s", err, ErrMalformedUtf8Encoding)
}
}
func TestLoadMalformedKey(t *testing.T) {
testLoadMalformed(t, malformedKey)
}
func TestLoadMalformedValue(t *testing.T) {
testLoadMalformed(t, malformedValue)
}
func TestString(t *testing.T) {
p := mustLoad(source)
if !eqString(p.String("string", "not found"), "found\t\n\r\f") {
t.Fail()
}
if !eqString(p.String("missed", "not found"), "not found") {
t.Fail()
}
}
func TestBool(t *testing.T) {
p := mustLoad(source)
if !eqBool(p.Bool("bool", false), true) {
t.Fail()
}
if !eqBool(p.Bool("missed", true), true) {
t.Fail()
}
}
func TestFloat(t *testing.T) {
p := mustLoad(source)
if !eqFloat64(p.Float("float", math.MaxFloat64), math.SmallestNonzeroFloat64) {
t.Fail()
}
if !eqFloat64(p.Float("missed", math.MaxFloat64), math.MaxFloat64) {
t.Fail()
}
}
func TestInt(t *testing.T) {
p := mustLoad(source)
if !eqInt64(p.Int("int", math.MaxInt64), int64(math.MinInt64)) {
t.Fail()
}
if !eqInt64(p.Int("missed", math.MaxInt64), int64(math.MaxInt64)) {
t.Fail()
}
if !eqInt64(p.Int("hex", 0xCAFEBABE), int64(0xCAFEBABE)) {
t.Fail()
}
}
func TestUint(t *testing.T) {
p := mustLoad(source)
if !eqUint64(p.Uint("uint", 42), uint64(math.MaxUint64)) {
t.Fail()
}
if !eqUint64(p.Uint("missed", 42), uint64(42)) {
t.Fail()
}
if !eqUint64(p.Uint("hex", 0xCAFEBABE), uint64(0xCAFEBABE)) {
t.Fail()
}
}
// Test1024Comment verifies that if the lineReader is in the middle
// of parsing nl comment when it goes to read the last < 1024 byte block,
// it doesn't get confused and return EOF.
func Test1024Comment(t *testing.T) {
config := "nl = b\n"
for i := 0; i < 1024; i++ {
config += "#"
}
config += "\nc = d\n"
p := make(Properties)
p.Load(bytes.NewReader([]byte(config)))
if !eqString(p.String("nl", "not found"), "b") {
t.Fail()
}
if !eqString(p.String("c", "not found"), "d") {
t.Fail()
}
}
func TestCornerCase1(t *testing.T) {
s := "k=1\\\r\n2\nl=1\n"
for i := 0; i < 1025; i++ {
s += "#"
}
p := make(Properties)
p.Load(iotest.DataErrReader(bytes.NewReader([]byte(s))))
if !eqString(p.String("k", "-"), "12") {
t.Fail()
}
}
const (
source = `\
# You are reading the ".properties" entry.
! The exclamation mark can also mark text as comments.
website = http://en.wikipedia.org/
language = English
# The backslash below tells the application to continue reading
# the value onto the next line.
message = Welcome to \
Wikipedia!
# Add spaces to the key
key\ with\ spaces = This is the value that could be looked up with the \
key "key with spaces".
# Empty lines are skipped
# Unicode
unicode=\u041F\u0440\u0438\u0432\u0435\u0442, \u0421\u043e\u0432\u0430!
# Comment
string=found\t\n\r\f
bool=true
float=4.940656458412465441765687928682213723651e-324
int=-9223372036854775808
uint=18446744073709551615
hex=0xCAFEBABE
`
malformedValue = `unicode=\uOHNO`
malformedKey = `\uOHNO=`
)