# -*- coding: utf-8 -*-
require "test_helper"
require 'securerandom'
class HTMLConverterASTTest < Sablon::TestCase
def setup
super
@converter = Sablon::HTMLConverter.new
@converter.instance_variable_set(:@env, Sablon::Environment.new(nil))
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_a
input = 'Lorem google
'
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 %w[1001 1001 1002 1003 1003], get_numpr_prop_from_ast(ast, :numId)
end
def test_nested_lists_have_the_same_numid
ast = @converter.processed_ast('')
assert_equal %w[1001 1001 1001], get_numpr_prop_from_ast(ast, :numId)
end
def test_keep_nested_list_order
input = ''
ast = @converter.processed_ast(input)
assert_equal %w[1001], get_numpr_prop_from_ast(ast, :numId).uniq
assert_equal %w[0 1 2 1 0 1 2], get_numpr_prop_from_ast(ast, :ilvl)
end
def test_table_tag
input=''
ast = @converter.processed_ast(input)
assert_equal ']>', ast.inspect
end
def test_table_with_table_row
input=''
ast = @converter.processed_ast(input)
assert_equal ']>]>', ast.inspect
end
def test_table_with_table_row_and_table_cell
input=''
ast = @converter.processed_ast(input)
assert_equal ']>]>]>]>]>', ast.inspect
end
def test_table_with_table_row_and_table_cell_and_caption
input=''
ast = @converter.processed_ast(input)
assert_equal ']>, []>]>]>]>]>', ast.inspect
#
input=''
ast = @converter.processed_ast(input)
assert_equal ']>]>]>], ]>>]>', ast.inspect
end
private
# returns the numid attribute from paragraphs
def get_numpr_prop_from_ast(ast, key)
values = []
ast.grep(Sablon::HTMLConverter::ListParagraph).each do |para|
numpr = para.instance_variable_get('@properties')[:numPr]
numpr.each { |val| values.push(val[key]) if val[key] }
end
values
end
end