# -*- coding: utf-8 -*- require "test_helper" require "support/document_xml_helper" require "support/xml_snippets" class ProcessorTest < Sablon::TestCase include DocumentXMLHelper include XMLSnippets def setup super @processor = Sablon::Processor end def test_simple_field_replacement result = process(snippet("simple_field"), {"first_name" => "Jack"}) assert_equal "Hello! My Name is Jack , nice to meet you.", text(result) assert_xml_equal <<-document, result Hello! My Name is Jack , nice to meet you. document end def test_context_can_contain_string_and_symbol_keys result = process(snippet("simple_fields"), {"first_name" => "Jack", last_name: "Davis"}) assert_equal "Jack Davis", text(result) end def test_complex_field_replacement result = process(snippet("complex_field"), {"last_name" => "Zane"}) assert_equal "Hello! My Name is Zane , nice to meet you.", text(result) assert_xml_equal <<-document, result Hello! My Name is Zane , nice to meet you. document end def test_complex_field_replacement_with_split_field result = process(snippet("edited_complex_field"), {"first_name" => "Daniel"}) assert_equal "Hello! My Name is Daniel , nice to meet you.", text(result) assert_xml_equal <<-document, result Hello! My Name is Daniel , nice to meet you. document end def test_paragraph_block_replacement result = process(snippet("paragraph_loop"), {"technologies" => ["Ruby", "Rails"]}) assert_equal "Ruby Rails", text(result) assert_xml_equal <<-document, result Ruby Rails document end def test_paragraph_block_within_table_cell result = process(snippet("paragraph_loop_within_table_cell"), {"technologies" => ["Puppet", "Chef"]}) assert_equal "Puppet Chef", text(result) assert_xml_equal <<-document, result Puppet Chef document end def test_paragraph_block_within_empty_table_cell_and_blank_replacement result = process(snippet("paragraph_loop_within_table_cell"), {"technologies" => []}) assert_equal "", text(result) assert_xml_equal <<-document, result document end def test_adds_blank_paragraph_to_empty_table_cells result = process(snippet("corrupt_table"), {}) assert_xml_equal <<-document, result Hans 1. Chef document end def test_single_row_table_loop item = Struct.new(:index, :label, :rating) result = process(snippet("table_row_loop"), {"items" => [item.new("1.", "Milk", "***"), item.new("2.", "Sugar", "**")]}) assert_xml_equal <<-document, result 1. Milk *** 2. Sugar ** document end def test_loop_over_collection_convertable_to_an_enumerable style_collection = Class.new do def to_ary ["CSS", "SCSS", "LESS"] end end result = process(snippet("paragraph_loop"), {"technologies" => style_collection.new}) assert_equal "CSS SCSS LESS", text(result) end def test_loop_over_collection_not_convertable_to_an_enumerable_raises_error not_a_collection = Class.new {} assert_raises Sablon::ContextError do process(snippet("paragraph_loop"), {"technologies" => not_a_collection.new}) end end def test_loop_with_missing_variable_raises_error e = assert_raises Sablon::ContextError do process(snippet("paragraph_loop"), {}) end assert_equal "The expression «technologies» should evaluate to an enumerable but was: nil", e.message end def test_loop_with_missing_end_raises_error e = assert_raises Sablon::TemplateError do process(snippet("loop_without_ending"), {}) end assert_equal "Could not find end field for «technologies:each(technology)». Was looking for «technologies:endEach»", e.message end def test_conditional_with_missing_end_raises_error e = assert_raises Sablon::TemplateError do process(snippet("conditional_without_ending"), {}) end assert_equal "Could not find end field for «middle_name:if». Was looking for «middle_name:endIf»", e.message end def test_multi_row_table_loop item = Struct.new(:index, :label, :body) context = {"foods" => [item.new("1.", "Milk", "Milk is a white liquid."), item.new("2.", "Sugar", "Sugar is the generalized name for carbohydrates.")]} result = process(snippet("table_multi_row_loop"), context) assert_equal "1. Milk Milk is a white liquid. 2. Sugar Sugar is the generalized name for carbohydrates.", text(result) end def test_conditional result = process(snippet("conditional"), {"middle_name" => "Michael"}) assert_equal "Anthony Michael Hall", text(result) result = process(snippet("conditional"), {"middle_name" => nil}) assert_equal "Anthony Hall", text(result) end def test_simple_field_conditional_inline result = process(snippet("conditional_inline"), {"middle_name" => true}) assert_equal "Anthony Michael Hall", text(result) end def test_complex_field_conditional_inline with_false = process(snippet("complex_field_inline_conditional"), {"boolean" => false}) assert_equal "ParagraphBefore Before After ParagraphAfter", text(with_false) with_true = process(snippet("complex_field_inline_conditional"), {"boolean" => true}) assert_equal "ParagraphBefore Before Content After ParagraphAfter", text(with_true) end def test_conditional_with_predicate result = process(snippet("conditional_with_predicate"), {"body" => ""}) assert_equal "some content", text(result) result = process(snippet("conditional_with_predicate"), {"body" => "not empty"}) assert_equal "", text(result) end private def process(document, context) @processor.process(wrap(document), context).to_xml end end