automatic_nodes.t 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. use strict;
  2. use Test::More;
  3. BEGIN {
  4. require Texinfo::ModulePath;
  5. Texinfo::ModulePath::init(undef, undef, 'updirs' => 2);
  6. }
  7. BEGIN { plan tests => 24; }
  8. use Texinfo::Parser qw(parse_texi_text);
  9. use Texinfo::Transformations;
  10. use Texinfo::Convert::Texinfo;
  11. use Data::Dumper;
  12. ok(1);
  13. sub test_new_node($$$$)
  14. {
  15. my $in = shift;
  16. my $normalized_ref = shift;
  17. my $out = shift;
  18. my $name = shift;
  19. my $parser = Texinfo::Parser::parser();
  20. my $line = $parser->parse_texi_line ($in);
  21. Texinfo::Structuring::associate_internal_references($parser);
  22. my $node = Texinfo::Transformations::_new_node($parser, $line);
  23. my ($texi_result, $normalized);
  24. if (defined($node)) {
  25. $texi_result = Texinfo::Convert::Texinfo::convert($node);
  26. Texinfo::Structuring::associate_internal_references($parser);
  27. $normalized = $node->{'extra'}->{'normalized'};
  28. my $labels = $parser->labels_information();
  29. my @labels = keys(%$labels);
  30. ok ((scalar(@labels) == 1 and $labels[0] eq $normalized), "$name label");
  31. }
  32. if (!defined($normalized_ref) and defined($normalized)) {
  33. print STDERR " --> $name($normalized): $texi_result";
  34. } else {
  35. is ($normalized_ref, $normalized, "$name normalized");
  36. is ($texi_result, $out, $name);
  37. }
  38. }
  39. test_new_node ('a node', 'a-node', '@node a node
  40. ', 'simple');
  41. test_new_node ('a node @code{in code} @c comment
  42. ', 'a-node-in-code-', '@node a node @code{in code} @c comment
  43. ', 'complex');
  44. test_new_node ('a ,, node @code{a,b,}', 'a-_002c_002c-node-a_002cb_002c',
  45. '@node a @comma{}@comma{} node @code{a@comma{}b@comma{}}
  46. ', 'with comma');
  47. test_new_node ('(in paren(too aaa', '_0028in-paren_0028too-aaa',
  48. '@node @asis{(}in paren(too aaa
  49. ', 'with parenthesis');
  50. test_new_node ('changed @ref{ @code{node}} and (@pxref{ ,, , @samp{file}})',
  51. 'changed-node-and-_0028file_0029',
  52. '@node changed @code{node} and (@samp{file})
  53. ',
  54. 'ref in new node');
  55. test_new_node ('@asis{}', '-1', '@node @asis{} 1
  56. ', 'empty node');
  57. my $parser = Texinfo::Parser::parser();
  58. my $tree = $parser->parse_texi_text('@node a node
  59. ');
  60. my $line_tree = Texinfo::Parser::parse_texi_line (undef, 'a node');
  61. Texinfo::Structuring::associate_internal_references($parser);
  62. my $node = Texinfo::Transformations::_new_node($parser, $line_tree);
  63. is ('@node a node 1
  64. ', Texinfo::Convert::Texinfo::convert($node), 'duplicate node added');
  65. #print STDERR Texinfo::Convert::Texinfo::convert($node);
  66. my $sections_text =
  67. '@top top section
  68. @part part
  69. @chapter chap, @code{a chap}
  70. @node a node
  71. @section section
  72. @section truc
  73. @subsection sub1
  74. Text.
  75. @subsection sub2 @c comment
  76. @section section
  77. @section section
  78. @unnumbered
  79. @section @asis{}
  80. @bye';
  81. my $reference =
  82. '@node Top
  83. @top top section
  84. @part part
  85. @node chap@comma{} @code{a chap}
  86. @chapter chap, @code{a chap}
  87. @node a node
  88. @section section
  89. @node truc
  90. @section truc
  91. @node sub1
  92. @subsection sub1
  93. Text.
  94. @node sub2
  95. @subsection sub2 @c comment
  96. @node section
  97. @section section
  98. @node section 1
  99. @section section
  100. @node 1
  101. @unnumbered
  102. @node @asis{} 2
  103. @section @asis{}
  104. @bye';
  105. $parser = Texinfo::Parser::parser();
  106. $tree = $parser->parse_texi_text ($sections_text);
  107. Texinfo::Structuring::associate_internal_references($parser);
  108. my ($new_content, $added_nodes) = Texinfo::Transformations::insert_nodes_for_sectioning_commands($parser, $tree);
  109. $tree->{'contents'} = $new_content;
  110. my $result = Texinfo::Convert::Texinfo::convert($tree);
  111. is ($reference, $result, 'add nodes');
  112. #print STDERR "$result";
  113. $parser = Texinfo::Parser::parser();
  114. $tree = $parser->parse_texi_text ('@node Top
  115. @top top
  116. @chapter chap
  117. @cindex index entry
  118. @menu
  119. * (some_manual)::
  120. @end menu
  121. ');
  122. Texinfo::Structuring::associate_internal_references($parser);
  123. ($new_content, $added_nodes)
  124. = Texinfo::Transformations::insert_nodes_for_sectioning_commands($parser, $tree);
  125. $tree->{'contents'} = $new_content;
  126. my ($index_names, $merged_indices) = $parser->indices_information();
  127. my $labels = $parser->labels_information();
  128. ok (($labels->{'chap'}->{'menus'} and @{$labels->{'chap'}->{'menus'}}
  129. and scalar(@{$labels->{'chap'}->{'menus'}}) == 1
  130. and !exists($labels->{'Top'}->{'menus'})), 'new node has a menu');
  131. is (Texinfo::Convert::Texinfo::convert($labels->{'chap'}->{'menus'}->[0]),
  132. '@menu
  133. * (some_manual)::
  134. @end menu
  135. ', 'reassociated menu is correct');
  136. #print STDERR join('|', keys(%{$index_names->{'cp'}->{'index_entries'}}))."\n";
  137. is ($labels->{'chap'}, $index_names->{'cp'}->{'index_entries'}->[0]->{'node'},
  138. 'index entry reassociated');
  139. #print STDERR Texinfo::Convert::Texinfo::convert($tree);
  140. # Note: this test doesn't pass anymore because we only notice duplicate
  141. # nodes at the end.
  142. # $parser = Texinfo::Parser::parser();
  143. # my $text_duplicate_nodes =
  144. # '@node NAME
  145. # @section DESCRIPTION
  146. #
  147. # @node NAME
  148. # @section SEE ALSO
  149. #
  150. # @cindex entry
  151. # ';
  152. # $tree = $parser->parse_texi_text ($text_duplicate_nodes);
  153. # # In fact, here we also check that there is no debugging message...
  154. # ($new_content, $added_nodes)
  155. # = Texinfo::Transformations::insert_nodes_for_sectioning_commands($parser, $tree);
  156. # ($index_names, $merged_indices) = $parser->indices_information();
  157. # $labels = $parser->labels_information();
  158. # is ($labels->{'SEE-ALSO'}, $index_names->{'cp'}->{'index_entries'}->[0]->{'node'},
  159. # 'index entry reassociated duplicate node ignored');