From c6553913e3e7304fad66a008f09bf5f744f162f1 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Fri, 28 Jul 2023 14:03:25 -0700 Subject: [PATCH] fix: replace `expect(generic)` with `expect(value)` (#1109) --- .../docs/api/class-apiresponseassertions.mdx | 2 +- dotnet/docs/release-notes.mdx | 2 +- dotnet/docs/test-assertions.mdx | 2 +- java/docs/api/class-apiresponseassertions.mdx | 2 +- java/docs/test-assertions.mdx | 2 +- .../docs/api/class-apiresponseassertions.mdx | 2 +- nodejs/docs/api/class-genericassertions.mdx | 20 +++++++++---------- nodejs/docs/api/class-testconfig.mdx | 4 ++-- nodejs/docs/api/class-testproject.mdx | 4 ++-- nodejs/docs/release-notes.mdx | 4 ++-- nodejs/docs/test-assertions.mdx | 2 +- nodejs/docs/test-configuration.mdx | 2 +- .../docs/api/class-apiresponseassertions.mdx | 4 ++-- python/docs/test-assertions.mdx | 2 +- src/format_csharp.js | 12 +++++------ src/format_java.js | 16 +++++++-------- src/format_js.js | 12 +++++------ src/format_python.js | 14 ++++++------- src/generator.js | 18 +++++++++++++++-- 19 files changed, 66 insertions(+), 60 deletions(-) diff --git a/dotnet/docs/api/class-apiresponseassertions.mdx b/dotnet/docs/api/class-apiresponseassertions.mdx index 693deed0a84..9c9a5f6e177 100644 --- a/dotnet/docs/api/class-apiresponseassertions.mdx +++ b/dotnet/docs/api/class-apiresponseassertions.mdx @@ -35,7 +35,7 @@ Makes the assertion check for the opposite condition. For example, this code tes **Usage** ```csharp -Expect(ApiResponse).Not +Expect(Response).Not ``` **Type** diff --git a/dotnet/docs/release-notes.mdx b/dotnet/docs/release-notes.mdx index 7c92a8d41b1..856dde14501 100644 --- a/dotnet/docs/release-notes.mdx +++ b/dotnet/docs/release-notes.mdx @@ -288,7 +288,7 @@ This version was also tested against the following stable channels: - [Expect(Locator).ToHaveTextAsync()](/api/class-locatorassertions.mdx#locator-assertions-to-have-text) now pierces open shadow roots. - New option `Editable` for [Expect(Locator).ToBeEditableAsync()](/api/class-locatorassertions.mdx#locator-assertions-to-be-editable). - New option `Visible` for [Expect(Locator).ToBeVisibleAsync()](/api/class-locatorassertions.mdx#locator-assertions-to-be-visible). -- [Expect(ApiResponse).ToBeOKAsync()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok) is now available. +- [Expect(Response).ToBeOKAsync()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok) is now available. ### Other highlights - New option `MaxRedirects` for [ApiRequestContext.GetAsync()](/api/class-apirequestcontext.mdx#api-request-context-get) and others to limit redirect count. diff --git a/dotnet/docs/test-assertions.mdx b/dotnet/docs/test-assertions.mdx index 795d3cf7534..66ebeb0e913 100644 --- a/dotnet/docs/test-assertions.mdx +++ b/dotnet/docs/test-assertions.mdx @@ -30,7 +30,7 @@ import HTMLCard from '@site/src/components/HTMLCard'; | [Expect(Locator).ToHaveValuesAsync()](/api/class-locatorassertions.mdx#locator-assertions-to-have-values) | Select has options selected | | [Expect(Page).ToHaveTitleAsync()](/api/class-pageassertions.mdx#page-assertions-to-have-title) | Page has a title | | [Expect(Page).ToHaveURLAsync()](/api/class-pageassertions.mdx#page-assertions-to-have-url) | Page has a URL | -| [Expect(ApiResponse).ToBeOKAsync()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok) | Response has an OK status | +| [Expect(Response).ToBeOKAsync()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok) | Response has an OK status | [Accessibility]: /api/class-accessibility.mdx "Accessibility" diff --git a/java/docs/api/class-apiresponseassertions.mdx b/java/docs/api/class-apiresponseassertions.mdx index ac02ea4902a..51579641b56 100644 --- a/java/docs/api/class-apiresponseassertions.mdx +++ b/java/docs/api/class-apiresponseassertions.mdx @@ -58,7 +58,7 @@ assertThat(response).not().isOK(); **Usage** ```java -assertThat(apiResponse).not() +assertThat(response).not() ``` **Returns** diff --git a/java/docs/test-assertions.mdx b/java/docs/test-assertions.mdx index 3b819981c0b..7effafc6906 100644 --- a/java/docs/test-assertions.mdx +++ b/java/docs/test-assertions.mdx @@ -30,7 +30,7 @@ import HTMLCard from '@site/src/components/HTMLCard'; | [assertThat(locator).hasValues()](/api/class-locatorassertions.mdx#locator-assertions-to-have-values) | Select has options selected | | [assertThat(page).hasTitle()](/api/class-pageassertions.mdx#page-assertions-to-have-title) | Page has a title | | [assertThat(page).hasURL()](/api/class-pageassertions.mdx#page-assertions-to-have-url) | Page has a URL | -| [assertThat(apiResponse).isOK()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok) | Response has an OK status | +| [assertThat(response).isOK()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok) | Response has an OK status | [APIRequest]: /api/class-apirequest.mdx "APIRequest" diff --git a/nodejs/docs/api/class-apiresponseassertions.mdx b/nodejs/docs/api/class-apiresponseassertions.mdx index 6cba9604fa7..fdc09b2ef66 100644 --- a/nodejs/docs/api/class-apiresponseassertions.mdx +++ b/nodejs/docs/api/class-apiresponseassertions.mdx @@ -53,7 +53,7 @@ await expect(response).not.toBeOK(); **Usage** ```js -expect(apiResponse).not +expect(response).not ``` **Type** diff --git a/nodejs/docs/api/class-genericassertions.mdx b/nodejs/docs/api/class-genericassertions.mdx index 4ce857da3f5..95c7258b60e 100644 --- a/nodejs/docs/api/class-genericassertions.mdx +++ b/nodejs/docs/api/class-genericassertions.mdx @@ -49,7 +49,7 @@ expect(value.prop).toBe(1); Added in: v1.9genericAssertions.toBeCloseTo -Compares floating point numbers for approximate equality. Use this method instead of [expect(generic).toBe()](/api/class-genericassertions.mdx#generic-assertions-to-be) when comparing floating point numbers. +Compares floating point numbers for approximate equality. Use this method instead of [expect(value).toBe()](/api/class-genericassertions.mdx#generic-assertions-to-be) when comparing floating point numbers. **Usage** @@ -308,9 +308,9 @@ expect(new Set(value)).toContain(2); Ensures that value is an `Array` or `Set` and contains an item equal to the expected. -For objects, this method recursively checks equality of all fields, rather than comparing objects by reference as performed by [expect(generic).toContain()](/api/class-genericassertions.mdx#generic-assertions-to-contain-2). +For objects, this method recursively checks equality of all fields, rather than comparing objects by reference as performed by [expect(value).toContain()](/api/class-genericassertions.mdx#generic-assertions-to-contain-2). -For primitive values, this method is equivalent to [expect(generic).toContain()](/api/class-genericassertions.mdx#generic-assertions-to-contain-2). +For primitive values, this method is equivalent to [expect(value).toContain()](/api/class-genericassertions.mdx#generic-assertions-to-contain-2). **Usage** @@ -337,9 +337,9 @@ expect(new Set(value)).toContainEqual({ another: 2 }); Compares contents of the value with contents of `expected`, performing "deep equality" check. -For objects, this method recursively checks equality of all fields, rather than comparing objects by reference as performed by [expect(generic).toBe()](/api/class-genericassertions.mdx#generic-assertions-to-be). +For objects, this method recursively checks equality of all fields, rather than comparing objects by reference as performed by [expect(value).toBe()](/api/class-genericassertions.mdx#generic-assertions-to-be). -For primitive values, this method is equivalent to [expect(generic).toBe()](/api/class-genericassertions.mdx#generic-assertions-to-be). +For primitive values, this method is equivalent to [expect(value).toBe()](/api/class-genericassertions.mdx#generic-assertions-to-be). **Usage** @@ -379,7 +379,7 @@ expect([1, 2, 3]).toHaveLength(3); Added in: v1.9genericAssertions.toHaveProperty -Ensures that property at provided `keyPath` exists on the object and optionally checks that property is equal to the `expected`. Equality is checked recursively, similarly to [expect(generic).toEqual()](/api/class-genericassertions.mdx#generic-assertions-to-equal). +Ensures that property at provided `keyPath` exists on the object and optionally checks that property is equal to the `expected`. Equality is checked recursively, similarly to [expect(value).toEqual()](/api/class-genericassertions.mdx#generic-assertions-to-equal). **Usage** @@ -431,7 +431,7 @@ expect(value).toMatch(/Is \d+ enough/); Added in: v1.9genericAssertions.toMatchObject -Compares contents of the value with contents of `expected`, performing "deep equality" check. Allows extra properties to be present in the value, unlike [expect(generic).toEqual()](/api/class-genericassertions.mdx#generic-assertions-to-equal), so you can check just a subset of object properties. +Compares contents of the value with contents of `expected`, performing "deep equality" check. Allows extra properties to be present in the value, unlike [expect(value).toEqual()](/api/class-genericassertions.mdx#generic-assertions-to-equal), so you can check just a subset of object properties. When comparing arrays, the number of items must match, and each item is checked recursively. @@ -462,7 +462,7 @@ expect([{ a: 1, b: 2 }]).toMatchObject([{ a: 1 }]); Compares contents of the value with contents of `expected` **and** their types. -Differences from [expect(generic).toEqual()](/api/class-genericassertions.mdx#generic-assertions-to-equal): +Differences from [expect(value).toEqual()](/api/class-genericassertions.mdx#generic-assertions-to-equal): * Keys with undefined properties are checked. For example, `{ a: undefined, b: 2 }` does not match `{ b: 2 }`. * Array sparseness is checked. For example, `[, 1]` does not match `[undefined, 1]`. * Object types are checked to be equal. For example, a class instance with fields `a` and `b` will not equal a literal object with fields `a` and `b`. @@ -520,7 +520,7 @@ expect(() => { Added in: v1.9genericAssertions.toThrowError -An alias for [expect(generic).toThrow()](/api/class-genericassertions.mdx#generic-assertions-to-throw). +An alias for [expect(value).toThrow()](/api/class-genericassertions.mdx#generic-assertions-to-throw). **Usage** @@ -553,7 +553,7 @@ await expect(value).not.toBe(2); **Usage** ```js -expect(generic).not +expect(value).not ``` **Type** diff --git a/nodejs/docs/api/class-testconfig.mdx b/nodejs/docs/api/class-testconfig.mdx index 1781979418c..d8a09b32dff 100644 --- a/nodejs/docs/api/class-testconfig.mdx +++ b/nodejs/docs/api/class-testconfig.mdx @@ -111,7 +111,7 @@ export default defineConfig({ an acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default. - Configuration for the [expect(snapshot).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1) method. + Configuration for the [expect(value).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1) method. --- @@ -603,7 +603,7 @@ export default defineConfig({ Added in: v1.28testConfig.snapshotPathTemplate -This option configures a template controlling location of snapshots generated by [expect(page).toHaveScreenshot()](/api/class-pageassertions.mdx#page-assertions-to-have-screenshot-1) and [expect(snapshot).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1). +This option configures a template controlling location of snapshots generated by [expect(page).toHaveScreenshot()](/api/class-pageassertions.mdx#page-assertions-to-have-screenshot-1) and [expect(value).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1). **Usage** diff --git a/nodejs/docs/api/class-testproject.mdx b/nodejs/docs/api/class-testproject.mdx index 339ee3ff96f..767ec2e414f 100644 --- a/nodejs/docs/api/class-testproject.mdx +++ b/nodejs/docs/api/class-testproject.mdx @@ -148,7 +148,7 @@ testProject.expect an acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default. - Configuration for the [expect(snapshot).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1) method. + Configuration for the [expect(value).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1) method. --- @@ -341,7 +341,7 @@ testProject.snapshotDir Added in: v1.28testProject.snapshotPathTemplate -This option configures a template controlling location of snapshots generated by [expect(page).toHaveScreenshot()](/api/class-pageassertions.mdx#page-assertions-to-have-screenshot-1) and [expect(snapshot).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1). +This option configures a template controlling location of snapshots generated by [expect(page).toHaveScreenshot()](/api/class-pageassertions.mdx#page-assertions-to-have-screenshot-1) and [expect(value).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1). **Usage** diff --git a/nodejs/docs/release-notes.mdx b/nodejs/docs/release-notes.mdx index 5559bee47dd..80f45dbb5fa 100644 --- a/nodejs/docs/release-notes.mdx +++ b/nodejs/docs/release-notes.mdx @@ -468,7 +468,7 @@ This version was also tested against the following stable channels: test('runs second', async ({ page }) => {}); ``` -* Use [testProject.snapshotPathTemplate](/api/class-testproject.mdx#test-project-snapshot-path-template) and [testConfig.snapshotPathTemplate](/api/class-testconfig.mdx#test-config-snapshot-path-template) to configure a template controlling location of snapshots generated by [expect(page).toHaveScreenshot()](/api/class-pageassertions.mdx#page-assertions-to-have-screenshot-1) and [expect(snapshot).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1). +* Use [testProject.snapshotPathTemplate](/api/class-testproject.mdx#test-project-snapshot-path-template) and [testConfig.snapshotPathTemplate](/api/class-testconfig.mdx#test-config-snapshot-path-template) to configure a template controlling location of snapshots generated by [expect(page).toHaveScreenshot()](/api/class-pageassertions.mdx#page-assertions-to-have-screenshot-1) and [expect(value).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1). ```js title="playwright.config.ts" import { defineConfig } from '@playwright/test'; @@ -927,7 +927,7 @@ WebServer is now considered "ready" if request to the specified url has any of t await expect(page).toHaveScreenshot(); ``` - The new [expect(page).toHaveScreenshot()](/api/class-pageassertions.mdx#page-assertions-to-have-screenshot-1) saves screenshots at the same location as [expect(snapshot).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1). + The new [expect(page).toHaveScreenshot()](/api/class-pageassertions.mdx#page-assertions-to-have-screenshot-1) saves screenshots at the same location as [expect(value).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1). ## Version 1.21 diff --git a/nodejs/docs/test-assertions.mdx b/nodejs/docs/test-assertions.mdx index 610613fe6f9..280f554b820 100644 --- a/nodejs/docs/test-assertions.mdx +++ b/nodejs/docs/test-assertions.mdx @@ -50,7 +50,7 @@ By default, the timeout for assertions is set to 5 seconds. Learn more about [va | [expect(page).toHaveScreenshot()](/api/class-pageassertions.mdx#page-assertions-to-have-screenshot-1) | Page has a screenshot | | [expect(page).toHaveTitle()](/api/class-pageassertions.mdx#page-assertions-to-have-title) | Page has a title | | [expect(page).toHaveURL()](/api/class-pageassertions.mdx#page-assertions-to-have-url) | Page has a URL | -| [expect(apiResponse).toBeOK()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok) | Response has an OK status | +| [expect(response).toBeOK()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok) | Response has an OK status | ## Negating Matchers diff --git a/nodejs/docs/test-configuration.mdx b/nodejs/docs/test-configuration.mdx index d8ff950fa98..1c7c8a4f363 100644 --- a/nodejs/docs/test-configuration.mdx +++ b/nodejs/docs/test-configuration.mdx @@ -148,7 +148,7 @@ export default defineConfig({ | :- | :- | | [testConfig.expect](/api/class-testconfig.mdx#test-config-expect) | [Web first assertions](./test-assertions.mdx) like `expect(locator).toHaveText()` have a separate timeout of 5 seconds by default. This is the maximum time the `expect()` should wait for the condition to be met. Learn more about [test and expect timeouts](./test-timeouts.mdx) and how to set them for a single test. | | [expect(page).toHaveScreenshot()](/api/class-pageassertions.mdx#page-assertions-to-have-screenshot-1) | Configuration for the `expect(locator).toHaveScreeshot()` method. | -| [expect(snapshot).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1)| Configuration for the `expect(locator).toMatchSnapshot()` method.| +| [expect(value).toMatchSnapshot()](/api/class-snapshotassertions.mdx#snapshot-assertions-to-match-snapshot-1)| Configuration for the `expect(locator).toMatchSnapshot()` method.| ### Add custom matchers using expect.extend diff --git a/python/docs/api/class-apiresponseassertions.mdx b/python/docs/api/class-apiresponseassertions.mdx index 124843a1180..1de60d23492 100644 --- a/python/docs/api/class-apiresponseassertions.mdx +++ b/python/docs/api/class-apiresponseassertions.mdx @@ -52,12 +52,12 @@ async def test_navigates_to_login_page(page: Page) -> None: Added in: v1.19apiResponseAssertions.not_to_be_ok -The opposite of [expect(api_response).to_be_ok()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok). +The opposite of [expect(response).to_be_ok()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok). **Usage** ```python -expect(api_response).not_to_be_ok() +expect(response).not_to_be_ok() ``` --- diff --git a/python/docs/test-assertions.mdx b/python/docs/test-assertions.mdx index 5bc4ea012eb..a55ffd76868 100644 --- a/python/docs/test-assertions.mdx +++ b/python/docs/test-assertions.mdx @@ -30,7 +30,7 @@ import HTMLCard from '@site/src/components/HTMLCard'; | [expect(locator).to_have_values()](/api/class-locatorassertions.mdx#locator-assertions-to-have-values) | Select has options selected | | [expect(page).to_have_title()](/api/class-pageassertions.mdx#page-assertions-to-have-title) | Page has a title | | [expect(page).to_have_url()](/api/class-pageassertions.mdx#page-assertions-to-have-url) | Page has a URL | -| [expect(api_response).to_be_ok()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok) | Response has an OK status | +| [expect(response).to_be_ok()](/api/class-apiresponseassertions.mdx#api-response-assertions-to-be-ok) | Response has an OK status | ## Custom Expect Message diff --git a/src/format_csharp.js b/src/format_csharp.js index bf519386e37..a1396e16e16 100644 --- a/src/format_csharp.js +++ b/src/format_csharp.js @@ -17,7 +17,7 @@ //@ts-check const Documentation = require('./documentation'); -const { toTitleCase } = require('./generator'); +const { toTitleCase, assertionArgument } = require('./generator'); /** @typedef {import('./generator').GeneratorFormatter} GeneratorFormatter */ /** @@ -29,7 +29,7 @@ class CSharpFormatter { } /** - * @param {Documentation.Member} member + * @param {Documentation.Member} member */ formatMember(member) { let args = []; @@ -38,9 +38,7 @@ class CSharpFormatter { if (member.clazz.varName === 'playwrightAssertions') { prefix = ''; } else if (member.clazz.varName.includes('Assertions')) { - const varName = member.clazz.varName.substring(0, member.clazz.varName.length -'Assertions'.length); - // Generate `expect(locator).` instead of `locatorAssertions.` - prefix = `Expect(${toTitleCase(varName)}).`; + prefix = `Expect(${toTitleCase(assertionArgument(member.clazz))}).`; } let name = toTitleCase(member.alias); @@ -109,7 +107,7 @@ class CSharpFormatter { } /** - * @param {Documentation.Type} type + * @param {Documentation.Type} type * @param {string} direction * @param {Documentation.Member} member */ @@ -181,7 +179,7 @@ class CSharpFormatter { } /** - * @param {import('./markdown').MarkdownNode} spec + * @param {import('./markdown').MarkdownNode} spec * @returns boolean */ filterComment(spec) { diff --git a/src/format_java.js b/src/format_java.js index f2f3d978b3c..85d408dc93b 100644 --- a/src/format_java.js +++ b/src/format_java.js @@ -17,7 +17,7 @@ //@ts-check const Documentation = require('./documentation'); -const { toTitleCase, renderJSSignatures } = require('./generator'); +const { toTitleCase, renderJSSignatures, assertionArgument } = require('./generator'); /** @typedef {import('./generator').GeneratorFormatter} GeneratorFormatter */ /** @typedef {import('./generator').FormatMode} FormatMode */ @@ -30,7 +30,7 @@ class JavaFormatter { } /** - * @param {Documentation.Member} member + * @param {Documentation.Member} member */ formatMember(member) { let args = []; @@ -39,10 +39,8 @@ class JavaFormatter { if (member.clazz.varName === 'playwrightAssertions') { prefix = ''; } else if (member.clazz.varName.includes('Assertions')) { - const varName = member.clazz.varName.substring(0, member.clazz.varName.length -'Assertions'.length); - // Generate `expect(locator).` instead of `locatorAssertions.` - prefix = `assertThat(${varName}).`; - } + prefix = `assertThat(${assertionArgument(member.clazz)}).`; + } let name = member.alias; if (member.kind === 'property') @@ -52,7 +50,7 @@ class JavaFormatter { let usages = [`${prefix}${name}`]; let link = `${prefix}${name}`; - + let signatures; if (member.kind === 'method') { args = member.argsArray; @@ -109,7 +107,7 @@ class JavaFormatter { } /** - * @param {Documentation.Type} type + * @param {Documentation.Type} type * @param {string} direction * @param {Documentation.Member} member */ @@ -176,7 +174,7 @@ class JavaFormatter { } /** - * @param {import('./markdown').MarkdownNode} spec + * @param {import('./markdown').MarkdownNode} spec * @returns boolean */ filterComment(spec) { diff --git a/src/format_js.js b/src/format_js.js index 6349f1726df..4e6e44583de 100644 --- a/src/format_js.js +++ b/src/format_js.js @@ -17,7 +17,7 @@ //@ts-check const Documentation = require('./documentation'); -const { renderJSSignatures } = require('./generator'); +const { renderJSSignatures, assertionArgument } = require('./generator'); /** @typedef {import('./generator').GeneratorFormatter} GeneratorFormatter */ /** @typedef {import('./markdown').MarkdownNode} MarkdownNode */ /** @typedef {import('./documentation').Member} Member */ @@ -31,7 +31,7 @@ class JavaScriptFormatter { } /** - * @param {Documentation.Member} member + * @param {Documentation.Member} member */ formatMember(member) { let args = []; @@ -41,10 +41,8 @@ class JavaScriptFormatter { if (member.clazz.varName === 'playwrightAssertions') { prefix = ''; } else if (member.clazz.varName.includes('Assertions')) { - const varName = member.clazz.varName.substring(0, member.clazz.varName.length -'Assertions'.length); - // Generate `expect(locator).` instead of `locatorAssertions.` - prefix = `expect(${varName}).`; - } + prefix = `expect(${assertionArgument(member.clazz)}).`; + } let name = member.alias; let usages = [`${prefix}${name}`]; @@ -106,7 +104,7 @@ class JavaScriptFormatter { } /** - * @param {MarkdownNode} spec + * @param {MarkdownNode} spec * @returns boolean */ filterComment(spec) { diff --git a/src/format_python.js b/src/format_python.js index 5a2310fdead..cad33d9b7dc 100644 --- a/src/format_python.js +++ b/src/format_python.js @@ -18,7 +18,7 @@ const md = require('./markdown'); const Documentation = require('./documentation'); -const { toSnakeCase } = require('./generator'); +const { toSnakeCase, assertionArgument } = require('./generator'); /** @typedef {import('./generator').GeneratorFormatter} GeneratorFormatter */ /** @typedef {import('./markdown').MarkdownNode} MarkdownNode */ @@ -31,7 +31,7 @@ class PythonFormatter { } /** - * @param {Documentation.Member} member + * @param {Documentation.Member} member */ formatMember(member) { const args = []; @@ -40,10 +40,8 @@ class PythonFormatter { if (member.clazz.varName === 'playwrightAssertions') { prefix = ''; } else if (member.clazz.varName.includes('Assertions')) { - const varName = member.clazz.varName.substring(0, member.clazz.varName.length - 'Assertions'.length); - // Generate `expect(locator).` instead of `locatorAssertions.` - prefix = `expect(${toSnakeCase(varName)}).`; - } + prefix = `expect(${toSnakeCase(assertionArgument(member.clazz))}).`; + } let name = toSnakeCase(member.alias); let usages = [`${prefix}${name}`]; @@ -87,7 +85,7 @@ class PythonFormatter { } /** - * @param {Documentation.Type} type + * @param {Documentation.Type} type * @param {string} direction */ renderType(type, direction) { @@ -117,7 +115,7 @@ class PythonFormatter { } /** - * @param {MarkdownNode} spec + * @param {MarkdownNode} spec * @returns boolean */ filterComment(spec) { diff --git a/src/generator.js b/src/generator.js index 5682abdb930..2268dd6abbe 100644 --- a/src/generator.js +++ b/src/generator.js @@ -429,7 +429,7 @@ import HTMLCard from '@site/src/components/HTMLCard';`); } /** - * @param {string} content + * @param {string} content * @returns {string} */ rewriteVersion(content) { @@ -734,4 +734,18 @@ function memberSection(member) { throw new Error(`Unsupported member kind ${member.kind} for ${member.name}`); } -module.exports = { Generator, toTitleCase, toSnakeCase, renderJSSignatures }; +/** + * @param {docs.Class} clazz + */ +function assertionArgument(clazz) { + switch (clazz.name.toLowerCase()) { + case 'locatorassertions': return 'locator'; + case 'pageassertions': return 'page'; + case 'genericassertions': return 'value'; + case 'snapshotassertions': return 'value'; + case 'apiresponseassertions': return 'response'; + } + throw new Error('Unexpected assertion class ' + clazz.name); +} + +module.exports = { Generator, toTitleCase, toSnakeCase, renderJSSignatures, assertionArgument };