Skip to content

Commit

Permalink
Merge pull request #58 from byteHulk/parameterizedTest
Browse files Browse the repository at this point in the history
feat: add ParameterizedTest decorator
  • Loading branch information
i5ting authored Nov 10, 2022
2 parents 580b67d + af1af24 commit ea39340
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ $ npm test
<em>inherited</em> unless they are <em>overridden</em>.
</p>
</td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
Expand Down
2 changes: 1 addition & 1 deletion common/profiles/tsconfig-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strict": true,
"strict": false, // 等类型解决完再设置为 true
"useDefineForClassFields": true,
"useUnknownInCatchVariables": false,
"esModuleInterop": true,
Expand Down
5 changes: 5 additions & 0 deletions examples/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export default class MyFirstJUnitJupiterTests {
assert.is(Math.sqrt(4), 2);
}

@ParameterizedTest([4, 4, 5])
sqrt(num: number) {
assert.is(Math.sqrt(num), 2);
}

@Test
addition() {
assert.is(Math.sqrt(4), 2);
Expand Down
26 changes: 26 additions & 0 deletions packages/decorator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import { debug } from "@ts-junit/utils";

let cache: any = {};
Expand Down Expand Up @@ -214,3 +215,28 @@ export function Disabled(message: string): ClassDecorator & PropertyDecorator {
}
};
}

/**
* Parameterized tests make it possible to run a test multiple times with different arguments.
*
* @alpha
*/

export function ParameterizedTest(params: Array<any>): MethodDecorator {
return function (
target: object | any,
propertyName: string | symbol,
descriptor: TypedPropertyDescriptor<any>,
) {
debug(target[propertyName] + descriptor);

const className = target.constructor.name;

if (!cache[className]) cache[className] = {};
if (!cache[className][propertyName]) cache[className][propertyName] = {};

cache[className][propertyName]["desc"] = "no display name";
cache[className][propertyName]["fn"] = target[propertyName];
cache[className][propertyName]["params"] = params;
};
}
64 changes: 35 additions & 29 deletions packages/strategy/src/uvu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,45 @@ export class UvuStrategy implements Strategy {
): void {
if (Clazz["skipClass"]) {
console.warn(`Class skip ${clz_name} reason: ${Clazz.skipClassReason}`);
} else {
for (const j in Clazz) {
if (j === "hook") {
for (const z in Clazz["hook"]) {
debug(z + " hook=" + Clazz["hook"][z]);

if (z.split(".").length == 2) {
const a = z.split(".");

debug("a1= " + a);

this.test[a[0]][a[1]](Clazz["hook"][z].bind(obj));
} else {
debug(z + " a2= " + Clazz["hook"][z]);
// console.dir("z")
// console.dir(Clazz['hook'][z])
// console.dir(z)
this.test[z](Clazz["hook"][z].bind(obj));
}
}
} else {
if (!Clazz[j]["skip"]) {
debug(`define testcase ${j}`);
debug(" test.handler = " + obj[j]);
return;
}

for (const j in Clazz) {
if (j === "hook") {
for (const z in Clazz["hook"]) {
debug(z + " hook=" + Clazz["hook"][z]);

if (z.split(".").length == 2) {
const a = z.split(".");

debug("a1= " + a);

this.test(j, obj[j].bind(obj));
this.test[a[0]][a[1]](Clazz["hook"][z].bind(obj));
} else {
debug(`skipReason ${j}`);
console.log(
`method skip ${Clazz[i]}#${j}() reason: ${Clazz[j]["skipReason"]}`,
);
debug(z + " a2= " + Clazz["hook"][z]);

this.test[z](Clazz["hook"][z].bind(obj));
}
}
} else if (Clazz[j]["skip"]) {
debug(`skipReason ${j}`);
console.log(
`method skip ${Clazz[i]}#${j}() reason: ${Clazz[j]["skipReason"]}`,
);
} else if (Clazz[j]["params"]) {
debug(`define params testcase ${j}`);
debug("test.handler = " + obj[j]);

const params = Clazz[j]["params"];

params.map((param) => {
this.test(j, obj[j].bind(obj, param));
});
} else {
debug(`define testcase ${j}`);
debug("test.handler = " + obj[j]);

this.test(j, obj[j].bind(obj));
}
}
}
Expand Down

0 comments on commit ea39340

Please sign in to comment.