1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
module Enumerable
module Statistics
module RSpecMacros
def self.included(mod)
mod.module_eval do
extend ExampleGroupMethods
end
end
module ExampleGroupMethods
def with_array(given_ary, &example_group_block)
describe "for #{given_ary.inspect}" do
let(:ary) { given_ary }
module_eval(&example_group_block)
end
end
def with_enum(given_enum, description=given_enum.inspect, &example_group_block)
if given_enum.is_a? Array
given_enum = given_enum.each
description += '.each'
end
describe "for #{description}" do
let(:enum) { given_enum }
module_eval(&example_group_block)
end
end
def with_init(init_value, &example_group_block)
context "with init=#{init_value.inspect}" do
let(:init) { init_value }
module_eval(&example_group_block)
end
end
def with_conversion(conversion_block, description, &example_group_block)
context "with conversion `#{description}`" do
let(:block) { conversion_block }
module_eval(&example_group_block)
end
end
def it_equals_with_type(x, type)
it { is_expected.to be_an(type) }
it { is_expected.to eq(x) }
end
def it_is_int_equal(n)
it_equals_with_type(n, Integer)
end
def it_is_rational_equal(n)
it_equals_with_type(n, Rational)
end
def it_is_float_equal(n)
it_equals_with_type(n, Float)
end
def it_is_float_nan
it { is_expected.to be_an(Float) }
it { is_expected.to be_nan }
end
def it_is_complex_equal(n)
it_equals_with_type(n, Complex)
end
end
end
end
end
RSpec.configure do |c|
c.include Enumerable::Statistics::RSpecMacros
end
|