From 4f3752060da216cfa6117e273862abf2131cf81b Mon Sep 17 00:00:00 2001 From: wancat Date: Wed, 6 Mar 2024 23:54:03 +0800 Subject: [PATCH] feat: condition matcher --- .eslintrc.js | 2 ++ packages/game/src/condition.ts | 25 ++++++++++++------- packages/game/test/condition.test.ts | 36 ++++++++++++++-------------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index c8110a1..d53065a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -64,6 +64,8 @@ module.exports = { '@typescript-eslint/no-unused-vars': 'error', 'no-useless-constructor': 'off', '@typescript-eslint/no-useless-constructor': 'error', + 'no-empty-function': 'off', + '@typescript-eslint/no-empty-function': 'error', }, overrides: [ { diff --git a/packages/game/src/condition.ts b/packages/game/src/condition.ts index c5febc9..7d9c9bf 100644 --- a/packages/game/src/condition.ts +++ b/packages/game/src/condition.ts @@ -3,7 +3,7 @@ import { Command } from './command' import { Matcher } from './matcher' export class PatternMatcher { - protected matcher: RegExp + private matcher: RegExp constructor(pattern: string, args?: string) { this.matcher = new RegExp(pattern, args) } @@ -19,16 +19,19 @@ export class CommandMatcher extends PatternMatcher implements Matcher { } } -export class ErrorMatcher extends PatternMatcher implements Matcher { - constructor(private errorPattern?: string) { - super(errorPattern || '') +export class ErrorMatcher implements Matcher { + private matcher?: PatternMatcher + constructor(errorPattern?: string) { + if (errorPattern) { + this.matcher = new PatternMatcher(errorPattern) + } } match(command: Partial): boolean { - if (!this.errorPattern) { - return command.error === '' + if (!this.matcher) { + return !command.error } - return !!command.error && this.test(command.error) + return this.matcher.test(command.error || '') } } @@ -58,8 +61,14 @@ export class Condition { constructor({ command, output, error, pwd }: ICondition) { if (command) this.matchers.push(new CommandMatcher(command)) - // if (output) this.matchers.push(new OutputMatcher(output)) + if (output) this.matchers.push(new OutputMatcher(output)) if (pwd) this.matchers.push(new PwdMatcher(pwd)) this.matchers.push(new ErrorMatcher(error)) // always check unexpected error } + + match(command: Partial): boolean { + return this.matchers.every((matcher) => { + return matcher.match(command) + }) + } } diff --git a/packages/game/test/condition.test.ts b/packages/game/test/condition.test.ts index 358f388..69368eb 100644 --- a/packages/game/test/condition.test.ts +++ b/packages/game/test/condition.test.ts @@ -71,22 +71,22 @@ test('output matcher', () => { expect(matcher.match({ output: 'start\n' })).toBe(true) }) -// test('condition match', () => { -// const condition = new Condition({ -// command: '^echo start$', -// pwd: '/home/user', -// }) +test('condition match', () => { + const condition = new Condition({ + command: '^echo start$', + }) -// expect( -// condition.match({ -// command: 'echo start', -// pwd: '/home/user', -// }) -// ).toBe(true) -// expect( -// condition.match({ -// command: 'echo start', -// pwd: '/home/user1', -// }) -// ).toBe(false) -// }) + expect( + condition.match({ + command: 'echo start', + }) + ).toBe(true) + expect( + condition.match({ + command: 'echo hello', + }) + ).toBe(false) + expect( + condition.match({ command: 'echo start', error: 'permission denied' }) + ).toBe(false) +})