review 0.6.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. data/ChangeLog +441 -0
  2. data/README.rdoc +25 -0
  3. data/Rakefile +13 -1
  4. data/VERSION +1 -1
  5. data/bin/review-check +1 -1
  6. data/bin/review-compile +19 -10
  7. data/bin/review-epubmaker +114 -17
  8. data/bin/review-index +8 -1
  9. data/bin/review-pdfmaker +378 -0
  10. data/bin/review-preproc +2 -3
  11. data/bin/review-vol +1 -2
  12. data/debian/README.Debian +12 -0
  13. data/debian/README.source +5 -0
  14. data/debian/changelog +5 -0
  15. data/debian/compat +1 -0
  16. data/debian/control +22 -0
  17. data/debian/copyright +60 -0
  18. data/debian/docs +5 -0
  19. data/debian/manpage.1.ex +59 -0
  20. data/debian/patches/path.diff +91 -0
  21. data/debian/patches/series +1 -0
  22. data/debian/review.install +13 -0
  23. data/debian/review.links +4 -0
  24. data/debian/rules +13 -0
  25. data/debian/source/format +1 -0
  26. data/doc/format.rdoc +477 -0
  27. data/doc/format.re +19 -0
  28. data/doc/format_idg.rdoc +180 -0
  29. data/doc/ruby-uuid/README +11 -0
  30. data/doc/ruby-uuid/README.ja +34 -0
  31. data/doc/sample.css +17 -0
  32. data/doc/sample.yaml +8 -4
  33. data/lib/lineinput.rb +1 -1
  34. data/lib/review/book.rb +43 -36
  35. data/lib/review/builder.rb +78 -33
  36. data/lib/review/compiler.rb +45 -48
  37. data/lib/review/epubbuilder.rb +1 -675
  38. data/lib/review/exception.rb +1 -1
  39. data/lib/review/htmlbuilder.rb +627 -49
  40. data/lib/review/htmlutils.rb +5 -0
  41. data/lib/review/idgxmlbuilder.rb +239 -250
  42. data/lib/review/index.rb +84 -7
  43. data/lib/review/latexbuilder.rb +261 -42
  44. data/lib/review/latexutils.rb +15 -6
  45. data/lib/review/preprocessor.rb +40 -6
  46. data/lib/review/textutils.rb +22 -0
  47. data/lib/review/topbuilder.rb +4 -1
  48. data/lib/uuid.rb +312 -0
  49. data/review.gemspec +44 -12
  50. data/test/CHAPS +2 -0
  51. data/test/bib.re +13 -0
  52. data/test/test.re +43 -0
  53. data/test/test_book.rb +1191 -0
  54. data/test/test_builder.rb +147 -0
  55. data/test/test_htmlbuilder.rb +191 -10
  56. data/test/test_htmlutils.rb +24 -0
  57. data/test/test_idgxmlbuilder.rb +310 -0
  58. data/test/test_index.rb +15 -0
  59. data/test/test_latexbuilder.rb +217 -6
  60. data/test/test_lineinput.rb +198 -0
  61. data/test/test_textutils.rb +68 -0
  62. data/test/test_uuid.rb +156 -0
  63. metadata +43 -10
  64. data/doc/format.txt +0 -434
  65. data/doc/format_idg.txt +0 -194
  66. data/doc/format_sjis.txt +0 -313
  67. data/setup.rb +0 -1587
  68. data/test/test_epubbuilder.rb +0 -73
@@ -5,14 +5,14 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{review}
8
- s.version = "0.6.0"
8
+ s.version = "0.9.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["kmuto", "takahashim"]
12
- s.date = %q{2010-07-15}
13
- s.description = %q{ReVIEW is a digital publishing system for books and ebooks. It supports InDesin, EPUB and LaTeX.}
12
+ s.date = %q{2010-12-01}
13
+ s.description = %q{ReVIEW is a digital publishing system for books and ebooks. It supports InDesign, EPUB and LaTeX.}
14
14
  s.email = %q{kmuto@debian.org}
15
- s.executables = ["review-check", "review-checkdep", "review-compile", "review-epubmaker", "review-index", "review-preproc", "review-validate", "review-vol"]
15
+ s.executables = ["review-check", "review-checkdep", "review-compile", "review-epubmaker", "review-index", "review-pdfmaker", "review-preproc", "review-validate", "review-vol"]
16
16
  s.extra_rdoc_files = [
17
17
  "ChangeLog",
18
18
  "README.rdoc"
@@ -28,13 +28,29 @@ Gem::Specification.new do |s|
28
28
  "bin/review-compile",
29
29
  "bin/review-epubmaker",
30
30
  "bin/review-index",
31
+ "bin/review-pdfmaker",
31
32
  "bin/review-preproc",
32
33
  "bin/review-validate",
33
34
  "bin/review-vol",
35
+ "debian/README.Debian",
36
+ "debian/README.source",
37
+ "debian/changelog",
38
+ "debian/compat",
39
+ "debian/control",
40
+ "debian/copyright",
41
+ "debian/docs",
42
+ "debian/manpage.1.ex",
43
+ "debian/patches/path.diff",
44
+ "debian/patches/series",
45
+ "debian/review.install",
46
+ "debian/review.links",
47
+ "debian/rules",
48
+ "debian/source/format",
49
+ "doc/format.rdoc",
34
50
  "doc/format.re",
35
- "doc/format.txt",
36
- "doc/format_idg.txt",
37
- "doc/format_sjis.txt",
51
+ "doc/format_idg.rdoc",
52
+ "doc/ruby-uuid/README",
53
+ "doc/ruby-uuid/README.ja",
38
54
  "doc/sample.css",
39
55
  "doc/sample.yaml",
40
56
  "lib/lineinput.rb",
@@ -61,12 +77,21 @@ Gem::Specification.new do |s|
61
77
  "lib/review/topbuilder.rb",
62
78
  "lib/review/unfold.rb",
63
79
  "lib/review/volume.rb",
80
+ "lib/uuid.rb",
64
81
  "review.gemspec",
65
- "setup.rb",
66
- "test/test_epubbuilder.rb",
82
+ "test/CHAPS",
83
+ "test/bib.re",
84
+ "test/test.re",
85
+ "test/test_book.rb",
86
+ "test/test_builder.rb",
67
87
  "test/test_helper.rb",
68
88
  "test/test_htmlbuilder.rb",
69
- "test/test_latexbuilder.rb"
89
+ "test/test_htmlutils.rb",
90
+ "test/test_idgxmlbuilder.rb",
91
+ "test/test_latexbuilder.rb",
92
+ "test/test_lineinput.rb",
93
+ "test/test_textutils.rb",
94
+ "test/test_uuid.rb"
70
95
  ]
