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