# -*- coding: utf-8 -*-
require "test_helper"
class HTMLConverterTest < Sablon::TestCase
def setup
super
@converter = Sablon::HTMLConverter.new
end
def test_convert_text_inside_div
input = '
Lorem ipsum dolor sit amet
'
expected_output = <<-DOCX.strip
Lorem ipsum dolor sit amet
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
end
def test_convert_text_inside_p
input = 'Lorem ipsum dolor sit amet
'
expected_output = <<-DOCX.strip
Lorem ipsum dolor sit amet
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
end
def test_convert_text_inside_multiple_divs
input = 'Lorem ipsum
dolor sit amet
'
expected_output = <<-DOCX.strip
Lorem ipsum
dolor sit amet
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
end
def test_convert_newline_inside_div
input = 'Lorem ipsum
dolor sit amet
'
expected_output = <<-DOCX.strip
Lorem ipsum
dolor sit amet
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
end
def test_convert_strong_tags_inside_div
input = 'Lorem ipsum dolor sit amet
'
expected_output = <<-DOCX.strip
Lorem
ipsum dolor
sit amet
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
end
def test_convert_u_tags_inside_p
input = 'Lorem ipsum dolor sit amet'
expected_output = <<-DOCX.strip
Lorem
ipsum dolor
sit amet
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
end
def test_convert_em_tags_inside_div
input = '
Lorem ipsum dolor sit amet
'
expected_output = <<-DOCX.strip
Lorem
ipsum dolor
sit amet
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
end
def test_convert_br_tags_inside_strong
input = '
Lorem ipsum
dolor sit amet
'
expected_output = <<-DOCX
Lorem ipsum
dolor sit amet
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
end
def test_convert_h1
input = 'Lorem ipsum dolor
'
expected_output = <<-DOCX.strip
Lorem ipsum dolor
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
end
def test_unorderd_lists
input = ''
expected_output = <<-DOCX.strip
Lorem
ipsum
dolor
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
assert_equal [Sablon::Numbering::Definition.new(1001, 'ListBullet')], Sablon::Numbering.instance.definitions
end
def test_ordered_lists
input = '- Lorem
- ipsum
- dolor
'
expected_output = <<-DOCX.strip
Lorem
ipsum
dolor
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
assert_equal [Sablon::Numbering::Definition.new(1001, 'ListNumber')], Sablon::Numbering.instance.definitions
end
def test_mixed_lists
input = '- Lorem
- dolor
'
expected_output = <<-DOCX.strip
Lorem
ipsum
dolor
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
assert_equal [Sablon::Numbering::Definition.new(1001, 'ListNumber'),
Sablon::Numbering::Definition.new(1002, 'ListBullet'),
Sablon::Numbering::Definition.new(1003, 'ListNumber')], Sablon::Numbering.instance.definitions
end
def test_nested_unordered_lists
input = ''
expected_output = <<-DOCX.strip
Lorem
ipsum
dolor
DOCX
assert_equal normalize_wordml(expected_output), @converter.process(input)
assert_equal [Sablon::Numbering::Definition.new(1001, 'ListBullet')], Sablon::Numbering.instance.definitions
end
private
def normalize_wordml(wordml)
wordml.gsub(/^\s+/, '').tr("\n", '')
end
end
class HTMLConverterASTTest < Sablon::TestCase
def setup
super
@converter = Sablon::HTMLConverter.new
end
def test_div
input = 'Lorem ipsum dolor sit amet
'
ast = @converter.processed_ast(input)
assert_equal ']>]>', ast.inspect
end
def test_p
input = 'Lorem ipsum dolor sit amet
'
ast = @converter.processed_ast(input)
assert_equal ']>]>', ast.inspect
end
def test_b
input = 'Lorem ipsum dolor sit amet
'
ast = @converter.processed_ast(input)
assert_equal ', ]>]>', ast.inspect
end
def test_i
input = 'Lorem ipsum dolor sit amet
'
ast = @converter.processed_ast(input)
assert_equal ', ]>]>', ast.inspect
end
def test_br_in_strong
input = 'Lorem
ipsum
dolor
'
par = @converter.processed_ast(input).grep(Sablon::HTMLConverter::Paragraph).first
assert_equal "[, , , , ]", par.runs.inspect
end
def test_br_in_em
input = 'Lorem
ipsum
dolor
'
par = @converter.processed_ast(input).grep(Sablon::HTMLConverter::Paragraph).first
assert_equal "[, , , , ]", par.runs.inspect
end
def test_nested_strong_and_em
input = 'Lorem ipsum dolor
'
par = @converter.processed_ast(input).grep(Sablon::HTMLConverter::Paragraph).first
assert_equal "[, , ]", par.runs.inspect
end
def test_ignore_last_br_in_div
input = 'Lorem ipsum dolor sit amet
'
par = @converter.processed_ast(input).grep(Sablon::HTMLConverter::Paragraph).first
assert_equal "[]", par.runs.inspect
end
def test_ignore_br_in_blank_div
input = '
'
par = @converter.processed_ast(input).grep(Sablon::HTMLConverter::Paragraph).first
assert_equal "[]", par.runs.inspect
end
def test_headings
input = 'First
Second
Third
'
ast = @converter.processed_ast(input)
assert_equal "]>, ]>, ]>]>", ast.inspect
end
def test_h_with_formatting
input = 'Lorem ipsum dolor sit amet
'
ast = @converter.processed_ast(input)
assert_equal ", , , ]>]>", ast.inspect
end
def test_ul
input = ''
ast = @converter.processed_ast(input)
assert_equal "]>, ]>]>", ast.inspect
end
def test_ol
input = '- Lorem
- ipsum
'
ast = @converter.processed_ast(input)
assert_equal "]>, ]>]>", ast.inspect
end
def test_num_id
ast = @converter.processed_ast('- Some
- Lorem
- dolor
- sit
')
assert_equal [1001, 1001, 1002, 1003, 1003], ast.grep(Sablon::HTMLConverter::ListParagraph).map(&:numid)
end
def test_nested_lists_have_the_same_numid
ast = @converter.processed_ast('')
assert_equal [1001, 1001, 1001], ast.grep(Sablon::HTMLConverter::ListParagraph).map(&:numid)
end
def test_keep_nested_list_order
input = ''
ast = @converter.processed_ast(input)
list_p = ast.grep(Sablon::HTMLConverter::ListParagraph)
assert_equal [1001], list_p.map(&:numid).uniq
assert_equal [0, 1, 2, 1, 0, 1, 2], list_p.map(&:ilvl)
end
end