Skip to content

Commit

Permalink
Add support for arbitrary Time objects
Browse files Browse the repository at this point in the history
  • Loading branch information
wuest committed Sep 16, 2024
1 parent 1b3ca97 commit b55c6aa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ The following types are provided by Minitest-Proptest:
* `Char` - any single character 0x00-0xff
* `ASCIIChar` - any character 0x00-0x7f
* `String` - arbitrary length string of `Char`s
* `Time` - any time between 1901-12-13 20:45:52 and 2038-01-19 03:14:07 UTC
* `Bool`
* Polymorphic types
* `Array a` - array of arbitrary length of another type
Expand Down
9 changes: 9 additions & 0 deletions lib/minitest/proptest/gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,15 @@ def for(*classes)
end.with_score_function do |_|
1
end

generator_for(Time) do
r = sized(0xffffffff)
Time.at((r & 0x80000000).zero? ? r : -((r ^ 0x7fffffff) - 0x7fffffff))
end.with_shrink_function do |t|
integral_shrink.call(t.to_i).map(&Time.method(:at))
end.with_score_function do |t|
t.to_i.abs
end
end
end
end
16 changes: 16 additions & 0 deletions test/property_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,22 @@ def test_shrink_bool
end
end

def test_shrink_time
gen = ::Minitest::Proptest::Gen.new(Random.new).for(Time)
property do
t = arbitrary Time
g = gen.force(t)
candidates = g.shrink_candidates

candidates.all? do |score, x|
score == x.to_i.abs &&
x.to_i <= 0x7fffffff &&
x.to_i >= -0x80000000 &&
( t.to_i < 0 ? x.to_i <= 1 : x.to_i >= -1 )
end
end
end

def test_where
property do
n = arbitrary Int32
Expand Down

0 comments on commit b55c6aa

Please sign in to comment.