rtf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/NodeTest.rb ADDED
@@ -0,0 +1,31 @@
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 NodeTest < Test::Unit::TestCase
11
+ def test01
12
+ nodes = []
13
+ nodes.push(Node.new(nil))
14
+ nodes.push(Node.new(nodes[0]))
15
+
16
+ assert(nodes[0].parent == nil)
17
+ assert(nodes[1].parent != nil)
18
+ assert(nodes[1].parent == nodes[0])
19
+
20
+ assert(nodes[0].is_root?)
21
+ assert(nodes[1].is_root? == false)
22
+
23
+ assert(nodes[0].root == nodes[0])
24
+ assert(nodes[1].root == nodes[0])
25
+
26
+ assert(nodes[0].previous_node == nil)
27
+ assert(nodes[0].next_node == nil)
28
+ assert(nodes[1].previous_node == nil)
29
+ assert(nodes[1].next_node == nil)
30
+ end
31
+ end
@@ -0,0 +1,87 @@
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 ParagraphStyleTest < Test::Unit::TestCase
11
+ def test_basics
12
+ style = ParagraphStyle.new
13
+
14
+ assert(style.is_character_style? == false)
15
+ assert(style.is_document_style? == false)
16
+ assert(style.is_paragraph_style? == true)
17
+ assert(style.is_table_style? == false)
18
+
19
+ assert(style.prefix(nil, nil) == '\ql')
20
+ assert(style.suffix(nil, nil) == nil)
21
+
22
+ assert(style.first_line_indent == nil)
23
+ assert(style.flow == ParagraphStyle::LEFT_TO_RIGHT)
24
+ assert(style.justification == ParagraphStyle::LEFT_JUSTIFY)
25
+ assert(style.left_indent == nil)
26
+ assert(style.right_indent == nil)
27
+ assert(style.line_spacing == nil)
28
+ assert(style.space_after == nil)
29
+ assert(style.space_before == nil)
30
+ end
31
+
32
+ def test_mutators
33
+ style = ParagraphStyle.new
34
+
35
+ style.first_line_indent = 100
36
+ assert(style.first_line_indent == 100)
37
+
38
+ style.flow = ParagraphStyle::RIGHT_TO_LEFT
39
+ assert(style.flow == ParagraphStyle::RIGHT_TO_LEFT)
40
+
41
+ style.justification = ParagraphStyle::RIGHT_JUSTIFY
42
+ assert(style.justification == ParagraphStyle::RIGHT_JUSTIFY)
43
+
44
+ style.left_indent = 234
45
+ assert(style.left_indent == 234)
46
+
47
+ style.right_indent = 1020
48
+ assert(style.right_indent == 1020)
49
+
50
+ style.line_spacing = 645
51
+ assert(style.line_spacing == 645)
52
+
53
+ style.space_after = 25
54
+ assert(style.space_after == 25)
55
+
56
+ style.space_before = 918
57
+ assert(style.space_before == 918)
58
+ end
59
+
60
+ def test_prefix
61
+ style = ParagraphStyle.new
62
+
63
+ style.first_line_indent = 100
64
+ assert(style.prefix(nil, nil) == '\ql\fi100')
65
+
66
+ style.flow = ParagraphStyle::RIGHT_TO_LEFT
67
+ assert(style.prefix(nil, nil) == '\ql\fi100\rtlpar')
68
+
69
+ style.justification = ParagraphStyle::RIGHT_JUSTIFY
70
+ assert(style.prefix(nil, nil) == '\qr\fi100\rtlpar')
71
+
72
+ style.left_indent = 234
73
+ assert(style.prefix(nil, nil) == '\qr\li234\fi100\rtlpar')
74
+
75
+ style.right_indent = 1020
76
+ assert(style.prefix(nil, nil) == '\qr\li234\ri1020\fi100\rtlpar')
77
+
78
+ style.line_spacing = 645
79
+ assert(style.prefix(nil, nil) == '\qr\li234\ri1020\fi100\sl645\rtlpar')
80
+
81
+ style.space_after = 25
82
+ assert(style.prefix(nil, nil) == '\qr\li234\ri1020\fi100\sa25\sl645\rtlpar')
83
+
84
+ style.space_before = 918
85
+ assert(style.prefix(nil, nil) == '\qr\li234\ri1020\fi100\sb918\sa25\sl645\rtlpar')
86
+ end
87
+ end
data/test/StyleTest.rb ADDED
@@ -0,0 +1,22 @@
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 StyleTest < Test::Unit::TestCase
11
+ def test_basics
12
+ style = Style.new
13
+
14
+ assert(style.is_character_style? == false)
15
+ assert(style.is_document_style? == false)
16
+ assert(style.is_paragraph_style? == false)
17
+ assert(style.is_table_style? == false)
18
+
19
+ assert(style.prefix(nil, nil) == nil)
20
+ assert(style.suffix(nil, nil) == nil)
21
+ end
22
+ end
@@ -0,0 +1,95 @@
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 TableCellNodeTest < Test::Unit::TestCase
11
+ def setup
12
+ @table = TableNode.new(nil, 3, 3, 100, 100, 100)
13
+ @row = TableRowNode.new(@table, 3, 100)
14
+ end
15
+
16
+ def test_basics
17
+ cells = []
18
+ cells.push(TableCellNode.new(@row))
19
+ cells.push(TableCellNode.new(@row, 1000))
20
+ cells.push(TableCellNode.new(@row, 250, nil, 5, 10, 15, 20))
21
+
22
+ assert(cells[0].parent == @row)
23
+ assert(cells[0].width == TableCellNode::DEFAULT_WIDTH)
24
+ assert(cells[0].top_border_width == 0)
25
+ assert(cells[0].right_border_width == 0)
26
+ assert(cells[0].bottom_border_width == 0)
27
+ assert(cells[0].left_border_width == 0)
28
+
29
+ assert(cells[1].parent == @row)
30
+ assert(cells[1].width == 1000)
31
+ assert(cells[1].top_border_width == 0)
32
+ assert(cells[1].right_border_width == 0)
33
+ assert(cells[1].bottom_border_width == 0)
34
+ assert(cells[1].left_border_width == 0)
35
+
36
+ assert(cells[2].parent == @row)
37
+ assert(cells[2].width == 250)
38
+ assert(cells[2].top_border_width == 5)
39
+ assert(cells[2].right_border_width == 10)
40
+ assert(cells[2].bottom_border_width == 15)
41
+ assert(cells[2].left_border_width == 20)
42
+
43
+ cells[0].top_border_width = 25
44
+ cells[0].bottom_border_width = 1
45
+ cells[0].left_border_width = 89
46
+ cells[0].right_border_width = 57
47
+
48
+ assert(cells[0].top_border_width == 25)
49
+ assert(cells[0].right_border_width == 57)
50
+ assert(cells[0].bottom_border_width == 1)
51
+ assert(cells[0].left_border_width == 89)
52
+
53
+ cells[0].top_border_width = 0
54
+ cells[0].bottom_border_width = nil
55
+ cells[0].left_border_width = -5
56
+ cells[0].right_border_width = -1000
57
+
58
+ assert(cells[0].top_border_width == 0)
59
+ assert(cells[0].right_border_width == 0)
60
+ assert(cells[0].bottom_border_width == 0)
61
+ assert(cells[0].left_border_width == 0)
62
+
63
+ assert(cells[2].border_widths == [5, 10, 15, 20])
64
+ end
65
+
66
+ def test_exceptions
67
+ begin
68
+ @row[0].paragraph
69
+ flunk("Successfully called the TableCellNode#paragraph method.")
70
+ rescue
71
+ end
72
+
73
+ begin
74
+ @row[0].parent = nil
75
+ flunk("Successfully called the TableCellNode#parent= method.")
76
+ rescue
77
+ end
78
+
79
+ begin
80
+ @row[0].table
81
+ flunk("Successfully called the TableCellNode#table method.")
82
+ rescue
83
+ end
84
+ end
85
+
86
+ def test_rtf_generation
87
+ cells = []
88
+ cells.push(TableCellNode.new(@row))
89
+ cells.push(TableCellNode.new(@row))
90
+ cells[1] << "Added text."
91
+
92
+ assert(cells[0].to_rtf == "\\pard\\intbl\n\n\\cell")
93
+ assert(cells[1].to_rtf == "\\pard\\intbl\nAdded text.\n\\cell")
94
+ end
95
+ end
@@ -0,0 +1,89 @@
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 TableNodeTest < Test::Unit::TestCase
11
+ def setup
12
+ @document = Document.new(Font.new(Font::ROMAN, 'Times New Roman'))
13
+ @colours = []
14
+
15
+ @colours << Colour.new(200, 200, 200)
16
+ @colours << Colour.new(200, 0, 0)
17
+ @colours << Colour.new(0, 200, 0)
18
+ @colours << Colour.new(0, 0, 200)
19
+ end
20
+
21
+ def test_basics
22
+ table = TableNode.new(@document, 3, 5, 10, 20, 30, 40, 50)
23
+
24
+ assert(table.rows == 3)
25
+ assert(table.columns == 5)
26
+ assert(table.size == 3)
27
+ assert(table.cell_margin == 100)
28
+ end
29
+
30
+ def test_mutators
31
+ table = TableNode.new(@document, 3, 3)
32
+
33
+ table.cell_margin = 250
34
+ assert(table.cell_margin == 250)
35
+ end
36
+
37
+ def test_colouring
38
+ table = TableNode.new(@document, 3, 3)
39
+
40
+ table.row_shading_colour(1, @colours[0])
41
+ assert(table[0][0].shading_colour == nil)
42
+ assert(table[0][1].shading_colour == nil)
43
+ assert(table[0][2].shading_colour == nil)
44
+ assert(table[1][0].shading_colour == @colours[0])
45
+ assert(table[1][1].shading_colour == @colours[0])
46
+ assert(table[1][2].shading_colour == @colours[0])
47
+ assert(table[2][0].shading_colour == nil)
48
+ assert(table[2][1].shading_colour == nil)
49
+ assert(table[2][2].shading_colour == nil)
50
+
51
+ table.column_shading_colour(2, @colours[1])
52
+ assert(table[0][0].shading_colour == nil)
53
+ assert(table[0][1].shading_colour == nil)
54
+ assert(table[0][2].shading_colour == @colours[1])
55
+ assert(table[1][0].shading_colour == @colours[0])
56
+ assert(table[1][1].shading_colour == @colours[0])
57
+ assert(table[1][2].shading_colour == @colours[1])
58
+ assert(table[2][0].shading_colour == nil)
59
+ assert(table[2][1].shading_colour == nil)
60
+ assert(table[2][2].shading_colour == @colours[1])
61
+
62
+ table.shading_colour(@colours[2]) {|cell, x, y| x == y}
63
+ assert(table[0][0].shading_colour == @colours[2])
64
+ assert(table[0][1].shading_colour == nil)
65
+ assert(table[0][2].shading_colour == @colours[1])
66
+ assert(table[1][0].shading_colour == @colours[0])
67
+ assert(table[1][1].shading_colour == @colours[2])
68
+ assert(table[1][2].shading_colour == @colours[1])
69
+ assert(table[2][0].shading_colour == nil)
70
+ assert(table[2][1].shading_colour == nil)
71
+ assert(table[2][2].shading_colour == @colours[2])
72
+ end
73
+
74
+ def test_border_width
75
+ table = TableNode.new(@document, 2, 2)
76
+
77
+ table.border_width = 5
78
+ assert(table[0][0].border_widths == [5, 5, 5, 5])
79
+ assert(table[0][1].border_widths == [5, 5, 5, 5])
80
+ assert(table[1][0].border_widths == [5, 5, 5, 5])
81
+ assert(table[1][1].border_widths == [5, 5, 5, 5])
82
+
83
+ table.border_width = 0
84
+ assert(table[0][0].border_widths == [0, 0, 0, 0])
85
+ assert(table[0][1].border_widths == [0, 0, 0, 0])
86
+ assert(table[1][0].border_widths == [0, 0, 0, 0])
87
+ assert(table[1][1].border_widths == [0, 0, 0, 0])
88
+ end
89
+ end
@@ -0,0 +1,65 @@
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 TableRowNodeTest < Test::Unit::TestCase
11
+ def setup
12
+ @table = TableNode.new(nil, 3, 3, 100, 100, 100)
13
+ end
14
+
15
+ def test_basics
16
+ rows = []
17
+ rows.push(TableRowNode.new(@table, 10))
18
+ rows.push(TableRowNode.new(@table, 3, 100, 200, 300))
19
+
20
+ assert(rows[0].size == 10)
21
+ assert(rows[1].size == 3)
22
+
23
+ assert(rows[0][0].width == TableCellNode::DEFAULT_WIDTH)
24
+ assert(rows[0][1].width == TableCellNode::DEFAULT_WIDTH)
25
+ assert(rows[0][2].width == TableCellNode::DEFAULT_WIDTH)
26
+ assert(rows[0][3].width == TableCellNode::DEFAULT_WIDTH)
27
+ assert(rows[0][4].width == TableCellNode::DEFAULT_WIDTH)
28
+ assert(rows[0][5].width == TableCellNode::DEFAULT_WIDTH)
29
+ assert(rows[0][6].width == TableCellNode::DEFAULT_WIDTH)
30
+ assert(rows[0][7].width == TableCellNode::DEFAULT_WIDTH)
31
+ assert(rows[0][8].width == TableCellNode::DEFAULT_WIDTH)
32
+ assert(rows[0][9].width == TableCellNode::DEFAULT_WIDTH)
33
+ assert(rows[1][0].width == 100)
34
+ assert(rows[1][1].width == 200)
35
+ assert(rows[1][2].width == 300)
36
+
37
+ assert(rows[0][1].border_widths == [0, 0, 0, 0])
38
+ rows[0].border_width = 10
39
+ assert(rows[0][1].border_widths == [10, 10, 10, 10])
40
+ end
41
+
42
+ def test_exceptions
43
+ row = TableRowNode.new(@table, 1)
44
+ begin
45
+ row.parent = nil
46
+ flunk("Successfully called the TableRowNode#parent=() method.")
47
+ rescue
48
+ end
49
+ end
50
+
51
+ def test_rtf_generation
52
+ rows = []
53
+ rows.push(TableRowNode.new(@table, 3, 50, 50, 50))
54
+ rows.push(TableRowNode.new(@table, 1, 134))
55
+ rows[1].border_width = 5
56
+ assert(rows[0].to_rtf == "\\trowd\\tgraph100\n\\cellx50\n\\cellx100\n"\
57
+ "\\cellx150\n\\pard\\intbl\n\n\\cell\n"\
58
+ "\\pard\\intbl\n\n\\cell\n"\
59
+ "\\pard\\intbl\n\n\\cell\n\\row")
60
+ assert(rows[1].to_rtf == "\\trowd\\tgraph100\n"\
61
+ "\\clbrdrt\\brdrw5\\brdrs\\clbrdrl\\brdrw5\\brdrs"\
62
+ "\\clbrdrb\\brdrw5\\brdrs\\clbrdrr\\brdrw5\\brdrs"\
63
+ "\\cellx134\n\\pard\\intbl\n\n\\cell\n\\row")
64
+ end
65
+ end
@@ -0,0 +1,56 @@
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 TextNodeTest < Test::Unit::TestCase
11
+ def setup
12
+ @node = Node.new(nil)
13
+ end
14
+
15
+ def test01
16
+ nodes = []
17
+ nodes.push(TextNode.new(@node))
18
+ nodes.push(TextNode.new(@node, 'Node 2'))
19
+ nodes.push(TextNode.new(@node))
20
+ nodes.push(TextNode.new(@node, ''))
21
+
22
+ assert(nodes[0].text == nil)
23
+ assert(nodes[1].text == 'Node 2')
24
+ assert(nodes[2].text == nil)
25
+ assert(nodes[3].text == '')
26
+
27
+ nodes[0].text = 'This is the altered text for node 1.'
28
+ assert(nodes[0].text == 'This is the altered text for node 1.')
29
+
30
+ nodes[1].append('La la la')
31
+ nodes[2].append('La la la')
32
+ assert(nodes[1].text == 'Node 2La la la')
33
+ assert(nodes[2].text == 'La la la')
34
+
35
+ nodes[2].text = nil
36
+ nodes[1].insert(' - ', 6)
37
+ nodes[2].insert('TEXT', 2)
38
+ assert(nodes[1].text == 'Node 2 - La la la')
39
+ assert(nodes[2].text == 'TEXT')
40
+
41
+ nodes[2].text = nil
42
+ nodes[3].text = '{\}'
43
+ assert(nodes[0].to_rtf == 'This is the altered text for node 1.')
44
+ assert(nodes[1].to_rtf == 'Node 2 - La la la')
45
+ assert(nodes[2].to_rtf == '')
46
+ assert(nodes[3].to_rtf == '\{\\\}')
47
+ end
48
+
49
+ def test02
50
+ begin
51
+ TextNode.new(nil)
52
+ flunk('Successfully created a TextNode with a nil parent.')
53
+ rescue => error
54
+ end
55
+ end
56
+ end