This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutil_test.go
130 lines (112 loc) · 3.76 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
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
package ldclient
import (
"encoding/json"
"testing"
"time"
)
func TestParseDateZero(t *testing.T) {
expectedTimeStamp := "1970-01-01T00:00:00Z"
expected, _ := time.Parse(time.RFC3339Nano, expectedTimeStamp)
testParseTime(t, expected, expected)
testParseTime(t, 0, expected)
testParseTime(t, 0.0, expected)
testParseTime(t, expectedTimeStamp, expected)
}
func TestParseUtcTimestamp(t *testing.T) {
expectedTimeStamp := "2016-04-16T22:57:31.684Z"
expected, _ := time.Parse(time.RFC3339Nano, expectedTimeStamp)
testParseTime(t, expected, expected)
testParseTime(t, 1460847451684, expected)
testParseTime(t, 1460847451684.0, expected)
testParseTime(t, expectedTimeStamp, expected)
}
func TestParseTimezone(t *testing.T) {
expectedTimeStamp := "2016-04-16T17:09:12.759-07:00"
expected, _ := time.Parse(time.RFC3339Nano, expectedTimeStamp)
testParseTime(t, expected, expected)
testParseTime(t, 1460851752759, expected)
testParseTime(t, 1460851752759.0, expected)
testParseTime(t, expectedTimeStamp, expected)
}
func TestParseTimezoneNoMillis(t *testing.T) {
expectedTimeStamp := "2016-04-16T17:09:12-07:00"
expected, _ := time.Parse(time.RFC3339Nano, expectedTimeStamp)
testParseTime(t, expected, expected)
testParseTime(t, 1460851752000, expected)
testParseTime(t, 1460851752000.0, expected)
testParseTime(t, expectedTimeStamp, expected)
}
func TestParseTimestampBeforeEpoch(t *testing.T) {
expectedTimeStamp := "1969-12-31T23:57:56.544-00:00"
expected, _ := time.Parse(time.RFC3339Nano, expectedTimeStamp)
testParseTime(t, expected, expected)
testParseTime(t, -123456, expected)
testParseTime(t, -123456.0, expected)
testParseTime(t, expectedTimeStamp, expected)
}
func testParseTime(t *testing.T, input interface{}, expected time.Time) {
actual := ParseTime(input)
if actual == nil {
t.Errorf("Got unexpected nil result when parsing: %+v", input)
return
}
if !actual.Equal(expected) {
t.Errorf("Got unexpected result: %+v Expected: %+v when parsing: %+v", actual, expected, input)
}
}
func TestParseFloat64(t *testing.T) {
testParseFloat64(t, 123, 123.0)
testParseFloat64(t, 123.0, 123.0)
testParseFloat64(t, -20, -20.0)
testParseFloat64(t, -20.0, -20.0)
testParseFloat64(t, 4e-2, .04)
testParseFloat64(t, 4.0e-2, .04)
}
func testParseFloat64(t *testing.T, input interface{}, expected float64) {
actual := ParseFloat64(input)
if actual == nil {
t.Errorf("Got unexpected nil result. Expected: %+v when parsing: %+v", expected, input)
return
}
if *actual != expected {
t.Errorf("Got unexpected result: %+v Expected: %+v when parsing: %+v", *actual, expected, input)
}
}
func TestParseBadNumber(t *testing.T) {
testParseBadNumber(t, nil)
testParseBadNumber(t, "")
testParseBadNumber(t, "a")
testParseBadNumber(t, "-")
testParseBadNumber(t, nil)
testParseBadNumber(t, "1,000.0")
testParseBadNumber(t, "$1000")
testParseBadNumber(t, "1000")
}
func testParseBadNumber(t *testing.T, input interface{}) {
actual := ParseFloat64(input)
if actual != nil {
t.Errorf("Expected nil result, but instead got: %+v when parsing: %+v", actual, input)
}
}
func TestToJsonRawMessage(t *testing.T) {
expectedJsonString := `{"FieldName":"fieldValue","NumericField":1.02}`
type expected struct {
FieldName string `json:"FieldName"`
NumericField float64 `json:"NumericField"`
}
expectedStruct := expected{FieldName: "fieldValue", NumericField: 1.02}
inputs := [3]interface{}{
json.RawMessage([]byte(expectedJsonString)),
[]byte(expectedJsonString),
expectedStruct,
}
for _, input := range inputs {
actual, err := ToJsonRawMessage(input)
if err != nil {
t.Errorf("Got unexpected error: %+v", err.Error())
}
if expectedJsonString != string(actual) {
t.Errorf("Got unexpected result: %+v but was expecting: %+v", string(actual), expectedJsonString)
}
}
}