From 58aaf5a52649621e56d6a4dc9234971042229c82 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Tue, 28 May 2024 18:24:12 +0530 Subject: [PATCH 1/5] feat: adding preliminary recursive testing code --- src/mergeData.ts | 2 +- src/runTests.ts | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/mergeData.ts b/src/mergeData.ts index 6b0f120..c7c11a9 100644 --- a/src/mergeData.ts +++ b/src/mergeData.ts @@ -138,7 +138,7 @@ function getMergedSetVars( * $. and $h. prefixes, as this is more convenient when specifying them. * We merge these into tests.json and tests.headers respectively. */ -function mergePrefixBasedTests(tests: RawTests) { +export function mergePrefixBasedTests(tests: RawTests) { if (!tests.json) tests.json = {}; if (!tests.headers) tests.headers = {}; for (const key of Object.keys(tests)) { diff --git a/src/runTests.ts b/src/runTests.ts index ee47898..e3db0f7 100644 --- a/src/runTests.ts +++ b/src/runTests.ts @@ -1,13 +1,14 @@ import jp from "jsonpath"; -import { getStringIfNotScalar } from "./utils/typeUtils"; +import { getStringIfNotScalar, isDict } from "./utils/typeUtils"; import { Tests, ResponseData, TestResult, Assertion } from "./models"; +import { getMergedTests, mergePrefixBasedTests } from "./mergeData"; export function runAllTests( tests: Tests, responseData: ResponseData, - stopOnFailure: boolean, + stopOnFailure: boolean ): TestResult[] { const results: TestResult[] = []; if (!tests) return results; @@ -47,8 +48,8 @@ export function runAllTests( function runTest(spec: string, expected: Assertion, received: any): TestResult[] { let results: TestResult[] = []; + // typeof null is also 'object' if (typeof expected !== "object" || expected == null) { - // typeof null is 'object', so we include it here expected = getStringIfNotScalar(expected); received = getStringIfNotScalar(received); const pass = expected === received; @@ -73,7 +74,7 @@ function getValueForJSONTests(responseContent: object, key: string): any { function runObjectTests( opVals: { [key: string]: any }, receivedObject: any, - spec: string, + spec: string ): TestResult[] { let results: TestResult[] = []; @@ -134,8 +135,18 @@ function runObjectTests( pass = typeof received === "string" && received.endsWith(expected); } else if (op === "$co") { pass = typeof received === "string" && received.includes(expected); - } else if (op == "$options") { + } else if (op === "$options") { continue; // do nothing. $regex will address it. + } else if (op === "$test") { + if (!isDict(expected)) { + pass = false; + message = "recursive tests must be dicts"; + } else { + mergePrefixBasedTests(expected); + const res = runAllTests(expected, received, false); + results.push(...res); + continue; + } } else { results.push({ pass: false, From 87e268ebf8f64bc8e95c791f973de0cb69663d92 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Tue, 28 May 2024 18:46:24 +0530 Subject: [PATCH 2/5] bug: ensuring passage of original objects instead of string variants in $test recursive call --- src/runTests.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/runTests.ts b/src/runTests.ts index e3db0f7..371ea2d 100644 --- a/src/runTests.ts +++ b/src/runTests.ts @@ -3,7 +3,7 @@ import jp from "jsonpath"; import { getStringIfNotScalar, isDict } from "./utils/typeUtils"; import { Tests, ResponseData, TestResult, Assertion } from "./models"; -import { getMergedTests, mergePrefixBasedTests } from "./mergeData"; +import { mergePrefixBasedTests } from "./mergeData"; export function runAllTests( tests: Tests, @@ -81,6 +81,7 @@ function runObjectTests( for (const op in opVals) { let expected = getStringIfNotScalar(opVals[op]); let received = getStringIfNotScalar(receivedObject); + let pass = false; let message = ""; if (op === "$eq") { @@ -138,12 +139,14 @@ function runObjectTests( } else if (op === "$options") { continue; // do nothing. $regex will address it. } else if (op === "$test") { - if (!isDict(expected)) { + const originalExpected = opVals[op], + originalReceived = receivedObject; + if (!isDict(originalExpected)) { pass = false; message = "recursive tests must be dicts"; } else { - mergePrefixBasedTests(expected); - const res = runAllTests(expected, received, false); + mergePrefixBasedTests(originalExpected); + const res = runAllTests(originalExpected, originalReceived, false); results.push(...res); continue; } From f07856c0c2403748006135aa3c3804ca83f9cd4b Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Tue, 28 May 2024 18:47:01 +0530 Subject: [PATCH 3/5] refactor: reformatted w prettier --- src/runTests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runTests.ts b/src/runTests.ts index 371ea2d..2f5ca1f 100644 --- a/src/runTests.ts +++ b/src/runTests.ts @@ -8,7 +8,7 @@ import { mergePrefixBasedTests } from "./mergeData"; export function runAllTests( tests: Tests, responseData: ResponseData, - stopOnFailure: boolean + stopOnFailure: boolean, ): TestResult[] { const results: TestResult[] = []; if (!tests) return results; @@ -74,7 +74,7 @@ function getValueForJSONTests(responseContent: object, key: string): any { function runObjectTests( opVals: { [key: string]: any }, receivedObject: any, - spec: string + spec: string, ): TestResult[] { let results: TestResult[] = []; From ebb322fbe72a9b9057e0fd1b759336e006affb25 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Tue, 28 May 2024 18:48:32 +0530 Subject: [PATCH 4/5] bug: replaced $test op with $tests --- src/runTests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runTests.ts b/src/runTests.ts index 2f5ca1f..8600f53 100644 --- a/src/runTests.ts +++ b/src/runTests.ts @@ -138,7 +138,7 @@ function runObjectTests( pass = typeof received === "string" && received.includes(expected); } else if (op === "$options") { continue; // do nothing. $regex will address it. - } else if (op === "$test") { + } else if (op === "$tests") { const originalExpected = opVals[op], originalReceived = receivedObject; if (!isDict(originalExpected)) { From e58a9b83f4f21622c5243a27e3f99a07df90ec97 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Tue, 28 May 2024 19:11:55 +0530 Subject: [PATCH 5/5] bug: passing recursively evaluated object into json key of ResponseData in recursive call --- src/runTests.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/runTests.ts b/src/runTests.ts index 8600f53..15adf5e 100644 --- a/src/runTests.ts +++ b/src/runTests.ts @@ -139,14 +139,22 @@ function runObjectTests( } else if (op === "$options") { continue; // do nothing. $regex will address it. } else if (op === "$tests") { - const originalExpected = opVals[op], - originalReceived = receivedObject; - if (!isDict(originalExpected)) { + const recursiveTests = opVals[op]; + + if (!isDict(recursiveTests)) { pass = false; message = "recursive tests must be dicts"; } else { - mergePrefixBasedTests(originalExpected); - const res = runAllTests(originalExpected, originalReceived, false); + mergePrefixBasedTests(recursiveTests); + const receivedObj: ResponseData = { + executionTime: 0, + body: "", + rawHeaders: "", + headers: {}, + json: receivedObject, + }; + + const res = runAllTests(recursiveTests, receivedObj, false); results.push(...res); continue; }