History

Refactor Doc.store_pages

Thibaut committed 11 years ago

Commit e9125c61 parent ecf774e2 files · +50 −92Browse files

Unified · Split

2 files
  1. lib/docs/core/doc.rb +14 −13
  2. test/lib/docs/core/doc_test.rb +36 −79
lib/docs/core/doc.rb+14−13
@@ -44,21 +44,22 @@ module Docs · 43 unmodified lines
44 end44 end
45 end45 end
46 46
47 def index_pages47 def store_pages(store)
48 index = EntryIndex.new48 index = EntryIndex.new
49 new.build_pages do |page|
50 next if page[:entries].blank?
51 yield page[:store_path], page[:output]
52 index.add page[:entries]
53 end
54 index.empty? ? nil : index
55 end
56 49
57 def store_pages(store)50 store.replace(path) do
58 store.replace path do51 new.build_pages do |page|
59 index = index_pages(&store.method(:write))52 next unless store_page?(page)
60 store.write INDEX_FILENAME, index.to_json if index53 store.write page[:store_path], page[:output]
61 !!index54 index.add page[:entries]
55 end
56
57 if index.present?
58 store.write INDEX_FILENAME, index.to_json
59 true
60 else
61 false
62 end
62 end63 end
63 end64 end
64 65
test/lib/docs/core/doc_test.rb+36−79
@@ -17,10 +17,6 @@ class DocsDocTest < MiniTest::Spec · 16 unmodified lines
17 Docs::Entry.new17 Docs::Entry.new
18 end18 end
19 19
20 let :index do
21 Docs::EntryIndex.new
22 end
23
24 let :store do20 let :store do
25 Docs::NullStore.new21 Docs::NullStore.new
26 end22 end
@@ -184,18 +180,18 @@ class DocsDocTest < MiniTest::Spec · 157 unmodified lines
184 end180 end
185 end181 end
186 182
187 describe ".index_pages" do183 describe ".store_pages" do
188 it "build the pages" do184 it "build the pages" do
189 any_instance_of(doc) do |instance|185 any_instance_of(doc) do |instance|
190 stub(instance).build_pages { @called = true }186 stub(instance).build_pages { @called = true }
191 end187 end
192 doc.index_pages {}188 doc.store_pages(store) {}
193 assert @called189 assert @called
194 end190 end
195 191
196 context "when pages are built successfully" do192 context "when pages are built successfully" do
197 let :pages do193 let :pages do
198 [page, page.dup]194 [page.dup, page.dup]
199 end195 end
200 196
201 before do197 before do
@@ -204,39 +200,51 @@ class DocsDocTest < MiniTest::Spec · 2 unmodified lines
204 end200 end
205 end201 end
206 202
207 it "yields pages that have :entries" do203 context "and at least one page has :entries" do
208 doc.index_pages { |*args| (@args ||= []) << args }204 it "returns true" do
209 assert_equal pages.length, @args.length205 assert doc.store_pages(store)
210 assert_equal [page[:store_path], page[:output]], @args.first206 end
211 end
212 207
213 it "doesn't yield pages that don't have :entries" do208 it "stores a file for each page that has :entries" do
214 pages.first[:entries] = []209 pages.first.merge!(entries: [], output: '')
215 doc.index_pages { |*args| (@args ||= []) << args }210 mock(store).write(page[:store_path], page[:output])
216 assert_equal pages.length - 1, @args.length211 mock(store).write('index.json', anything)
217 end212 doc.store_pages(store)
213 end
218 214
219 describe "and at least one has :entries" do215 it "stores an index that contains all the pages' entries" do
220 it "returns an EntryIndex" do216 stub(store).write(page[:store_path], page[:output])
221 assert_instance_of Docs::EntryIndex, doc.index_pages {}217 mock(store).write('index.json', anything) do |path, json|
218 json = JSON.parse(json)
219 assert_equal pages.length, json['entries'].length
220 assert_includes json['entries'], entry.as_json.stringify_keys
221 end
222 doc.store_pages(store)
222 end223 end
223 224
224 describe "the index" do225 it "replaces the .path directory before storing the files" do
225 it "contains all the pages' entries" do226 stub(doc).path { 'path' }
226 index = doc.index_pages {}227 stub(store).write { assert false }
227 assert_equal pages.length, index.entries.length228 mock(store).replace('path') do |_, block|
228 assert_includes index.entries, entry229 stub(store).write
230 block.call
229 end231 end
232 doc.store_pages(store)
230 end233 end
231 end234 end
232 235
233 context "and none have :entries" do236 context "and no pages have :entries" do
234 before do237 before do
235 pages.each { |page| page[:entries] = [] }238 pages.each { |page| page[:entries] = [] }
236 end239 end
237 240
238 it "returns nil" do241 it "returns false" do
239 assert_nil doc.index_pages {}242 refute doc.store_pages(store)
243 end
244
245 it "doesn't store files" do
246 dont_allow(store).write
247 doc.store_pages(store)
240 end248 end
241 end249 end
242 end250 end
@@ -248,57 +256,6 @@ class DocsDocTest < MiniTest::Spec · 5 unmodified lines
248 end256 end
249 end257 end
250 258
251 it "doesn't yield" do
252 doc.index_pages { |*_| @yield = true }
253 refute @yield
254 end
255
256 it "returns nil" do
257 assert_nil doc.index_pages {}
258 end
259 end
260 end
261
262 describe ".store_pages" do
263 context "when pages are indexed successfully" do
264 before do
265 stub(store).write
266 stub(doc).index_pages do |block|
267 2.times { block.call page[:store_path], page[:output] }
268 index
269 end
270 end
271
272 it "returns true" do
273 assert doc.store_pages(store)
274 end
275
276 it "stores a file for each page" do
277 2.times { mock(store).write(page[:store_path], page[:output]) }
278 doc.store_pages(store)
279 end
280
281 it "stores the index" do
282 mock(store).write('index.json', index.to_json)
283 doc.store_pages(store)
284 end
285
286 it "replaces the .path directory before storing the files" do
287 stub(doc).path { 'path' }
288 stub(store).write { assert false }
289 mock(store).replace('path') do |_, block|
290 stub(store).write
291 block.call
292 end
293 doc.store_pages(store)
294 end
295 end
296
297 context "when no pages are indexed successfully" do
298 before do
299 stub(doc).index_pages { nil }
300 end
301
302 it "returns false" do259 it "returns false" do
303 refute doc.store_pages(store)260 refute doc.store_pages(store)
304 end261 end