-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
101 lines (92 loc) · 1.98 KB
/
parser_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
package pkgparser
import (
"reflect"
"strconv"
"testing"
"github.com/antandros/go-pkgparser/model"
)
func TestConvertStringToFloat(t *testing.T) {
parser := &Parser{}
floatExpect, _ := strconv.ParseFloat("12.34", 32)
tests := []struct {
input string
expect float64
err bool
}{
{
input: "12.34",
expect: floatExpect,
err: false,
},
{
input: "invalid_float",
expect: 0,
err: true,
},
}
for _, tt := range tests {
result, err := parser.convertStringToFloat(tt.input, reflect.TypeOf(1.1))
if tt.err && err == nil {
t.Errorf("Expected an error for input %s but got none", tt.input)
} else if !tt.err && result != tt.expect {
t.Errorf("Expected %v but got %v for input %s", tt.expect, result, tt.input)
}
}
}
func TestConvertStringToInt(t *testing.T) {
parser := &Parser{}
tests := []struct {
input string
expect int
err bool
}{
{
input: "1234",
expect: 1234,
err: false,
},
{
input: "invalid_int",
expect: 0,
err: true,
},
}
for _, tt := range tests {
result, err := parser.convertStringToInt(tt.input, reflect.TypeOf(1))
if tt.err && err == nil {
t.Errorf("Expected an error for input %s but got none", tt.input)
} else if !tt.err && result != tt.expect {
t.Errorf("Expected %v but got %v for input %s", tt.expect, result, tt.input)
}
}
}
func TestConvertContact(t *testing.T) {
parser := &Parser{}
tests := []struct {
input string
expect model.PackageContact
}{
{
input: "John Doe <john.doe@example.com>",
expect: model.PackageContact{
Contact: "john.doe@example.com",
Name: "John Doe",
Type: "email",
},
},
{
input: "Zoom <https://zoom.us>",
expect: model.PackageContact{
Contact: "https://zoom.us",
Name: "Zoom",
Type: "website",
},
},
}
for _, tt := range tests {
result, _ := parser.convertContact(tt.input, nil)
if result != tt.expect {
t.Errorf("Expected %v but got %v", tt.expect, result)
}
}
}