From 3614f5b6201a1a5bf57a79d7819afb401dbae8ea Mon Sep 17 00:00:00 2001 From: Karel De Smet Date: Mon, 21 Dec 2020 17:35:40 +0100 Subject: [PATCH 1/8] Test for toBeLevel2xx() --- helpers/__tests__/postwomanTesting.spec.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/helpers/__tests__/postwomanTesting.spec.js b/helpers/__tests__/postwomanTesting.spec.js index e0824ec41..1bf16a060 100644 --- a/helpers/__tests__/postwomanTesting.spec.js +++ b/helpers/__tests__/postwomanTesting.spec.js @@ -62,3 +62,25 @@ describe("toHaveProperty", () => { ).toEqual(FAIL) }) }) + +describe("toBeLevel2xx", () => { + test("test for numbers", () => { + expect(getTestResult("pw.expect(200).toBeLevel2xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(200).not.toBeLevel2xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(300).toBeLevel2xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(300).not.toBeLevel2xx()", 0)).toEqual(PASS) + }) + test("test for strings", () => { + expect(getTestResult("pw.expect('200').toBeLevel2xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect('200').not.toBeLevel2xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('300').toBeLevel2xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('300').not.toBeLevel2xx()", 0)).toEqual(PASS) + }) + test("failed to parse to integer", () => { + expect(getTestResult("pw.expect(undefined).toBeLevel2xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(null).toBeLevel2xx()", 0)).toEqual(FAIL) + expect(() => { + runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel2xx()") + }).toThrow() + }) +}) From d92412175c03a5054dfaa4aa18adabc965ca83a1 Mon Sep 17 00:00:00 2001 From: Karel De Smet Date: Tue, 22 Dec 2020 08:01:55 +0100 Subject: [PATCH 2/8] Test for toBeLevel3xx() --- helpers/__tests__/postwomanTesting.spec.js | 22 ++++++++++++++++++++++ helpers/postwomanTesting.js | 8 ++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/helpers/__tests__/postwomanTesting.spec.js b/helpers/__tests__/postwomanTesting.spec.js index 1bf16a060..5340e4aa7 100644 --- a/helpers/__tests__/postwomanTesting.spec.js +++ b/helpers/__tests__/postwomanTesting.spec.js @@ -84,3 +84,25 @@ describe("toBeLevel2xx", () => { }).toThrow() }) }) + +describe("toBeLevel3xx()", () => { + test("test for numbers", () => { + expect(getTestResult("pw.expect(300).toBeLevel3xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(300).not.toBeLevel3xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(400).toBeLevel3xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(400).not.toBeLevel3xx()", 0)).toEqual(PASS) + }) + test("test for strings", () => { + expect(getTestResult("pw.expect('300').toBeLevel3xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect('300').not.toBeLevel3xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('400').toBeLevel3xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('400').not.toBeLevel3xx()", 0)).toEqual(PASS) + }) + test("failed to parse to integer", () => { + expect(getTestResult("pw.expect(undefined).toBeLevel3xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(null).toBeLevel3xx()", 0)).toEqual(FAIL) + expect(() => { + runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel3xx()") + }).toThrow() + }) +}) diff --git a/helpers/postwomanTesting.js b/helpers/postwomanTesting.js index 30d0bb8d4..1de644e94 100644 --- a/helpers/postwomanTesting.js +++ b/helpers/postwomanTesting.js @@ -113,7 +113,7 @@ class Expectation { if (Number.isNaN(code)) { return this._fail(`Expected 200-level status but could not parse value ${this.expectValue}`) } - return this._satisfies(code >= 200 && code < 300) + return this._satisfies(code >= 200 && code < 300, true) ? this._pass() : this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 200-level status`)) } @@ -122,7 +122,7 @@ class Expectation { if (Number.isNaN(code)) { return this._fail(`Expected 300-level status but could not parse value ${this.expectValue}`) } - return this._satisfies(code >= 300 && code < 400) + return this._satisfies(code >= 300 && code < 400, true) ? this._pass() : this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 300-level status`)) } @@ -131,7 +131,7 @@ class Expectation { if (Number.isNaN(code)) { return this._fail(`Expected 400-level status but could not parse value ${this.expectValue}`) } - return this._satisfies(code >= 400 && code < 500) + return this._satisfies(code >= 400 && code < 500, true) ? this._pass() : this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 400-level status`)) } @@ -140,7 +140,7 @@ class Expectation { if (Number.isNaN(code)) { return this._fail(`Expected 500-level status but could not parse value ${this.expectValue}`) } - return this._satisfies(code >= 500 && code < 600) + return this._satisfies(code >= 500 && code < 600, true) ? this._pass() : this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 500-level status`)) } From a61e6efdd468d0839939eb202d559ee631306503 Mon Sep 17 00:00:00 2001 From: Karel De Smet Date: Tue, 22 Dec 2020 08:10:54 +0100 Subject: [PATCH 3/8] Test for toBeLevel4xx() --- helpers/__tests__/postwomanTesting.spec.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/helpers/__tests__/postwomanTesting.spec.js b/helpers/__tests__/postwomanTesting.spec.js index 5340e4aa7..3b6e19551 100644 --- a/helpers/__tests__/postwomanTesting.spec.js +++ b/helpers/__tests__/postwomanTesting.spec.js @@ -106,3 +106,25 @@ describe("toBeLevel3xx()", () => { }).toThrow() }) }) + +describe("toBeLevel4xx()", () => { + test("test for numbers", () => { + expect(getTestResult("pw.expect(400).toBeLevel4xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(400).not.toBeLevel4xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(500).toBeLevel4xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(500).not.toBeLevel4xx()", 0)).toEqual(PASS) + }) + test("test for strings", () => { + expect(getTestResult("pw.expect('400').toBeLevel4xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect('400').not.toBeLevel4xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('500').toBeLevel4xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('500').not.toBeLevel4xx()", 0)).toEqual(PASS) + }) + test("failed to parse to integer", () => { + expect(getTestResult("pw.expect(undefined).toBeLevel4xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(null).toBeLevel4xx()", 0)).toEqual(FAIL) + expect(() => { + runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel4xx()") + }).toThrow() + }) +}) From ea227e09fa6a919ee65d9d48795502f377a2852a Mon Sep 17 00:00:00 2001 From: Karel De Smet Date: Tue, 22 Dec 2020 19:19:21 +0100 Subject: [PATCH 4/8] Explicitly pass radix 10 to parseInt functions in toBeLevelxxx() to avoid unexpected results --- helpers/postwomanTesting.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/helpers/postwomanTesting.js b/helpers/postwomanTesting.js index 1de644e94..3bf8fcf49 100644 --- a/helpers/postwomanTesting.js +++ b/helpers/postwomanTesting.js @@ -109,7 +109,7 @@ class Expectation { ) } toBeLevel2xx() { - const code = parseInt(this.expectValue) + const code = parseInt(this.expectValue, 10) if (Number.isNaN(code)) { return this._fail(`Expected 200-level status but could not parse value ${this.expectValue}`) } @@ -118,7 +118,7 @@ class Expectation { : this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 200-level status`)) } toBeLevel3xx() { - const code = parseInt(this.expectValue) + const code = parseInt(this.expectValue, 10) if (Number.isNaN(code)) { return this._fail(`Expected 300-level status but could not parse value ${this.expectValue}`) } @@ -127,7 +127,7 @@ class Expectation { : this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 300-level status`)) } toBeLevel4xx() { - const code = parseInt(this.expectValue) + const code = parseInt(this.expectValue, 10) if (Number.isNaN(code)) { return this._fail(`Expected 400-level status but could not parse value ${this.expectValue}`) } @@ -136,7 +136,7 @@ class Expectation { : this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 400-level status`)) } toBeLevel5xx() { - const code = parseInt(this.expectValue) + const code = parseInt(this.expectValue, 10) if (Number.isNaN(code)) { return this._fail(`Expected 500-level status but could not parse value ${this.expectValue}`) } From de1d06528e1db5bca337862549ce6ac467f7ff3c Mon Sep 17 00:00:00 2001 From: Karel De Smet Date: Tue, 22 Dec 2020 19:33:27 +0100 Subject: [PATCH 5/8] Test for toBeLevel5xx() --- helpers/__tests__/postwomanTesting.spec.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/helpers/__tests__/postwomanTesting.spec.js b/helpers/__tests__/postwomanTesting.spec.js index 3b6e19551..9746610c0 100644 --- a/helpers/__tests__/postwomanTesting.spec.js +++ b/helpers/__tests__/postwomanTesting.spec.js @@ -128,3 +128,25 @@ describe("toBeLevel4xx()", () => { }).toThrow() }) }) + +describe("toBeLevel5xx()", () => { + test("test for numbers", () => { + expect(getTestResult("pw.expect(500).toBeLevel5xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(500).not.toBeLevel5xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(200).toBeLevel5xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(200).not.toBeLevel5xx()", 0)).toEqual(PASS) + }) + test("test for strings", () => { + expect(getTestResult("pw.expect('500').toBeLevel5xx()", 0)).toEqual(PASS) + expect(getTestResult("pw.expect('500').not.toBeLevel5xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('200').toBeLevel5xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('200').not.toBeLevel5xx()", 0)).toEqual(PASS) + }) + test("failed to parse to integer", () => { + expect(getTestResult("pw.expect(undefined).toBeLevel5xx()", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(null).toBeLevel5xx()", 0)).toEqual(FAIL) + expect(() => { + runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel5xx()") + }).toThrow() + }) +}) From f53ac25d906211d5e793beae63a39e10f0f48e8a Mon Sep 17 00:00:00 2001 From: Karel De Smet Date: Tue, 22 Dec 2020 20:05:56 +0100 Subject: [PATCH 6/8] Test for toHaveLength() --- helpers/__tests__/postwomanTesting.spec.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/helpers/__tests__/postwomanTesting.spec.js b/helpers/__tests__/postwomanTesting.spec.js index 9746610c0..fbd77e7cb 100644 --- a/helpers/__tests__/postwomanTesting.spec.js +++ b/helpers/__tests__/postwomanTesting.spec.js @@ -150,3 +150,19 @@ describe("toBeLevel5xx()", () => { }).toThrow() }) }) + +describe("toHaveLength()", () => { + test("test for strings", () => { + expect(getTestResult("pw.expect('word').toHaveLength(4)", 0)).toEqual(PASS) + expect(getTestResult("pw.expect('word').toHaveLength(5)", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('word').not.toHaveLength(4)", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect('word').not.toHaveLength(5)", 0)).toEqual(PASS) + }) + test("test for arrays", () => { + const fruits = "['apples', 'bananas', 'oranges', 'grapes', 'strawberries', 'cherries']" + expect(getTestResult(`pw.expect(${fruits}).toHaveLength(6)`, 0)).toEqual(PASS) + expect(getTestResult(`pw.expect(${fruits}).toHaveLength(7)`, 0)).toEqual(FAIL) + expect(getTestResult(`pw.expect(${fruits}).not.toHaveLength(6)`, 0)).toEqual(FAIL) + expect(getTestResult(`pw.expect(${fruits}).not.toHaveLength(7)`, 0)).toEqual(PASS) + }) +}) From a64b32d12c4c5087f3e68902d1f720892c8859c0 Mon Sep 17 00:00:00 2001 From: Karel De Smet Date: Tue, 22 Dec 2020 21:12:53 +0100 Subject: [PATCH 7/8] Test for toBeType() --- helpers/__tests__/postwomanTesting.spec.js | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/helpers/__tests__/postwomanTesting.spec.js b/helpers/__tests__/postwomanTesting.spec.js index fbd77e7cb..1afa61622 100644 --- a/helpers/__tests__/postwomanTesting.spec.js +++ b/helpers/__tests__/postwomanTesting.spec.js @@ -166,3 +166,29 @@ describe("toHaveLength()", () => { expect(getTestResult(`pw.expect(${fruits}).not.toHaveLength(7)`, 0)).toEqual(PASS) }) }) + +describe("toBeType()", () => { + test("test for positive assertion", () => { + expect(getTestResult("pw.expect('random').toBeType('string')", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(true).toBeType('boolean')", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(5).toBeType('number')", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(new Date()).toBeType('object')", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(undefined).toBeType('undefined')", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(BigInt(123)).toBeType('bigint')", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(Symbol('test')).toBeType('symbol')", 0)).toEqual(PASS) + expect(getTestResult("pw.expect(function() {}).toBeType('function')", 0)).toEqual(PASS) + }) + test("test for negative assertion", () => { + expect(getTestResult("pw.expect('random').not.toBeType('string')", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(true).not.toBeType('boolean')", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(5).not.toBeType('number')", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(new Date()).not.toBeType('object')", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(undefined).not.toBeType('undefined')", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(BigInt(123)).not.toBeType('bigint')", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(Symbol('test')).not.toBeType('symbol')", 0)).toEqual(FAIL) + expect(getTestResult("pw.expect(function() {}).not.toBeType('function')", 0)).toEqual(FAIL) + }) + test("unexpected type", () => { + expect(getTestResult("pw.expect('random').toBeType('unknown')", 0)).toEqual(FAIL) + }) +}) From 15ecb19c6592221505355d084af9875b5adccc34 Mon Sep 17 00:00:00 2001 From: Karel De Smet Date: Tue, 22 Dec 2020 21:15:20 +0100 Subject: [PATCH 8/8] Added extra test for error handling --- helpers/__tests__/postwomanTesting.spec.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/helpers/__tests__/postwomanTesting.spec.js b/helpers/__tests__/postwomanTesting.spec.js index 1afa61622..83184b39a 100644 --- a/helpers/__tests__/postwomanTesting.spec.js +++ b/helpers/__tests__/postwomanTesting.spec.js @@ -19,6 +19,11 @@ describe("Error handling", () => { test("errors array is empty on a successful test", () => { expect(getErrors("pw.expect(1).toBe(1)")).toStrictEqual([]) }) + test("throws error at a variable which is not declared", () => { + expect(() => { + runTestScriptWithVariables("someVariable") + }).toThrow() + }) }) describe("toBe", () => {