rtf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'rtf'
5
+
6
+ include RTF
7
+
8
+ # ColourTable class unit test class.
9
+ class ColourTableTest < Test::Unit::TestCase
10
+ def setup
11
+ @colours = [Colour.new(0, 0, 0),
12
+ Colour.new(100, 100, 100),
13
+ Colour.new(150, 150, 150),
14
+ Colour.new(200, 200, 200),
15
+ Colour.new(250, 250, 250)]
16
+ end
17
+
18
+ def test_01
19
+ tables = []
20
+ tables.push(ColourTable.new)
21
+ tables.push(ColourTable.new(@colours[0], @colours[2]))
22
+ tables.push(ColourTable.new(*@colours))
23
+ tables.push(ColourTable.new(@colours[0], @colours[1], @colours[0]))
24
+
25
+ assert(tables[0].size == 0)
26
+ assert(tables[1].size == 2)
27
+ assert(tables[2].size == 5)
28
+ assert(tables[3].size == 2)
29
+
30
+ assert(tables[0][0] == nil)
31
+ assert(tables[1][1] == @colours[2])
32
+ assert(tables[2][3] == @colours[3])
33
+ assert(tables[3][5] == nil)
34
+
35
+ assert(tables[0].index(@colours[1]) == nil)
36
+ assert(tables[1].index(@colours[2]) == 2)
37
+ assert(tables[2].index(@colours[3]) == 4)
38
+ assert(tables[3].index(@colours[4]) == nil)
39
+
40
+ tables[0].add(@colours[0])
41
+ assert(tables[0].size == 1)
42
+ assert(tables[0].index(@colours[0]) == 1)
43
+
44
+ tables[0] << @colours[1]
45
+ assert(tables[0].size == 2)
46
+ assert(tables[0].index(@colours[1]) == 2)
47
+
48
+ tables[0].add(@colours[1])
49
+ assert(tables[0].size == 2)
50
+ assert([tables[0][0], tables[0][1]] == [@colours[0], @colours[1]])
51
+
52
+ tables[0] << @colours[0]
53
+ assert(tables[0].size == 2)
54
+ assert([tables[0][0], tables[0][1]] == [@colours[0], @colours[1]])
55
+
56
+ flags = [false, false, false, false, false]
57
+ tables[2].each do |colour|
58
+ flags[@colours.index(colour)] = true if @colours.index(colour) != nil
59
+ end
60
+ assert(flags.index(false) == nil)
61
+
62
+ assert(tables[0].to_s(2) == " Colour Table (2 colours)\n"\
63
+ " Colour (0/0/0)\n"\
64
+ " Colour (100/100/100)")
65
+ assert(tables[1].to_s(4) == " Colour Table (2 colours)\n"\
66
+ " Colour (0/0/0)\n"\
67
+ " Colour (150/150/150)")
68
+ assert(tables[2].to_s(-6) == "Colour Table (5 colours)\n"\
69
+ " Colour (0/0/0)\n"\
70
+ " Colour (100/100/100)\n"\
71
+ " Colour (150/150/150)\n"\
72
+ " Colour (200/200/200)\n"\
73
+ " Colour (250/250/250)")
74
+ assert(tables[3].to_s == "Colour Table (2 colours)\n"\
75
+ " Colour (0/0/0)\n"\
76
+ " Colour (100/100/100)")
77
+
78
+ assert(tables[0].to_rtf(3) == " {\\colortbl\n ;\n"\
79
+ " \\red0\\green0\\blue0;\n"\
80
+ " \\red100\\green100\\blue100;\n"\
81
+ " }")
82
+ assert(tables[1].to_rtf(6) == " {\\colortbl\n ;\n"\
83
+ " \\red0\\green0\\blue0;\n"\
84
+ " \\red150\\green150\\blue150;\n"\
85
+ " }")
86
+ assert(tables[2].to_rtf(-9) == "{\\colortbl\n;\n"\
87
+ "\\red0\\green0\\blue0;\n"\
88
+ "\\red100\\green100\\blue100;\n"\
89
+ "\\red150\\green150\\blue150;\n"\
90
+ "\\red200\\green200\\blue200;\n"\
91
+ "\\red250\\green250\\blue250;\n"\
92
+ "}")
93
+ assert(tables[3].to_rtf == "{\\colortbl\n;\n"\
94
+ "\\red0\\green0\\blue0;\n"\
95
+ "\\red100\\green100\\blue100;\n"\
96
+ "}")
97
+ end
98
+ end
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'rtf'
6
+
7
+ include RTF
8
+
9
+ # Colour class unit test class.
10
+ class ColourTest < Test::Unit::TestCase
11
+ def test_01
12
+ colours = []
13
+ colours.push(Colour.new(255, 255, 255))
14
+ colours.push(Colour.new(200, 0, 0))
15
+ colours.push(Colour.new(0, 150, 0))
16
+ colours.push(Colour.new(0, 0, 100))
17
+ colours.push(Colour.new(0, 0, 0))
18
+
19
+ assert(colours[0] == colours[0])
20
+ assert(!(colours[1] == colours[2]))
21
+ assert(colours[3] == Colour.new(0, 0, 100))
22
+ assert(!(colours[4] == 12345))
23
+
24
+ assert(colours[0].red == 255)
25
+ assert(colours[0].green == 255)
26
+ assert(colours[0].blue == 255)
27
+
28
+ assert(colours[1].red == 200)
29
+ assert(colours[1].green == 0)
30
+ assert(colours[1].blue == 0)
31
+
32
+ assert(colours[2].red == 0)
33
+ assert(colours[2].green == 150)
34
+ assert(colours[2].blue == 0)
35
+
36
+ assert(colours[3].red == 0)
37
+ assert(colours[3].green == 0)
38
+ assert(colours[3].blue == 100)
39
+
40
+ assert(colours[4].red == 0)
41
+ assert(colours[4].green == 0)
42
+ assert(colours[4].blue == 0)
43
+
44
+ assert(colours[0].to_s(3) == ' Colour (255/255/255)')
45
+ assert(colours[1].to_s(6) == ' Colour (200/0/0)')
46
+ assert(colours[2].to_s(-20) == 'Colour (0/150/0)')
47
+ assert(colours[3].to_s == 'Colour (0/0/100)')
48
+ assert(colours[4].to_s == 'Colour (0/0/0)')
49
+
50
+ assert(colours[0].to_rtf(2) == ' \red255\green255\blue255;')
51
+ assert(colours[1].to_rtf(4) == ' \red200\green0\blue0;')
52
+ assert(colours[2].to_rtf(-6) == '\red0\green150\blue0;')
53
+ assert(colours[3].to_rtf == '\red0\green0\blue100;')
54
+ assert(colours[4].to_rtf == '\red0\green0\blue0;')
55
+
56
+ begin
57
+ Colour.new(256, 0, 0)
58
+ flunk("Successfully created a colour with an invalid red setting.")
59
+ rescue RTFError
60
+ rescue Test::Unit::AssertionFailedError => error
61
+ raise error
62
+ rescue => error
63
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
64
+ "Message: #{error.message}")
65
+ end
66
+
67
+ begin
68
+ Colour.new(0, 1000, 0)
69
+ flunk("Successfully created a colour with an invalid green setting.")
70
+ rescue RTFError
71
+ rescue Test::Unit::AssertionFailedError => error
72
+ raise error
73
+ rescue => error
74
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
75
+ "Message: #{error.message}")
76
+ end
77
+
78
+ begin
79
+ Colour.new(0, 0, -300)
80
+ flunk("Successfully created a colour with an invalid blue setting.")
81
+ rescue RTFError
82
+ rescue Test::Unit::AssertionFailedError => error
83
+ raise error
84
+ rescue => error
85
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
86
+ "Message: #{error.message}")
87
+ end
88
+
89
+ begin
90
+ Colour.new('La la la', 0, 0)
91
+ flunk("Successfully created a colour with an invalid red type.")
92
+ rescue RTFError
93
+ rescue Test::Unit::AssertionFailedError => error
94
+ raise error
95
+ rescue => error
96
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
97
+ "Message: #{error.message}")
98
+ end
99
+
100
+ begin
101
+ Colour.new(0, {}, 0)
102
+ flunk("Successfully created a colour with an invalid green type.")
103
+ rescue RTFError
104
+ rescue Test::Unit::AssertionFailedError => error
105
+ raise error
106
+ rescue => error
107
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
108
+ "Message: #{error.message}")
109
+ end
110
+
111
+ begin
112
+ Colour.new(0, 0, [])
113
+ flunk("Successfully created a colour with an invalid blue type.")
114
+ rescue RTFError
115
+ rescue Test::Unit::AssertionFailedError => error
116
+ raise error
117
+ rescue => error
118
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
119
+ "Message: #{error.message}")
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,220 @@
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 CommandNodeTest < Test::Unit::TestCase
11
+ def test_basics
12
+ nodes = []
13
+ nodes.push(CommandNode.new(nil, 'prefix'))
14
+ nodes.push(CommandNode.new(nil, '', 'lalala'))
15
+ nodes.push(CommandNode.new(nodes[0], '', nil, false))
16
+
17
+ assert(nodes[0].prefix == 'prefix')
18
+ assert(nodes[1].prefix == '')
19
+ assert(nodes[2].prefix == '')
20
+
21
+ assert(nodes[0].suffix == nil)
22
+ assert(nodes[1].suffix == 'lalala')
23
+ assert(nodes[2].suffix == nil)
24
+
25
+ assert(nodes[0].split == true)
26
+ assert(nodes[1].split == true)
27
+ assert(nodes[2].split == false)
28
+ end
29
+
30
+ # Test line breaks.
31
+ def test_line_break
32
+ root = CommandNode.new(nil, nil)
33
+
34
+ assert(root.line_break == nil)
35
+ assert(root.size == 1)
36
+ assert(root[0].class == CommandNode)
37
+ assert(root[0].prefix == '\line')
38
+ assert(root[0].suffix == nil)
39
+ assert(root[0].split == false)
40
+ end
41
+
42
+ # Test paragraphs.
43
+ def test_paragraph
44
+ root = CommandNode.new(nil, nil)
45
+ style = ParagraphStyle.new
46
+
47
+ assert(root.paragraph(style) != nil)
48
+ assert(root.size == 1)
49
+ assert(root[0].class == CommandNode)
50
+ assert(root[0].prefix == '\pard\ql')
51
+ assert(root[0].suffix == '\par')
52
+ assert(root.split == true)
53
+
54
+ style.justification = ParagraphStyle::RIGHT_JUSTIFY
55
+ assert(root.paragraph(style).prefix == '\pard\qr')
56
+
57
+ style.justification = ParagraphStyle::CENTRE_JUSTIFY
58
+ assert(root.paragraph(style).prefix == '\pard\qc')
59
+
60
+ style.justification = ParagraphStyle::FULL_JUSTIFY
61
+ assert(root.paragraph(style).prefix == '\pard\qj')
62
+
63
+ style.justification = ParagraphStyle::LEFT_JUSTIFY
64
+ style.space_before = 100
65
+ root.paragraph(style)
66
+ assert(root[-1].prefix == '\pard\ql\sb100')
67
+
68
+ style.space_before = nil
69
+ style.space_after = 1234
70
+ root.paragraph(style)
71
+ assert(root[-1].prefix == '\pard\ql\sa1234')
72
+
73
+ style.space_before = nil
74
+ style.space_after = nil
75
+ style.left_indent = 10
76
+ root.paragraph(style)
77
+ assert(root[-1].prefix == '\pard\ql\li10')
78
+
79
+ style.space_before = nil
80
+ style.space_after = nil
81
+ style.left_indent = nil
82
+ style.right_indent = 234
83
+ root.paragraph(style)
84
+ assert(root[-1].prefix == '\pard\ql\ri234')
85
+
86
+ style.space_before = nil
87
+ style.space_after = nil
88
+ style.left_indent = nil
89
+ style.right_indent = nil
90
+ style.first_line_indent = 765
91
+ root.paragraph(style)
92
+ assert(root[-1].prefix == '\pard\ql\fi765')
93
+
94
+ style.space_before = 12
95
+ style.space_after = 23
96
+ style.left_indent = 34
97
+ style.right_indent = 45
98
+ style.first_line_indent = 56
99
+ root.paragraph(style)
100
+ assert(root[-1].prefix == '\pard\ql\li34\ri45\fi56\sb12\sa23')
101
+
102
+ node = nil
103
+ root.paragraph(style) {|n| node = n}
104
+ assert(node == root[-1])
105
+ end
106
+
107
+ # Test applications of styles.
108
+ def test_style
109
+ root = Document.new(Font.new(Font::ROMAN, 'Arial'))
110
+ style = CharacterStyle.new
111
+ style.bold = true
112
+
113
+ assert(root.apply(style) != nil)
114
+ assert(root.size == 1)
115
+ assert(root[0].class == CommandNode)
116
+ assert(root[0].prefix == '\b')
117
+ assert(root[0].suffix == nil)
118
+ assert(root[0].split == true)
119
+
120
+ style.underline = true
121
+ assert(root.apply(style).prefix == '\b\ul')
122
+
123
+ style.bold = false
124
+ style.superscript = true
125
+ assert(root.apply(style).prefix == '\ul\super')
126
+
127
+ style.underline = false
128
+ style.italic = true
129
+ assert(root.apply(style).prefix == '\i\super')
130
+
131
+ style.italic = false
132
+ style.bold = true
133
+ assert(root.apply(style).prefix == '\b\super')
134
+
135
+ style.bold = false
136
+ style.superscript = false
137
+ style.italic = true
138
+ style.font_size = 20
139
+ assert(root.apply(style).prefix == '\i\fs20')
140
+
141
+ node = nil
142
+ root.apply(style) {|n| node = n}
143
+ assert(node == root[-1])
144
+
145
+ # Test style short cuts.
146
+ node = root.bold
147
+ assert(node.prefix == '\b')
148
+ assert(node.suffix == nil)
149
+ assert(node == root[-1])
150
+
151
+ node = root.italic
152
+ assert(node.prefix == '\i')
153
+ assert(node.suffix == nil)
154
+ assert(node == root[-1])
155
+
156
+ node = root.underline
157
+ assert(node.prefix == '\ul')
158
+ assert(node.suffix == nil)
159
+ assert(node == root[-1])
160
+
161
+ node = root.superscript
162
+ assert(node.prefix == '\super')
163
+ assert(node.suffix == nil)
164
+ assert(node == root[-1])
165
+ end
166
+
167
+ # Test text node addition.
168
+ def test_text
169
+ root = CommandNode.new(nil, nil)
170
+
171
+ root << 'A block of text.'
172
+ assert(root.size == 1)
173
+ assert(root[0].class == TextNode)
174
+ assert(root[0].text == 'A block of text.')
175
+
176
+ root << " More text."
177
+ assert(root.size == 1)
178
+ assert(root[0].class == TextNode)
179
+ assert(root[0].text == 'A block of text. More text.')
180
+
181
+ root.paragraph
182
+ root << "A new node."
183
+ assert(root.size == 3)
184
+ assert(root[0].class == TextNode)
185
+ assert(root[-1].class == TextNode)
186
+ assert(root[0].text == 'A block of text. More text.')
187
+ assert(root[-1].text == 'A new node.')
188
+ end
189
+
190
+ # Test table addition.
191
+ def test_table
192
+ root = CommandNode.new(nil, nil)
193
+
194
+ table = root.table(3, 3, 100, 150, 200)
195
+ assert(root.size == 1)
196
+ assert(root[0].class == TableNode)
197
+ assert(root[0] == table)
198
+ assert(table.rows == 3)
199
+ assert(table.columns == 3)
200
+
201
+ assert(table[0][0].width == 100)
202
+ assert(table[0][1].width == 150)
203
+ assert(table[0][2].width == 200)
204
+ end
205
+
206
+ # This test checks the previous_node and next_node methods that could not be
207
+ # fully and properly checked in the NodeTest.rb file.
208
+ def test_peers
209
+ root = Document.new(Font.new(Font::ROMAN, 'Arial'))
210
+ nodes = []
211
+ nodes.push(root.paragraph)
212
+ nodes.push(root.bold)
213
+ nodes.push(root.underline)
214
+
215
+ assert(root.previous_node == nil)
216
+ assert(root.next_node == nil)
217
+ assert(nodes[1].previous_node == nodes[0])
218
+ assert(nodes[1].next_node == nodes[2])
219
+ end
220
+ end
@@ -0,0 +1,70 @@
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 ContainerNodeTest < Test::Unit::TestCase
11
+ def test01
12
+ nodes = []
13
+ nodes.push(ContainerNode.new(nil))
14
+ nodes.push(ContainerNode.new(nodes[0]))
15
+
16
+ assert(nodes[0].size == 0)
17
+ assert(nodes[1].size == 0)
18
+
19
+ assert(nodes[0].first == nil)
20
+ assert(nodes[0].last == nil)
21
+ assert(nodes[1].first == nil)
22
+ assert(nodes[1].last == nil)
23
+
24
+ assert(nodes[0][0] == nil)
25
+ assert(nodes[1][-1] == nil)
26
+
27
+ count = 0
28
+ nodes[0].each {|entry| count += 1}
29
+ assert(count == 0)
30
+ nodes[1].each {|entry| count += 1}
31
+ assert(count == 0)
32
+ end
33
+
34
+ def test02
35
+ node = ContainerNode.new(nil)
36
+ child1 = ContainerNode.new(nil)
37
+ child2 = ContainerNode.new(nil)
38
+
39
+ node.store(child1)
40
+ assert(node.size == 1)
41
+ assert(node[0] == child1)
42
+ assert(node[-1] == child1)
43
+ assert(node[0].parent == node)
44
+ assert(node.first == child1)
45
+ assert(node.last == child1)
46
+
47
+ count = 0
48
+ node.each {|entry| count += 1}
49
+ assert(count == 1)
50
+
51
+ node.store(child2)
52
+ assert(node.size == 2)
53
+ assert(node[0] == child1)
54
+ assert(node[1] == child2)
55
+ assert(node[-1] == child2)
56
+ assert(node[-2] == child1)
57
+ assert(node[0].parent == node)
58
+ assert(node[1].parent == node)
59
+ assert(node.first == child1)
60
+ assert(node.last == child2)
61
+ end
62
+
63
+ def test03
64
+ begin
65
+ ContainerNode.new(nil).to_rtf
66
+ flunk("Successfully called ContainerNode#to_rtf().")
67
+ rescue => error
68
+ end
69
+ end
70
+ end