Skip to content

Commit

Permalink
Don't break in shrink and expand when the generator produces falsy va…
Browse files Browse the repository at this point in the history
…lues
  • Loading branch information
papandreou committed Sep 2, 2024
1 parent 370242f commit 66da660
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/PickoneGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class PickoneGenerator extends Generator {
}

shrink(item) {
if (this.lastUnwrappedValue === item && this.lastValue.shrink) {
if (
this.lastUnwrappedValue === item &&
this.lastValue &&
this.lastValue.shrink
) {
return this.lastValue.shrink(item);
} else {
return new ConstantGenerator(item);
Expand All @@ -24,7 +28,9 @@ class PickoneGenerator extends Generator {

expand(item) {
const expandableItem =
this.lastUnwrappedValue === item && this.lastValue.expand;
this.lastUnwrappedValue === item &&
this.lastValue &&
this.lastValue.expand;

return new WeightedGenerator([
[this, 10],
Expand Down
8 changes: 8 additions & 0 deletions src/PickoneGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ describe("PickoneGenerator", () => {
ConstantGenerator
);
});

it("does not break when the generator emits falsy values", () => {
new PickoneGenerator([null]).shrink().first();
});
});

describe("expand", () => {
it("returns a new generator that is more likely to pick the given item", () => {
expect(generator.expand(5), "to yield items", [7, 5, 7, 5, 5, 5, 4, 0]);
});

it("does not break when the generator emits falsy values", () => {
new PickoneGenerator([null]).expand().first();
});
});

describe("when the items contains generators", () => {
Expand Down

0 comments on commit 66da660

Please sign in to comment.