-
Notifications
You must be signed in to change notification settings - Fork 8
/
converter_test.go
68 lines (59 loc) · 1.58 KB
/
converter_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
package week
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncodeISOWeekDate(t *testing.T) {
tests := []struct {
Year int
Week int
Expected string
Error bool
}{
{Year: 0, Week: 1, Expected: `0000-W01`},
{Year: 9999, Week: 52, Expected: `9999-W52`},
{Year: 1999, Week: 21, Expected: `1999-W21`},
{Year: 1999, Week: 0, Error: true},
{Year: -1, Week: 21, Error: true},
}
for _, tt := range tests {
result, err := encodeISOWeekDate(tt.Year, tt.Week)
if tt.Error {
assert.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tt.Expected, string(result))
}
}
}
func TestDecodeISOWeekDate(t *testing.T) {
tests := []struct {
Value string
ExpectedYear int
ExpectedWeek int
Error bool
}{
{Value: `0000-W01`, ExpectedYear: 0, ExpectedWeek: 1},
{Value: `9999-W52`, ExpectedYear: 9999, ExpectedWeek: 52},
{Value: `1800-W11`, ExpectedYear: 1800, ExpectedWeek: 11},
{Value: `0000W01`, ExpectedYear: 0, ExpectedWeek: 1},
{Value: `9999W52`, ExpectedYear: 9999, ExpectedWeek: 52},
{Value: `1800W11`, ExpectedYear: 1800, ExpectedWeek: 11},
{Value: `18000-w11`, Error: true},
{Value: `0000-W00`, Error: true},
{Value: `weekdate`, Error: true},
{Value: ``, Error: true},
{Value: `-100-W-1`, Error: true},
}
for _, tt := range tests {
year, week, err := decodeISOWeekDate([]byte(tt.Value))
if tt.Error {
assert.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tt.ExpectedYear, year)
assert.Equal(t, tt.ExpectedWeek, week)
}
}
}