-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy patherror_formatter_spec.rb
68 lines (59 loc) · 2.48 KB
/
error_formatter_spec.rb
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
RSpec.describe "Contracts::ErrorFormatters" do
before :all do
@o = GenericExample.new
end
C = Contracts::Builtin
describe "self.class_for" do
let(:keywordargs_contract) { C::KeywordArgs[:name => String, :age => Fixnum] }
let(:other_contract) { [C::Num, C::Num, C::Num] }
it "returns KeywordArgsErrorFormatter for KeywordArgs contract" do
data_keywordargs = {:contract => keywordargs_contract, :arg => {:b => 2}}
expect(Contracts::ErrorFormatters.class_for(data_keywordargs)).to eq(Contracts::KeywordArgsErrorFormatter)
end
it "returns Contracts::DefaultErrorFormatter for other contracts" do
data_default = {:contract => other_contract, :arg => {:b => 2}}
expect(Contracts::ErrorFormatters.class_for(data_default)).to eq(Contracts::DefaultErrorFormatter)
end
end
def format_message(str)
str.split("\n").map(&:strip).join("\n")
end
def fails(msg, &block)
expect { block.call }.to raise_error do |e|
expect(e).to be_a(ParamContractError)
expect(format_message(e.message)).to include(format_message(msg))
end
end
if ruby_version > 1.8
describe "self.failure_msg" do
it "includes normal information" do
msg = %{Contract violation for argument 1 of 1:
Expected: (KeywordArgs[{:name=>String, :age=>Fixnum}])
Actual: {:age=>"2", :invalid_third=>1}
Missing Contract: {:invalid_third=>1}
Invalid Args: [{:age=>"2", :contract=>Fixnum}]
Missing Args: {:name=>String}
Value guarded in: GenericExample::simple_keywordargs
With Contract: KeywordArgs => NilClass}
fails msg do
@o.simple_keywordargs(:age => "2", :invalid_third => 1)
end
end
it "includes Missing Contract information" do
fails %{Missing Contract: {:invalid_third=>1, :invalid_fourth=>1}} do
@o.simple_keywordargs(:age => "2", :invalid_third => 1, :invalid_fourth => 1)
end
end
it "includes Invalid Args information" do
fails %{Invalid Args: [{:age=>"2", :contract=>Fixnum}]} do
@o.simple_keywordargs(:age => "2", :invalid_third => 1)
end
end
it "includes Missing Args information" do
fails %{Missing Args: {:name=>String}} do
@o.simple_keywordargs(:age => "2", :invalid_third => 1)
end
end
end
end
end