From 7fd2f6b25b2abd48a7292f7598e53ebd357e24f7 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Wed, 27 Feb 2013 22:27:02 +0100 Subject: reworked the renderer The renderer is now a bit smaller and asks the TemplateFinder for the actual resources. That way it can just concentrate on rendering instead of finding out, which stuff actually exists and which not. --- spec/unit/zero/renderer/initialize_spec.rb | 11 ++++ .../zero/renderer/read_template_path_bang_spec.rb | 16 ----- spec/unit/zero/renderer/render_spec.rb | 71 +++++++++++++--------- spec/unit/zero/renderer/template_path.rb | 8 --- spec/unit/zero/renderer/templates_spec.rb | 13 ++++ spec/unit/zero/renderer/type_map_spec.rb | 13 ---- 6 files changed, 65 insertions(+), 67 deletions(-) create mode 100644 spec/unit/zero/renderer/initialize_spec.rb delete mode 100644 spec/unit/zero/renderer/read_template_path_bang_spec.rb delete mode 100644 spec/unit/zero/renderer/template_path.rb create mode 100644 spec/unit/zero/renderer/templates_spec.rb delete mode 100644 spec/unit/zero/renderer/type_map_spec.rb (limited to 'spec/unit') diff --git a/spec/unit/zero/renderer/initialize_spec.rb b/spec/unit/zero/renderer/initialize_spec.rb new file mode 100644 index 0000000..39d7302 --- /dev/null +++ b/spec/unit/zero/renderer/initialize_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Zero::Renderer, '#initialize' do + subject { described_class.new(template_path, type_map) } + let(:template_path) { 'foo/' } + let(:type_map) { {'html' => ['text/html'] } } + + its(:path) { should be(template_path) } + its(:layout) { should match(/layout/) } + its(:types) { should be(type_map) } +end diff --git a/spec/unit/zero/renderer/read_template_path_bang_spec.rb b/spec/unit/zero/renderer/read_template_path_bang_spec.rb deleted file mode 100644 index 9a2f875..0000000 --- a/spec/unit/zero/renderer/read_template_path_bang_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'spec_helper' - -describe Zero::Renderer, '#read_template_path!' do - subject { Zero::Renderer.new(template_path, type_map) } - let(:template_path) { 'spec/fixtures/templates/' } - let(:type_map) { {'html' => ['text/html']} } - let(:result) { { - 'text/html' => template_path + 'index.html.erb', - 'json' => template_path + 'index.json.erb' - } } - - it "loads the templates" do - subject.read_template_path! - expect(subject.templates['index']).to eq(result) - end -end diff --git a/spec/unit/zero/renderer/render_spec.rb b/spec/unit/zero/renderer/render_spec.rb index 3270ac3..ed11857 100644 --- a/spec/unit/zero/renderer/render_spec.rb +++ b/spec/unit/zero/renderer/render_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Zero::Renderer, '#render' do - subject { Zero::Renderer.new(template_path, type_map) } + subject { described_class.new(template_path, type_map) } let(:template_path) { 'spec/fixtures/templates/' } let(:type_map) {{ 'html' => ['text/html', 'text/xml', '*/*'], @@ -12,43 +12,54 @@ describe Zero::Renderer, '#render' do let(:foo_types) { ['foo/bar', 'bar/foo'] } let(:binding) { SpecTemplateContext.new('foo') } - before :each do - subject.read_template_path! - end + context 'with default layout' do + it 'returns a tilt template' do + subject.render('index', html_types, binding).should be_kind_of(String) + end - it 'returns a tilt template' do - subject.render('index', html_types, binding).should be_kind_of(String) - end + it 'renders html content' do + subject.render('index', html_types, binding).should match('success') + end - it 'renders html content' do - subject.render('index', html_types, binding).should match('success') - end + it 'returns a tilt template for different types' do + subject.render('index', json_types, binding).should be_kind_of(String) + end - it 'returns a tilt template for different types' do - subject.render('index', json_types, binding).should be_kind_of(String) - end + it 'renders json content' do + subject.render('index', json_types, binding).should match("{text: 'success'}") + end - it 'renders json content' do - subject.render('index', json_types, binding).should match("{text: 'success'}") - end + it 'returns an ArgumentError, if given template does not exist' do + expect { + subject.render('foobar', html_types, binding) + }.to raise_error(ArgumentError, /Template 'foobar' does not exist/) + end - it 'returns an ArgumentError, if given template does not exist' do - expect { - subject.render('foobar', html_types, binding) - }.to raise_error(ArgumentError, "No template found for 'foobar'!") - end + it 'returns an ArgumentError, if no template fits types' do + expect { + subject.render('index', foo_types, binding) + }.to raise_error( + ArgumentError, /Template 'index' not found/) + end - it 'returns an ArgumentError, if no template fits types' do - expect { - subject.render('index', foo_types, binding) - }.to raise_error( - ArgumentError, - "No template found for any of this types #{foo_types.join ', '}!" - ) + it 'uses the context' do + subject.render('context', html_types, binding).should match('foo') + + end end - it 'uses the context' do - subject.render('context', html_types, binding).should match('foo') + context 'with special layout' do + subject { described_class.new(template_path, layout, type_map) } + let(:layout) { 'special_layout' } + + it 'uses the layout for rendering' do + expect(subject.render('index', html_types, binding) + ).to match(/layout loaded/) + end + it 'renders the template into the layout' do + expect(subject.render('index', html_types, binding) + ).to match(/success/) + end end end diff --git a/spec/unit/zero/renderer/template_path.rb b/spec/unit/zero/renderer/template_path.rb deleted file mode 100644 index 261faa8..0000000 --- a/spec/unit/zero/renderer/template_path.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'spec_helper' - -describe Zero::Renderer, '#template_path' do - subject { Zero::Renderer.new(template_path) } - let(:template_path) { 'foo' } - - its(:type_map) { should be(template_path) } -end diff --git a/spec/unit/zero/renderer/templates_spec.rb b/spec/unit/zero/renderer/templates_spec.rb new file mode 100644 index 0000000..f86961b --- /dev/null +++ b/spec/unit/zero/renderer/templates_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +describe Zero::Renderer, '#templates' do + let(:object) { described_class.new(template_path, types) } + subject { object.templates } + + let(:template_path) { 'spec/fixtures/templates/' } + let(:types) { {'html' => ['text/html']} } + + it 'loads the template tree' do + expect(subject).to be_kind_of(Zero::Renderer::TemplateFinder) + end +end diff --git a/spec/unit/zero/renderer/type_map_spec.rb b/spec/unit/zero/renderer/type_map_spec.rb deleted file mode 100644 index 290e579..0000000 --- a/spec/unit/zero/renderer/type_map_spec.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'spec_helper' - -describe Zero::Renderer, '#type_map' do - subject { Zero::Renderer.new(template_path, type_map) } - let(:template_path) { 'foo' } - let(:type_map) { {'html' => ['text/html']} } - - its(:type_map) { should be(type_map) } - - it 'returns an empty Hash, if type_map is not set while initialization' do - Zero::Renderer.new(template_path).type_map.should eq({}) - end -end -- cgit v1.2.3-70-g09d2