# -*- coding: utf-8 -*-
require File.join(File.dirname(__FILE__), "..", "test-helper")
describe RD2ODT, "ah_to_xml" do
# not supported.
# it "returns empty string if empty array" do
# RD2ODT.ah_to_xml([]).should == nil
# end
it "returns xml document" do
RD2ODT.ah_to_xml([:br]).should == "
"
RD2ODT.ah_to_xml([:text__p]).should == ""
RD2ODT.ah_to_xml([:text__style_name]).should == ""
RD2ODT.ah_to_xml([:text__sequence_decl,
{
:text__display_outline_level => "0",
:text__name => "Table",
},
]).should ==
''
RD2ODT.ah_to_xml([:text__p, "this is a pen."]).should ==
"this is a pen."
RD2ODT.ah_to_xml([:text__p, '&"><']).should ==
"&"><"
RD2ODT.ah_to_xml([:text__p,
%Q'Look>'.to_sym]).should ==
%Q'Look>'
RD2ODT.ah_to_xml([:text__p,
{:text__style_name => "Text_20_body"},
"this is a pen.",
]).should ==
'this is a pen.'
RD2ODT.ah_to_xml([:office__text,
[:text__p,
{:text__style_name => "Text_20_body"},
"this is a pen.",
],
]).should ==
<
this is a pen.
EOF
RD2ODT.ah_to_xml([:office__body,
{
:abc__def_ghi => "123",
:jkl_mno__pqr => "456",
},
[:office__text,
[:text__p,
{:text__style_name => "Text_20_body"},
"this is a pen.",
],
],
]).should ==
<
this is a pen.
EOF
RD2ODT.ah_to_xml([
[:text__p,
{:text__style_name => "Preformatted_20_Text"},
"text-1",
],
[:text__p,
{:text__style_name => "Preformatted_20_Text"},
"text-2",
],
]).should ==
<text-1
text-2
EOF
end
end
def str_to_treat_input_ary(s)
ary = s.split("\n").map { |l|
l + "\n"
}
# last line does not have "\n" case.
if !/\n\z/.match(s)
ary.last.chomp!
end
return ary
end
describe RD2ODT, "treat_input" do
it "adds '=begin' to first line and adds '=end' to last line if both lines is
not exist." do
actual_str = < "Text_20_body"},
"本文",
]
result = @visitor.apply_to_DocumentElement(nil,
[sub_content,
sub_content])
result.should ==
[:office__document_content,
{
:xmlns__office => "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
:xmlns__style => "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
:xmlns__text => "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
:xmlns__table => "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
:xmlns__draw => "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
:xmlns__fo =>
"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
:xmlns__xlink => "http://www.w3.org/1999/xlink",
:xmlns__dc => "http://purl.org/dc/elements/1.1/",
:xmlns__meta => "urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
:xmlns__number =>
"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
:xmlns__svg =>
"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
:xmlns__chart => "urn:oasis:names:tc:opendocument:xmlns:chart:1.0",
:xmlns__dr3d => "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0",
:xmlns__math => "http://www.w3.org/1998/Math/MathML",
:xmlns__form => "urn:oasis:names:tc:opendocument:xmlns:form:1.0",
:xmlns__script => "urn:oasis:names:tc:opendocument:xmlns:script:1.0",
:xmlns__ooo => "http://openoffice.org/2004/office",
:xmlns__ooow => "http://openoffice.org/2004/writer",
:xmlns__oooc => "http://openoffice.org/2004/calc",
:xmlns__dom => "http://www.w3.org/2001/xml-events",
:xmlns__xforms => "http://www.w3.org/2002/xforms",
:xmlns__xsd => "http://www.w3.org/2001/XMLSchema",
:xmlns__xsi => "http://www.w3.org/2001/XMLSchema-instance",
:xmlns__field =>
"urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:field:1.0",
:office__version => "1.1",
},
[:office__scripts],
[:office__font_face_decls,
[:style__font_face,
{
:style__name => "さざなみ明朝",
:svg__font_family => "さざなみ明朝",
:style__font_family_generic => "roman",
:style__font_pitch => "variable",
}],
[:style__font_face,
{
:style__name => "IPAゴシック",
:svg__font_family => "IPAゴシック",
:style__font_family_generic => "swiss",
:style__font_pitch => "variable",
}],
[:style__font_face,
{
:style__name => "IPAゴシック1",
:svg__font_family => "IPAゴシック",
:style__font_family_generic => "system",
:style__font_pitch => "variable",
}],
], # :office__font_face_decls
[:office__automatic_styles],
[:office__body,
[:office__text,
[:text__sequence_decls,
[:text__sequence_decl,
{
:text__display_outline_level => "0",
:text__name => "Illustration",
}],
[:text__sequence_decl,
{
:text__display_outline_level => "0",
:text__name => "Table",
}],
[:text__sequence_decl,
{
:text__display_outline_level => "0",
:text__name => "Text",
}],
[:text__sequence_decl,
{
:text__display_outline_level => "0",
:text__name => "Drawing",
}],
], # :text__sequence_decls
sub_content,
sub_content,
], # :office__text
], # :office__body
] # :office__document_content
end
it "returns top level document structure with office:automatic-styles" do
sub_content = [:text__p,
{:text__style_name => "Text_20_body"},
"本文",
]
styles = []
styles << ""
styles << ""
styles.each do |s|
@visitor.automatic_styles << REXML::Document.new(s)
end
result = @visitor.apply_to_DocumentElement(nil,
[sub_content,
sub_content])
result.should ==
[:office__document_content,
{
:xmlns__office => "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
:xmlns__style => "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
:xmlns__text => "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
:xmlns__table => "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
:xmlns__draw => "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
:xmlns__fo =>
"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
:xmlns__xlink => "http://www.w3.org/1999/xlink",
:xmlns__dc => "http://purl.org/dc/elements/1.1/",
:xmlns__meta => "urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
:xmlns__number =>
"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
:xmlns__svg =>
"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
:xmlns__chart => "urn:oasis:names:tc:opendocument:xmlns:chart:1.0",
:xmlns__dr3d => "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0",
:xmlns__math => "http://www.w3.org/1998/Math/MathML",
:xmlns__form => "urn:oasis:names:tc:opendocument:xmlns:form:1.0",
:xmlns__script => "urn:oasis:names:tc:opendocument:xmlns:script:1.0",
:xmlns__ooo => "http://openoffice.org/2004/office",
:xmlns__ooow => "http://openoffice.org/2004/writer",
:xmlns__oooc => "http://openoffice.org/2004/calc",
:xmlns__dom => "http://www.w3.org/2001/xml-events",
:xmlns__xforms => "http://www.w3.org/2002/xforms",
:xmlns__xsd => "http://www.w3.org/2001/XMLSchema",
:xmlns__xsi => "http://www.w3.org/2001/XMLSchema-instance",
:xmlns__field =>
"urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:field:1.0",
:office__version => "1.1",
},
[:office__scripts],
[:office__font_face_decls,
[:style__font_face,
{
:style__name => "さざなみ明朝",
:svg__font_family => "さざなみ明朝",
:style__font_family_generic => "roman",
:style__font_pitch => "variable",
}],
[:style__font_face,
{
:style__name => "IPAゴシック",
:svg__font_family => "IPAゴシック",
:style__font_family_generic => "swiss",
:style__font_pitch => "variable",
}],
[:style__font_face,
{
:style__name => "IPAゴシック1",
:svg__font_family => "IPAゴシック",
:style__font_family_generic => "system",
:style__font_pitch => "variable",
}],
], # :office__font_face_decls
[:office__automatic_styles,
*styles.map { |s|
s.to_sym
}
],
[:office__body,
[:office__text,
[:text__sequence_decls,
[:text__sequence_decl,
{
:text__display_outline_level => "0",
:text__name => "Illustration",
}],
[:text__sequence_decl,
{
:text__display_outline_level => "0",
:text__name => "Table",
}],
[:text__sequence_decl,
{
:text__display_outline_level => "0",
:text__name => "Text",
}],
[:text__sequence_decl,
{
:text__display_outline_level => "0",
:text__name => "Drawing",
}],
], # :text__sequence_decls
sub_content,
sub_content,
], # :office__text
], # :office__body
] # :office__document_content
end
end
describe RD2ODT::RD2ODTVisitor, "apply_to_StringElement" do
before do
@visitor = RD2ODT::RD2ODTVisitor.new
end
it "returns text:p element" do
element = RD::StringElement.new("本文\n")
result = @visitor.apply_to_StringElement(element)
result.should == "本文"
end
it "remove \\r and \\n charactor" do
element = RD::StringElement.new("本文1\n本文2\n本文3\n")
result = @visitor.apply_to_StringElement(element)
result.should == "本文1本文2本文3"
element = RD::StringElement.new("本文1\r\n本文2\r\n本文3\r\n")
result = @visitor.apply_to_StringElement(element)
result.should == "本文1本文2本文3"
end
end
describe RD2ODT::RD2ODTVisitor, "apply_to_TextBlock" do
before do
@visitor = RD2ODT::RD2ODTVisitor.new
end
it "returns text:p element" do
sub_content = "本文"
result = @visitor.apply_to_TextBlock(nil, [sub_content])
result.should == [:text__p,
{:text__style_name => "Text_20_body"},
sub_content,
]
end
end
describe RD2ODT::RD2ODTVisitor, "apply_to_Headline" do
before do
@visitor = RD2ODT::RD2ODTVisitor.new
end
it "returns text:list element in '=' level without text:continue-numbering when first '='" do
level = 1
title = "見出し1"
element = RD::Headline.new(level)
element.title << title
result = @visitor.apply_to_Headline(element, element.title)
result.should == [:text__list,
{:text__style_name => "Numbering_20_2"},
[:text__list_item,
[:text__p,
{:text__style_name => "Heading_20_1"},
"見出し1"
],
],
]
@visitor.continue_numbering_headline.should == true
end
it "returns text:list element in '=' level with text:continue-numbering when not first '='" do
level = 1
title = "見出し2"
element = RD::Headline.new(level)
element.title << title
@visitor.continue_numbering_headline = true
result = @visitor.apply_to_Headline(element, element.title)
result.should == [:text__list,
{
:text__style_name => "Numbering_20_2",
:text__continue_numbering => "true",
},
[:text__list_item,
[:text__p,
{:text__style_name => "Heading_20_1"},
"見出し2"
],
],
]
@visitor.continue_numbering_headline.should == true
end
it "returns text:list element in '==' level" do
level = 2
title = "見出し2"
element = RD::Headline.new(level)
element.title << title
result = @visitor.apply_to_Headline(element, element.title)
result.should == [:text__list,
{
:text__style_name => "Numbering_20_2",
:text__continue_numbering => "true",
},
[:text__list_item,
[:text__list,
{:text__continue_numbering => "true"},
[:text__list_item,
[:text__p,
{:text__style_name => "Heading_20_2"},
"見出し2",
],
],
],
],
]
end
it "returns text:list element in '===' level" do
level = 3
title = "見出し3"
element = RD::Headline.new(level)
element.title << title
result = @visitor.apply_to_Headline(element, element.title)
result.should == [:text__list,
{
:text__style_name => "Numbering_20_2",
:text__continue_numbering => "true",
},
[:text__list_item,
[:text__list,
{:text__continue_numbering => "true"},
[:text__list_item,
[:text__list,
{:text__continue_numbering => "true"},
[:text__list_item,
[:text__p,
{:text__style_name => "Heading_20_3"},
"見出し3",
],
],
],
],
],
],
]
end
end
describe RD2ODT::RD2ODTVisitor, "apply_to_EnumListItem" do
before do
@visitor = RD2ODT::RD2ODTVisitor.new
end
it "returns text:list-item element" do
result = @visitor.apply_to_EnumListItem(nil, [:sub_content_1,
:sub_content_2])
result.should == [:text__list_item,
:sub_content_1,
:sub_content_2,
]
end
end
describe RD2ODT::RD2ODTVisitor, "apply_to_EnumList" do
before do
@visitor = RD2ODT::RD2ODTVisitor.new
end
it "returns text:list element" do
result = @visitor.apply_to_EnumList(nil, [:sub_content_1,
:sub_content_2])
result.should == [:text__list,
{
:text__style_name => "Numbering_20_1",
:text__continue_numbering => "false",
},
:sub_content_1,
:sub_content_2,
]
end
end
describe RD2ODT::RD2ODTVisitor, "apply_to_ItemListItem" do
before do
@visitor = RD2ODT::RD2ODTVisitor.new
end
# same as apply_to_EnumListItem
it "returns text:list-item element" do
result = @visitor.apply_to_ItemListItem(nil, [:sub_content_1,
:sub_content_2])
result.should == [:text__list_item,
:sub_content_1,
:sub_content_2,
]
end
end
describe RD2ODT::RD2ODTVisitor, "apply_to_ItemList" do
before do
@visitor = RD2ODT::RD2ODTVisitor.new
end
it "returns text:list element" do
result = @visitor.apply_to_ItemList(nil, [:sub_content_1,
:sub_content_2])
result.should == [:text__list,
{:text__style_name => "List_20_1"},
:sub_content_1,
:sub_content_2,
]
end
end
describe RD2ODT::RD2ODTVisitor, "apply_to_Verbatim" do
before do
@visitor = RD2ODT::RD2ODTVisitor.new
end
it "returns text:list element" do
content_strings = [
"This is verbatim element.\n",
" " * 2 + "Lo" + " " * 5 + "ok!\n",
"\tLo" + "\t\t" + "ok!\n",
"In the last line, text:line-break is not exist.\n",
]
element = RD::Verbatim.new(content_strings)
result = @visitor.apply_to_Verbatim(element)
result.should == [:text__p,
{:text__style_name => "Preformatted_20_Text"},
[
"This is verbatim element.",
'Look!Look!',
"In the last line, text:line-break is not exist.",
].join("").to_sym,
]
end
end
describe RD2ODT::RD2ODTVisitor, "apply_to_Include" do
before do
class Time
class << self
alias orig_now now
def now
return Time.mktime(2008, 12, 30, 12, 34, 56, 78901)
end
end
end
@name_prefix = sprintf("rd2odt:%d:%06d:%d:",
Time.now.tv_sec, Time.now.tv_usec, 1)
@visitor = RD2ODT::RD2ODTVisitor.new
sample_dir_path = File.join($top_srcdir, "doc", "sample")
tree_mock = Struct.new(:include_paths).new([sample_dir_path])
@parent_element = Struct.new(:tree).new(tree_mock)
end
after do
class Time
class << self
alias now orig_now
end
end
end
it "includes other document" do
element = RD::Include.new("include-file-simple-text.odt")
element.parent = @parent_element
result = @visitor.apply_to_Include(element)
result[0].should == <これは<<<のサンプルです.
EOF
result[1].should == <単純なテキストのincludeサンプルです.
EOF
result[2].should == <
EOF
result.length.should == 3
result.class.should == Array
@visitor.number_of_include_files.should == 1
@visitor.additional_styles[0].to_s.should == <
EOF
@visitor.additional_styles[1].to_s.should == <
EOF
# additional_styles[2..6] test is omitted.
@visitor.additional_styles.length.should == 7
@visitor.additional_styles.class == Array
end
=begin
it "includes other document" do
element = RD::Include.new("include-file-simple-styled-text.odt")
element.parent = @parent_element
result = @visitor.apply_to_Include(element)
result[0].should == <これは<<<のサンプルです.
EOF
result[1].should == <標準: 単純なテキストのincludeサンプルです.
EOF
result[2].should == <
EOF
result[3].should == <見出し1
EOF
result[4].should == <見出し1.1
EOF
result[5].should == <本文
EOF
result[6].should == <
EOF
result[7].should == <書式設定前テキスト
EOF
result[8].should == <
EOF
result[9].should == <文字用書式スタイルのincludeテスト
EOF
result[10].should == <強調標準
EOF
result[11].should == <変数標準
EOF
result[12].should == <等幅フォント標準
EOF
result[13].should == <
EOF
result.length.should == 14
result.class.should == Array
end
=end
it "includes other document which containts table:table" do
element = RD::Include.new("include-file-table.odt")
element.parent = @parent_element
result = @visitor.apply_to_Include(element)
result[0].should == <これは<<<のサンプルです.
EOF
result[1].should == <
表つき文書の
埋め込み
サンプル
1
2
3
EOF
result[2].should == <
EOF
result.length.should == 3
result.class.should == Array
@visitor.automatic_styles[0].to_s.should == <
EOF
@visitor.automatic_styles[1].to_s.should == <
EOF
# automatic_styles[2..5] test is omitted.
@visitor.automatic_styles.length.should == 6
@visitor.automatic_styles.class.should == Array
end
it "includes other document which containts draw:*" do
element = RD::Include.new("include-file-shape.odt")
element.parent = @parent_element
result = @visitor.apply_to_Include(element)
result[0].should == <これは<<<のサンプルです.
EOF
result[1].should == <
Yes,
This is the sample!
シェイプ付きのOpen Documentファイルです.
EOF
result[2].should == <
EOF
result.length.should == 3
result.class.should == Array
@visitor.automatic_styles[0].to_s.should == <
EOF
@visitor.automatic_styles[1].to_s.should == <
EOF
@visitor.automatic_styles[2].to_s.should == <
EOF
# automatic_styles[3] test is omitted.
@visitor.automatic_styles.length.should == 4
@visitor.automatic_styles.class.should == Array
end
end