diff --git a/lib/docs/core/doc.rb b/lib/docs/core/doc.rb index 4a4f9177..e4177e4b 100644 --- a/lib/docs/core/doc.rb +++ b/lib/docs/core/doc.rb @@ -34,17 +34,18 @@ module Docs end def index_page(id) - if page = new.build_page(id) + if (page = new.build_page(id)) && page[:entries].present? yield page[:store_path], page[:output] index = EntryIndex.new index.add page[:entries] + index end - index end def index_pages index = EntryIndex.new new.build_pages do |page| + next if page[:entries].blank? yield page[:store_path], page[:output] index.add page[:entries] end diff --git a/lib/docs/filters/core/entries.rb b/lib/docs/filters/core/entries.rb index 2f358a74..a86f1ac1 100644 --- a/lib/docs/filters/core/entries.rb +++ b/lib/docs/filters/core/entries.rb @@ -7,7 +7,7 @@ module Docs def entries entries = [] - entries << default_entry if include_default_entry? + entries << default_entry if root_page? || include_default_entry? entries.concat(additional_entries) build_entries(entries) end diff --git a/test/lib/docs/core/doc_test.rb b/test/lib/docs/core/doc_test.rb index 8e41b73c..29bb62fe 100644 --- a/test/lib/docs/core/doc_test.rb +++ b/test/lib/docs/core/doc_test.rb @@ -129,19 +129,36 @@ class DocsDocTest < MiniTest::Spec end end - it "yields the page's :store_path and :output" do - doc.index_page('') { |*args| @args = args } - assert_equal [page[:store_path], page[:output]], @args - end + context "and it has :entries" do + it "yields the page's :store_path and :output" do + doc.index_page('') { |*args| @args = args } + assert_equal [page[:store_path], page[:output]], @args + end - it "returns an EntryIndex" do - assert_instance_of Docs::EntryIndex, doc.index_page('') {} + it "returns an EntryIndex" do + assert_instance_of Docs::EntryIndex, doc.index_page('') {} + end + + describe "the index" do + it "contains the page's entries" do + index = doc.index_page('') {} + assert_equal page[:entries], index.entries + end + end end - describe "the index" do - it "contains the page's entries" do - index = doc.index_page('') {} - assert_equal page[:entries], index.entries + context "and it doesn't have :entries" do + before do + page[:entries] = [] + end + + it "doesn't yield" do + doc.index_page('') { |*_| @yield = true } + refute @yield + end + + it "returns nil" do + assert_nil doc.index_page('') {} end end end @@ -184,21 +201,39 @@ class DocsDocTest < MiniTest::Spec end end - it "yields each page's :store_path and :output" do + it "yields pages that have :entries" do doc.index_pages { |*args| (@args ||= []) << args } assert_equal pages.length, @args.length assert_equal [page[:store_path], page[:output]], @args.first end - it "returns an EntryIndex" do - assert_instance_of Docs::EntryIndex, doc.index_pages {} + it "doesn't yield pages that don't have :entries" do + pages.first[:entries] = [] + doc.index_pages { |*args| (@args ||= []) << args } + assert_equal pages.length - 1, @args.length + end + + describe "and at least one has :entries" do + it "returns an EntryIndex" do + assert_instance_of Docs::EntryIndex, doc.index_pages {} + end + + describe "the index" do + it "contains all the pages' entries" do + index = doc.index_pages {} + assert_equal pages.length, index.entries.length + assert_includes index.entries, entry + end + end end - describe "the index" do - it "contains all pages' entries" do - index = doc.index_pages {} - assert_equal pages.length, index.entries.length - assert_includes index.entries, entry + context "and none have :entries" do + before do + pages.each { |page| page[:entries] = [] } + end + + it "returns nil" do + assert_nil doc.index_pages {} end end end diff --git a/test/lib/docs/filters/core/entries_test.rb b/test/lib/docs/filters/core/entries_test.rb index 68108d71..c5298f4d 100644 --- a/test/lib/docs/filters/core/entries_test.rb +++ b/test/lib/docs/filters/core/entries_test.rb @@ -26,7 +26,7 @@ class EntriesFilterTest < MiniTest::Spec it "includes the default entry when #include_default_entry? is true" do stub(filter).include_default_entry? { true } - assert_equal 1, entries.length + refute_empty entries end it "doesn't include the default entry when #include_default_entry? is false" do @@ -34,6 +34,12 @@ class EntriesFilterTest < MiniTest::Spec assert_empty entries end + it "always includes the default entry when #root_page? is true" do + stub(filter).include_default_entry? { false } + stub(filter).root_page? { true } + refute_empty entries + end + describe "the default entry" do it "has the #name, #path and #type" do assert_equal 'name', entries.first.name