71
96
  s.homepage = %q{http://github.com/kmuto/review}
72
97
  s.rdoc_options = ["--charset=UTF-8"]
@@ -74,10 +99,17 @@ Gem::Specification.new do |s|
74
99
  s.rubygems_version = %q{1.3.6}
75
100
  s.summary = %q{ReVIEW: a easy-to-use digital publishing system}
76
101
  s.test_files = [
77
- "test/test_epubbuilder.rb",
102
+ "test/test_book.rb",
103
+ "test/test_builder.rb",
78
104
  "test/test_helper.rb",
79
105
  "test/test_htmlbuilder.rb",
80
- "test/test_latexbuilder.rb"
106
+ "test/test_htmlutils.rb",
107
+ "test/test_idgxmlbuilder.rb",
108
+ "test/test_index.rb",
109
+ "test/test_latexbuilder.rb",
110
+ "test/test_lineinput.rb",
111
+ "test/test_textutils.rb",
112
+ "test/test_uuid.rb"
81
113
  ]
82
114
 
83
115
  if s.respond_to? :specification_version then
@@ -0,0 +1,2 @@
1
+ test.re
2
+ bib.re
@@ -0,0 +1,13 @@
1
+
2
+ //bibpaper[lins][Lins, 1991]{
3
+ Refael D. Lins. A shared memory architecture for parallel study of
4
+ algorithums for cyclic reference_counting. Technical Report 92,
5
+ Computing Laboratory, The University of Kent at Canterbury , August
6
+ 1991
7
+ //}
8
+
9
+ //bibpaper[nari][nari, 1991]{
10
+ nari paper
11
+ //}
12
+
13
+ //bibpaper[nari][Hogefuge]
@@ -0,0 +1,43 @@
1
+ = 見出し
2
+ 以下を実行
3
+ //emlist{
4
+ ./bin/review-compile --inencoding=UTF-8 --outencoding=UTF-8 --hdnumberingmode test.re --target=html
5
+ }
6
+ == 節1
7
+
8
+ == 節2
9
+
10
+ ==[column]コラム
11
+
12
+ == 節3
13
+
14
+ ソースコードの引用
15
+
16
+ //source[/hello/world.rb]{
17
+ puts "hello world!"
18
+ //}
19
+
20
+ ■行番号付きキャプションなしリスト
21
+ //emlistnum{
22
+ hoge
23
+ fuge
24
+ //}
25
+
26
+ ■行番号付きキャプションありリスト
27
+ //listnum[hoge][ほげ]{
28
+ hoge
29
+ fuge
30
+ //}
31
+
32
+ ほげは@<list>{hoge}でもわかるとおり
33
+
34
+ ■本文中でのソースコード引用
35
+ 擬似コード内のの@<code>{p = obj.ref_cnt}では…
36
+
37
+ ■参考文献の参照方法
38
+ …がしられています( @<bib>{lins} )
39
+
40
+
41
+ //emlist{
42
+ hoge
43
+ //}
@@ -0,0 +1,1191 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test_helper'
4
+ require 'review/book'
5
+
6
+ require 'stringio'
7
+ require 'tempfile'
8
+ require 'tmpdir'
9
+
10
+ include ReVIEW
11
+
12
+ module BookTestHelper
13
+ def mktmpbookdir(files = {})
14
+ created_files = {}
15
+ Dir.mktmpdir do |dir|
16
+ files.each_pair do |basename, content|
17
+ path = File.join(dir, basename)
18
+ File.open(path, 'w') {|o| o.print content }
19
+ created_files[basename] = path
20
+ end
21
+ book = Book.new(dir)
22
+ yield(dir, book, created_files)
23
+ end
24
+ end
25
+
26
+ def get_instance_variables(obj)
27
+ obj.instance_variables.inject({}) do |memo, name|
28
+ value = obj.instance_variable_get(name)
29
+ if value.instance_variables.empty?
30
+ memo[name] = value
31
+ else
32
+ memo[name] = get_instance_variables(value)
33
+ end
34
+ memo
35
+ end
36
+ end
37
+ end
38
+
39
+ class BookTest < Test::Unit::TestCase
40
+ include BookTestHelper
41
+
42
+ def assert_same_path(expected, result, *options)
43
+ require 'pathname'
44
+ ex_path = Pathname(expected).realpath
45
+ re_path = Pathname(result).realpath
46
+ assert_equal ex_path, re_path, *options
47
+ end
48
+
49
+ def test_s_load_default
50
+ Dir.mktmpdir do |dir|
51
+ File.open(File.join(dir, 'CHAPS'), 'w') {}
52
+ Dir.chdir(dir) do
53
+ assert_same_path dir, File.expand_path(Book.load_default.basedir), "error in dir CHAPS"
54
+ end
55
+
56
+ subdir = File.join(dir, 'sub')
57
+ Dir.mkdir(subdir)
58
+ Dir.chdir(subdir) do
59
+ assert_same_path dir, File.expand_path(Book.load_default.basedir), "error in dir sub"
60
+ end
61
+
62
+ sub2dir = File.join(dir, 'sub', 'sub')
63
+ Dir.mkdir(sub2dir)
64
+ Dir.chdir(sub2dir) do
65
+ assert_same_path dir, File.expand_path(Book.load_default.basedir), "error in dir sub sub"
66
+ end
67
+
68
+ sub3dir = File.join(dir, 'sub', 'sub', 'sub')
69
+ Dir.mkdir(sub3dir)
70
+ Dir.chdir(sub3dir) do
71
+ assert_same_path sub3dir, File.expand_path(Book.load_default.basedir), "error in dir sub sub sub"
72
+ end
73
+ end
74
+
75
+ # tests for ReVIEW.book
76
+ default_book = nil
77
+ Dir.mktmpdir do |dir|
78
+ Dir.chdir(dir) do
79
+ default_book = ReVIEW.book
80
+ assert default_book
81
+ end
82
+ end
83
+ Dir.mktmpdir do |dir|
84
+ Dir.chdir(dir) do
85
+ assert_equal default_book, ReVIEW.book, "chdir mktmpdir"
86
+ end
87
+ end
88
+ end
89
+
90
+ def test_s_load
91
+ Dir.mktmpdir do |dir|
92
+ book = Book.load(dir)
93
+ defs = get_instance_variables(Parameters.default)
94
+ pars = get_instance_variables(book.instance_eval { @parameters })
95
+ assert_equal defs, pars
96
+ end
97
+ end
98
+
99
+ def test_s_update_rubyenv
100
+ save_load_path = $LOAD_PATH.dup
101
+
102
+ Dir.mktmpdir do |dir|
103
+ Book.update_rubyenv(dir)
104
+ assert_equal save_load_path, $LOAD_PATH
105
+ end
106
+
107
+ Dir.mktmpdir do |dir|
108
+ local_lib_path = File.join(dir, 'lib')
109
+ Dir.mkdir(local_lib_path)
110
+ Book.update_rubyenv(dir)
111
+ assert_equal save_load_path, $LOAD_PATH
112
+ end
113
+
114
+ begin
115
+ Dir.mktmpdir do |dir|
116
+ local_lib_path = File.join(dir, 'lib')
117
+ Dir.mkdir(local_lib_path)
118
+ Dir.mkdir(File.join(local_lib_path, 'review'))
119
+ Book.update_rubyenv(dir)
120
+ assert save_load_path != $LOAD_PATH
121
+ assert $LOAD_PATH.index(local_lib_path)
122
+ end
123
+ ensure
124
+ $LOAD_PATH.replace save_load_path
125
+ end
126
+
127
+ num = rand(99999)
128
+ test_const = "ReVIEW__BOOK__TEST__#{num}"
129
+ begin
130
+ Dir.mktmpdir do |dir|
131
+ File.open(File.join(dir, 'review-ext.rb'), 'w') do |o|
132
+ o.puts "#{test_const} = #{num}"
133
+ end
134
+ Book.update_rubyenv(dir)
135
+ assert_equal num, Object.class_eval { const_get(test_const) }
136
+ end
137
+ ensure
138
+ Object.class_eval { remove_const(test_const) }
139
+ end
140
+ end
141
+
142
+ def test_ext
143
+ book = Book.new(File.dirname(__FILE__))
144
+ assert_equal '.re', book.ext
145
+ end
146
+
147
+ def test_read_CHAPS
148
+ Dir.mktmpdir do |dir|
149
+ book = Book.new(dir)
150
+ assert_equal "", book.read_CHAPS
151
+
152
+ chaps_path = File.join(dir, 'CHAPS')
153
+ re1_path = File.join(dir, "123#{book.ext}")
154
+ re2_path = File.join(dir, "456#{book.ext}")
155
+
156
+ File.open(chaps_path, 'w') {|o| o.print "abc\n" }
157
+ File.open(re1_path, 'w') {|o| o.print "123\n" }
158
+ File.open(re2_path, 'w') {|o| o.print "456\n" }
159
+
160
+ assert_equal "abc\n", book.read_CHAPS
161
+
162
+ File.unlink(chaps_path)
163
+ assert_equal "#{re1_path}\n#{re2_path}", book.read_CHAPS
164
+
165
+ File.unlink(re1_path)
166
+ assert_equal "#{re2_path}", book.read_CHAPS
167
+
168
+ File.unlink(re2_path)
169
+ assert_equal "", book.read_CHAPS
170
+ end
171
+ end
172
+
173
+ def test_read_PART
174
+ Dir.mktmpdir do |dir|
175
+ book = Book.new(dir)
176
+ assert !book.part_exist?
177
+ assert_raises Errno::ENOENT do # XXX: OK?
178
+ book.read_PART
179
+ end
180
+
181
+ chaps_path = File.join(dir, 'CHAPS')
182
+ chaps_content = "abc\n"
183
+ File.open(chaps_path, 'w') {|o| o.print chaps_content }
184
+
185
+ assert book.part_exist?
186
+ assert_equal chaps_content, book.read_PART
187
+
188
+ File.open(chaps_path, 'w') {|o| o.print "XYZ\n" }
189
+ assert_equal chaps_content, book.read_PART
190
+ end
191
+ end
192
+
193
+ def test_read_bib
194
+ Dir.mktmpdir do |dir|
195
+ book = Book.new(dir)
196
+ assert !book.bib_exist?
197
+ assert_raises Errno::ENOENT do # XXX: OK?
198
+ book.read_bib
199
+ end
200
+
201
+ bib_path = File.join(dir, "bib#{book.ext}")
202
+ File.open(bib_path, 'w') {|o| o.print "abc\n" }
203
+
204
+ assert book.bib_exist?
205
+ assert_equal "abc\n", book.read_bib
206
+ end
207
+ end
208
+
209
+ def test_setParameter
210
+ book = Book.new(File.dirname(__FILE__))
211
+ book.param = :test
212
+ assert_equal :test, book.instance_eval {@param}
213
+ end
214
+
215
+ def test_parse_chapters
216
+ mktmpbookdir 'CHAPS' => '' do |dir, book, files|
217
+ parts = book.instance_eval { parse_chapters }
218
+ assert_equal 0, parts.size
219
+ end
220
+
221
+ mktmpbookdir 'CHAPS' => "chapter1.re\nchapter2\n" do |dir, book, files|
222
+ parts = book.instance_eval { parse_chapters }
223
+ assert_equal 1, parts.size
224
+
225
+ assert_equal 1, parts[0].number
226
+ assert_equal 'chapter1.re', parts[0].name # XXX: OK?
227
+ assert_equal 2, parts[0].chapters.size
228
+ chaps = parts[0].chapters.map {|ch| [ch.number, ch.name, ch.path] }
229
+ expect = [
230
+ [1, 'chapter1', File.join(dir, 'chapter1.re')],
231
+ [2, 'chapter2', File.join(dir, 'chapter2')],
232
+ ]
233
+ assert_equal expect, chaps
234
+ end
235
+
236
+ mktmpbookdir 'CHAPS' => <<EOC do |dir, book, files|
237
+ part1_chapter1.re
238
+ part1_chapter2.re
239
+
240
+
241
+ part2_chapter1.re
242
+ part2_chapter2.re
243
+ part2_chapter3.re
244
+
245
+ part3_chapter1.re
246
+ EOC
247
+ parts = book.instance_eval { parse_chapters }
248
+ assert_equal 3, parts.size
249
+
250
+ assert_equal 1, parts[0].number
251
+ assert_equal 'part1_chapter1.re', parts[0].name
252
+ assert_equal 2, parts[0].chapters.size
253
+ chaps = parts[0].chapters.map {|ch| [ch.number, ch.name, ch.path] }
254
+ expect = [
255
+ [1, 'part1_chapter1', File.join(dir, 'part1_chapter1.re')],
256
+ [2, 'part1_chapter2', File.join(dir, 'part1_chapter2.re')],
257
+ ]
258
+ assert_equal expect, chaps
259
+
260
+ assert_equal 2, parts[1].number
261
+ assert_equal 'part1_chapter2.re', parts[1].name # XXX: OK?
262
+ assert_equal 3, parts[1].chapters.size
263
+ chaps = parts[1].chapters.map {|ch| [ch.number, ch.name, ch.path] }
264
+ expect = [
265
+ [3, 'part2_chapter1', File.join(dir, 'part2_chapter1.re')],
266
+ [4, 'part2_chapter2', File.join(dir, 'part2_chapter2.re')],
267
+ [5, 'part2_chapter3', File.join(dir, 'part2_chapter3.re')],
268
+ ]
269
+ assert_equal expect, chaps
270
+
271
+ assert_equal 3, parts[2].number
272
+ assert_equal '', parts[2].name # XXX: OK?
273
+ assert_equal 1, parts[2].chapters.size
274
+ chaps = parts[2].chapters.map {|ch| [ch.number, ch.name, ch.path] }
275
+ expect = [
276
+ [6, 'part3_chapter1', File.join(dir, 'part3_chapter1.re')],
277
+ ]
278
+ assert_equal expect, chaps
279
+ end
280
+ end
281
+
282
+ def test_parse_chpaters_with_parts_file
283
+ n_test = 0
284
+ [
285
+ [
286
+ # 期待されるパートの数, :chapter_fileの内容, :part_fileの内容, 期待されるパートタイトルのリスト
287
+ 2,
288
+ "part1_chapter1.re\n\npart2_chpater1.re\n",
289
+ "part1\npart2\npart3\n",
290
+ %w(part1 part2),
291
+ ],
292
+ [
293
+ 3,
294
+ "part1_chapter1.re\n\npart2_chapter1.re\n\npart3_chapter1.re",
295
+ "part1\n",
296
+ [
297
+ "part1",
298
+ nil, # XXX: OK?
299
+ ""
300
+ ],
301
+ ],
302
+ [
303
+ 1,
304
+ "part1_chapter1.re\n",
305
+ "",
306
+ [
307
+ nil, # XXX: OK?
308
+ ],
309
+ ],
310
+ [
311
+ 1,
312
+ "part1_chapter1.re\n",
313
+ nil,
314
+ [
315
+ "",
316
+ ],
317
+ ],
318
+ ].each do |n_parts, chaps_text, parts_text, part_names|
319
+ n_test += 1
320
+ Dir.mktmpdir do |dir|
321
+ params = Parameters.new(:part_file => 'PARTS')
322
+ book = Book.new(dir, params)
323
+ chaps_path = File.join(dir, 'CHAPS')
324
+ File.open(chaps_path, 'w') {|o| o.print chaps_text }
325
+ unless parts_text.nil?
326
+ parts_path = File.join(dir, 'PARTS')
327
+ File.open(parts_path, 'w') {|o| o.print parts_text }
328
+ end
329
+
330
+ parts = book.instance_eval { parse_chapters }
331
+ assert_equal n_parts, parts.size, "\##{n_test}"
332
+ assert_equal part_names, parts.map {|p| p.name }, "\##{n_test}"
333
+ end
334
+ end
335
+ end
336
+
337
+ def test_prefaces
338
+ mktmpbookdir do |dir, book, files|
339
+ assert_equal nil, book.prefaces
340
+ end
341
+
342
+ mktmpbookdir 'preface.re' => '' do |dir, book, files|
343
+ assert_kind_of Part, book.prefaces
344
+ assert_equal '', book.prefaces.name
345
+ assert_equal 1, book.prefaces.chapters.size
346
+ assert_equal "preface", book.prefaces.chapters.first.name
347
+ assert_equal files['preface.re'], book.prefaces.chapters.first.path
348
+ assert_equal nil, book.prefaces.chapters.first.number
349
+ end
350
+
351
+ mktmpbookdir 'preface.re' => '',
352
+ 'PREDEF' => '' do |dir, book, files|
353
+ assert_equal nil, book.prefaces # XXX: OK?
354
+ end
355
+
356
+ mktmpbookdir 'PREDEF' => '' do |dir, book, files|
357
+ assert_equal nil, book.prefaces
358
+ end
359
+
360
+ mktmpbookdir 'PREDEF' => 'chapter1',
361
+ 'chapter1.re' => '' do |dir, book, files|
362
+ assert_kind_of Part, book.prefaces
363
+ assert_equal '', book.prefaces.name
364
+ assert_equal 1, book.prefaces.chapters.size
365
+ assert_equal "chapter1", book.prefaces.chapters.first.name
366
+ assert_equal files['chapter1.re'], book.prefaces.chapters.first.path
367
+ end
368
+
369
+ mktmpbookdir 'PREDEF' => "chapter1\n\nchapter2",
370
+ 'chapter1.re' => '', 'chapter2.re' => '' do |dir, book, files|
371
+ assert_kind_of Part, book.prefaces
372
+ assert_equal '', book.prefaces.name
373
+ assert_equal 2, book.prefaces.chapters.size
374
+ assert_equal "chapter1", book.prefaces.chapters.first.name
375
+ assert_equal files['chapter1.re'], book.prefaces.chapters.first.path
376
+ assert_equal "chapter2", book.prefaces.chapters.last.name
377
+ assert_equal files['chapter2.re'], book.prefaces.chapters.last.path
378
+ end
379
+
380
+ mktmpbookdir 'PREDEF' => "chapter1 chapter2",
381
+ 'chapter1.re' => '', 'chapter2.re' => '' do |dir, book, files|
382
+ assert_kind_of Part, book.prefaces
383
+ assert_equal '', book.prefaces.name
384
+ assert_equal 2, book.prefaces.chapters.size # XXX: OK?
385
+ end
386
+
387
+ mktmpbookdir 'PREDEF' => 'not_exist' do |dir, book, files|
388
+ assert_raises FileNotFound do
389
+ assert_equal nil, book.prefaces
390
+ end
391
+ end
392
+
393
+ mktmpbookdir 'PREDEF' => 'chapter1.re',
394
+ 'chapter1.re' => '' do |dir, book, files|
395
+ assert_kind_of Part, book.prefaces
396
+ assert_equal '', book.prefaces.name
397
+ assert_equal 1, book.prefaces.chapters.size
398
+ end
399
+ end
400
+
401
+ def test_postscripts
402
+ mktmpbookdir do |dir, book, files|
403
+ assert_equal nil, book.postscripts
404
+ end
405
+
406
+ mktmpbookdir 'appendix.re' => '' do |dir, book, files|
407
+ assert_kind_of Part, book.postscripts
408
+ assert_equal '', book.postscripts.name
409
+ assert_equal 1, book.postscripts.chapters.size
410
+ assert_equal "appendix", book.postscripts.chapters.first.name
411
+ assert_equal files['appendix.re'], book.postscripts.chapters.first.path
412
+ assert_equal nil, book.postscripts.chapters.first.number
413
+ end
414
+
415
+ mktmpbookdir 'postscript.re' => '' do |dir, book, files|
416
+ assert_kind_of Part, book.postscripts
417
+ assert_equal '', book.postscripts.name
418
+ assert_equal 1, book.postscripts.chapters.size
419
+ assert_equal "postscript", book.postscripts.chapters.first.name
420
+ assert_equal files['postscript.re'], book.postscripts.chapters.first.path
421
+ assert_equal nil, book.postscripts.chapters.first.number
422
+ end
423
+
424
+ mktmpbookdir 'appendix.re' => '',
425
+ 'postscript.re' => '' do |dir, book, files|
426
+ assert_kind_of Part, book.postscripts
427
+ assert_equal '', book.postscripts.name
428
+ assert_equal 2, book.postscripts.chapters.size
429
+ assert_equal "appendix", book.postscripts.chapters.first.name
430
+ assert_equal files['appendix.re'], book.postscripts.chapters.first.path
431
+ assert_equal nil, book.postscripts.chapters.first.number
432
+ assert_equal "postscript", book.postscripts.chapters.last.name
433
+ assert_equal files['postscript.re'], book.postscripts.chapters.last.path
434
+ assert_equal nil, book.postscripts.chapters.last.number
435
+ end
436
+
437
+ mktmpbookdir 'preface.re' => '',
438
+ 'POSTDEF' => '' do |dir, book, files|
439
+ assert_equal nil, book.postscripts # XXX: OK?
440
+ end
441
+
442
+ mktmpbookdir 'POSTDEF' => '' do |dir, book, files|
443
+ assert_equal nil, book.postscripts
444
+ end
445
+
446
+ mktmpbookdir 'POSTDEF' => 'chapter1',
447
+ 'chapter1.re' => '' do |dir, book, files|
448
+ assert_kind_of Part, book.postscripts
449
+ assert_equal '', book.postscripts.name
450
+ assert_equal 1, book.postscripts.chapters.size
451
+ assert_equal "chapter1", book.postscripts.chapters.first.name
452
+ assert_equal files['chapter1.re'], book.postscripts.chapters.first.path
453
+ end
454
+
455
+ mktmpbookdir 'POSTDEF' => "chapter1\n\nchapter2",
456
+ 'chapter1.re' => '', 'chapter2.re' => '' do |dir, book, files|
457
+ assert_kind_of Part, book.postscripts
458
+ assert_equal '', book.postscripts.name
459
+ assert_equal 2, book.postscripts.chapters.size
460
+ assert_equal "chapter1", book.postscripts.chapters.first.name
461
+ assert_equal files['chapter1.re'], book.postscripts.chapters.first.path
462
+ assert_equal "chapter2", book.postscripts.chapters.last.name
463
+ assert_equal files['chapter2.re'], book.postscripts.chapters.last.path
464
+ end
465
+
466
+ mktmpbookdir 'POSTDEF' => "chapter1 chapter2",
467
+ 'chapter1.re' => '', 'chapter2.re' => '' do |dir, book, files|
468
+ assert_kind_of Part, book.postscripts
469
+ assert_equal '', book.postscripts.name
470
+ assert_equal 2, book.postscripts.chapters.size # XXX: OK?
471
+ end
472
+
473
+ mktmpbookdir 'POSTDEF' => 'not_exist' do |dir, book, files|
474
+ assert_raises FileNotFound do
475
+ assert_equal nil, book.postscripts
476
+ end
477
+ end
478
+ end
479
+
480
+ def test_parts
481
+ mktmpbookdir do |dir, book, files|
482
+ assert book.parts.empty?
483
+ assert !book.part(0)
484
+ assert !book.part(1)
485
+ assert !book.no_part?
486
+
487
+ tmp = []
488
+ book.each_part { tmp << true }
489
+ assert tmp.empty?
490
+ end
491
+
492
+ mktmpbookdir 'CHAPS' => "ch1\nch2\n\nch3" do |dir, book, files|
493
+ parts = book.parts
494
+ assert_equal 2, parts.size
495
+ assert !book.part(0)
496
+ assert book.part(1)
497
+ assert book.part(2)
498
+ assert !book.part(3)
499
+ assert !book.no_part? # XXX: OK?
500
+
501
+ tmp = []
502
+ book.each_part {|p| tmp << p.number }
503
+ assert_equal [1, 2], tmp
504
+ end
505
+
506
+ mktmpbookdir 'CHAPS' => "ch1\nch2\n\nch3",
507
+ 'preface.re' => '' do |dir, book, files|
508
+ parts = book.parts
509
+ assert_equal 3, parts.size
510
+ assert book.part(1)
511
+ assert book.part(2)
512
+ assert !book.part(3)
513
+ assert book.part(nil) # XXX: OK?
514
+ assert_equal 'preface', parts.first.chapters.first.name
515
+
516
+ tmp = []
517
+ book.each_part {|p| tmp << p.number }
518
+ assert_equal [nil, 1, 2], tmp
519
+ end
520
+
521
+ mktmpbookdir 'CHAPS' => "ch1\nch2\n\nch3",
522
+ 'postscript.re' => '' do |dir, book, files|
523
+ parts = book.parts
524
+ assert_equal 3, parts.size
525
+ assert book.part(1)
526
+ assert book.part(2)
527
+ assert !book.part(3)
528
+ assert book.part(nil) # XXX: OK?
529
+ assert_equal 'postscript', parts.last.chapters.last.name
530
+
531
+ tmp = []
532
+ book.each_part {|p| tmp << p.number }
533
+ assert_equal [1, 2, nil], tmp
534
+ end
535
+
536
+ mktmpbookdir 'CHAPS' => "ch1\nch2\n\nch3",
537
+ 'preface.re' => '', 'postscript.re' => '' do |dir, book, files|
538
+ parts = book.parts
539
+ assert_equal 4, parts.size
540
+ assert book.part(1)
541
+ assert book.part(2)
542
+ assert !book.part(3)
543
+ assert !book.part(4)
544
+ assert book.part(nil) # XXX: OK?
545
+ assert_equal 'preface', parts.first.chapters.first.name
546
+ assert_equal 'postscript', parts.last.chapters.last.name
547
+
548
+ tmp = []
549
+ book.each_part {|p| tmp << p.number }
550
+ assert_equal [nil, 1, 2, nil], tmp
551
+ end
552
+ end
553
+
554
+ def test_chapters
555
+ mktmpbookdir 'CHAPS' => "ch1\nch2\n\nch3",
556
+ 'preface.re' => '', 'postscript.re' => '' do |dir, book, files|
557
+ chapters = book.chapters
558
+ assert_equal 5, chapters.size
559
+
560
+ ch_names = %w(preface ch1 ch2 ch3 postscript)
561
+ tmp = []
562
+ book.each_chapter {|ch| tmp << ch.name }
563
+ assert_equal ch_names, tmp
564
+
565
+ ch_names.each do |name|
566
+ assert book.chapter(name)
567
+ assert_equal name, book.chapter(name).name
568
+ end
569
+
570
+ assert_raises IndexError,KeyError do
571
+ book.chapter('not exist')
572
+ end
573
+ end
574
+ end
575
+
576
+ def test_volume
577
+ mktmpbookdir do |dir, book, files|
578
+ assert book.volume
579
+ assert_equal 0, book.volume.bytes
580
+ assert_equal 0, book.volume.chars
581
+ assert_equal 0, book.volume.lines
582
+ end
583
+
584
+ mktmpbookdir 'CHAPS' => 'chapter1.re', 'chapter1.re' => '12345' do |dir, book, files|
585
+ assert book.volume
586
+ assert book.volume.bytes > 0
587
+ assert book.volume.chars > 0
588
+ assert book.volume.lines > 0
589
+ end
590
+
591
+ mktmpbookdir 'preface.re' => '12345' do |dir, book, files|
592
+ assert_raises Errno::ENOENT do # XXX: OK?
593
+ book.volume
594
+ end
595
+
596
+ Dir.chdir(dir) do
597
+ book2 = Book.new('.')
598
+ assert book2.volume
599
+ assert book2.volume.bytes > 0
600
+ assert book2.volume.chars > 0
601
+ assert book2.volume.lines > 0
602
+ end
603
+ end
604
+ end
605
+
606
+ def test_basedir
607
+ Dir.mktmpdir do |dir|
608
+ book = Book.new(dir)
609
+ assert_equal dir, book.basedir
610
+ end
611
+ end
612
+ end
613
+
614
+ class ParametersTest < Test::Unit::TestCase
615
+ def test_s_default
616
+ assert Parameters.default
617
+ end
618
+
619
+ def test_s_load
620
+ Tempfile.open('parameters_test') do |io|
621
+ io.puts 'CHAPS_FILE = "x_CHAPS"'
622
+ io.puts 'PAPER = "B5"' # XXX: avoid erros of the last line of Parameters.get_page_metric
623
+ io.close
624
+
625
+ params = Parameters.load(io.path)
626
+ assert_equal '/x_CHAPS', params.chapter_file # XXX: OK? (leading / and uninitialized @basedir)
627
+ assert_equal '/CHAPS', params.part_file
628
+ end
629
+ end
630
+
631
+ def test_s_get_page_metric
632
+ mod = Module.new
633
+ assert_raises ArgumentError do # XXX: OK?
634
+ params = Parameters.get_page_metric(mod)
635
+ assert params
636
+ end
637
+
638
+ mod = Module.new
639
+ mod.module_eval { const_set(:PAPER, 'A5') }
640
+ assert_nothing_raised do
641
+ params = Parameters.get_page_metric(mod)
642
+ assert params
643
+ end
644
+
645
+ mod = Module.new
646
+ mod.module_eval { const_set(:PAPER, 'X5') }
647
+ assert_raises ConfigError do
648
+ Parameters.get_page_metric(mod)
649
+ end
650
+ end
651
+ end
652
+
653
+ class PartTest < Test::Unit::TestCase
654
+ def test_initialize
655
+ part = Part.new(nil, nil)
656
+ assert_equal nil, part.number
657
+ assert_equal nil, part.chapters
658
+ assert_equal '', part.name
659
+
660
+ part = Part.new(123, [], 'name')
661
+ assert_equal 123, part.number
662
+ assert_equal [], part.chapters
663
+ assert_equal 'name', part.name
664
+ end
665
+
666
+ def test_each_chapter
667
+ part = Part.new(nil, [1, 2, 3])
668
+
669
+ tmp = []
670
+ part.each_chapter do |ch|
671
+ tmp << ch
672
+ end
673
+ assert_equal [1, 2, 3], tmp
674
+ end
675
+
676
+ def test_volume
677
+ part = Part.new(nil, [])
678
+ assert part.volume
679
+ assert_equal 0, part.volume.bytes
680
+ assert_equal 0, part.volume.chars
681
+ assert_equal 0, part.volume.lines
682
+
683
+ chs = []
684
+ Tempfile.open('part_test') do |o|
685
+ o.print "12345"
686
+ chs << Chapter.new(nil, nil, nil, o.path)
687
+ end
688
+ Tempfile.open('part_test') do |o|
689
+ o.print "67890"
690
+ chs << Chapter.new(nil, nil, nil, o.path)
691
+ end
692
+
693
+ part = Part.new(nil, chs)
694
+ assert part.volume
695
+ assert part.volume.bytes > 0
696
+ assert part.volume.chars > 0
697
+ assert part.volume.lines > 0
698
+ end
699
+ end
700
+
701
+ class ChapterTest < Test::Unit::TestCase
702
+ include BookTestHelper
703
+
704
+ def setup
705
+
706
+ if "".respond_to?(:encode)
707
+ @utf8_str = "あいうえお"
708
+ @eucjp_str = "あいうえお".encode("EUC-JP")
709
+ @sjis_str = "あいうえお".encode("Shift_JIS")
710
+ @jis_str = "あいうえお".encode("ISO-2022-JP")
711
+ else
712
+ @utf8_str = "\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a" # "あいうえお"
713
+ @eucjp_str = "\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa"
714
+ @sjis_str = "\x82\xa0\x82\xa2\x82\xa4\x82\xa6\x82\xa8"
715
+ @jis_str = "\x1b\x24\x42\x24\x22\x24\x24\x24\x26\x24\x28\x24\x2a\x1b\x28\x42"
716
+ end
717
+ =begin
718
+ @utf8_str = "\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a" # "あいうえお"
719
+ @eucjp_str = "\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa"
720
+ @sjis_str = "\x82\xa0\x82\xa2\x82\xa4\x82\xa6\x82\xa8"
721
+ @jis_str = "\x1b\x24\x42\x24\x22\x24\x24\x24\x26\x24\x28\x24\x2a\x1b\x28\x42"
722
+ =end
723
+ end
724
+
725
+ def test_s_intern_pathes
726
+ dir1_files = {
727
+ 'CHAPS' => "ch1.re\nch2.re\n",
728
+ 'ch1.re' => 'ch1',
729
+ 'ch2.re' => 'ch2',
730
+ }
731
+ dir2_files = {
732
+ 'CHAPS' => "ch1.re\n",
733
+ 'ch1.re' => 'ch1',
734
+ }
735
+ mktmpbookdir dir1_files do |dir1, book1, files1|
736
+ mktmpbookdir dir2_files do |dir2, book2, files2|
737
+ paths = (files1.values + files2.values).flatten.grep(/\.re\z/)
738
+ chs = Chapter.intern_pathes(paths)
739
+
740
+ assert_equal 3, chs.size
741
+ assert chs[0].book == chs[1].book
742
+ assert chs[0].book != chs[2].book
743
+ assert dir1, chs[0].book.basedir
744
+ assert dir2, chs[2].book.basedir
745
+ end
746
+ end
747
+
748
+ dir_files = {
749
+ 'CHAPS' => "ch1.re\n",
750
+ 'ch1.re' => 'ch1',
751
+ }
752
+ mktmpbookdir dir_files do |dir, book, files|
753
+ paths = files.values.grep(/\.re\z/)
754
+ paths << __FILE__ + ' not exist file.re'
755
+ assert_raises IndexError,KeyError do # XXX: OK? (KeyError?)
756
+ Chapter.intern_pathes(paths)
757
+ end
758
+ end
759
+
760
+ dir_files = {
761
+ 'ch1.re' => 'ch1 not in CHAPS',
762
+ 'ch2.re' => 'ch2 not in CHAPS',
763
+ }
764
+ mktmpbookdir dir_files do |dir, book, files|
765
+ paths = files.values.grep(/\.re\z/)
766
+ assert_nothing_raised do
767
+ Chapter.intern_pathes(paths)
768
+ end
769
+ end
770
+
771
+ dir_files = {
772
+ 'CHAPS' => "ch1.re\n",
773
+ 'ch1.re' => 'ch1',
774
+ 'ch2.re' => 'ch2 not in CHAPS',
775
+ }
776
+ mktmpbookdir dir_files do |dir, book, files|
777
+ paths = files.values.grep(/\.re\z/)
778
+ assert_raises IndexError,KeyError do # XXX: OK? (should not be raised?)
779
+ Chapter.intern_pathes(paths)
780
+ end
781
+ end
782
+
783
+ dir_files = {
784
+ 'CHAPS' => "ch1.re\n",
785
+ 'ch1.re' => 'ch1',
786
+ 'preface.re' => 'preface',
787
+ }
788
+ mktmpbookdir dir_files do |dir, book, files|
789
+ paths = files.values.grep(/\.re\z/)
790
+ assert_nothing_raised do
791
+ Chapter.intern_pathes(paths)
792
+ end
793
+ end
794
+ end
795
+
796
+ def test_s_for_stdin
797
+ assert Chapter.for_stdin
798
+ end
799
+
800
+ def test_s_for_path
801
+ assert Chapter.for_path(1, __FILE__)
802
+ end
803
+
804
+ def test_initialize
805
+ ch = Chapter.new(:book, :number, :name, '/foo/bar', :io)
806
+ assert_equal :book, ch.env
807
+ assert_equal :book, ch.book
808
+ assert_equal :number, ch.number
809
+ assert_equal '/foo/bar', ch.path
810
+ assert_equal "#<ReVIEW::Chapter number /foo/bar>", ch.inspect
811
+ end
812
+
813
+ def test_dirname_and_basename
814
+ ch = Chapter.new(nil, nil, nil, nil, nil)
815
+ assert_equal nil, ch.dirname
816
+ assert_equal nil, ch.basename
817
+
818
+ ch = Chapter.new(nil, nil, nil, '/foo/bar', nil)
819
+ assert_equal '/foo', ch.dirname
820
+ assert_equal 'bar', ch.basename
821
+
822
+ ch = Chapter.new(nil, nil, nil, 'bar', nil)
823
+ assert_equal '.', ch.dirname
824
+ assert_equal 'bar', ch.basename
825
+ end
826
+
827
+ def test_name
828
+ ch = Chapter.new(nil, nil, 'foo', nil)
829
+ assert_equal 'foo', ch.name
830
+
831
+ ch = Chapter.new(nil, nil, 'foo.bar', nil)
832
+ assert_equal 'foo', ch.name
833
+
834
+ ch = Chapter.new(nil, nil, nil, nil)
835
+ assert_raises(TypeError) { ch.name } # XXX: OK?
836
+ end
837
+
838
+ def test_open
839
+ ch = Chapter.new(nil, nil, nil, __FILE__, :io)
840
+ assert_equal :io, ch.open
841
+ assert_equal [:io], ch.open {|io| [io] }
842
+
843
+ ch = Chapter.new(nil, nil, nil, __FILE__)
844
+ assert_equal __FILE__, ch.open.path
845
+ assert_equal [__FILE__], ch.open {|io| [io.path] }
846
+ end
847
+
848
+ def test_size
849
+ ch = Chapter.new(nil, nil, nil, __FILE__, :io)
850
+ assert_equal File.size(__FILE__), ch.size
851
+
852
+ File.open(__FILE__, 'r') do |i|
853
+ ch = Chapter.new(nil, nil, nil, nil, i)
854
+ assert_raises(TypeError) do # XXX: OK?
855
+ ch.size
856
+ end
857
+ end
858
+ end
859
+
860
+ def test_title
861
+ io = StringIO.new
862
+ ch = Chapter.new(nil, nil, nil, nil, io)
863
+ assert_equal '', ch.title
864
+
865
+ io = StringIO.new("=1\n=2\n")
866
+ ch = Chapter.new(nil, nil, nil, nil, io)
867
+ assert_equal '1', ch.title
868
+
869
+
870
+ [
871
+ ['EUC', @eucjp_str],
872
+ ['SJIS', @sjis_str],
873
+ # ['JIS', @jis_str],
874
+ ['XYZ', @eucjp_str],
875
+ ].each do |enc, instr|
876
+ io = StringIO.new("= #{instr}\n")
877
+ ch = Chapter.new(nil, nil, nil, nil, io)
878
+ ReVIEW.book.param = {'inencoding' => enc}
879
+ assert_equal @utf8_str, ch.title
880
+ assert_equal @utf8_str, ch.instance_eval { @title }
881
+ end
882
+ end
883
+
884
+ def test_content
885
+ [
886
+ ['EUC', @eucjp_str],
887
+ ['SJIS', @sjis_str],
888
+ ['JIS', @jis_str],
889
+ ['XYZ', @eucjp_str],
890
+ ].each do |enc, instr|
891
+ tf = Tempfile.new('chapter_test')
892
+ begin
893
+ tf.print instr
894
+ tf.close
895
+
896
+ ch = Chapter.new(nil, nil, nil, tf.path)
897
+ ReVIEW.book.param = {'inencoding' => enc}
898
+ assert_equal @utf8_str, ch.content
899
+ assert_equal @utf8_str, ch.instance_eval { @content }
900
+ ensure
901
+ tf.close(true)
902
+ end
903
+
904
+ tf1 = Tempfile.new('chapter_test1')
905
+ tf2 = Tempfile.new('chapter_test2')
906
+ begin
907
+ tf1.puts instr
908
+ tf1.puts instr
909
+ tf1.close
910
+ tf2.puts instr
911
+ tf1.close
912
+
913
+ ch = Chapter.new(nil, nil, nil, tf1.path, tf2)
914
+ ReVIEW.book.param = {'inencoding' => enc}
915
+ assert_equal "#{@utf8_str}\n#{@utf8_str}\n", ch.content # XXX: OK?
916
+ ensure
917
+ tf1.close(true)
918
+ tf2.close(true)
919
+ end
920
+ end
921
+ end
922
+
923
+ def test_lines
924
+ lines = ["1\n", "2\n", "3"]
925
+ tf = Tempfile.new('chapter_test')
926
+ tf.print lines.join('')
927
+ tf.close
928
+
929
+ ch = Chapter.new(nil, nil, nil, tf.path)
930
+ assert_equal lines, ch.lines
931
+
932
+ lines = ["1\n", "2\n", "3"]
933
+ tf1 = Tempfile.new('chapter_test1')
934
+ tf1.print lines.join('')
935
+ tf1.close
936
+ tf2 = Tempfile.new('chapter_test2')
937
+ tf2.puts lines.join('')
938
+ tf2.puts lines.join('')
939
+ tf2.close
940
+
941
+ ch = Chapter.new(nil, nil, nil, tf1.path, tf2.path)
942
+ assert_equal lines, ch.lines # XXX: OK?
943
+ end
944
+
945
+ def test_volume
946
+ content = "abc\ndef"
947
+ tf1 = Tempfile.new('chapter_test1')
948
+ tf1.print content
949
+ tf1.close
950
+ tf2 = Tempfile.new('chapter_test2')
951
+ tf2.print content
952
+ tf2.print content
953
+ tf2.close
954
+
955
+ ch = Chapter.new(nil, nil, nil, tf1.path)
956
+ assert ch.volume
957
+ assert_equal content.gsub(/\s/, '').size, ch.volume.bytes
958
+
959
+ ch = Chapter.new(nil, nil, nil, tf1.path, tf2)
960
+ assert ch.volume
961
+ assert_equal content.gsub(/\s/, '').size, ch.volume.bytes # XXX: OK?
962
+ end
963
+
964
+ def test_on_CHAPS?
965
+ mktmpbookdir 'CHAPS' => "chapter1.re\nchapter2.re",
966
+ 'chapter1.re' => '12345', 'preface.re' => 'abcde' do |dir, book, files|
967
+ ch1 = Chapter.new(book, 1, 'chapter1', files['chapter1.re'])
968
+ pre = Chapter.new(book, nil, 'preface', files['preface.re'])
969
+
970
+ assert ch1.on_CHAPS?
971
+ assert !pre.on_CHAPS?
972
+
973
+ ch2_path = File.join(dir, 'chapter2.er')
974
+ File.open(ch2_path, 'w') {}
975
+ ch2 = Chapter.new(book, 2, 'chapter2', ch2_path)
976
+
977
+ ch3_path = File.join(dir, 'chapter3.er')
978
+ File.open(ch3_path, 'w') {}
979
+ ch3 = Chapter.new(book, 3, 'chapter3', ch3_path)
980
+
981
+ assert ch2.on_CHAPS?
982
+ assert !ch3.on_CHAPS?
983
+ end
984
+ end
985
+
986
+ def test_list_index
987
+ do_test_index(<<E, ListIndex, :list_index, :list)
988
+ //list
989
+ //listnum [abc]
990
+ //list [def]
991
+ //table [def]
992
+ //table [others]
993
+ E
994
+ end
995
+
996
+ def test_table_index
997
+ do_test_index(<<E, TableIndex, :table_index, :table)
998
+ //table
999
+ //table [abc]
1000
+ //table [def]
1001
+ //list [def]
1002
+ //list [others]
1003
+ E
1004
+ end
1005
+
1006
+ def test_footnote_index
1007
+ content = <<E
1008
+ //footnote
1009
+ //footnote [abc][text...]
1010
+ //footnote [def][text...]
1011
+ //footnote [xyz]
1012
+ //list [def]
1013
+ //list [others]
1014
+ E
1015
+ do_test_index(content, FootnoteIndex, :footnote_index, :footnote) do |ch|
1016
+ assert_raises IndexError,KeyError do
1017
+ ch.footnote('xyz')
1018
+ end
1019
+ end
1020
+ end
1021
+
1022
+ def test_bibpaper
1023
+ do_test_index(<<E, BibpaperIndex, :bibpaper_index, :bibpaper, :filename => 'bib.re')
1024
+ //bibpaper
1025
+ //bibpaper [abc][text...]
1026
+ //bibpaper [def][text...]
1027
+ //bibpaper [xyz]
1028
+ //list [def]
1029
+ //list [others]
1030
+ E
1031
+ assert_raises FileNotFound do
1032
+ do_test_index('', BibpaperIndex, :bibpaper_index, :bibpaper, :filename => 'bib')
1033
+ end
1034
+ end
1035
+
1036
+ def test_headline_index
1037
+ do_test_index(<<E, HeadlineIndex, :headline_index, :headline, :propagate => false)
1038
+ ==
1039
+ == abc
1040
+ == def
1041
+ === def
1042
+ //table others
1043
+ E
1044
+ end
1045
+
1046
+ def test_image
1047
+ do_test_index(<<E, ImageIndex, :image_index, :image)
1048
+ //image
1049
+ //image [abc]
1050
+ //image [def]
1051
+ //list [def]
1052
+ //list [others]
1053
+ E
1054
+
1055
+ do_test_index(<<E, NumberlessImageIndex, :numberless_image_index, :image, :propagate => false)
1056
+ //numberlessimage
1057
+ //numberlessimage [abc]
1058
+ //numberlessimage [def]
1059
+ //list [def]
1060
+ //list [others]
1061
+ E
1062
+
1063
+ do_test_index(<<E, ImageIndex, :image_index, :image)
1064
+ //image
1065
+ //numberlessimage [abc]
1066
+ //image [def]
1067
+ //list [def]
1068
+ //list [others]
1069
+ E
1070
+
1071
+ do_test_index(<<E, NumberlessImageIndex, :numberless_image_index, :image, :propagate => false)
1072
+ //image
1073
+ //numberlessimage [abc]
1074
+ //image [def]
1075
+ //list [def]
1076
+ //list [others]
1077
+ E
1078
+ end
1079
+
1080
+ def do_test_index(content, klass, list_method, ref_method, opts = {})
1081
+ Dir.mktmpdir do |dir|
1082
+ path = File.join(dir, opts[:filename] || 'chapter.re')
1083
+
1084
+ book = Book.new(dir)
1085
+
1086
+ ch = nil
1087
+ File.open(path, 'w') do |o|
1088
+ o.print content
1089
+ ch = Chapter.new(book, 1, 'chapter', o.path)
1090
+ end
1091
+
1092
+ assert ch.__send__(ref_method, 'abc')
1093
+ assert ch.__send__(ref_method, 'def')
1094
+ assert_raises IndexError,KeyError do
1095
+ ch.__send__(ref_method, nil)
1096
+ end
1097
+ assert_raises IndexError,KeyError do
1098
+ ch.__send__(ref_method, 'others')
1099
+ end
1100
+ assert_raises IndexError,KeyError do
1101
+ ch.__send__(ref_method, 'not exist id')
1102
+ end
1103
+
1104
+ yield(ch) if block_given?
1105
+ end
1106
+ end
1107
+ end
1108
+
1109
+ class ChapterSetTest < Test::Unit::TestCase
1110
+ include BookTestHelper
1111
+
1112
+ def test_s_for_pathes
1113
+ dir_files = {
1114
+ 'ch1.re' => 'ch1',
1115
+ 'ch2.re' => 'ch2',
1116
+ }
1117
+ mktmpbookdir dir_files do |dir, book, files|
1118
+ paths = files.values.grep(/\.re\z/)
1119
+ cs = nil
1120
+ assert_nothing_raised do
1121
+ cs = ChapterSet.for_pathes(paths)
1122
+ end
1123
+ assert_equal 2, cs.chapters.size
1124
+ end
1125
+ end
1126
+
1127
+ def test_s_for_argv
1128
+ begin
1129
+ paths = []
1130
+ ChapterSet.class_eval { const_set(:ARGV, paths) }
1131
+
1132
+ dir_files = {
1133
+ 'ch1.re' => 'ch1',
1134
+ 'ch2.re' => 'ch2',
1135
+ }
1136
+ mktmpbookdir dir_files do |dir, book, files|
1137
+ paths.concat files.values.grep(/\.re\z/)
1138
+ cs = nil
1139
+ assert_nothing_raised do
1140
+ cs = ChapterSet.for_argv
1141
+ end
1142
+ assert_equal 2, cs.chapters.size
1143
+ assert_equal ['ch1', 'ch2'], cs.chapters.map(&:name).sort
1144
+ end
1145
+
1146
+ ensure
1147
+ ChapterSet.class_eval { remove_const(:ARGV) }
1148
+ ChapterSet.class_eval { const_set(:ARGV, []) }
1149
+ end
1150
+
1151
+ begin
1152
+ $stdin = StringIO.new('abc')
1153
+ cs = nil
1154
+ assert_nothing_raised do
1155
+ cs = ChapterSet.for_argv
1156
+ end
1157
+ assert_equal 1, cs.chapters.size
1158
+ assert_equal '-', cs.chapters.first.name
1159
+
1160
+ ensure
1161
+ $stdin = STDIN
1162
+ end
1163
+ end
1164
+
1165
+ def test_no_part?
1166
+ cs = ChapterSet.new([])
1167
+ assert cs.no_part?
1168
+
1169
+ ch = Chapter.new(nil, nil, nil, nil, StringIO.new)
1170
+ cs = ChapterSet.new([ch])
1171
+ assert cs.no_part?
1172
+ end
1173
+
1174
+ def test_chapters
1175
+ ch1 = Chapter.new(nil, 123, nil, nil, StringIO.new)
1176
+ ch2 = Chapter.new(nil, 456, nil, nil, StringIO.new)
1177
+ cs = ChapterSet.new([ch1, ch2])
1178
+ assert_equal [123, 456], cs.chapters.map(&:number)
1179
+
1180
+ tmp = [ch1, ch2]
1181
+ cs.each_chapter do |ch|
1182
+ assert tmp.delete(ch)
1183
+ end
1184
+ assert tmp.empty?
1185
+ end
1186
+
1187
+ def test_ext
1188
+ cs = ChapterSet.new([])
1189
+ assert_equal '.re', cs.ext
1190
+ end
1191
+ end