From 8280f7073d69e5dc4bde28c76964c1d6b35d2bc9 Mon Sep 17 00:00:00 2001 From: wancat Date: Wed, 6 Mar 2024 23:57:35 +0800 Subject: [PATCH] refactor: remove unnecessary patter matcher --- packages/game/src/condition.ts | 23 +++++++++++------------ packages/game/test/condition.test.ts | 21 --------------------- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/packages/game/src/condition.ts b/packages/game/src/condition.ts index 7d9c9bf..9b4857d 100644 --- a/packages/game/src/condition.ts +++ b/packages/game/src/condition.ts @@ -2,28 +2,22 @@ import { Command } from './command' import { Matcher } from './matcher' -export class PatternMatcher { +export class CommandMatcher implements Matcher { private matcher: RegExp constructor(pattern: string, args?: string) { this.matcher = new RegExp(pattern, args) } - test(input: string): boolean { - return this.matcher.test(input) - } -} - -export class CommandMatcher extends PatternMatcher implements Matcher { match(command: Partial): boolean { - return !!command.command && this.test(command.command) + return !!command.command && this.matcher.test(command.command) } } export class ErrorMatcher implements Matcher { - private matcher?: PatternMatcher + private matcher?: RegExp constructor(errorPattern?: string) { if (errorPattern) { - this.matcher = new PatternMatcher(errorPattern) + this.matcher = new RegExp(errorPattern) } } @@ -43,9 +37,14 @@ export class PwdMatcher implements Matcher { } } -export class OutputMatcher extends PatternMatcher implements Matcher { +export class OutputMatcher implements Matcher { + private matcher: RegExp + constructor(pattern: string, args?: string) { + this.matcher = new RegExp(pattern, args) + } + match(command: Partial): boolean { - return command.output ? this.test(command.output.trim()) : false + return !!command.output && this.matcher.test(command.output.trim()) } } diff --git a/packages/game/test/condition.test.ts b/packages/game/test/condition.test.ts index 69368eb..53c8922 100644 --- a/packages/game/test/condition.test.ts +++ b/packages/game/test/condition.test.ts @@ -1,6 +1,5 @@ import { test, expect } from '@jest/globals' import { - PatternMatcher, CommandMatcher, ErrorMatcher, PwdMatcher, @@ -9,26 +8,6 @@ import { } from '../src/condition' import { Command } from '../src/command' -test('pattern match', () => { - const matcher = new PatternMatcher('^echo start$') - - expect(matcher.test('echo start')).toBe(true) - expect(matcher.test('echo start ')).toBe(false) -}) - -test('pattern match with flag', () => { - const matcher = new PatternMatcher('^echo start$', 'i') - - expect(matcher.test('echo start')).toBe(true) - expect(matcher.test('echo Start')).toBe(true) - expect(matcher.test('echo Start ')).toBe(false) -}) - -test('empty pattern return true', () => { - const matcher = new PatternMatcher('') - expect(matcher.test('echo start')).toBe(true) -}) - test('command match', () => { const command: Command = { command: 'echo start',