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

Add support for arbitrary Time objects #9

Merged
merged 1 commit into from
Sep 16, 2024
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
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