diff --git a/README.md b/README.md index 45f10b1..75abe2a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/minitest/proptest/gen.rb b/lib/minitest/proptest/gen.rb index c436caa..8af598c 100644 --- a/lib/minitest/proptest/gen.rb +++ b/lib/minitest/proptest/gen.rb @@ -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 diff --git a/test/property_test.rb b/test/property_test.rb index 921a2f6..1306b86 100644 --- a/test/property_test.rb +++ b/test/property_test.rb @@ -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