@@ -23,105 +23,111 @@ import (
23
23
func TestMappings (t * testing.T ) {
24
24
t .Parallel ()
25
25
26
- vu := & k6modulestest.VU {
27
- RuntimeField : goja .New (),
28
- InitEnvField : & k6common.InitEnvironment {
29
- Registry : k6metrics .NewRegistry (),
30
- },
26
+ type test struct {
27
+ apiInterface any
28
+ mapp func () mapping
31
29
}
32
- rt := vu .Runtime ()
33
30
34
- type test struct {
35
- apiObj any
36
- mapp func () mapping
31
+ var (
32
+ vu = & k6modulestest.VU {
33
+ RuntimeField : goja .New (),
34
+ InitEnvField : & k6common.InitEnvironment {
35
+ Registry : k6metrics .NewRegistry (),
36
+ },
37
+ }
38
+ rt = vu .Runtime ()
39
+ wildcards = wildcards ()
40
+ )
41
+
42
+ // testMapping tests that all the methods of an API are mapped
43
+ // to the module. And wildcards are mapped correctly and their
44
+ // methods are not mapped.
45
+ testMapping := func (tt test ) {
46
+ var (
47
+ typ = reflect .TypeOf (tt .apiInterface ).Elem ()
48
+ mapped = tt .mapp ()
49
+ )
50
+ for i := 0 ; i < typ .NumMethod (); i ++ {
51
+ method := typ .Method (i )
52
+ require .NotNil (t , method )
53
+
54
+ // goja uses methods that starts with lowercase.
55
+ // so we need to convert the first letter to lowercase.
56
+ m := toFirstLetterLower (method .Name )
57
+
58
+ wm , wok := isWildcard (wildcards , typ .Name (), m )
59
+ // if the method is a wildcard method, it should not
60
+ // be mapped to the module. so we should not find it
61
+ // in the mapped methods.
62
+ if _ , ok := mapped [m ]; wok && ok {
63
+ t .Errorf ("method %s should not be mapped" , m )
64
+ }
65
+ // change the method name if it is mapped to a wildcard
66
+ // method. these wildcard methods are not exist on our
67
+ // API. so we need to use the mapped method instead.
68
+ if wok {
69
+ m = wm
70
+ }
71
+ if _ , ok := mapped [m ]; ! ok {
72
+ t .Errorf ("method %s not found" , m )
73
+ }
74
+ }
37
75
}
38
- mappers := map [string ]test {
76
+
77
+ for name , tt := range map [string ]test {
39
78
"browserType" : {
40
- apiObj : (* api .BrowserType )(nil ),
79
+ apiInterface : (* api .BrowserType )(nil ),
41
80
mapp : func () mapping {
42
81
return mapBrowserType (rt , & chromium.BrowserType {})
43
82
},
44
83
},
45
84
"browser" : {
46
- apiObj : (* api .Browser )(nil ),
85
+ apiInterface : (* api .Browser )(nil ),
47
86
mapp : func () mapping {
48
87
return mapBrowser (rt , & chromium.Browser {})
49
88
},
50
89
},
51
90
"browserContext" : {
52
- apiObj : (* api .BrowserContext )(nil ),
91
+ apiInterface : (* api .BrowserContext )(nil ),
53
92
mapp : func () mapping {
54
93
return mapBrowserContext (rt , & common.BrowserContext {})
55
94
},
56
95
},
57
96
"page" : {
58
- apiObj : (* api .Page )(nil ),
97
+ apiInterface : (* api .Page )(nil ),
59
98
mapp : func () mapping {
60
99
return mapPage (rt , & common.Page {})
61
100
},
62
101
},
63
102
"elementHandle" : {
64
- apiObj : (* api .ElementHandle )(nil ),
103
+ apiInterface : (* api .ElementHandle )(nil ),
65
104
mapp : func () mapping {
66
105
return mapElementHandle (rt , & common.ElementHandle {})
67
106
},
68
107
},
69
108
"frame" : {
70
- apiObj : (* api .Frame )(nil ),
109
+ apiInterface : (* api .Frame )(nil ),
71
110
mapp : func () mapping {
72
111
return mapFrame (rt , & common.Frame {})
73
112
},
74
113
},
75
114
"mapRequest" : {
76
- apiObj : (* api .Request )(nil ),
115
+ apiInterface : (* api .Request )(nil ),
77
116
mapp : func () mapping {
78
117
return mapRequest (rt , & common.Request {})
79
118
},
80
119
},
81
120
"mapResponse" : {
82
- apiObj : (* api .Response )(nil ),
121
+ apiInterface : (* api .Response )(nil ),
83
122
mapp : func () mapping {
84
123
return mapResponse (rt , & common.Response {})
85
124
},
86
125
},
87
- }
88
-
89
- wildcards := wildcards ()
90
-
91
- for name , tt := range mappers {
126
+ } {
92
127
tt := tt
93
128
t .Run (name , func (t * testing.T ) {
94
129
t .Parallel ()
95
-
96
- var (
97
- typ = reflect .TypeOf (tt .apiObj ).Elem ()
98
- mapped = tt .mapp ()
99
- )
100
- for i := 0 ; i < typ .NumMethod (); i ++ {
101
- method := typ .Method (i )
102
- require .NotNil (t , method )
103
-
104
- // goja uses methods that starts with lowercase.
105
- // so we need to convert the first letter to lowercase.
106
- m := toFirstLetterLower (method .Name )
107
-
108
- wm , wok := isWildcard (wildcards , typ .Name (), m )
109
- // if the method is a wildcard method, it should not
110
- // be mapped to the module. so we should not find it
111
- // in the mapped methods.
112
- if _ , ok := mapped [m ]; wok && ok {
113
- t .Errorf ("method %s should not be mapped" , m )
114
- }
115
- // change the method name if it is mapped to a wildcard
116
- // method. these wildcard methods are not exist on our
117
- // API. so we need to use the mapped method instead.
118
- if wok {
119
- m = wm
120
- }
121
- if _ , ok := mapped [m ]; ! ok {
122
- t .Errorf ("method %s not found" , m )
123
- }
124
- }
130
+ testMapping (tt )
125
131
})
126
132
}
127
133
}
0 commit comments