# -*- coding: utf-8 -*-
require "test_helper"
require "support/document_xml_helper"
module MailMergeParser
module SharedBehavior
include DocumentXMLHelper
def setup
super
@parser = Sablon::Parser::MailMerge.new
end
def fields
@document = xml
@parser.parse_fields(@document)
end
def body_xml
@document.search(".//w:body").children.map(&:to_xml).join.strip
end
end
class FldSimpleTest < Sablon::TestCase
include SharedBehavior
def test_recognizes_expression
assert_equal ["=first_name"], fields.map(&:expression)
end
def test_replace
field = fields.first
field.replace("Hello")
assert_equal <<-body_xml.strip, body_xml
Hello
body_xml
end
def test_replace_with_newlines
field = fields.first
field.replace("First\nSecond\n\nThird")
assert_equal <<-body_xml.strip, body_xml
FirstSecondThird
body_xml
end
private
def xml
wrap(<<-xml)
«=first_name»
xml
end
end
class FldCharTest < Sablon::TestCase
include SharedBehavior
def test_recognizes_expression
assert_equal ["=last_name"], fields.map(&:expression)
end
def test_replace
field = fields.first
field.replace("Hello")
assert_equal <<-body_xml.strip, body_xml
Hello
body_xml
end
def test_replace_with_newlines
field = fields.first
field.replace("First\nSecond\n\nThird")
assert_equal <<-body_xml.strip, body_xml
FirstSecondThird
body_xml
end
private
def xml
wrap(<<-xml)
MERGEFIELD =last_name \\* MERGEFORMAT
«=last_name»
xml
end
end
end