test/unit/test_render.rb in spontaneous-0.1.0.alpha1 vs test/unit/test_render.rb in spontaneous-0.2.0.alpha1

- old
+ new

@@ -1,30 +1,39 @@ # encoding: UTF-8 -require 'test_helper' +require File.expand_path('../../test_helper', __FILE__) +require 'sinatra/base' class RenderTest < MiniTest::Spec include Spontaneous def setup + @site = setup_site @saved_engine_class = Spontaneous::Render.renderer_class - Schema.reset! end + def teardown + teardown_site Spontaneous::Render.renderer_class = @saved_engine_class end def template_root @template_root ||= File.expand_path(File.join(File.dirname(__FILE__), "../fixtures/templates")) end - context "First render step" do + context "Publish rendering step" do setup do + Content.delete self.template_root = template_root Spontaneous::Render.renderer_class = Spontaneous::Render::PublishingRenderer + class ::Page < Spontaneous::Page + field :title + box :sections1 + box :sections2 + end class ::TemplateClass < Content field :title do def to_epub to_html end @@ -43,16 +52,71 @@ end @content = TemplateClass.new @content.style.should == TemplateClass.default_style @content.title = "The Title" @content.description = "The Description" + @root = ::Page.create(:title => "Home") + @section1 = ::Page.new(:title => "Section 1") + @section2 = ::Page.new(:title => "Section 2") + @section3 = ::Page.new(:title => "Section 3") + @section4 = ::Page.new(:title => "Section 4") + @root.sections1 << @section1 + @root.sections1 << @section2 + @root.sections2 << @section3 + @root.sections2 << @section4 + + @root.sections2.entries.last.set_position(0) + @root.save.reload end teardown do Object.send(:remove_const, :TemplateClass) rescue nil + Object.send(:remove_const, :Page) rescue nil end + should "render strings correctly" do + S::Render.render_string('#{title} {{ Time.now }}', @content, :html, {}).should == "The Title {{ Time.now }}" + end + + should "use a cache for the site root" do + S::Render.with_publishing_renderer do + a = S::Render.render_string('#{root.object_id} #{root.object_id}', @content, :html, {}) + a.should_not == "#{nil.object_id} #{nil.object_id}" + a.split.uniq.length.should == 1 + end + end + + should "iterate through the sections" do + template = '%%{ navigation(%s) do |section, active| }#{section.title}/#{active} %%{ end }' + a = S::Render.render_string(template % "", @section1, :html, {}) + a.should == "Section 1/true Section 2/false Section 4/false Section 3/false " + a = S::Render.render_string(template % "1", @section2, :html, {}) + a.should == "Section 1/false Section 2/true Section 4/false Section 3/false " + a = S::Render.render_string(template % ":section", @section1, :html, {}) + a.should == "Section 1/true Section 2/false Section 4/false Section 3/false " + end + + should "use a cache for navigation pages" do + a = b = c = nil + template = '%{ navigation do |section, active| }#{section.object_id} %{ end }' + a = S::Render.render_string(template, S::Content[@section1.id], :html, {}).strip + b = S::Render.render_string(template, S::Content[@section1.id], :html, {}).strip + a.should_not == b + + S::Render.with_publishing_renderer do + template = '%{ navigation do |section, active| }#{section.object_id} %{ end }' + a = S::Render.render_string(template, S::Content[@section1.id], :html, {}).strip + b = S::Render.render_string(template, S::Content[@section1.id], :html, {}).strip + a.should == b + end + S::Render.with_publishing_renderer do + template = '%{ navigation do |section, active| }#{section.object_id} %{ end }' + c = S::Render.render_string(template, S::Content[@section1.id], :html, {}).strip + end + a.should_not == c + end + should "be able to render themselves to HTML" do @content.render.should == "<html><title>The Title</title><body>The Description</body></html>\n" end should "be able to render themselves to PDF" do @@ -90,13 +154,13 @@ should "only show visible pieces" do child = TemplateClass.new child.title = "Child2 Title" child.description = "Child2 Description" - @content << child - @content.pieces.last.style = TemplateClass.get_style(:this_template) - @content.pieces.last.hide! + @content.bits << child + @content.bits.last.style = TemplateClass.get_style(:this_template) + @content.bits.last.hide! @content.render.should == "<complex>\nThe Title\n<piece><html><title>Child Title</title><body>Child Description</body></html>\n</piece>\n</complex>\n" end end context "boxes" do @@ -128,11 +192,11 @@ field :introduction end class ::AnImage < Content; end AnImage.field :title - AnImage.template '<img>{{title}}</img>' + AnImage.template '<img>#{title}</img>' @root = TemplateClass.new @root.images.introduction = "Images below:" @image1 = AnImage.new @image1.title = "Image 1" @@ -158,11 +222,11 @@ field :introduction end class ::AnImage < Content; end AnImage.field :title - AnImage.template '<img>{{title}}</img>' + AnImage.template '<img>#{title}</img>' @root = TemplateClass.new @root.images_with_template.introduction = "Images below:" @image1 = AnImage.new @image1.title = "Image 1" @@ -272,10 +336,11 @@ @page.image.src.should == "/images/fromage.jpg" @page.render.should =~ /alt="Smelly"/ end end end + context "Request rendering" do setup do self.template_root = template_root class ::PreviewRender < Page @@ -285,11 +350,11 @@ PreviewRender.box :images PreviewRender.field :description, :markdown @page = PreviewRender.new(:title => "PAGE", :description => "DESCRIPTION") # @page.stubs(:id).returns(24) @page.save - @session = ::Rack::MockSession.new(Sinatra::Application) + @session = ::Rack::MockSession.new(::Sinatra::Application) end teardown do Object.send(:remove_const, :PreviewRender) end @@ -298,10 +363,16 @@ setup do Spontaneous::Render.renderer_class = Spontaneous::Render::PreviewRenderer PreviewRender.layout :preview_render end + should "something" do + @now = Time.now + ::Time.stubs(:now).returns(@now) + S::Render.render_string('#{title} {{ Time.now }}', @page, :html, {}).should == "PAGE #{@now.to_s}" + end + should "render all tags & include preview edit markers" do @page.render.should == <<-HTML PAGE <p>DESCRIPTION</p> <!-- spontaneous:previewedit:start:box id:#{@page.images.schema_id} --> @@ -352,8 +423,25 @@ result = Spontaneous.template_engine.request_renderer.new(@root).render_file(@file, nil) result.should == "correct\n" end end + context "variables in templates" do + setup do + Spontaneous::Render.renderer_class = Spontaneous::Render::PublishingRenderer + PreviewRender.layout :variables + PreviewRender.style :variables + + @page.layout = :variables + @first = PreviewRender.new(:title => "first") + @page.images << @first + @page.images.first.style = :variables + end + + should "be passed to page content" do + @page.render(:html, :param => "param").should == "param\n<variable/param/>\n\nlocal\n" + end + end end + end