Skip to content

Commit 65b201b

Browse files
committed
Make purpose of test cases more clear
1 parent b3e14d9 commit 65b201b

File tree

1 file changed

+151
-149
lines changed

1 file changed

+151
-149
lines changed

spec/lib/annotate/annotate_routes_spec.rb

Lines changed: 151 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -45,187 +45,189 @@
4545
end
4646

4747
context 'When "config/routes.rb" exists' do
48-
let :rake_routes_result do
49-
<<-EOS
50-
Prefix Verb URI Pattern Controller#Action
51-
myaction1 GET /url1(.:format) mycontroller1#action
52-
myaction2 POST /url2(.:format) mycontroller2#action
53-
myaction3 DELETE|GET /url3(.:format) mycontroller3#action
54-
EOS
55-
end
56-
57-
let :route_file_content do
58-
''
59-
end
60-
6148
before(:each) do
6249
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
6350
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once
6451

6552
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once
6653
end
6754

68-
context 'When the file does not contain magic comment' do
69-
context 'When the file does not contain annotation yet' do
70-
context 'When no option is passed' do
71-
let :expected_result do
72-
<<~EOS
73-
74-
# == Route Map
75-
#
76-
# Prefix Verb URI Pattern Controller#Action
77-
# myaction1 GET /url1(.:format) mycontroller1#action
78-
# myaction2 POST /url2(.:format) mycontroller2#action
79-
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
80-
EOS
81-
end
82-
83-
it 'annotates normally' do
84-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
85-
expect(mock_file).to receive(:puts).with(expected_result).once
86-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
87-
88-
AnnotateRoutes.do_annotations
89-
end
55+
context 'When the result of `rake routes` is present' do
56+
context 'When the file does not contain magic comment' do
57+
let :rake_routes_result do
58+
<<-EOS
59+
Prefix Verb URI Pattern Controller#Action
60+
myaction1 GET /url1(.:format) mycontroller1#action
61+
myaction2 POST /url2(.:format) mycontroller2#action
62+
myaction3 DELETE|GET /url3(.:format) mycontroller3#action
63+
EOS
9064
end
9165

92-
context 'When the option "format_markdown" is passed' do
93-
let :expected_result do
94-
<<~EOS
66+
let :route_file_content do
67+
''
68+
end
9569

96-
# ## Route Map
97-
#
98-
# Prefix | Verb | URI Pattern | Controller#Action
99-
# --------- | ---------- | --------------- | --------------------
100-
# myaction1 | GET | /url1(.:format) | mycontroller1#action
101-
# myaction2 | POST | /url2(.:format) | mycontroller2#action
102-
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
103-
EOS
104-
end
70+
context 'When the file does not contain annotation yet' do
71+
context 'When no option is passed' do
72+
let :expected_result do
73+
<<~EOS
74+
75+
# == Route Map
76+
#
77+
# Prefix Verb URI Pattern Controller#Action
78+
# myaction1 GET /url1(.:format) mycontroller1#action
79+
# myaction2 POST /url2(.:format) mycontroller2#action
80+
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
81+
EOS
82+
end
10583

106-
it 'annotates in Markdown format' do
107-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
108-
expect(mock_file).to receive(:puts).with(expected_result).once
109-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
84+
it 'annotates normally' do
85+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
86+
expect(mock_file).to receive(:puts).with(expected_result).once
87+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
11088

111-
AnnotateRoutes.do_annotations(format_markdown: true)
89+
AnnotateRoutes.do_annotations
90+
end
11291
end
113-
end
11492

115-
context 'When the options "wrapper_open" and "wrapper_close" are passed' do
116-
let :expected_result do
117-
<<~EOS
93+
context 'When the option "format_markdown" is passed' do
94+
let :expected_result do
95+
<<~EOS
96+
97+
# ## Route Map
98+
#
99+
# Prefix | Verb | URI Pattern | Controller#Action
100+
# --------- | ---------- | --------------- | --------------------
101+
# myaction1 | GET | /url1(.:format) | mycontroller1#action
102+
# myaction2 | POST | /url2(.:format) | mycontroller2#action
103+
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
104+
EOS
105+
end
118106

119-
# START
120-
# == Route Map
121-
#
122-
# Prefix Verb URI Pattern Controller#Action
123-
# myaction1 GET /url1(.:format) mycontroller1#action
124-
# myaction2 POST /url2(.:format) mycontroller2#action
125-
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
126-
# END
127-
EOS
107+
it 'annotates in Markdown format' do
108+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
109+
expect(mock_file).to receive(:puts).with(expected_result).once
110+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
111+
112+
AnnotateRoutes.do_annotations(format_markdown: true)
113+
end
128114
end
129115

130-
it 'annotates and wraps annotation with specified words' do
131-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
132-
expect(mock_file).to receive(:puts).with(expected_result).once
133-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
116+
context 'When the options "wrapper_open" and "wrapper_close" are passed' do
117+
let :expected_result do
118+
<<~EOS
119+
120+
# START
121+
# == Route Map
122+
#
123+
# Prefix Verb URI Pattern Controller#Action
124+
# myaction1 GET /url1(.:format) mycontroller1#action
125+
# myaction2 POST /url2(.:format) mycontroller2#action
126+
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
127+
# END
128+
EOS
129+
end
134130

135-
AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
131+
it 'annotates and wraps annotation with specified words' do
132+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
133+
expect(mock_file).to receive(:puts).with(expected_result).once
134+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
135+
136+
AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
137+
end
136138
end
137139
end
138140
end
139-
end
140141

