mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
refactor: remove unnecessary patter matcher
This commit is contained in:
1 parent
4f3752060d
commit
8280f7073d
2 files changed
+11
-33
No files matched your search
@@ -2,28 +2,22 @@
|
|||||||
import { Command } from './command'
|
import { Command } from './command'
|
||||||
import { Matcher } from './matcher'
|
import { Matcher } from './matcher'
|
||||||
|
|
||||||
export class PatternMatcher {
|
export class CommandMatcher implements Matcher {
|
||||||
private matcher: RegExp
|
private matcher: RegExp
|
||||||
constructor(pattern: string, args?: string) {
|
constructor(pattern: string, args?: string) {
|
||||||
this.matcher = new RegExp(pattern, args)
|
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<Command>): boolean {
|
match(command: Partial<Command>): boolean {
|
||||||
return !!command.command && this.test(command.command)
|
return !!command.command && this.matcher.test(command.command)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ErrorMatcher implements Matcher {
|
export class ErrorMatcher implements Matcher {
|
||||||
private matcher?: PatternMatcher
|
private matcher?: RegExp
|
||||||
constructor(errorPattern?: string) {
|
constructor(errorPattern?: string) {
|
||||||
if (errorPattern) {
|
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<Command>): boolean {
|
match(command: Partial<Command>): boolean {
|
||||||
return command.output ? this.test(command.output.trim()) : false
|
return !!command.output && this.matcher.test(command.output.trim())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { test, expect } from '@jest/globals'
|
import { test, expect } from '@jest/globals'
|
||||||
import {
|
import {
|
||||||
PatternMatcher,
|
|
||||||
CommandMatcher,
|
CommandMatcher,
|
||||||
ErrorMatcher,
|
ErrorMatcher,
|
||||||
PwdMatcher,
|
PwdMatcher,
|
||||||
@@ -9,26 +8,6 @@ import {
|
|||||||
} from '../src/condition'
|
} from '../src/condition'
|
||||||
import { Command } from '../src/command'
|
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', () => {
|
test('command match', () => {
|
||||||
const command: Command = {
|
const command: Command = {
|
||||||
command: 'echo start',
|
command: 'echo start',
|
||||||
|
|||||||
Reference in new issue
Block a user