瀏覽代碼

Don't store pages with no entries

Thibaut 12 年之前
父節點
當前提交
706270d89c
共有 4 個文件被更改,包括 64 次插入22 次删除
  1. 3 2
      lib/docs/core/doc.rb
  2. 1 1
      lib/docs/filters/core/entries.rb
  3. 53 18
      test/lib/docs/core/doc_test.rb
  4. 7 1
      test/lib/docs/filters/core/entries_test.rb

+ 3 - 2
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

+ 1 - 1
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

+ 53 - 18
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

+ 7 - 1
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