Skip to content

Commit

Permalink
Merge 678d696 into 98cf9d0
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Jan 29, 2019
2 parents 98cf9d0 + 678d696 commit ea94f4d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 48 deletions.
59 changes: 11 additions & 48 deletions src/check/runner/Runner.ts
Expand Up @@ -11,6 +11,7 @@ import { QualifiedParameters } from './configuration/QualifiedParameters';
import { VerbosityLevel } from './configuration/VerbosityLevel';
import { RunDetails } from './reporter/RunDetails';
import { RunExecution } from './reporter/RunExecution';
import { RunnerIterator } from './RunnerIterator';
import { SourceValuesIterator } from './SourceValuesIterator';
import { toss } from './Tosser';
import { pathWalk } from './utils/PathWalker';
Expand All @@ -22,31 +23,12 @@ function runIt<Ts>(
sourceValues: SourceValuesIterator<Shrinkable<Ts>>,
verbose: VerbosityLevel
): RunExecution<Ts> {
const runExecution = new RunExecution<Ts>(verbose);
let done = false;
let values: IterableIterator<Shrinkable<Ts>> = sourceValues;
while (!done) {
done = true;
let idx = 0;
for (const v of values) {
const out = property.run(v.value_) as PreconditionFailure | string | null;
if (out != null && typeof out === 'string') {
runExecution.fail(v.value_, idx, out);
values = v.shrink();
done = false;
break;
}
if (out != null) {
// skipped the run
runExecution.skip(v.value_);
sourceValues.skippedOne();
} else {
runExecution.success(v.value_);
}
++idx;
}
const runner = new RunnerIterator(sourceValues, verbose);
for (const v of runner) {
const out = property.run(v) as PreconditionFailure | string | null;
runner.handleResult(out);
}
return runExecution;
return runner.runExecution;
}

/** @hidden */
Expand All @@ -55,31 +37,12 @@ async function asyncRunIt<Ts>(
sourceValues: SourceValuesIterator<Shrinkable<Ts>>,
verbose: VerbosityLevel
): Promise<RunExecution<Ts>> {
const runExecution = new RunExecution<Ts>(verbose);
let done = false;
let values: IterableIterator<Shrinkable<Ts>> = sourceValues;
while (!done) {
done = true;
let idx = 0;
for (const v of values) {
const out = await property.run(v.value_);
if (out != null && typeof out === 'string') {
runExecution.fail(v.value_, idx, out);
values = v.shrink();
done = false;
break;
}
if (out != null) {
// skipped the run
runExecution.skip(v.value_);
sourceValues.skippedOne();
} else {
runExecution.success(v.value_);
}
++idx;
}
const runner = new RunnerIterator(sourceValues, verbose);
for (const v of runner) {
const out = await property.run(v);
runner.handleResult(out);
}
return runExecution;
return runner.runExecution;
}

/** @hidden */
Expand Down
53 changes: 53 additions & 0 deletions src/check/runner/RunnerIterator.ts
@@ -0,0 +1,53 @@
import { Shrinkable } from '../arbitrary/definition/Shrinkable';
import { PreconditionFailure } from '../precondition/PreconditionFailure';
import { VerbosityLevel } from './configuration/VerbosityLevel';
import { RunExecution } from './reporter/RunExecution';
import { SourceValuesIterator } from './SourceValuesIterator';

/**
* @hidden
* Responsible for the iteration logic
*
* Workflow:
* 1- Call to `next` gives back the value to test
* 2- Call to `handleResult` takes into account the execution status
* 3- Back to 1
*/
export class RunnerIterator<Ts> implements IterableIterator<Ts> {
runExecution: RunExecution<Ts>;
private currentIdx: number;
private currentShrinkable: Shrinkable<Ts>;
private nextValues: IterableIterator<Shrinkable<Ts>>;
constructor(readonly sourceValues: SourceValuesIterator<Shrinkable<Ts>>, verbose: VerbosityLevel) {
this.runExecution = new RunExecution<Ts>(verbose);
this.currentIdx = -1;
this.nextValues = sourceValues;
}
[Symbol.iterator](): IterableIterator<Ts> {
return this;
}
next(value?: any): IteratorResult<Ts> {
const nextValue = this.nextValues.next();
if (nextValue.done) {
return { done: true, value };
}
this.currentShrinkable = nextValue.value;
++this.currentIdx;
return { done: false, value: nextValue.value.value_ };
}
handleResult(result: PreconditionFailure | string | null) {
if (result != null && typeof result === 'string') {
// failed run
this.runExecution.fail(this.currentShrinkable.value_, this.currentIdx, result);
this.currentIdx = -1;
this.nextValues = this.currentShrinkable.shrink();
} else if (result != null) {
// skipped run
this.runExecution.skip(this.currentShrinkable.value_);
this.sourceValues.skippedOne();
} else {
// successful run
this.runExecution.success(this.currentShrinkable.value_);
}
}
}

0 comments on commit ea94f4d

Please sign in to comment.