Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 48b63ba

Browse files
committed
Refactor module mappings test
1 parent 033adcf commit 48b63ba

File tree

1 file changed

+59
-53
lines changed

1 file changed

+59
-53
lines changed

browser/mapping_test.go

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,105 +23,111 @@ import (
2323
func TestMappings(t *testing.T) {
2424
t.Parallel()
2525

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
3129
}
32-
rt := vu.Runtime()
3330

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+
}
3775
}
38-
mappers := map[string]test{
76+
77+
for name, tt := range map[string]test{
3978
"browserType": {
40-
apiObj: (*api.BrowserType)(nil),
79+
apiInterface: (*api.BrowserType)(nil),
4180
mapp: func() mapping {
4281
return mapBrowserType(rt, &chromium.BrowserType{})
4382
},
4483
},
4584
"browser": {
46-
apiObj: (*api.Browser)(nil),
85+
apiInterface: (*api.Browser)(nil),
4786
mapp: func() mapping {
4887
return mapBrowser(rt, &chromium.Browser{})
4988
},
5089
},
5190
"browserContext": {
52-
apiObj: (*api.BrowserContext)(nil),
91+
apiInterface: (*api.BrowserContext)(nil),
5392
mapp: func() mapping {
5493
return mapBrowserContext(rt, &common.BrowserContext{})
5594
},
5695
},
5796
"page": {
58-
apiObj: (*api.Page)(nil),
97+
apiInterface: (*api.Page)(nil),
5998
mapp: func() mapping {
6099
return mapPage(rt, &common.Page{})
61100
},
62101
},
63102
"elementHandle": {
64-
apiObj: (*api.ElementHandle)(nil),
103+
apiInterface: (*api.ElementHandle)(nil),
65104
mapp: func() mapping {
66105
return mapElementHandle(rt, &common.ElementHandle{})
67106
},
68107
},
69108
"frame": {
70-
apiObj: (*api.Frame)(nil),
109+
apiInterface: (*api.Frame)(nil),
71110
mapp: func() mapping {
72111
return mapFrame(rt, &common.Frame{})
73112
},
74113
},
75114
"mapRequest": {
76-
apiObj: (*api.Request)(nil),
115+
apiInterface: (*api.Request)(nil),
77116
mapp: func() mapping {
78117
return mapRequest(rt, &common.Request{})
79118
},
80119
},
81120
"mapResponse": {
82-
apiObj: (*api.Response)(nil),
121+
apiInterface: (*api.Response)(nil),
83122
mapp: func() mapping {
84123
return mapResponse(rt, &common.Response{})
85124
},
86125
},
87-
}
88-
89-
wildcards := wildcards()
90-
91-
for name, tt := range mappers {
126+
} {
92127
tt := tt
93128
t.Run(name, func(t *testing.T) {
94129
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)
125131
})
126132
}
127133
}

0 commit comments

Comments
 (0)