From 714c540e4b40c931be365d12c31bbe9cbdfa5fb9 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Wed, 27 Feb 2013 21:15:05 +0100 Subject: reworked parts of the template finder The template finder is now the instance to ask for templates. It was already looking for them, so it should be able to handle questions regarding the existence of templates too. --- .../exist_for_types_predicate_spec.rb | 25 +++++++++ .../template_finder/exist_predicate_spec.rb | 15 +++++ .../unit/zero/renderer/template_finder/get_spec.rb | 27 +++++++++ .../renderer/template_finder/get_templates_spec.rb | 65 ---------------------- .../renderer/template_finder/templates_spec.rb | 58 +++++++++++++++++++ 5 files changed, 125 insertions(+), 65 deletions(-) create mode 100644 spec/unit/zero/renderer/template_finder/exist_for_types_predicate_spec.rb create mode 100644 spec/unit/zero/renderer/template_finder/exist_predicate_spec.rb create mode 100644 spec/unit/zero/renderer/template_finder/get_spec.rb delete mode 100644 spec/unit/zero/renderer/template_finder/get_templates_spec.rb create mode 100644 spec/unit/zero/renderer/template_finder/templates_spec.rb (limited to 'spec/unit') diff --git a/spec/unit/zero/renderer/template_finder/exist_for_types_predicate_spec.rb b/spec/unit/zero/renderer/template_finder/exist_for_types_predicate_spec.rb new file mode 100644 index 0000000..794a4c9 --- /dev/null +++ b/spec/unit/zero/renderer/template_finder/exist_for_types_predicate_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe Zero::Renderer::TemplateFinder, '#exist_for_types?' do + subject { described_class.new(template_path, type_map) } + let(:template_path) { 'spec/fixtures/templates/' } + let(:type_map) { {'html' => 'text/html'} } + let(:html_types) { ['text/html'] } + let(:foo_types) { ['foo/bar', 'bar/foo'] } + + it 'returns true when template exists' do + expect(subject.exist_for_types?('index', html_types)).to be(true) + end + + it 'returns false when template does not exist' do + expect(subject.exist_for_types?('not_found', html_types)).to be(false) + end + + it 'returns false when template for types does not exists' do + expect(subject.exist_for_types?('index', foo_types)).to be(false) + end + + it 'returns false when no types are defined' do + expect(subject.exist_for_types?('index', [])).to be(false) + end +end diff --git a/spec/unit/zero/renderer/template_finder/exist_predicate_spec.rb b/spec/unit/zero/renderer/template_finder/exist_predicate_spec.rb new file mode 100644 index 0000000..0444622 --- /dev/null +++ b/spec/unit/zero/renderer/template_finder/exist_predicate_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe Zero::Renderer::TemplateFinder, '#exist?' do + subject { described_class.new(template_path, type_map) } + let(:template_path) { 'spec/fixtures/templates/' } + let(:type_map) { {'html' => 'text/html'} } + + it 'returns true when the template exists' do + expect(subject.exist?('index')).to be(true) + end + + it 'returns false when the template does not exist' do + expect(subject.exist?('not_found')).to be(false) + end +end diff --git a/spec/unit/zero/renderer/template_finder/get_spec.rb b/spec/unit/zero/renderer/template_finder/get_spec.rb new file mode 100644 index 0000000..d4d40e9 --- /dev/null +++ b/spec/unit/zero/renderer/template_finder/get_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe Zero::Renderer::TemplateFinder, '#get' do + subject { described_class.new(template_path, type_map) } + let(:template_path) { 'spec/fixtures/templates/' } + let(:type_map) {{ + 'html' => ['text/html', 'text/xml', '*/*'], + 'json' => ['application/json', 'plain/text'] + }} + let(:html_types) { ['text/html'] } + let(:json_types) { ['application/json'] } + let(:foo_types) { ['foo/bar', 'bar/foo'] } + + it 'returns a Tilt template' do + expect(subject.get('index', html_types)).to be_kind_of(Tilt::Template) + end + + it 'raises an Error when the template is not found' do + expect { subject.get('not_exist', html_types) }.to raise_error( + ArgumentError, /Template 'not_exist' does not exist/) + end + + it 'raises an Error when no type for the template was found' do + expect { subject.get('index', foo_types) }.to raise_error( + ArgumentError, /Template 'index' not found/) + end +end diff --git a/spec/unit/zero/renderer/template_finder/get_templates_spec.rb b/spec/unit/zero/renderer/template_finder/get_templates_spec.rb deleted file mode 100644 index 5928e66..0000000 --- a/spec/unit/zero/renderer/template_finder/get_templates_spec.rb +++ /dev/null @@ -1,65 +0,0 @@ -require 'spec_helper' - -describe Zero::Renderer::TemplateFinder, '#initialize' do - subject { described_class.new(template_path, type_map) } - let(:template_path) { 'foo/' } - let(:file_list) { ['foo/welcome/index.html.erb'] } - - before :each do - Dir.stub(:[]) do |arg| - if arg == 'foo/**/*.*' - file_list - else - [] - end - end - end - - shared_examples_for 'a template loader' do - it 'creates a template tree' do - subject.get_templates['welcome/index'].should eq(result) - end - end - - context 'without mapping' do - let(:type_map) { {} } - let(:result) { { 'html' => 'foo/welcome/index.html.erb' } } - - it_behaves_like 'a template loader' - end - - context 'with a single mapping' do - let(:type_map) { {'html' => 'text/html' } } - let(:result) { { 'text/html' => 'foo/welcome/index.html.erb' } } - - it_behaves_like 'a template loader' - end - - context 'with multiple mappings' do - let(:type_map) { {'html' => ['text/html', 'text/xml'] } } - let(:result) { { - 'text/html' => 'foo/welcome/index.html.erb', - 'text/xml' => 'foo/welcome/index.html.erb' - } } - - it_behaves_like 'a template loader' - end - - context 'with default template' do - let(:file_list) {['foo/welcome/index.erb']} - let(:type_map) { {'default' => ['text/html', 'text/xml'] } } - let(:result) { { - 'text/html' => 'foo/welcome/index.erb', - 'text/xml' => 'foo/welcome/index.erb' - } } - - it_behaves_like 'a template loader' - end - - it 'creates an empty templates list without templates in path' do - subject = Zero::Renderer.new("bar/", {}) - subject.read_template_path! - - subject.templates.should eq({}) - end -end diff --git a/spec/unit/zero/renderer/template_finder/templates_spec.rb b/spec/unit/zero/renderer/template_finder/templates_spec.rb new file mode 100644 index 0000000..e6c0edd --- /dev/null +++ b/spec/unit/zero/renderer/template_finder/templates_spec.rb @@ -0,0 +1,58 @@ +require 'spec_helper' + +describe Zero::Renderer::TemplateFinder, '#initialize' do + subject { described_class.new(template_path, type_map) } + let(:template_path) { 'foo/' } + let(:file_list) { ['foo/welcome/index.html.erb'] } + + before :each do + Dir.stub(:[]) do |arg| + if arg == 'foo/**/*.*' + file_list + else + [] + end + end + end + + shared_examples_for 'a template loader' do + it 'creates a template tree' do + subject.templates['welcome/index'].should eq(result) + end + end + + context 'without mapping' do + let(:type_map) { {} } + let(:result) { { 'html' => 'foo/welcome/index.html.erb' } } + + it_behaves_like 'a template loader' + end + + context 'with a single mapping' do + let(:type_map) { {'html' => 'text/html' } } + let(:result) { { 'text/html' => 'foo/welcome/index.html.erb' } } + + it_behaves_like 'a template loader' + end + + context 'with multiple mappings' do + let(:type_map) { {'html' => ['text/html', 'text/xml'] } } + let(:result) { { + 'text/html' => 'foo/welcome/index.html.erb', + 'text/xml' => 'foo/welcome/index.html.erb' + } } + + it_behaves_like 'a template loader' + end + + context 'with default template' do + let(:file_list) {['foo/welcome/index.erb']} + let(:type_map) { {'default' => ['text/html', 'text/xml'] } } + let(:result) { { + 'text/html' => 'foo/welcome/index.erb', + 'text/xml' => 'foo/welcome/index.erb' + } } + + it_behaves_like 'a template loader' + end +end -- cgit v1.2.3-70-g09d2