-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_test.go
85 lines (70 loc) · 1.56 KB
/
format_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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
var formatTestCases = []struct {
format Format
str string
path string
}{
{
format: Json,
str: "json",
path: "/format/json",
},
{
format: Html,
str: "html",
path: "/format/html",
},
{
format: Csv,
str: "csv",
path: "/format/csv",
},
{
format: Xml,
str: "xml",
path: "/format/xml",
},
}
func TestFormat(t *testing.T) {
t.Run("format set returns nil when text exists", func(t *testing.T) {
for _, f := range formatTestCases {
var format Format
assert.Nil(t, format.Set(f.str))
}
})
t.Run("format set returns error when text don't exist", func(t *testing.T) {
var format Format
got := format.Set("notexistent")
want := ErrParsingFormatType
assert.ErrorIs(t, got, want)
})
t.Run("parse format returns the right format when exist", func(t *testing.T) {
for _, want := range formatTestCases {
got, err := ParseFormat(want.str)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, want.format, got)
}
})
t.Run("parse format returns error when not exist", func(t *testing.T) {
_, got := ParseFormat("notexistent")
want := ErrParsingFormatType
assert.ErrorIs(t, got, want)
})
t.Run("to path returns format of the Format type", func(t *testing.T) {
for _, want := range formatTestCases {
got := want.format.ToPath()
assert.Equal(t, got, want.path)
}
})
t.Run("format type is string", func(t *testing.T) {
for _, f := range formatTestCases {
assert.Equal(t, "string", f.format.Type())
}
})
}