Skip to content

Instantly share code, notes, and snippets.

@niku
Last active February 17, 2024 20:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niku/6dec8e397943cdc372b969fff26299e7 to your computer and use it in GitHub Desktop.
Save niku/6dec8e397943cdc372b969fff26299e7 to your computer and use it in GitHub Desktop.
a sample repository of prop_check with test-unit

This repository is working sample of prop_check with test-unit.

$ ruby run_test.rb
Loaded suite 6dec8e397943cdc372b969fff26299e7
Started
E
================================================================================================================================================
Error: test_that_it_returns_an_integer_for_any_input(TestNaiveAverage):
  ZeroDivisionError:
  (after 2 successful property test runs)
  Failed on:
  `[]`

  Exception message:
  ---
  divided by 0
  ---

  (shrinking impossible)
/Users/niku/src/6dec8e397943cdc372b969fff26299e7/naive_average.rb:2:in `/'
/Users/niku/src/6dec8e397943cdc372b969fff26299e7/naive_average.rb:2:in `naive_average'
/Users/niku/src/6dec8e397943cdc372b969fff26299e7/test_my.rb:10:in `block in test_that_it_returns_an_integer_for_any_input'
      7:
      8:   def test_that_it_returns_an_integer_for_any_input
      9:     PropCheck.forall(G.array(G.integer)) do |numbers|
  => 10:       result = naive_average(numbers)
     11:
     12:       assert_instance_of(Integer, result)
     13:     end
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/helper.rb:41:in `call_splatted'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/property.rb:298:in `check_attempt'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/property.rb:254:in `block in check'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/hooks.rb:125:in `<<'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/hooks.rb:125:in `block in each'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/hooks.rb:52:in `block in call'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/hooks.rb:22:in `block in initialize'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/hooks.rb:51:in `call'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/hooks.rb:40:in `block in wrap_block'
<internal:kernel>:187:in `loop'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/hooks.rb:128:in `each'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/property.rb:252:in `each'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/property.rb:252:in `each'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/property.rb:252:in `each'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/property.rb:252:in `check'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check/property.rb:46:in `forall'
/Users/niku/.asdf/installs/ruby/3.3.0-rc1/lib/ruby/gems/3.3.0+0/gems/prop_check-0.18.1/lib/prop_check.rb:34:in `forall'
/Users/niku/src/6dec8e397943cdc372b969fff26299e7/test_my.rb:9:in `test_that_it_returns_an_integer_for_any_input'
================================================================================================================================================
Finished in 0.00091 seconds.
------------------------------------------------------------------------------------------------------------------------------------------------
1 tests, 2 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications
0% passed
------------------------------------------------------------------------------------------------------------------------------------------------
1098.90 tests/s, 2197.80 assertions/s
source "https://rubygems.org"
gem "test-unit", "~> 3.6"
gem "prop_check", "~> 0.18.1"
GEM
remote: https://rubygems.org/
specs:
amazing_print (1.5.0)
power_assert (2.0.3)
prop_check (0.18.1)
amazing_print (~> 1.2)
test-unit (3.6.1)
power_assert
PLATFORMS
arm64-darwin-23
ruby
DEPENDENCIES
prop_check (~> 0.18.1)
test-unit (~> 3.6)
BUNDLED WITH
2.5.0.dev
def naive_average(array)
array.sum / array.length
end
current_dir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(current_dir)
require 'test/unit'
exit Test::Unit::AutoRunner.run(true, current_dir)
require "naive_average"
require "prop_check"
require "test-unit"
class TestNaiveAverage < Test::Unit::TestCase
G = PropCheck::Generators
def test_that_it_returns_an_integer_for_any_input
PropCheck.forall(G.array(G.integer)) do |numbers|
result = naive_average(numbers)
assert_instance_of(Integer, result)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment