From 93fcd5687c7e349b019c4a076fb88254ba934614 Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Thu, 22 Aug 2013 12:12:35 -0400 Subject: [PATCH] Broken warnings implementation. --- lib/liquid/block.rb | 15 +++++++++++++++ lib/liquid/tag.rb | 4 ++++ lib/liquid/template.rb | 8 ++++++++ test/liquid/error_handling_test.rb | 6 +++--- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/liquid/block.rb b/lib/liquid/block.rb index 737fadf2..e4b2398b 100644 --- a/lib/liquid/block.rb +++ b/lib/liquid/block.rb @@ -56,6 +56,21 @@ module Liquid assert_missing_delimitation! end + # warnings of this block and all sub-tags + def warnings + all_warnings = [] + all_warnings.concat(@warnings) if @warnings + + return all_warnings unless @nodelist + @nodelist.each do |node| + p node + node_warns = node.respond_to?(:warnings) ? node.warnings : nil + all_warnings.concat(node_warns) if node_warns + end + + all_warnings + end + def end_tag end diff --git a/lib/liquid/tag.rb b/lib/liquid/tag.rb index ce0df63a..da951e02 100644 --- a/lib/liquid/tag.rb +++ b/lib/liquid/tag.rb @@ -33,6 +33,10 @@ module Liquid @blank || true end + def warnings + @warnings + end + def parse_with_selected_parser(markup) case @options[:error_mode] || Template.error_mode when :strict then strict_parse_with_error_context(markup) diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index b43a2a38..4617e1e8 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -69,9 +69,17 @@ module Liquid # Returns self for easy chaining def parse(source, options = {}) @root = Document.new(tokenize(source), options) + @warnings = nil self end + # memoize because the warnings operation + # could be expensive. + def warnings + return [] unless @root + @warnings ||= @root.warnings + end + def registers @registers ||= {} end diff --git a/test/liquid/error_handling_test.rb b/test/liquid/error_handling_test.rb index 80902a31..6ac2e490 100644 --- a/test/liquid/error_handling_test.rb +++ b/test/liquid/error_handling_test.rb @@ -92,10 +92,10 @@ class ErrorHandlingTest < Test::Unit::TestCase end def test_warnings - template = Liquid::Template.parse('{% if ~~~ %}derp{% else %}wat{% endif %}', :error_mode => :warn) + template = Liquid::Template.parse('{% if ~~~ %}{{%%%}}{% else %}wat{% endif %}', :error_mode => :warn) + assert_equal 2, template.warnings.size + assert_equal 'Unexpected character ~ in "~~~"', template.warnings.first.message assert_equal 'wat', template.render - assert_equal 1, template.errors.size - assert_equal 'Unexpected character ~ in "~~~"', template.errors.first.message end # Liquid should not catch Exceptions that are not subclasses of StandardError, like Interrupt and NoMemoryError