Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean complex cloning handling (moved to Shrinkable) #259

Merged
merged 1 commit into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/check/arbitrary/ArrayArbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ArrayArbitrary<T> extends Arbitrary<T[]> {
for (let idx = 0; idx !== items.length; ++idx) {
const s = items[idx];
cloneable = cloneable || s.hasToBeCloned;
vs.push(shrunkOnce ? s.value : s.value_);
vs.push(s.value);
}
if (cloneable) {
ArrayArbitrary.makeItCloneable(vs, items);
Expand Down
6 changes: 3 additions & 3 deletions src/check/arbitrary/TupleArbitrary.generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ class GenericTupleArbitrary<Ts> extends Arbitrary<Ts[]> {
};
return vs;
}
private static wrapper<Ts>(shrinkables: Shrinkable<Ts>[], postGenerate?: boolean): Shrinkable<Ts[]> {
private static wrapper<Ts>(shrinkables: Shrinkable<Ts>[]): Shrinkable<Ts[]> {
let cloneable = false;
const vs = [];
for (let idx = 0; idx !== shrinkables.length; ++idx) {
const s = shrinkables[idx];
cloneable = cloneable || s.hasToBeCloned;
vs.push(postGenerate === true ? s.value_ : s.value);
vs.push(s.value);
}
if (cloneable) {
GenericTupleArbitrary.makeItCloneable(vs, shrinkables);
}
return new Shrinkable(vs, () => GenericTupleArbitrary.shrinkImpl(shrinkables).map(GenericTupleArbitrary.wrapper));
}
generate(mrng: Random): Shrinkable<Ts[]> {
return GenericTupleArbitrary.wrapper(this.arbs.map(a => a.generate(mrng)), true);
return GenericTupleArbitrary.wrapper(this.arbs.map(a => a.generate(mrng)));
}
private static shrinkImpl<Ts>(value: Shrinkable<Ts>[]): Stream<Shrinkable<Ts>[]> {
// shrinking one by one is the not the most comprehensive
Expand Down
11 changes: 11 additions & 0 deletions src/check/arbitrary/definition/Shrinkable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export class Shrinkable<T> {
* If <true> the value will be cloned each time it gets accessed
*/
readonly hasToBeCloned: boolean;
/**
* Flag indicating whether or not the this.value has already been called once
* If so, the underlying will be cloned
* Only set when hasToBeCloned = true
*/
private readOnce: boolean;
/**
* Safe value of the shrinkable
* Depending on {@link hasToBeCloned} it will either be {@link value_} or a clone of it
Expand All @@ -24,12 +30,17 @@ export class Shrinkable<T> {
// tslint:disable-next-line:variable-name
constructor(readonly value_: T, readonly shrink: () => Stream<Shrinkable<T>> = () => Stream.nil<Shrinkable<T>>()) {
this.hasToBeCloned = hasCloneMethod(value_);
this.readOnce = false;
Object.defineProperty(this, 'value', { get: this.getValue });
}

/** @hidden */
private getValue() {
if (this.hasToBeCloned) {
if (!this.readOnce) {
this.readOnce = true;
return this.value_;
}
return ((this.value_ as unknown) as WithCloneMethod<T>)[cloneMethod]();
}
return this.value_;
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/StateFullArbitraries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,25 @@ describe(`StateFullArbitraries (seed: ${seed})`, () => {
assert.equal(dict[k].size(), 1);
}
});
it('fc.infiniteStream', () => {
let alwaysWithElements = true;
let nonClonedDetected = false;
const status = fc.check(
fc.property(fc.integer(), fc.infiniteStream(fc.context()), fc.integer(), (a, s, b) => {
let accessedCtx = 0;
for (const ctx of s.take(3)) {
++accessedCtx;
nonClonedDetected = nonClonedDetected || ctx.size() !== 0;
ctx.log('logging stuff'); // not really useful as streams are supposed to be cleaned
}
alwaysWithElements = alwaysWithElements && accessedCtx === 3;
return a < b;
}),
{ seed }
);
assert.ok(status.failed);
assert.ok(!nonClonedDetected);
assert.ok(alwaysWithElements);
});
});
});
6 changes: 4 additions & 2 deletions test/unit/check/arbitrary/definition/Shrinkable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Shrinkable', () => {
new Shrinkable(cloneable);
assert.equal(numCalls, 0);
});
it('Should call [cloneMethod] on value accessor', () => {
it('Should call [cloneMethod] on second call to value accessor', () => {
let numCalls = 0;
const theClone = {};
const cloneable = {
Expand All @@ -34,10 +34,12 @@ describe('Shrinkable', () => {
}
};
const s = new Shrinkable(cloneable);
assert.ok(s.value === cloneable);
assert.equal(numCalls, 0);
assert.ok(s.value === theClone);
assert.equal(numCalls, 1);
});
it('Should not call [cloneMethod] on value accessor', () => {
it('Should not call [cloneMethod] on (drity) value_ accessor', () => {
let numCalls = 0;
const theClone = {};
const cloneable = {
Expand Down