forked from zdavatz/oddb.org
/
test_writer.rb
executable file
·240 lines (234 loc) · 8.78 KB
/
test_writer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env ruby
#encoding: utf-8
# ODDB::ChapterParse::TestWriter -- oddb.org -- 06.06.2011 -- mhatakeyama@ywesee.com
# ODDB::ChapterParse::TestWriter -- oddb.org -- 12.08.2005 -- ffricker@ywesee.com
$: << File.expand_path('../../../src', File.dirname(__FILE__))
$: << File.expand_path('../src', File.dirname(__FILE__))
require 'minitest/autorun'
require 'flexmock/minitest'
require 'writer'
module ODDB
module ChapterParse
class TestWriter <Minitest::Test
def setup
@writer = Writer.new
end
def test_section
@writer.new_font([nil, 1, nil, nil])
@writer.send_flowing_data("Subheading")
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal('Subheading', section.subheading)
assert_equal(0, section.paragraphs.size)
end
def test_section_and_paragraph
@writer.new_font([nil, 1, nil, nil])
@writer.send_flowing_data('Subheading')
@writer.new_font(nil)
@writer.send_flowing_data("\302\240First Paragraph")
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal('Subheading', section.subheading)
assert_equal(1, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal('First Paragraph', paragraph.text)
end
def test_section_and_paragraph__on_new_line
@writer.new_font([nil, 1, nil, nil])
@writer.send_flowing_data("Subheading")
@writer.send_line_break
@writer.new_font(nil)
@writer.send_flowing_data('First Paragraph')
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal("Subheading\n", section.subheading)
assert_equal(1, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal('First Paragraph', paragraph.text)
end
def test_section_and_paragraph__on_new_line__2
@writer.new_font([nil, 1, nil, nil])
@writer.send_flowing_data("Subheading")
@writer.new_font(nil)
@writer.send_line_break
@writer.send_flowing_data('First Paragraph')
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal("Subheading\n", section.subheading)
assert_equal(1, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal('First Paragraph', paragraph.text)
end
def test_section_and_2_paragraphs
@writer.new_font([nil, 1, nil, nil])
@writer.send_flowing_data('Subheading')
@writer.new_font(nil)
@writer.send_flowing_data('First Paragraph')
@writer.send_line_break
@writer.send_flowing_data('Second Paragraph')
@writer.send_line_break
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal('Subheading', section.subheading)
assert_equal(2, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal('First Paragraph', paragraph.text)
paragraph = section.paragraphs.last
assert_equal('Second Paragraph', paragraph.text)
end
def test_2_sections_and_paragraphs
@writer.new_font([nil, 1, nil, nil])
@writer.send_flowing_data('Subheading 1')
@writer.new_font(nil)
@writer.send_flowing_data('First Paragraph')
@writer.send_line_break
@writer.new_font([nil, 1, nil, nil])
@writer.send_flowing_data('Subheading 2')
@writer.new_font(nil)
@writer.send_flowing_data('Second Paragraph')
@writer.send_line_break
chapter = @writer.chapter
assert_equal(2, chapter.sections.size)
section = chapter.sections.first
assert_equal('Subheading 1', section.subheading)
assert_equal(1, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal('First Paragraph', paragraph.text)
section = chapter.sections.last
assert_equal('Subheading 2', section.subheading)
assert_equal(1, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal('Second Paragraph', paragraph.text)
end
def test_section_without_subheading
@writer.send_flowing_data('First Paragraph')
@writer.send_line_break
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal('', section.subheading)
assert_equal(1, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal('First Paragraph', paragraph.text)
end
def test_paragraph_with_italic
@writer.send_flowing_data('First Paragraph')
@writer.new_font([nil, 1, nil, nil])
@writer.send_flowing_data(' with italic text')
@writer.new_font(nil)
@writer.send_flowing_data(' and some more normal.')
@writer.send_line_break
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal('', section.subheading)
assert_equal(1, section.paragraphs.size)
paragraph = section.paragraphs.first
expected = 'First Paragraph with italic text and some more normal.'
assert_equal(expected, paragraph.text)
assert_equal(3, paragraph.formats.size)
format = paragraph.formats.at(1)
assert_equal(true, format.italic?)
assert_equal(15..31, format.range)
end
def test_preformatted
@writer.new_font([nil, nil, nil, 1])
@writer.send_literal_data('First Paragraph')
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal('', section.subheading)
assert_equal(1, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal(true, paragraph.preformatted?)
assert_equal('First Paragraph', paragraph.text)
end
def test_preformatted_2_lines
@writer.new_font([nil, nil, nil, 1])
@writer.send_literal_data('First Line')
@writer.send_line_break
@writer.new_font(nil)
@writer.send_paragraph({})
@writer.new_font([nil, nil, nil, 1])
@writer.send_literal_data('Second Line')
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal('', section.subheading)
assert_equal(1, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal(true, paragraph.preformatted?)
expected = "First Line\nSecond Line"
assert_equal(expected, paragraph.text)
end
def test_preformatted_2_lines__too_many_newlines
@writer.new_font([nil, nil, nil, 1])
@writer.send_literal_data('First Line')
@writer.send_line_break
@writer.new_font(nil)
@writer.send_paragraph({})
@writer.send_line_break
@writer.send_line_break
@writer.send_line_break
@writer.new_font([nil, nil, nil, 1])
@writer.send_literal_data('Second Line')
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal('', section.subheading)
assert_equal(1, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal(true, paragraph.preformatted?)
expected = "First Line\nSecond Line"
assert_equal(expected, paragraph.text)
end
def test_preformatted_to_normal
@writer.new_font([nil, nil, nil, 1])
@writer.send_literal_data('First Line')
@writer.send_line_break
@writer.new_font(nil)
@writer.send_paragraph({})
@writer.send_flowing_data('Second Line')
chapter = @writer.chapter
assert_equal(1, chapter.sections.size)
section = chapter.sections.first
assert_equal('', section.subheading)
assert_equal(2, section.paragraphs.size)
paragraph = section.paragraphs.first
assert_equal(true, paragraph.preformatted?)
expected = "First Line\n"
assert_equal(expected, paragraph.text)
paragraph = section.paragraphs.last
assert_equal(false, paragraph.preformatted?)
expected = "Second Line"
assert_equal(expected, paragraph.text)
end
def test_new_fonthandler
fonthandler = flexmock('fonthandler', :attribute => nil)
assert_kind_of(ODDB::Text::Format, @writer.new_fonthandler(fonthandler))
end
def test_new_fonthandler__vertical_align
fonthandler = flexmock('fonthandler') do |fh|
fh.should_receive(:attribute).once.with('face').and_return('Symbol')
fh.should_receive(:attribute).once.with('vertical-align').and_return('align')
end
assert_kind_of(ODDB::Text::Format, @writer.new_fonthandler(fonthandler))
end
def test_new_tablehandler
tablehandler = flexmock('tablehandler')
assert_nil(@writer.new_tablehandler(tablehandler))
assert_kind_of(ODDB::Text::Paragraph, @writer.new_tablehandler(nil))
end
def test_send_line_break
tablehandler = flexmock('tablehandler', :next_line => 'next_line')
@writer.new_tablehandler(tablehandler)
assert_equal('next_line', @writer.send_line_break)
end
end
end
end