This repository has been archived by the owner on May 18, 2022. It is now read-only.
/
default_source_test.go
120 lines (88 loc) · 3.08 KB
/
default_source_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
package configo
import (
"net/url"
"testing"
"time"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestDefaultSourceFixture(t *testing.T) {
gunit.Run(new(DefaultSourceFixture), t)
}
type DefaultSourceFixture struct {
*gunit.Fixture
source *DefaultSource
pairs []DefaultPair
}
func (this *DefaultSourceFixture) TestEmptyKeyReportsNoValues() {
this.assertError(ErrKeyNotFound)
}
func (this *DefaultSourceFixture) TestAddedStringCanBeRetrieved() {
this.addValues("Hello, World!")
this.assertValues([]string{"Hello, World!"})
}
func (this *DefaultSourceFixture) TestAddingMultipleStringsRetrievesAll() {
this.addValues("Hello,")
this.addValues("World!")
this.assertValues([]string{"Hello,", "World!"})
}
func (this *DefaultSourceFixture) TestAddingBooleanTypesRetrievesAll() {
this.addValues(true, false)
this.assertValues([]string{"true", "false"})
}
func (this *DefaultSourceFixture) TestAddingIntegerTypesRetrievesAll() {
this.addValues(-1)
this.addValues(-int64(2))
this.addValues(-int32(3))
this.addValues(-int16(4))
this.addValues(-int8(5))
this.assertValues([]string{"-1", "-2", "-3", "-4", "-5"})
}
func (this *DefaultSourceFixture) TestAddingUnsignedIntegerTypesRetrievesAll() {
this.addValues(uint64(6))
this.addValues(uint32(7))
this.addValues(uint16(8))
this.addValues(uint8(9))
this.addValues(uint(10))
this.assertValues([]string{"6", "7", "8", "9", "10"})
}
func (this *DefaultSourceFixture) TestAddingUnknownTypeReturnsEmptyValue() {
this.addValues([]string{"slice"})
this.assertValues([]string{""})
}
func (this *DefaultSourceFixture) TestAddingFloatingPointTypesRetrievesAll() {
this.addValues(float32(1.23456), float64(1.2345678901234567))
this.assertValues([]string{"1.23456", "1.2345678901234567"})
}
func (this *DefaultSourceFixture) TestAddingURLTypesRetrievesAll() {
url, _ := url.Parse("https://user:pass@host:1234/path?query=value#segment")
this.addValues(url, *url)
this.assertValues([]string{"https://user:pass@host:1234/path?query=value#segment", "https://user:pass@host:1234/path?query=value#segment"})
}
func (this *DefaultSourceFixture) TestAddingDurationTypesRetrievesAll() {
this.addValues(time.Second)
this.addValues(time.Millisecond)
this.addValues(time.Microsecond)
this.addValues(time.Nanosecond)
this.assertValues([]string{"1s", "1ms", "1µs", "1ns"})
}
func (this *DefaultSourceFixture) TestAddingTimeTypesRetrievesAll() {
now := time.Now().UTC()
this.addValues(now)
this.assertValues([]string{now.String()})
}
func (this *DefaultSourceFixture) addValues(values ...interface{}) {
this.pairs = append(this.pairs, Default("key", values...))
}
func (this *DefaultSourceFixture) assertValues(expected []string) {
this.source = NewDefaultSource(this.pairs...)
values, err := this.source.Strings("key")
this.So(err, should.BeNil)
this.So(values, should.Resemble, expected)
}
func (this *DefaultSourceFixture) assertError(expected error) {
this.source = NewDefaultSource(this.pairs...)
values, err := this.source.Strings("key")
this.So(err, should.Equal, expected)
this.So(values, should.BeNil)
}