rtf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'rtf'
6
+
7
+ include RTF
8
+
9
+ # Information class unit test class.
10
+ class DocumentStyleTest < Test::Unit::TestCase
11
+ def test_basics
12
+ style = DocumentStyle.new
13
+
14
+ assert(style.is_character_style? == false)
15
+ assert(style.is_document_style? == true)
16
+ assert(style.is_paragraph_style? == false)
17
+ assert(style.is_table_style? == false)
18
+
19
+ assert(style.prefix(nil, nil) == '\paperw11907\paperh16840\margl1800'\
20
+ '\margr1800\margt1440\margb1440')
21
+ assert(style.suffix(nil, nil) == nil)
22
+
23
+ assert(style.bottom_margin == DocumentStyle::DEFAULT_BOTTOM_MARGIN)
24
+ assert(style.gutter == nil)
25
+ assert(style.left_margin == DocumentStyle::DEFAULT_LEFT_MARGIN)
26
+ assert(style.orientation == DocumentStyle::PORTRAIT)
27
+ assert(style.paper == Paper::A4)
28
+ assert(style.right_margin == DocumentStyle::DEFAULT_RIGHT_MARGIN)
29
+ assert(style.top_margin == DocumentStyle::DEFAULT_TOP_MARGIN)
30
+ end
31
+
32
+ def test_mutators
33
+ style = DocumentStyle.new
34
+
35
+ style.bottom_margin = 200
36
+ assert(style.bottom_margin == 200)
37
+
38
+ style.gutter = 1000
39
+ assert(style.gutter == 1000)
40
+
41
+ style.left_margin = 34
42
+ assert(style.left_margin == 34)
43
+
44
+ style.orientation = DocumentStyle::LANDSCAPE
45
+ assert(style.orientation == DocumentStyle::LANDSCAPE)
46
+
47
+ style.paper = Paper::LETTER
48
+ assert(style.paper == Paper::LETTER)
49
+
50
+ style.right_margin = 345
51
+ assert(style.right_margin == 345)
52
+
53
+ style.top_margin = 819
54
+ assert(style.top_margin == 819)
55
+ end
56
+
57
+ def test_prefix
58
+ style = DocumentStyle.new
59
+
60
+ style.left_margin = style.right_margin = 200
61
+ style.top_margin = style.bottom_margin = 100
62
+ style.gutter = 300
63
+ style.orientation = DocumentStyle::LANDSCAPE
64
+ style.paper = Paper::A5
65
+
66
+ assert(style.prefix(nil, nil) == '\paperw11907\paperh8392\margl200'\
67
+ '\margr200\margt100\margb100\gutter300'\
68
+ '\sectd\lndscpsxn')
69
+ end
70
+
71
+ def test_body_method
72
+ style = DocumentStyle.new
73
+
74
+ lr_margin = style.left_margin + style.right_margin
75
+ tb_margin = style.top_margin + style.bottom_margin
76
+
77
+ assert(style.body_width == Paper::A4.width - lr_margin)
78
+ assert(style.body_height == Paper::A4.height - tb_margin)
79
+
80
+ style.orientation = DocumentStyle::LANDSCAPE
81
+
82
+ assert(style.body_width == Paper::A4.height - lr_margin)
83
+ assert(style.body_height == Paper::A4.width - tb_margin)
84
+ end
85
+ end
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'rtf'
6
+
7
+ include RTF
8
+
9
+ # Information class unit test class.
10
+ class DocumentTest < Test::Unit::TestCase
11
+ def setup
12
+ @fonts = FontTable.new
13
+
14
+ @fonts << Font.new(Font::ROMAN, 'Arial')
15
+ @fonts << Font.new(Font::MODERN, 'Courier')
16
+ end
17
+
18
+ def test_basics
19
+ documents = []
20
+ style = DocumentStyle.new
21
+
22
+ documents << Document.new(@fonts[0])
23
+ documents << Document.new(@fonts[1], style)
24
+ documents << Document.new(@fonts[0], nil, Document::CS_MAC)
25
+ documents << Document.new(@fonts[1], style, Document::CS_PC,
26
+ Document::LC_ENGLISH_US)
27
+
28
+ lr_margin = DocumentStyle::DEFAULT_LEFT_MARGIN +
29
+ DocumentStyle::DEFAULT_RIGHT_MARGIN
30
+ tb_margin = DocumentStyle::DEFAULT_TOP_MARGIN +
31
+ DocumentStyle::DEFAULT_BOTTOM_MARGIN
32
+
33
+ fonts = []
34
+ fonts << FontTable.new(@fonts[0])
35
+ fonts << FontTable.new(@fonts[1])
36
+
37
+ assert(documents[0].character_set == Document::CS_ANSI)
38
+ assert(documents[1].character_set == Document::CS_ANSI)
39
+ assert(documents[2].character_set == Document::CS_MAC)
40
+ assert(documents[3].character_set == Document::CS_PC)
41
+
42
+ assert(documents[0].fonts.size == 1)
43
+ assert(documents[1].fonts.size == 1)
44
+ assert(documents[2].fonts.size == 1)
45
+ assert(documents[3].fonts.size == 1)
46
+
47
+ assert(documents[0].fonts[0] == @fonts[0])
48
+ assert(documents[1].fonts[0] == @fonts[1])
49
+ assert(documents[2].fonts[0] == @fonts[0])
50
+ assert(documents[3].fonts[0] == @fonts[1])
51
+
52
+ assert(documents[0].colours.size == 0)
53
+ assert(documents[1].colours.size == 0)
54
+ assert(documents[2].colours.size == 0)
55
+ assert(documents[3].colours.size == 0)
56
+
57
+ assert(documents[0].language == Document::LC_ENGLISH_UK)
58
+ assert(documents[1].language == Document::LC_ENGLISH_UK)
59
+ assert(documents[2].language == Document::LC_ENGLISH_UK)
60
+ assert(documents[3].language == Document::LC_ENGLISH_US)
61
+
62
+ assert(documents[0].style != nil)
63
+ assert(documents[1].style == style)
64
+ assert(documents[2].style != nil)
65
+ assert(documents[3].style == style)
66
+
67
+ assert(documents[0].body_width == Paper::A4.width - lr_margin)
68
+ assert(documents[0].body_height == Paper::A4.height - tb_margin)
69
+ assert(documents[0].default_font == @fonts[0])
70
+ assert(documents[0].paper == Paper::A4)
71
+ assert(documents[0].header == nil)
72
+ assert(documents[0].header(HeaderNode::UNIVERSAL) == nil)
73
+ assert(documents[0].header(HeaderNode::LEFT_PAGE) == nil)
74
+ assert(documents[0].header(HeaderNode::RIGHT_PAGE) == nil)
75
+ assert(documents[0].header(HeaderNode::FIRST_PAGE) == nil)
76
+ assert(documents[0].footer == nil)
77
+ assert(documents[0].footer(FooterNode::UNIVERSAL) == nil)
78
+ assert(documents[0].footer(FooterNode::LEFT_PAGE) == nil)
79
+ assert(documents[0].footer(FooterNode::RIGHT_PAGE) == nil)
80
+ assert(documents[0].footer(FooterNode::FIRST_PAGE) == nil)
81
+ end
82
+
83
+ def test_mutators
84
+ document = Document.new(@fonts[0])
85
+
86
+ document.default_font = @fonts[1]
87
+ assert(document.default_font == @fonts[1])
88
+
89
+ document.character_set = Document::CS_PCA
90
+ assert(document.character_set == Document::CS_PCA)
91
+
92
+ document.language = Document::LC_CZECH
93
+ assert(document.language == Document::LC_CZECH)
94
+ end
95
+
96
+ def test_page_break
97
+ document = Document.new(@fonts[0])
98
+
99
+ assert(document.page_break == nil)
100
+ assert(document.size == 1)
101
+ assert(document[0].prefix == '\page')
102
+ end
103
+
104
+ def test_exceptions
105
+ document = Document.new(@fonts[0])
106
+ begin
107
+ document.parent = document
108
+ flunk("Successfully change the parent of a Document object.")
109
+ rescue
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'rtf'
5
+
6
+ include RTF
7
+
8
+ # FontTable class unit test class.
9
+ class FontTableTest < Test::Unit::TestCase
10
+ def setup
11
+ @fonts = []
12
+ @fonts.push(Font.new(Font::MODERN, "Courier New"))
13
+ @fonts.push(Font.new(Font::ROMAN, "Arial"))
14
+ @fonts.push(Font.new(Font::SWISS, "Tahoma"))
15
+ @fonts.push(Font.new(Font::NIL, "La La La"))
16
+ end
17
+
18
+ def test_01
19
+ tables = []
20
+ tables.push(FontTable.new)
21
+ tables.push(FontTable.new(@fonts[0], @fonts[2]))
22
+ tables.push(FontTable.new(*@fonts))
23
+ tables.push(FontTable.new(@fonts[0], @fonts[2], @fonts[0]))
24
+
25
+ assert(tables[0].size == 0)
26
+ assert(tables[1].size == 2)
27
+ assert(tables[2].size == 4)
28
+ assert(tables[3].size == 2)
29
+
30
+ assert(tables[0][2] == nil)
31
+ assert(tables[1][1] == @fonts[2])
32
+ assert(tables[2][3] == @fonts[3])
33
+ assert(tables[3][2] == nil)
34
+
35
+ assert(tables[0].index(@fonts[0]) == nil)
36
+ assert(tables[1].index(@fonts[2]) == 1)
37
+ assert(tables[2].index(@fonts[2]) == 2)
38
+ assert(tables[3].index(@fonts[1]) == nil)
39
+
40
+ tables[0].add(@fonts[0])
41
+ assert(tables[0].size == 1)
42
+ assert(tables[0].index(@fonts[0]) == 0)
43
+
44
+ tables[0] << @fonts[1]
45
+ assert(tables[0].size == 2)
46
+ assert(tables[0].index(@fonts[1]) == 1)
47
+
48
+ tables[0].add(@fonts[0])
49
+ assert(tables[0].size == 2)
50
+ assert([tables[0][0], tables[0][1]] == [@fonts[0], @fonts[1]])
51
+
52
+ tables[0] << @fonts[1]
53
+ assert(tables[0].size == 2)
54
+ assert([tables[0][0], tables[0][1]] == [@fonts[0], @fonts[1]])
55
+
56
+ flags = [false, false, false, false]
57
+ tables[2].each do |font|
58
+ flags[@fonts.index(font)] = true if @fonts.index(font) != nil
59
+ end
60
+ assert(flags.index(false) == nil)
61
+
62
+ assert(tables[0].to_s == "Font Table (2 fonts)\n"\
63
+ " Family: modern, Name: Courier New\n"\
64
+ " Family: roman, Name: Arial")
65
+ assert(tables[1].to_s(6) == " Font Table (2 fonts)\n"\
66
+ " Family: modern, Name: Courier New\n"\
67
+ " Family: swiss, Name: Tahoma")
68
+ assert(tables[2].to_s(3) == " Font Table (4 fonts)\n"\
69
+ " Family: modern, Name: Courier New\n"\
70
+ " Family: roman, Name: Arial\n"\
71
+ " Family: swiss, Name: Tahoma\n"\
72
+ " Family: nil, Name: La La La")
73
+ assert(tables[3].to_s(-10) == "Font Table (2 fonts)\n"\
74
+ " Family: modern, Name: Courier New\n"\
75
+ " Family: swiss, Name: Tahoma")
76
+
77
+ assert(tables[0].to_rtf == "{\\fonttbl\n"\
78
+ "{\\f0\\fmodern Courier New;}\n"\
79
+ "{\\f1\\froman Arial;}\n"\
80
+ "}")
81
+ assert(tables[1].to_rtf(4) == " {\\fonttbl\n"\
82
+ " {\\f0\\fmodern Courier New;}\n"\
83
+ " {\\f1\\fswiss Tahoma;}\n"\
84
+ " }")
85
+ assert(tables[2].to_rtf(2) == " {\\fonttbl\n"\
86
+ " {\\f0\\fmodern Courier New;}\n"\
87
+ " {\\f1\\froman Arial;}\n"\
88
+ " {\\f2\\fswiss Tahoma;}\n"\
89
+ " {\\f3\\fnil La La La;}\n"\
90
+ " }")
91
+ assert(tables[3].to_rtf(-6) == "{\\fonttbl\n"\
92
+ "{\\f0\\fmodern Courier New;}\n"\
93
+ "{\\f1\\fswiss Tahoma;}\n"\
94
+ "}")
95
+ end
96
+ end
data/test/FontTest.rb ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'rtf'
5
+
6
+ include RTF
7
+
8
+ # Font class unit test class.
9
+ class FontTest < Test::Unit::TestCase
10
+ def test_01
11
+ fonts = []
12
+ fonts.push(Font.new(Font::MODERN, "Courier New"))
13
+ fonts.push(Font.new(Font::ROMAN, "Arial"))
14
+ fonts.push(Font.new(Font::SWISS, "Tahoma"))
15
+ fonts.push(Font.new(Font::NIL, "La La La"))
16
+
17
+ assert(fonts[0] == fonts[0])
18
+ assert(!(fonts[0] == fonts[1]))
19
+ assert(!(fonts[1] == 'a string of text'))
20
+ assert(fonts[2] == Font.new(Font::SWISS, "Tahoma"))
21
+
22
+ assert(fonts[0].family == Font::MODERN)
23
+ assert(fonts[1].family == Font::ROMAN)
24
+ assert(fonts[2].family == Font::SWISS)
25
+ assert(fonts[3].family == Font::NIL)
26
+
27
+ assert(fonts[0].name == 'Courier New')
28
+ assert(fonts[1].name == 'Arial')
29
+ assert(fonts[2].name == 'Tahoma')
30
+ assert(fonts[3].name == 'La La La')
31
+
32
+ assert(fonts[0].to_s == 'Family: modern, Name: Courier New')
33
+ assert(fonts[1].to_s(3) == ' Family: roman, Name: Arial')
34
+ assert(fonts[2].to_s(6) == ' Family: swiss, Name: Tahoma')
35
+ assert(fonts[3].to_s(-1) == 'Family: nil, Name: La La La')
36
+
37
+ assert(fonts[0].to_rtf == '\fmodern Courier New;')
38
+ assert(fonts[1].to_rtf(2) == ' \froman Arial;')
39
+ assert(fonts[2].to_rtf(4) == ' \fswiss Tahoma;')
40
+ assert(fonts[3].to_rtf(-6) == '\fnil La La La;')
41
+
42
+ begin
43
+ Font.new(12345, "Ningy")
44
+ flunk("Created a Font object with an invalid family setting.")
45
+ rescue RTFError
46
+ rescue Test::Unit::AssertionFailedError => error
47
+ raise error
48
+ rescue => error
49
+ flunk("Unexpected exception caught checking font creation with "\
50
+ "an invalid family. Exception type #{error.class.name}.")
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'rtf'
6
+
7
+ include RTF
8
+
9
+ # Information class unit test class.
10
+ class FooterNodeTest < Test::Unit::TestCase
11
+ def setup
12
+ @document = Document.new(Font.new(Font::ROMAN, 'Arial'))
13
+ end
14
+
15
+ def test_basics
16
+ footers = []
17
+
18
+ footers << FooterNode.new(@document)
19
+ footers << FooterNode.new(@document, FooterNode::LEFT_PAGE)
20
+
21
+ assert(footers[0].parent == @document)
22
+ assert(footers[1].parent == @document)
23
+
24
+ assert(footers[0].type == FooterNode::UNIVERSAL)
25
+ assert(footers[1].type == FooterNode::LEFT_PAGE)
26
+ end
27
+
28
+ def test_exceptions
29
+ footer = FooterNode.new(@document)
30
+ begin
31
+ footer.footnote("La la la.")
32
+ flunk("Successfully added a footnote to a footer.")
33
+ rescue
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'rtf'
6
+
7
+ include RTF
8
+
9
+ # Information class unit test class.
10
+ class HeaderNodeTest < Test::Unit::TestCase
11
+ def setup
12
+ @document = Document.new(Font.new(Font::ROMAN, 'Arial'))
13
+ end
14
+
15
+ def test_basics
16
+ headers = []
17
+
18
+ headers << HeaderNode.new(@document)
19
+ headers << HeaderNode.new(@document, HeaderNode::LEFT_PAGE)
20
+
21
+ assert(headers[0].parent == @document)
22
+ assert(headers[1].parent == @document)
23
+
24
+ assert(headers[0].type == HeaderNode::UNIVERSAL)
25
+ assert(headers[1].type == HeaderNode::LEFT_PAGE)
26
+ end
27
+
28
+ def test_exceptions
29
+ headers = HeaderNode.new(@document)
30
+ begin
31
+ headers.footnote("La la la.")
32
+ flunk("Successfully added a footnote to a header.")
33
+ rescue
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'rtf'
5
+ require 'stringio'
6
+
7
+ include RTF
8
+
9
+ # Information class unit test class.
10
+ class InformationTest < Test::Unit::TestCase
11
+ def test_01
12
+ date = Time.local(1985, 6, 22, 14, 33, 22)
13
+ info = []
14
+ info.push(Information.new)
15
+ info.push(Information.new('Title 1'))
16
+ info.push(Information.new('Title 2', 'Peter Wood'))
17
+ info.push(Information.new('La la la', '', 'Nowhere Ltd.'))
18
+ info.push(Information.new('', 'N. O. Body', 'Oobly', 'Joobly'))
19
+ info.push(Information.new('Title 5', 'J. Bloggs', '', '', date))
20
+ info.push(Information.new('Title 6', 'J. Bloggs', '', '', '1985-06-22 14:33:22 GMT'))
21
+
22
+ assert(info[0].title == nil)
23
+ assert(info[0].author == nil)
24
+ assert(info[0].company == nil)
25
+ assert(info[0].comments == nil)
26
+
27
+ assert(info[1].title == 'Title 1')
28
+ assert(info[1].author == nil)
29
+ assert(info[1].company == nil)
30
+ assert(info[1].comments == nil)
31
+
32
+ assert(info[2].title == 'Title 2')
33
+ assert(info[2].author == 'Peter Wood')
34
+ assert(info[2].company == nil)
35
+ assert(info[2].comments == nil)
36
+
37
+ assert(info[3].title == 'La la la')
38
+ assert(info[3].author == '')
39
+ assert(info[3].company == 'Nowhere Ltd.')
40
+ assert(info[3].comments == nil)
41
+
42
+ assert(info[4].title == '')
43
+ assert(info[4].author == 'N. O. Body')
44
+ assert(info[4].company == 'Oobly')
45
+ assert(info[4].comments == 'Joobly')
46
+
47
+ assert(info[5].title == 'Title 5')
48
+ assert(info[5].author == 'J. Bloggs')
49
+ assert(info[5].company == '')
50
+ assert(info[5].comments == '')
51
+ assert(info[5].created == date)
52
+
53
+ assert(info[6].title == 'Title 6')
54
+ assert(info[6].author == 'J. Bloggs')
55
+ assert(info[6].company == '')
56
+ assert(info[6].comments == '')
57
+ assert(info[6].created == date)
58
+
59
+ info[6].title = 'Alternative Title'
60
+ assert(info[6].title == 'Alternative Title')
61
+
62
+ info[6].author = 'A. Person'
63
+ assert(info[6].author == 'A. Person')
64
+
65
+ info[6].company = nil
66
+ assert(info[6].company == nil)
67
+
68
+ info[6].comments = 'New information comment text.'
69
+ assert(info[6].comments == 'New information comment text.')
70
+
71
+ date = Time.new
72
+ info[6].created = date
73
+ assert(info[6].created == date)
74
+
75
+ date = Time.local(1985, 6, 22, 14, 33, 22)
76
+ info[6].created = '1985-06-22 14:33:22 GMT'
77
+ assert(info[6].created == date)
78
+
79
+ assert(info[0].to_s(2) == " Information\n Created: "\
80
+ "#{info[0].created}")
81
+ assert(info[1].to_s(4) == " Information\n Title: Title 1\n"\
82
+ " Created: #{info[1].created}")
83
+ assert(info[2].to_s(-10) == "Information\n Title: Title 2\n "\
84
+ "Author: Peter Wood\n Created: "\
85
+ "#{info[2].created}")
86
+ assert(info[3].to_s == "Information\n Title: La la la\n "\
87
+ "Author: \n Company: Nowhere Ltd.\n "\
88
+ "Created: #{info[3].created}")
89
+ assert(info[4].to_s == "Information\n Title: \n Author: "\
90
+ "N. O. Body\n Company: Oobly\n Comments: "\
91
+ "Joobly\n Created: #{info[4].created}")
92
+ assert(info[5].to_s == "Information\n Title: Title 5\n Author: "\
93
+ "J. Bloggs\n Company: \n Comments: \n "\
94
+ "Created: #{date}")
95
+ assert(info[6].to_s == "Information\n Title: Alternative Title"\
96
+ "\n Author: A. Person\n Comments: New "\
97
+ "information comment text.\n Created: #{date}")
98
+
99
+ assert(info[0].to_rtf(3) == " {\\info\n #{to_rtf(info[0].created)}"\
100
+ "\n }")
101
+ assert(info[1].to_rtf(6) == " {\\info\n {\\title Title 1}\n"\
102
+ " #{to_rtf(info[1].created)}\n }")
103
+ assert(info[2].to_rtf(-5) == "{\\info\n{\\title Title 2}\n"\
104
+ "{\\author Peter Wood}\n"\
105
+ "#{to_rtf(info[2].created)}\n}")
106
+ assert(info[3].to_rtf == "{\\info\n{\\title La la la}\n"\
107
+ "{\\author }\n"\
108
+ "{\\company Nowhere Ltd.}\n"\
109
+ "#{to_rtf(info[3].created)}\n}")
110
+ assert(info[4].to_rtf == "{\\info\n{\\title }\n"\
111
+ "{\\author N. O. Body}\n"\
112
+ "{\\company Oobly}\n"\
113
+ "{\\doccomm Joobly}\n"\
114
+ "#{to_rtf(info[4].created)}\n}")
115
+ assert(info[5].to_rtf(3) == " {\\info\n {\\title Title 5}\n"\
116
+ " {\\author J. Bloggs}\n"\
117
+ " {\\company }\n"\
118
+ " {\\doccomm }\n"\
119
+ " #{to_rtf(date)}\n }")
120
+ assert(info[6].to_rtf == "{\\info\n{\\title Alternative Title}\n"\
121
+ "{\\author A. Person}\n"\
122
+ "{\\doccomm New information comment text.}\n"\
123
+ "#{to_rtf(date)}\n}")
124
+ end
125
+
126
+ def to_rtf(time)
127
+ text = StringIO.new
128
+ text << "{\\createim\\yr#{time.year}"
129
+ text << "\\mo#{time.month}\\dy#{time.day}"
130
+ text << "\\hr#{time.hour}\\min#{time.min}}"
131
+ text.string
132
+ end
133
+ end