Skip to content

Commit

Permalink
Merge pull request #14 from agrostar/#11_partial-matching-output
Browse files Browse the repository at this point in the history
changing output format for recursive test results
  • Loading branch information
vasan-agrostar committed Jun 3, 2024
2 parents 17e8b1a + f447b4b commit 3972d80
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 29 deletions.
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"zzapi": "^1.2.1"
"yaml": "^2.4.3",
"zzapi": "^1.4.0"
}
}
86 changes: 65 additions & 21 deletions src/getResponse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { window, ProgressLocation } from "vscode";

import { ResponseData, RequestSpec, GotRequest, TestResult } from "zzapi";
import { ResponseData, RequestSpec, GotRequest, TestResult, SpecResult } from "zzapi";
import { executeGotRequest } from "zzapi";
import { runAllTests } from "zzapi";
import { captureVariables } from "zzapi";
Expand All @@ -10,21 +10,76 @@ import { displayUndefs, getOutputChannel } from "./utils/outputChannel";
import { getVarStore } from "./variables";
import { getGotRequest } from "./reformatRequest";

function formatTestResults(results: TestResult[]): string {
function formatTestResults(results: TestResult[], spec: string): string[] {
const resultLines: string[] = [];
for (const r of results) {
let line: string;
if (r.pass) {
line = `\t[INFO] test ${r.spec}: expected ${r.op}: ${r.expected} OK`;
line = `\t[INFO] test ${spec}: expected ${r.op}: ${r.expected} OK`;
} else {
line = `\t[FAIL] test ${r.spec}: expected ${r.op}: ${r.expected} | got ${r.received}`;
}
if (r.message) {
line = `${line} [${r.message}]`;
line = `\t[FAIL] test ${spec}: expected ${r.op}: ${r.expected} | got ${r.received}`;
}
if (r.message) line += `[${r.message}]`;

resultLines.push(line);
}
return resultLines.join("\n");
return resultLines;
}

function getFormattedResult(
specRes: SpecResult,
method: string,
name: string,
status: number | undefined,
size: number,
execTime: string | number,
): string {
function getResultData(res: SpecResult): [number, number] {
const rootResults = res.results;
// console.log(typeof rootResults);
let passed = rootResults.filter((r) => r.pass).length,
all = rootResults.length;

for (const s of res.subResults) {
const [subPassed, subAll] = getResultData(s);
passed += subPassed;
all += subAll;
}

return [passed, all];
}

const [passed, all] = getResultData(specRes);

let message = `${new Date().toLocaleString()} `;
message += all === passed ? "[INFO] " : "[ERROR] ";

const testString = all == 0 ? "" : `tests: ${passed}/${all} passed`;
message += `${method} ${name} status: ${status} size: ${size} B time: ${execTime} ${testString}`;

function getResult(res: SpecResult, preSpec?: string): string {
if (passed === all) return "";

const getFullSpec = (): string => {
if (!res.spec) return "";
return (preSpec ? preSpec + " / " : "") + res.spec;
};

const spec = getFullSpec();
const resultLines = formatTestResults(res.results, spec);
const subResultLines = [];
for (const s of res.subResults) {
const subRes = getResult(s, spec);
if (subRes) subResultLines.push(subRes);
}

return [...resultLines, ...subResultLines].join("\n");
}

const specResult = getResult(specRes);
if (specResult) message += "\n" + specResult;

return message;
}

export async function allRequestsWithProgress(
Expand Down Expand Up @@ -134,19 +189,8 @@ export async function allRequestsWithProgress(
}

const results = runAllTests(requestData.tests, response, requestData.options.stopOnFailure);
const passed = results.filter((r) => r.pass).length;
const all = results.length;

if (all == passed) {
out.append(`${new Date().toLocaleString()} [INFO] `);
} else {
out.append(`${new Date().toLocaleString()} [ERROR] `);
}
const testString = all == 0 ? "" : `tests: ${passed}/${all} passed`;
out.appendLine(`${method} ${name} status: ${status} size: ${size} B time: ${et} ${testString}`);
if (all != passed) {
out.appendLine(formatTestResults(results));
}
const message = getFormattedResult(results, method, name, status, size, et);
out.appendLine(message);

const captureOutput = captureVariables(requestData, response);
const capturedVariables = captureOutput.capturedVars;
Expand Down

0 comments on commit 3972d80

Please sign in to comment.