Skip to content

Commit

Permalink
Add comments for mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Dec 14, 2022
1 parent 29f50b2 commit 4f23e29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions browser/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func (mi *ModuleInstance) Exports() k6modules.Exports {
return k6modules.Exports{Default: mi.mod}
}

// mapping is a type of mapping between our API (api/) and the JS
// module. It acts like a bridge and allows adding wildcard methods
// and customization over our API.
type mapping map[string]any

// wildcards is a list of extra mappings for our API (api/).
Expand Down Expand Up @@ -116,6 +119,7 @@ func mapBrowserToGoja(vu k6modules.VU) *goja.Object {
return obj
}

// mapRequest to the JS module.
func mapRequest(rt *goja.Runtime, r api.Request) mapping {
maps := mapping{
"allHeaders": r.AllHeaders,
Expand Down Expand Up @@ -153,6 +157,7 @@ func mapRequest(rt *goja.Runtime, r api.Request) mapping {
return maps
}

// mapResponse to the JS module.
func mapResponse(rt *goja.Runtime, r api.Response) mapping {
maps := mapping{
"allHeaders": r.AllHeaders,
Expand Down Expand Up @@ -183,6 +188,8 @@ func mapResponse(rt *goja.Runtime, r api.Response) mapping {
return maps
}

// mapPage to the JS module.
//
//nolint:funlen
func mapPage(rt *goja.Runtime, p api.Page) mapping {
maps := mapping{
Expand Down Expand Up @@ -293,6 +300,8 @@ func mapPage(rt *goja.Runtime, p api.Page) mapping {
return maps
}

// mapFrame to the JS module.
//
//nolint:funlen
func mapFrame(rt *goja.Runtime, f api.Frame) mapping {
maps := mapping{
Expand Down Expand Up @@ -386,6 +395,8 @@ func mapFrame(rt *goja.Runtime, f api.Frame) mapping {
return maps
}

// mapElementHandle to the JS module.
//
//nolint:funlen
func mapElementHandle(rt *goja.Runtime, eh api.ElementHandle) mapping {
maps := mapping{
Expand Down Expand Up @@ -465,6 +476,7 @@ func mapElementHandle(rt *goja.Runtime, eh api.ElementHandle) mapping {
return maps
}

// mapBrowserContext to the JS module.
func mapBrowserContext(rt *goja.Runtime, bc api.BrowserContext) mapping {
return mapping{
"addCookies": bc.AddCookies,
Expand Down Expand Up @@ -511,6 +523,7 @@ func mapBrowserContext(rt *goja.Runtime, bc api.BrowserContext) mapping {
}
}

// mapBrowser to the JS module.
func mapBrowser(rt *goja.Runtime, b api.Browser) mapping {
return mapping{
"close": b.Close,
Expand All @@ -532,6 +545,7 @@ func mapBrowser(rt *goja.Runtime, b api.Browser) mapping {
}
}

// mapBrowserType to the JS module.
func mapBrowserType(rt *goja.Runtime, bt api.BrowserType) mapping {
return mapping{
"connect": bt.Connect,
Expand Down
4 changes: 4 additions & 0 deletions browser/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func TestModuleNewDisabled(t *testing.T) {
assert.Panics(t, func() { New().NewModuleInstance(vu) })
}

// TestModuleMappings tests that all the methods of the API (api/) are
// mapped to the module. This is to ensure that we don't forget to map
// a new method to the module.
func TestModuleMappings(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -145,6 +148,7 @@ func TestModuleMappings(t *testing.T) {
}
}

// toFirstLetterLower converts the first letter of the string to lower case.
func toFirstLetterLower(s string) string {
// Special case for URL.
// Instead of loading up an acronyms list, just do this.
Expand Down

0 comments on commit 4f23e29

Please sign in to comment.