Skip to content

Commit

Permalink
feat: add check park on Judge
Browse files Browse the repository at this point in the history
Signed-off-by: himself65 <himself6565@gmail.com>
  • Loading branch information
himself65 committed Apr 13, 2019
1 parent 4fa1bc7 commit 3319fa8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
51 changes: 46 additions & 5 deletions packages/rrreol-core/src/judge.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export class Judge extends EventEmitter {
const {
input = [],
output = [],
file = isString(props) ? props : null
file = isString(props) ? props : null,
ignoreEndEnter = true
} = props

this.__outputs = output.map((val) => {
Expand All @@ -27,13 +28,19 @@ export class Judge extends EventEmitter {
return new FileManager(val)
})
this.__answer = []
this.__config = Object.assign({
ignoreEndEnter: true // if ignore the `\n` at the line end
}, {
ignoreEndEnter
})

this.__path = file || null
this.__target = 0
this.__target = 1

this.on('start', this.exec)
this.on('finished', () => {
// todo
console.log(chalk.greenBright('finished'))
})
}

Expand Down Expand Up @@ -83,6 +90,7 @@ export class Judge extends EventEmitter {
if (isNil(this.__outputs[this.__target])) {
console.log(chalk.red('error') + 'target file is empty')
} else if (isString(val)) {
// todo
findIndex(propEq('name', val))(this.__outputs)
}
this.__target = val
Expand All @@ -105,16 +113,49 @@ export class Judge extends EventEmitter {
const path = await Compiler.compile(this.__path, outputPath)
console.log(`${chalk.yellowBright('Compiled')} file`)
const runner = new Runner(path)

let index = 0
for await (const input of this.__inputs) {
const res = await runner.execUnsafe(input)
const fm = FileManager.of().loadContent(res)
this.__answer.push(fm)
this.emit('finished', fm)
const fileManager = FileManager.of().loadContent(res)
this.__answer.push(fileManager)
// check output
const success = this.compareFileManager(fileManager, this.out(index + 1))
if (!success) {
console.log(`${chalk.red('error on file')} ${index + 1}`)
}

++index
}
return this
}

compareFileManager = (realFM, expectedFM) => {
if (!(realFM instanceof FileManager) ||
!(expectedFM instanceof FileManager)) {
throw new TypeError()
}
let success = true
Object.keys(Array(realFM.lines())).forEach((_, line) => {
const realLine = realFM.line(++line)
const expectedLine = expectedFM.line(line)
const same = equals(realLine, expectedLine)
if (!same) {
success = false
// todo: support log column
console.log(`${chalk.red('wrong answer: On line')} ${line}, read ${realLine} expected ${expectedLine}`)
}
})
return success
}

run = () => this.emit('start')

line = (line) => {
return JudgeWrapper.of.call(this, curry((val) => {
this.targetFile.__content[line + 1] = val
}))
}
}

export default Judge
12 changes: 12 additions & 0 deletions packages/rrreol-core/test/unit/judge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe('Judge base test', () => {
judge = new Judge()
})

it('should have correct props', () => {
expect(judge.__config.ignoreEndEnter).toBe(true)
})

it('should throw error when invoke get method', () => {
expect(() => {
judge.targetFile = 1
Expand Down Expand Up @@ -64,6 +68,14 @@ describe('Judge check test', () => {
expect(judge.answer(1).content).toEqual('1 2')
})

it('should emit check running success', () => {
const fm1 = FileManager.of().loadContent('1\n2\n')
const fm2 = FileManager.of().loadContent('1\n2\n')
judge.rawListeners('check')
.map(f => f(fm1, fm2))
.every(v => expect(v).toBeTruthy())
})

afterAll(() => {
removeFiles(fixturesPath, /\.test\.out$/)
})
Expand Down

0 comments on commit 3319fa8

Please sign in to comment.