141-
context 'When the file contains magic comments' do
142-
MAGIC_COMMENTS.each do |magic_comment|
143-
describe "magic comment: #{magic_comment.inspect}" do
144-
let :route_file_content do
145-
<<~EOS
146-
#{magic_comment}
147-
EOS
148-
end
142+
context 'When the file contains magic comments' do
143+
MAGIC_COMMENTS.each do |magic_comment|
144+
describe "magic comment: #{magic_comment.inspect}" do
145+
let :route_file_content do
146+
<<~EOS
147+
#{magic_comment}
148+
EOS
149+
end
149150

150-
let :rake_routes_result do
151-
<<-EOS
151+
let :rake_routes_result do
152+
<<-EOS
152153
Prefix Verb URI Pattern Controller#Action
153154
myaction1 GET /url1(.:format) mycontroller1#action
154155
myaction2 POST /url2(.:format) mycontroller2#action
155156
myaction3 DELETE|GET /url3(.:format) mycontroller3#action
156-
EOS
157-
end
158-
159-
context 'When the file does not contain annotation yet' do
160-
context 'When no option is passed' do
161-
let :expected_result do
162-
<<~EOS
163-
#{magic_comment}
164-
165-
# == Route Map
166-
#
167-
# Prefix Verb URI Pattern Controller#Action
168-
# myaction1 GET /url1(.:format) mycontroller1#action
169-
# myaction2 POST /url2(.:format) mycontroller2#action
170-
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
171-
EOS
172-
end
173-
174-
it 'annotates normally' do
175-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
176-
expect(mock_file).to receive(:puts).with(expected_result).once
177-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
178-
179-
AnnotateRoutes.do_annotations
180-
end
157+
EOS
181158
end
182159

183-
context 'When the option "format_markdown" is passed' do
184-
let :expected_result do
185-
<<~EOS
186-
#{magic_comment}
187-
188-
# ## Route Map
189-
#
190-
# Prefix | Verb | URI Pattern | Controller#Action
191-
# --------- | ---------- | --------------- | --------------------
192-
# myaction1 | GET | /url1(.:format) | mycontroller1#action
193-
# myaction2 | POST | /url2(.:format) | mycontroller2#action
194-
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
195-
EOS
160+
context 'When the file does not contain annotation yet' do
161+
context 'When no option is passed' do
162+
let :expected_result do
163+
<<~EOS
164+
#{magic_comment}
165+
166+
# == Route Map
167+
#
168+
# Prefix Verb URI Pattern Controller#Action
169+
# myaction1 GET /url1(.:format) mycontroller1#action
170+
# myaction2 POST /url2(.:format) mycontroller2#action
171+
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
172+
EOS
173+
end
174+
175+
it 'annotates normally' do
176+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
177+
expect(mock_file).to receive(:puts).with(expected_result).once
178+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
179+
180+
AnnotateRoutes.do_annotations
181+
end
196182
end
197183

198-
it 'annotates in Markdown format' do
199-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
200-
expect(mock_file).to receive(:puts).with(expected_result).once
201-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
202-
203-
AnnotateRoutes.do_annotations(format_markdown: true)
184+
context 'When the option "format_markdown" is passed' do
185+
let :expected_result do
186+
<<~EOS
187+
#{magic_comment}
188+
189+
# ## Route Map
190+
#
191+
# Prefix | Verb | URI Pattern | Controller#Action
192+
# --------- | ---------- | --------------- | --------------------
193+
# myaction1 | GET | /url1(.:format) | mycontroller1#action
194+
# myaction2 | POST | /url2(.:format) | mycontroller2#action
195+
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action
196+
EOS
197+
end
198+
199+
it 'annotates in Markdown format' do
200+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
201+
expect(mock_file).to receive(:puts).with(expected_result).once
202+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
203+
204+
AnnotateRoutes.do_annotations(format_markdown: true)
205+
end
204206
end
205-
end
206-
207-
context 'When the options "wrapper_open" and "wrapper_close" are passed' do
208-
let :expected_result do
209-
<<~EOS
210-
#{magic_comment}
211-
212-
# START
213-
# == Route Map
214-
#
215-
# Prefix Verb URI Pattern Controller#Action
216-
# myaction1 GET /url1(.:format) mycontroller1#action
217-
# myaction2 POST /url2(.:format) mycontroller2#action
218-
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
219-
# END
220-
EOS
221-
end
222-
223-
it 'annotates and wraps annotation with specified words' do
224-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
225-
expect(mock_file).to receive(:puts).with(expected_result).once
226-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
227207

228-
AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
208+
context 'When the options "wrapper_open" and "wrapper_close" are passed' do
209+
let :expected_result do
210+
<<~EOS
211+
#{magic_comment}
212+
213+
# START
214+
# == Route Map
215+
#
216+
# Prefix Verb URI Pattern Controller#Action
217+
# myaction1 GET /url1(.:format) mycontroller1#action
218+
# myaction2 POST /url2(.:format) mycontroller2#action
219+
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
220+
# END
221+
EOS
222+
end
223+
224+
it 'annotates and wraps annotation with specified words' do
225+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
226+
expect(mock_file).to receive(:puts).with(expected_result).once
227+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
228+
229+
AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
230+
end
229231
end
230232
end
231233
end
@@ -235,7 +237,7 @@
235237
end
236238
end
237239

238-
describe 'When adding' do
240+
describe 'When the result of `rake routes` is blank' do
239241
before(:each) do
240242
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
241243
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once

0 commit comments

Comments
 (0)