1require 'rdoc/test_case'
2
3class TestRDocParserChangeLog < RDoc::TestCase
4
5  def setup
6    super
7
8    @tempfile  = Tempfile.new 'ChangeLog'
9    @top_level = @store.add_file @tempfile.path
10    @options   = RDoc::Options.new
11    @stats     = RDoc::Stats.new @store, 0
12  end
13
14  def teardown
15    @tempfile.close
16  end
17
18  def mu_pp obj
19    s = ''
20    s = PP.pp obj, s
21    s = s.force_encoding Encoding.default_external if defined? Encoding
22    s.chomp
23  end
24
25  def test_class_can_parse
26    parser = RDoc::Parser::ChangeLog
27
28    temp_dir do
29      FileUtils.touch 'ChangeLog'
30      assert_equal parser, parser.can_parse('ChangeLog')
31
32      assert_equal parser, parser.can_parse(@tempfile.path)
33
34      FileUtils.touch 'ChangeLog.rb'
35      assert_equal RDoc::Parser::Ruby, parser.can_parse('ChangeLog.rb')
36    end
37  end
38
39  def test_continue_entry_body
40    parser = util_parser
41
42    entry_body = ['a']
43
44    parser.continue_entry_body entry_body, 'b'
45
46    assert_equal ['a b'], entry_body
47  end
48
49  def test_continue_entry_body_empty
50    parser = util_parser
51
52    entry_body = []
53
54    parser.continue_entry_body entry_body, ''
55
56    assert_empty entry_body
57  end
58
59  def test_continue_entry_body_function
60    parser = util_parser
61
62    entry_body = ['file: (func1)']
63
64    parser.continue_entry_body entry_body, '(func2): blah'
65
66    assert_equal ['file: (func1, func2): blah'], entry_body
67  end
68
69  def test_create_document
70    parser = util_parser
71
72    groups = {
73      '2012-12-04' => [
74        ['Tue Dec  4 08:33:46 2012  Eric Hodel  <drbrain@segment7.net>',
75          %w[a:one b:two]],
76        ['Tue Dec  4 08:32:10 2012  Eric Hodel  <drbrain@segment7.net>',
77          %w[c:three d:four]]],
78      '2012-12-03' => [
79        ['Mon Dec  3 20:28:02 2012  Koichi Sasada  <ko1@atdot.net>',
80          %w[e:five f:six]]],
81    }
82
83    expected =
84      doc(
85        head(1, File.basename(@tempfile.path)),
86        blank_line,
87        head(2, '2012-12-04'),
88        blank_line,
89        head(3, 'Tue Dec  4 08:33:46 2012  Eric Hodel  <drbrain@segment7.net>'),
90        blank_line,
91        list(:NOTE, item('a', para('one')), item('b', para('two'))),
92        head(3, 'Tue Dec  4 08:32:10 2012  Eric Hodel  <drbrain@segment7.net>'),
93        blank_line,
94        list(:NOTE, item('c', para('three')), item('d', para('four'))),
95        head(2, '2012-12-03'),
96        blank_line,
97        head(3, 'Mon Dec  3 20:28:02 2012  Koichi Sasada  <ko1@atdot.net>'),
98        blank_line,
99        list(:NOTE, item('e', para('five')), item('f', para('six'))))
100
101    expected.file = @top_level
102
103    document = parser.create_document(groups)
104
105    assert_equal expected, document
106
107    assert_equal 2, document.omit_headings_below
108
109    headings = document.parts.select do |part|
110      RDoc::Markup::Heading === part and part.level == 2
111    end
112
113    refute headings.all? { |heading| heading.text.frozen? }
114  end
115
116  def test_create_entries
117    parser = util_parser
118
119    entries = [
120      ['Tue Dec  1 02:03:04 2012  Eric Hodel  <drbrain@segment7.net>',
121        %w[a:one b:two]],
122      ['Tue Dec  5 06:07:08 2012  Eric Hodel  <drbrain@segment7.net>',
123        %w[c:three d:four]],
124    ]
125
126    expected = [
127      head(3, 'Tue Dec  1 02:03:04 2012  Eric Hodel  <drbrain@segment7.net>'),
128      blank_line,
129      list(:NOTE, item('a', para('one')), item('b', para('two'))),
130      head(3, 'Tue Dec  5 06:07:08 2012  Eric Hodel  <drbrain@segment7.net>'),
131      blank_line,
132      list(:NOTE, item('c', para('three')), item('d', para('four'))),
133    ]
134
135    entries = parser.create_entries(entries)
136    assert_equal expected, entries
137  end
138
139  def test_create_entries_colons
140    parser = util_parser
141
142    entries = [
143      ['Wed Dec  5 12:17:11 2012  Naohisa Goto  <ngotogenome@gmail.com>',
144        ['func.rb (DL::Function#bind): log stuff [ruby-core:50562]']],
145    ]
146
147    expected = [
148      head(3,
149           'Wed Dec  5 12:17:11 2012  Naohisa Goto  <ngotogenome@gmail.com>'),
150      blank_line,
151      list(:NOTE,
152           item('func.rb (DL::Function#bind)',
153                para('log stuff [ruby-core:50562]')))]
154
155    assert_equal expected, parser.create_entries(entries)
156  end
157
158  def test_create_items
159    parser = util_parser
160
161    items = [
162	    'README.EXT:  Converted to RDoc format',
163	    'README.EXT.ja:  ditto',
164    ]
165
166    expected =
167      list(:NOTE,
168        item('README.EXT',
169          para('Converted to RDoc format')),
170        item('README.EXT.ja',
171          para('ditto')))
172
173    assert_equal expected, parser.create_items(items)
174  end
175
176  def test_group_entries
177    parser = util_parser
178
179    entries = [
180      [ 'Tue Dec  4 08:33:46 2012  Eric Hodel  <drbrain@segment7.net>',
181        %w[one two]],
182      [ 'Tue Dec  4 08:32:10 2012  Eric Hodel  <drbrain@segment7.net>',
183        %w[three four]],
184      [ 'Mon Dec  3 20:28:02 2012  Koichi Sasada  <ko1@atdot.net>',
185        %w[five six]],
186      [ '2008-01-30  H.J. Lu  <hongjiu.lu@intel.com>',
187        %w[seven eight]]]
188
189    expected = {
190      '2012-12-04' => [
191        ['Tue Dec  4 08:33:46 2012  Eric Hodel  <drbrain@segment7.net>',
192          %w[one two]],
193        ['Tue Dec  4 08:32:10 2012  Eric Hodel  <drbrain@segment7.net>',
194          %w[three four]]],
195      '2012-12-03' => [
196        ['Mon Dec  3 20:28:02 2012  Koichi Sasada  <ko1@atdot.net>',
197          %w[five six]]],
198      '2008-01-30' => [
199        ['2008-01-30  H.J. Lu  <hongjiu.lu@intel.com>',
200          %w[seven eight]]],
201    }
202
203    assert_equal expected, parser.group_entries(entries)
204  end
205
206  def test_parse_entries
207    parser = util_parser <<-ChangeLog
208Tue Dec  4 08:33:46 2012  Eric Hodel  <drbrain@segment7.net>
209
210	* README.EXT:  Converted to RDoc format
211	* README.EXT.ja:  ditto
212
213Mon Dec  3 20:28:02 2012  Koichi Sasada  <ko1@atdot.net>
214
215	* compile.c (iseq_specialized_instruction):
216	  change condition of using `opt_send_simple'.
217	  More method invocations can be simple.
218
219Other note that will be ignored
220
221    ChangeLog
222
223    expected = [
224      [ 'Tue Dec  4 08:33:46 2012  Eric Hodel  <drbrain@segment7.net>',
225        [ 'README.EXT:  Converted to RDoc format',
226          'README.EXT.ja:  ditto']],
227      [ 'Mon Dec  3 20:28:02 2012  Koichi Sasada  <ko1@atdot.net>',
228        [ 'compile.c (iseq_specialized_instruction): change condition of ' +
229          'using `opt_send_simple\'. More method invocations can be simple.']]]
230
231    assert_equal expected, parser.parse_entries
232  end
233
234  def test_parse_entries_bad_time
235    parser = util_parser <<-ChangeLog
2362008-01-30  H.J. Lu  <hongjiu.lu@intel.com>
237
238        PR libffi/34612
239        * src/x86/sysv.S (ffi_closure_SYSV): Pop 4 byte from stack when
240        returning struct.
241
242    ChangeLog
243
244    expected = [
245      [ '2008-01-30  H.J. Lu  <hongjiu.lu@intel.com>',
246        [ 'src/x86/sysv.S (ffi_closure_SYSV): Pop 4 byte from stack when ' +
247          'returning struct.']]
248    ]
249
250    assert_equal expected, parser.parse_entries
251  end
252
253  def test_parse_entries_gnu
254    parser = util_parser <<-ChangeLog
2551998-08-17  Richard Stallman  <rms@gnu.org>
256
257* register.el (insert-register): Return nil.
258(jump-to-register): Likewise.
259
260* sort.el (sort-subr): Return nil.
261
262* keyboard.c (menu_bar_items, tool_bar_items)
263(Fexecute_extended_command): Deal with 'keymap' property.
264    ChangeLog
265
266    expected = [
267      [ '1998-08-17  Richard Stallman  <rms@gnu.org>',
268        [ 'register.el (insert-register): Return nil.',
269          '(jump-to-register): Likewise.',
270          'sort.el (sort-subr): Return nil.',
271          'keyboard.c (menu_bar_items, tool_bar_items, ' +
272          'Fexecute_extended_command): Deal with \'keymap\' property.']]]
273
274    assert_equal expected, parser.parse_entries
275  end
276
277  def test_scan
278    parser = util_parser <<-ChangeLog
279Tue Dec  4 08:32:10 2012  Eric Hodel  <drbrain@segment7.net>
280
281	* lib/rdoc/ri/driver.rb:  Fixed ri page display for files with
282	  extensions.
283	* test/rdoc/test_rdoc_ri_driver.rb:  Test for above
284
285Mon Dec  3 20:37:22 2012  Koichi Sasada  <ko1@atdot.net>
286
287	* vm_exec.c: check VM_COLLECT_USAGE_DETAILS.
288
289    ChangeLog
290
291    parser.scan
292
293    expected = doc(
294      head(1, File.basename(@tempfile.path)),
295      blank_line,
296      head(2, '2012-12-04'),
297      blank_line,
298      head(3, 'Tue Dec  4 08:32:10 2012  Eric Hodel  <drbrain@segment7.net>'),
299      blank_line,
300      list(:NOTE,
301        item('lib/rdoc/ri/driver.rb', para('Fixed ri page display for ' +
302                       'files with extensions.')),
303        item('test/rdoc/test_rdoc_ri_driver.rb', para('Test for above'))),
304      head(2, '2012-12-03'),
305      blank_line,
306      head(3, 'Mon Dec  3 20:37:22 2012  Koichi Sasada  <ko1@atdot.net>'),
307      blank_line,
308      list(:NOTE,
309        item('vm_exec.c', para('check VM_COLLECT_USAGE_DETAILS.'))))
310
311    expected.file = @top_level
312
313    assert_equal expected, @top_level.comment
314  end
315
316  def util_parser content = ''
317    RDoc::Parser::ChangeLog.new \
318      @top_level, @tempfile.path, content, @options, @stats
319  end
320
321end
322
323