Last active
December 30, 2022 08:59
-
-
Save atian25/5286b911907a9a382d352ae8104a5bd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://tsplay.dev/WG4bKw | |
class TestRunner { | |
plugin(plugins) { | |
for (const key of Object.keys(plugins)) { | |
const initFn = plugins[key]; | |
this[key] = (...args) => { | |
initFn(this, ...args); | |
return this; | |
}; | |
} | |
return this; | |
} | |
end() { | |
console.log('done'); | |
} | |
} | |
// test case | |
const test_plugins = { | |
stdout(runner: TestRunner, expected: string) { | |
console.log('stdout', expected); | |
}, | |
code(runner: TestRunner, expected: number) { | |
console.log('code', expected); | |
}, | |
}; | |
new TestRunner() | |
.plugin({ ...test_plugins }) | |
.stdout('a test') | |
// should invalid, expected is not string | |
.stdout(/aaaa/) | |
.code(0) | |
.end(); | |
// invalid case | |
const invalidPlugin = { | |
// invalid plugin, the first params should be runner: TestRunner | |
xx: (str: string) => { | |
console.log('### xx', typeof str); | |
}, | |
}; | |
new TestRunner() | |
.plugin({ ...test_plugins, ...invalidPlugin }) | |
.stdout('a test') | |
.xx('a test') | |
.code(0) | |
.end(); |
Author
atian25
commented
Dec 30, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment