-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
109 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pbt | ||
module Arbitrary | ||
class TupleArbitrary | ||
# @param arbs [Array<Pbt::Arbitrary>] | ||
def initialize(*arbs) | ||
@arbs = arbs | ||
end | ||
|
||
# @return [Array] | ||
def generate(rng) | ||
@arbs.map { |arb| arb.generate(rng) } | ||
end | ||
|
||
# @return [Enumerator] | ||
def shrink(current) | ||
# This is not the most comprehensive but allows a reasonable number of entries in the shrink | ||
Enumerator.new do |y| | ||
@arbs.each_with_index do |arb, idx| | ||
arb.shrink(current[idx]).each do |v| | ||
next_values = current.dup | ||
next_values[idx] = v | ||
y << next_values | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Pbt::Arbitrary::TupleArbitrary do | ||
describe "#generate" do | ||
it "generates an array" do | ||
val = Pbt::Arbitrary::TupleArbitrary.new(Pbt.integer, Pbt.integer).generate(Random.new) | ||
expect(val).to be_a(Array) | ||
end | ||
|
||
it "generates an array of given arbitrary" do | ||
val = Pbt::Arbitrary::TupleArbitrary.new(Pbt.integer, Pbt.integer).generate(Random.new) | ||
val.each { |e| expect(e).to be_a(Integer) } | ||
end | ||
end | ||
|
||
describe "#shrink" do | ||
it "returns an Enumerator" do | ||
arb = Pbt::Arbitrary::TupleArbitrary.new(Pbt.integer) | ||
val = arb.generate(Random.new) | ||
expect(arb.shrink(val)).to be_a(Enumerator) | ||
end | ||
|
||
it "returns an Enumerator that returns shrunken values" do | ||
arb = Pbt::Arbitrary::TupleArbitrary.new(Pbt.integer) | ||
expect(arb.shrink([50]).to_a).to eq [ | ||
[25], | ||
[13], | ||
[7], | ||
[4], | ||
[2], | ||
[1], | ||
[0] | ||
] | ||
end | ||
|
||
it "returns an Enumerator that returns shrunken values for each arbitraries" do | ||
arb = Pbt::Arbitrary::TupleArbitrary.new(Pbt.integer, Pbt.integer) | ||
expect(arb.shrink([10, 20]).to_a).to eq [ | ||
[5, 20], | ||
[3, 20], | ||
[2, 20], | ||
[1, 20], | ||
[0, 20], | ||
[10, 10], | ||
[10, 5], | ||
[10, 3], | ||
[10, 2], | ||
[10, 1], | ||
[10, 0] | ||
] | ||
end | ||
|
||
context "when current value and target is same" do | ||
it "returns an empty Enumerator" do | ||
arb = Pbt::Arbitrary::TupleArbitrary.new(Pbt.integer, Pbt.integer) | ||
expect(arb.shrink([0, 0]).to_a).to eq [] | ||
end | ||
end | ||
end | ||
end |