Skip to content

Commit

Permalink
Add support for Set
Browse files Browse the repository at this point in the history
  • Loading branch information
wuest committed Sep 29, 2024
1 parent eec4de3 commit 6e778dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/minitest/proptest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'minitest/proptest/property'
require 'minitest/proptest/status'
require 'minitest/proptest/version'
require 'set'
require 'yaml'

module Minitest
Expand Down
15 changes: 15 additions & 0 deletions lib/minitest/proptest/gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,21 @@ def for(*classes)
xm.merge(ym)
end.with_empty { {} }

generator_for(Set) do |x|
Set[x]
end.with_shrink_function do |f, xs|
list_shrink.call(f, xs.to_a)
.map { |x| Set.new(x) }
.uniq
end.with_score_function do |f, xs|
xs.reduce(1) do |c, x|
y = f.call(x).abs
c * (y > 0 ? y + 1 : 1)
end.to_i * xs.length
end.with_append(0, 0x10) do |xs, ys|
xs + ys
end.with_empty { Set[] }

generator_for(Range) do |x|
(x..x)
end.with_shrink_function(&range_shrink).with_score_function do |f, r|
Expand Down
14 changes: 14 additions & 0 deletions test/property_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ def test_shrink_array
end
end

def test_shrink_set
gen = ::Minitest::Proptest::Gen.new(Random.new).for(Set, Int8)
property do
a = arbitrary Set, Int8
g = gen.force(a)
candidates = g.shrink_candidates

candidates.all? do |score, x|
score <= g.score &&
x.length <= a.length
end
end
end

def test_shrink_range
gen = ::Minitest::Proptest::Gen.new(Random.new).for(Range, Int8)
property do
Expand Down

0 comments on commit 6e778dd

Please sign in to comment.