add specs to test when an error occurs during the rendering of a HAML template

did committed May 22, 2015
commit 772f047d02706547ae23700929d9a8b1ca96ff44
Showing 3 changed files with 71 additions and 1 deletions
locomotive/steam/decorators/template_decorator.rb b/lib/locomotive/steam/decorators/template_decorator.rb +1 -1
@@ @@ -18,7 +18,7 @@ module Locomotive
private
def source_from_template_file
- source = File.open(template_path).read.force_encoding('utf-8')
+ source = File.read(template_path).force_encoding('utf-8')
if match = source.match(FRONTMATTER_REGEXP)
source = match[:template]
spec/unit/decorators/template_decorator_spec.rb +42 -0
@@ @@ -0,0 +1,42 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Decorators::TemplateDecorator do
+
+ let(:template_path) { 'template.liquid' }
+ let(:page) { instance_double('Page', localized_attributes: [], template_path: template_path) }
+ let(:locale) { 'fr' }
+ let(:default_locale) { nil }
+ let(:decorated) { described_class.new(page, locale, default_locale) }
+
+ describe '#liquid_source' do
+
+ let(:content) { 'Lorem ipsum' }
+
+ before { allow(File).to receive(:read).and_return(content) }
+
+ subject { decorated.liquid_source.strip }
+
+ it { is_expected.to eq 'Lorem ipsum' }
+
+ context 'HAML file' do
+
+ let(:template_path) { 'template.liquid.haml' }
+ let(:content) { '%p Lorem ipsum' }
+
+ it { is_expected.to eq '<p>Lorem ipsum</p>' }
+
+ context 'incorrect HAML syntax' do
+
+ let(:content) { "foo\n %p TEST" }
+
+ it 'raises an error' do
+ expect { subject }.to raise_error Locomotive::Steam::RenderError
+ end
+
+ end
+
+ end
+
+ end
+
+ end
spec/unit/errors_spec.rb +28 -0
@@ @@ -0,0 +1,28 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::RenderError do
+
+ let(:message) { 'Wrong syntax' }
+ let(:file) { 'template.liquid.haml' }
+ let(:source) { %w(a b c d e f g h i j k l m n o p q r s t u v w y z).join("\n") }
+ let(:line) { 10 }
+ let(:backtrace) { 'Backtrace' }
+ let(:error) { described_class.new(message, file, source, line, backtrace) }
+
+ describe '#code_lines' do
+
+ subject { error.code_lines }
+
+ it { is_expected.to eq [[5, 'f'], [6, 'g'], [7, 'h'], [8, 'i'], [9, 'j'], [10, 'k'], [11, 'l'], [12, 'm'], [13, 'n'], [14, 'o'], [15, 'p']] }
+
+ end
+
+ describe '#backtrace' do
+
+ subject { error.original_backtrace }
+
+ it { is_expected.to eq 'Backtrace' }
+
+ end
+
+ end