forked from cldwalker/hirb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter_test.rb
181 lines (152 loc) · 6.52 KB
/
formatter_test.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require File.join(File.dirname(__FILE__), 'test_helper')
describe "Formatter" do
def set_formatter(hash={})
@formatter = Formatter.new(hash)
end
describe "klass_config" do
it "recursively merges ancestor options" do
@formatter = set_formatter "String"=>{:args=>[1,2], :options=>{:fields=>[:to_s]}},
"Object"=>{:method=>:object_output, :ancestor=>true, :options=>{:vertical=>true}},
"Kernel"=>{:method=>:default_output}
expected_result = {:method=>:object_output, :args=>[1, 2], :ancestor=>true, :options=>{:fields=>[:to_s], :vertical=>true}}
@formatter.klass_config(::String).should == expected_result
end
it "doesn't merge ancestor options" do
@formatter = set_formatter "String"=>{:args=>[1,2]}, "Object"=>{:method=>:object_output},
"Kernel"=>{:method=>:default_output}
@formatter.klass_config(::String).should == {:args=>[1, 2]}
end
it "returns hash when nothing found" do
set_formatter.klass_config(::String).should == {}
end
describe "with dynamic_config" do
def set_formatter(hash={})
@formatter = Formatter.new(hash)
end
after { Formatter.dynamic_config = {}}
it "merges ancestor options and sets local config" do
Formatter.dynamic_config = {"Object"=>{:method=>:blah}, "Kernel"=>{:args=>[1,2], :ancestor=>true}}
set_formatter.klass_config(::String).should == {:args=>[1,2], :ancestor=>true}
@formatter.config['Kernel'].should == {:args=>[1,2], :ancestor=>true}
end
it "uses local config over dynamic_config" do
Formatter.dynamic_config = {"String"=>{:method=>:blah}}
set_formatter "String"=>{:args=>[1,2]}
@formatter.klass_config(::String).should == {:args=>[1,2]}
end
it "uses dynamic_config and sets local config" do
Formatter.dynamic_config = {"String"=>{:method=>:blah}}
set_formatter.klass_config(::String).should == {:method=>:blah}
@formatter.config['String'].should == {:method=>:blah}
end
end
end
describe "formatter methods:" do
before_all { eval "module ::Dooda; end" }
it "#add_view sets formatter config" do
@formatter = set_formatter
@formatter.add_view ::Dooda, :class=>"DoodaView"
@formatter.klass_config(::Dooda).should == {:class=>"DoodaView"}
end
it "#add_view overwrites existing formatter config" do
@formatter = set_formatter "Dooda"=>{:class=>"DoodaView"}
@formatter.add_view ::Dooda, :class=>"DoodaView2"
@formatter.klass_config(::Dooda).should == {:class=>"DoodaView2"}
end
it "#parse_console_options passes all options except for formatter options into :options" do
@formatter = set_formatter
options = {:class=>'blah', :method=>'blah', :output_method=>'blah', :blah=>'blah'}
expected_options = {:class=>'blah', :method=>'blah', :output_method=>'blah', :options=>{:blah=>'blah'}}
@formatter.parse_console_options(options).should == expected_options
end
it "#determine_output_class bypasses exceptions for to_a" do
@formatter.determine_output_class(STDOUT).should == IO
@formatter.determine_output_class({:a=>1}).should == Hash
end
it "#determine_output_class bypasses subclasses of exceptions for to_a" do
class Hash2 < Hash; end
@formatter.determine_output_class(Hash2[1=>2]).should == Hash2
end
end
describe "format_output" do
def view_output(*args, &block); View.view_output(*args, &block); end
def render_method(*args); View.render_method(*args); end
def enable_with_output(value)
Hirb.enable :output=>value
end
before_all {
eval %[module ::Commify
def self.render(strings)
strings = Array(strings)
strings.map {|e| e.split('').join(',')}.join("\n")
end
end]
reset_config
}
before { View.formatter = nil; reset_config }
after { Hirb.disable }
it "formats with method option" do
eval "module ::Kernel; def commify(string); string.split('').join(','); end; end"
enable_with_output "String"=>{:method=>:commify}
render_method.expects(:call).with('d,u,d,e')
view_output('dude')
end
it "formats with class option" do
enable_with_output "String"=>{:class=>"Commify"}
render_method.expects(:call).with('d,u,d,e')
view_output('dude')
end
it "formats with class option as symbol" do
enable_with_output "String"=>{:class=>:auto_table}
Helpers::AutoTable.expects(:render)
view_output('dude')
end
it "formats arrays" do
enable_with_output "String"=>{:class=>"Commify"}
render_method.expects(:call).with('d,u,d,e')
view_output(['dude'])
end
it "formats array-like objects" do
enable_with_output "String"=>{:class=>"Commify"}
render_method.expects(:call).with('d,u,d,e')
require 'set'
view_output Set.new(['dude'])
end
it "formats with options option" do
eval "module ::Blahify; def self.render(*args); end; end"
enable_with_output "String"=>{:class=>"Blahify", :options=>{:fields=>%w{a b}}}
Blahify.expects(:render).with('dude', :fields=>%w{a b})
view_output('dude')
end
it "doesn't format and returns false when no format method found" do
Hirb.enable
render_method.expects(:call).never
view_output(Date.today).should == false
end
it "formats with output_method option as method" do
enable_with_output 'String'=>{:class=>"Commify", :output_method=>:chop}
render_method.expects(:call).with('d,u,d')
view_output('dude')
end
it "formats with output_method option as proc" do
enable_with_output 'String'=>{:class=>"Commify", :output_method=>lambda {|e| e.chop}}
render_method.expects(:call).with('d,u,d')
view_output('dude')
end
it "formats output array with output_method option" do
enable_with_output 'String'=>{:class=>"Commify", :output_method=>:chop}
render_method.expects(:call).with("d,u,d\nm,a")
view_output(['dude', 'man'])
end
it "formats with explicit class option" do
enable_with_output 'String'=>{:class=>"Blahify"}
render_method.expects(:call).with('d,u,d,e')
view_output('dude', :class=>"Commify")
end
it "formats with explicit options option merges with existing options" do
enable_with_output "String"=>{:class=>"Commify", :options=>{:fields=>%w{f1 f2}}}
Commify.expects(:render).with('dude', :max_width=>10, :fields=>%w{f1 f2})
view_output('dude', :options=>{:max_width=>10})
end
end
end