# # File 'document_spec.rb' created on 13 gen 2008 at 17:46:37. # See 'dokkit.rb' or +LICENSE+ for licence information. # # (c)2006, 2007 Andrea Fazzi (and contributors). # # To execute this spec run: # # spec spec/document_spec.rb # $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__),'../lib')) require 'rubygems' require 'spec' require 'dokkit' require 'dokkit/document' require 'dokkit/pathhelper' require 'dokkit/filters/all' require File.dirname(__FILE__) + '/spec_helper.rb' describe 'a configured Document instance', :shared => true do include SpecPathHelper, CaptureStdout before(:all) do @initial_dir = Dir.pwd Dir.chdir(File.join(File.dirname(__FILE__), SpecPathHelper::DATA_TEST_DIR)) end before do @cache = Dokkit::CacheManager.instance @cache.init('deps.yaml', '.cache') @config = mock('config') @config.should_receive(:document_dir).at_least(1).and_return(document_path) @config.should_receive(:config_dir).at_least(1).and_return(config_path) @config.should_receive(:layout_dir).at_least(1).and_return(layout_path) @config.should_receive(:output_dir).at_least(1).and_return(output_path) end after(:all) do Dir.chdir(@initial_dir) end end describe Dokkit::Document, ' when initialized with document_1' do it_should_behave_like 'a configured Document instance' before do @document = Dokkit::Document.new(document_path('document_1'), @config) end it 'should be initialized with the name of the source file' do @document.document.should == document_path('document_1') end it 'should get the basename of the source file' do @document.document_basename.should == 'document_1' end it 'should resolve its configuration files' do @document.configs.should == [ config_path('document_1.yaml'), document_path('COMMON.yaml'), document_path('document_1.yaml'), config_path('required.yaml') ] end it 'should read the configuration files and fill a configuration hash' do @document.configuration.should_not be_empty end it 'should read document source file' do @document.source_text.should =~ /\* Title for document_1/ end it 'should define output targets' do @document.targets['html'].should == output_path('document_1.html') @document.targets['tex'].should == output_path('latex/document_1.latex') end it 'should define layouts grouped by output target' do @document.layouts['html'].should == [layout_path('document_1.html')] @document.layouts['tex'].should == [layout_path('document_1.latex')] end it 'should know the filters to use for rendering in the given output format' do @document.filter_chain['html'].should == ['deplate-html'] @document.filter_chain['tex'].should == ['deplate-latex'] end it 'should return the list of prerequisites grouped by output format' do @document.prerequisites['html'].should include( document_path('document_1'), config_path('document_1.yaml'), config_path('required.yaml'), document_path('document_1.yaml'), document_path('COMMON.yaml'), layout_path('document_1.html') ) end describe ' and rendered in html' do before do @result = @document.render('html') end it 'should execute embedded erb code and render the document' do @result.should =~ /test embedded erb/ end it 'should render the content of the configuration hash' do @result.should =~ /test configuration rendering/ @result.should =~ /test the correct merge/ end it 'should return a default value if a configuration key is not found' do @document.configuration['notexist'].should match(/'notexist' not defined/) end it 'should know which is the current format' do @result.should =~ /Current rendering format is: html/ end it 'should apply filters to render in html format' do @result.should =~ /\.*\<\/h1\>/ end it 'should render partial inside it' do @result.should =~ /partial should be rendered/ end end describe ' and rendered in tex' do before do @result = @document.render('tex') end it 'should apply filters to render in html format' do @result.should =~ /\\section\{.*\}/ end end end describe Dokkit::Document, ' when initialized with document_nolayout.ext' do it_should_behave_like 'a configured Document instance' before do @document = Dokkit::Document.new(document_path('document_nolayout.ext'), @config) end it 'should define output targets' do @document.targets['html'].should == output_path('document_nolayout.html') end it 'should set a nil layout' do @document.layouts['html'].should be_empty end it 'should know the filters to use for rendering in the given output format' do @document.filter_chain['html'].should == ['deplate-html'] end it 'should return the list of prerequisites grouped by output format' do @document.prerequisites['html'].should include( document_path('document_nolayout.ext'), document_path('document_nolayout.yaml'), document_path('COMMON.yaml') ) end it 'should resolve its configuration files' do @document = Dokkit::Document.new(document_path('document_nolayout.ext'), @config) @document.configs.should == [ document_path('COMMON.yaml'), document_path('document_nolayout.yaml'), ] end it 'should raise a -layout not found- warning' do capture_stderr do @document = Dokkit::Document.new(document_path('document_nolayout.ext'), @config) end.should match(/Layout not found/) end describe ' and rendered in html' do before do @document = Dokkit::Document.new(document_path('document_nolayout.ext'), @config) @result = @document.render('html') end it 'should apply filters to render in html format' do @result.should =~ /\.*\<\/h1\>/ end end end describe Dokkit::Document, ' when initialized with subdir/document_4' do it_should_behave_like 'a configured Document instance' before do @document = Dokkit::Document.new(document_path('subdir/document_4'), @config) end it 'should get the basename of the source file' do @document.document_basename == 'document_4' end it 'should resolve its configuration files' do @document.configs.should == [ config_path('subdir/document_4.yaml'), File.expand_path(document_path('COMMON.yaml')), document_path('subdir/COMMON.yaml'), ] end it 'should define output targets' do @document.targets['html'].should == output_path('subdir/document_4.html') end it 'should define layouts grouped by output format' do @document.layouts['html'].should == [layout_path('document_1.html')] end it 'should know the filters to use for rendering in the given output format' do @document.filter_chain['html'].should == ['deplate-html'] end it 'should resolve relative path' do @document.relative('/images/image.img').should == '../images/image.img' end describe ' and rendered in html' do before do @result = @document.render('html') end it 'should apply filters to render in html format' do @result.should =~ /\.*\<\/h1\>/ end end end describe Dokkit::Document, ' when initialized with subdir/subdir/document_6' do it_should_behave_like 'a configured Document instance' before do @document = Dokkit::Document.new(document_path('subdir/subdir/document_6'), @config) end it 'should resolve its configuration files' do @document.configs.should == [ File.expand_path(document_path('COMMON.yaml')), File.expand_path(document_path('subdir/COMMON.yaml')), document_path('subdir/subdir/COMMON.yaml'), ] end it 'should define output targets' do @document.targets['html'].should == output_path('subdir/subdir/document_6.html') end it 'should define layouts grouped by output target' do @document.layouts['html'].should == [layout_path('document_1.html')] end end describe Dokkit::Document, ' when initialized with document_7' do it_should_behave_like 'a configured Document instance' before do @document = Dokkit::Document.new(document_path('document_7'), @config) end it 'should resolve its configuration files' do @document.configs.should == [ document_path('COMMON.yaml'), document_path('document_7.yaml'), ] end it 'should define output targets' do @document.targets['html'].should == output_path('document_7.html') end it 'should define layouts grouped by output target' do @document.layouts['html'].should == [layout_path('document_1.html')] end describe ' and rendered in html using maruku filter' do before do @result = @document.render('html') end it 'should apply filters to render in html format' do @result.should =~ /\.*\<\/h1\>/ end end describe ' and rendered using a not defined target format' do it 'should raise an error' do lambda { @document.render('notexistent') }.should raise_error(RuntimeError, /Don't know how to render in '.*' format!/) end it 'should warn that a filter was not found' do capture_stderr do @document.render('html') end.should match(/Filter '.*' not found/) end end end describe Dokkit::Document, ' when initialized with document_with_partials' do it_should_behave_like 'a configured Document instance' before do @document = Dokkit::Document.new(document_path('document_with_partials'), @config) end describe ' and rendered in html' do it 'should fail if a circular dependency is detected' do lambda { @document.render('html') }.should raise_error(RuntimeError, /Circular dependency detected in/) end end end describe Dokkit::Document, ' when initialized with document_nested' do it_should_behave_like 'a configured Document instance' before do @result = Dokkit::Document.new(document_path('document_nested'), @config).render('html') end describe ' and rendered in html' do it 'should render nested layout' do @result.should match(/