-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrcset_test.go
More file actions
126 lines (120 loc) · 2.92 KB
/
Copy pathsrcset_test.go
File metadata and controls
126 lines (120 loc) · 2.92 KB
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
package srcset
import (
"reflect"
"testing"
)
func fl(x float64) *float64 {
return &x
}
func i(x int64) *int64 {
return &x
}
func Test_parse(t *testing.T) {
type args struct {
input string
}
tests := []struct {
name string
args args
want SourceSet
}{
{
name: "URL only",
args: args{"logo-printer-friendly.svg"},
want: SourceSet{
ImageSource{URL: "logo-printer-friendly.svg"},
},
},
{
name: "Parse URL & density",
args: args{"image-1x.png 1x, image-2x.png 2x, image-3x.png 3x, image-4x.png 4x"},
want: SourceSet{
ImageSource{URL: "image-1x.png", Density: fl(1), Offset: 0},
ImageSource{URL: "image-2x.png", Density: fl(2), Offset: 17},
ImageSource{URL: "image-3x.png", Density: fl(3), Offset: 34},
ImageSource{URL: "image-4x.png", Density: fl(4), Offset: 51},
},
},
{
name: "Parse URL & width - with line break whitespace",
args: args{`elva-fairy-320w.jpg 320w,
elva-fairy-480w.jpg 480w,
elva-fairy-800w.jpg 800w`},
want: SourceSet{
ImageSource{URL: "elva-fairy-320w.jpg", Width: i(320), Offset: 0},
ImageSource{URL: "elva-fairy-480w.jpg", Width: i(480), Offset: 41},
ImageSource{URL: "elva-fairy-800w.jpg", Width: i(800), Offset: 82},
},
},
{
name: "Parse URL & height - with line break whitespace",
args: args{`elva-fairy-320h.jpg 320h,
elva-fairy-480h.jpg 480h,
elva-fairy-800h.jpg 800h`},
want: SourceSet{
ImageSource{URL: "elva-fairy-320h.jpg", Height: i(320), Offset: 0},
ImageSource{URL: "elva-fairy-480h.jpg", Height: i(480), Offset: 41},
ImageSource{URL: "elva-fairy-800h.jpg", Height: i(800), Offset: 82},
},
},
{
name: "Invalid: Multiple densities",
args: args{"test.png 1x 2x"},
want: SourceSet{},
},
{
name: "Invalid: Density and width",
args: args{"test.png 1x 200w"},
want: SourceSet{},
},
{
name: "Invalid: negative width",
args: args{"test.png -100w"},
want: SourceSet{},
},
{
name: "Invalid: zero width",
args: args{"test.png 0w"},
want: SourceSet{},
},
{
name: "Invalid: None-number width",
args: args{"test.png f55w"},
want: SourceSet{},
},
{
name: "Invalid: negative height",
args: args{"test.png -100h"},
want: SourceSet{},
},
{
name: "Invalid: zero height",
args: args{"test.png 0h"},
want: SourceSet{},
},
{
name: "Invalid: multiple heights",
args: args{"test.png 124h 234h"},
want: SourceSet{},
},
{
name: "Invalid: negative density",
args: args{"test.png -1.3x"},
want: SourceSet{},
},
{
name: "Super funky",
args: args{"data:,a ( , data:,b 1x, ), data:,c"},
want: SourceSet{
ImageSource{URL: "data:,c", Offset: 27},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Parse(tt.args.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("%q. Parse() = %v, want %v", tt.name, got, tt.want)
}
})
}
}