spec/gorillib/exception/raisers_spec.rb in gorillib-0.4.1pre vs spec/gorillib/exception/raisers_spec.rb in gorillib-0.4.2pre
- old
+ new
@@ -7,10 +7,11 @@
expect{ yield }.to raise_error(described_class, msg)
end
def should_return_true
yield.should be_true
end
+ # different rubies have different error messages ARRGH.
def capture_error
message = 'should have raised, did not'
begin
yield
rescue described_class => err
@@ -40,10 +41,15 @@
end
it 'matches the message a native arity error would' do
should_raise_my_error(capture_error{ [].fill() }){ described_class.check_arity!([], 1..3) }
should_raise_my_error(capture_error{ [].to_s(1) }){ described_class.check_arity!([1], 0) }
end
+ it 'appends result of block (if given) to message' do
+ str = "esiar no delave ylno"
+ ->{ described_class.check_arity!([], 1..3){ str.reverse! } }.should raise_error(/only evaled on raise/)
+ str.should == "only evaled on raise"
+ end
end
context '.arity_at_least!' do
let(:error_message){ /wrong number of arguments/ }
it 'raises if there are fewer than that many args' do
@@ -55,6 +61,39 @@
should_return_true{ described_class.arity_at_least!(['a'], 0) }
should_return_true{ described_class.arity_at_least!(['a'], 1) }
end
end
end
+
+ describe TypeMismatchError do
+ context '.mismatched!' do
+ let(:error_message){ /.+ has mismatched type/ }
+ it 'raises an error' do
+ should_raise_my_error{ described_class.mismatched!("string", Integer) }
+ should_raise_my_error{ described_class.mismatched!(Object.new) }
+ end
+ end
+
+ context '.check_type!' do
+ let(:error_message){ /.+ has mismatched type; expected .+/ }
+ it 'raises true if any type matches' do
+ should_return_true{ described_class.check_type!("string", [Integer, String]) }
+ end
+ it 'raises an error if nothing matches' do
+ should_raise_my_error{ described_class.check_type!("string", [Integer, Float]) }
+ should_raise_my_error{ described_class.check_type!("string", [Integer]) }
+ should_raise_my_error{ described_class.check_type!("string", Integer) }
+ end
+ it 'checks is_a? given a class' do
+ should_return_true{ described_class.check_type!("string", [Integer, String]) }
+ should_return_true{ described_class.check_type!(7, [Integer, String]) }
+ should_raise_my_error{ described_class.check_type!(:symbol, [Integer, String]) }
+ end
+ it 'checks responds_to? given a symbol' do
+ should_return_true{ described_class.check_type!("string", [:to_str, :to_int]) }
+ should_return_true{ described_class.check_type!(7, [:to_str, :to_int]) }
+ should_raise_my_error{ described_class.check_type!(:symbol, [:to_str, :to_int]) }
+ end
+ end
+ end
+
end