test_sort.t 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. use strict;
  2. BEGIN {
  3. require Texinfo::ModulePath;
  4. Texinfo::ModulePath::init(undef, undef, 'updirs' => 2);
  5. }
  6. use Test::More;
  7. BEGIN { plan tests => 6; };
  8. use Texinfo::Convert::Text;
  9. use Texinfo::Parser;
  10. use Texinfo::Structuring;
  11. use Test::Deep;
  12. ok(1, "modules loading");
  13. my $tree = Texinfo::Parser::parse_texi_line(undef, '@copyright{} @quotedblbase{}');
  14. my $result = Texinfo::Convert::Text::convert($tree, {'sort_string' => 1});
  15. is ($result, 'C ,,', 'sort no encoding');
  16. $result = Texinfo::Convert::Text::convert($tree, {'sort_string' => 1,
  17. 'enabled_encoding' => 'utf-8'});
  18. is ($result, "\x{00A9} \x{201E}", 'sort utf-8');
  19. $result = Texinfo::Convert::Text::convert($tree, {'sort_string' => 1,
  20. 'enabled_encoding' => 'iso-8859-1'});
  21. is ($result, "\x{00A9} ,,", 'sort iso-8859-1');
  22. my $parser = Texinfo::Parser::parser();
  23. $tree = $parser->parse_texi_text('@node Top
  24. @cindex !
  25. @cindex e
  26. @cindex E
  27. @cindex ``
  28. @cindex @~e
  29. @cindex
  30. @cindex aaaaaaaaaaaa
  31. @cindex @l{}
  32. ');
  33. my ($index_names, $merged_indices) =
  34. $parser->indices_information();
  35. my $index_entries = Texinfo::Structuring::merge_indices($index_names);
  36. my $sorted_index_entries
  37. = Texinfo::Structuring::sort_indices($parser, $index_entries, $index_names);
  38. my @entries = ();
  39. foreach my $entry (@{$sorted_index_entries->{'cp'}}) {
  40. push @entries, $entry->{'key'};
  41. }
  42. # e is before E because they are equal and e appears before E in the document
  43. my @entries_ref = ('!', '"', 'aaaaaaaaaaaa', 'e', 'E', 'e~', 'l');
  44. cmp_deeply (\@entries, \@entries_ref, 'sorted index entries');
  45. my $sorted_index_entries_by_letter
  46. = Texinfo::Structuring::sort_indices_by_letter($parser, $index_entries,
  47. $index_names);
  48. my @letter_entries_ref = (
  49. {'!' => [ '!' ]},
  50. {'"' => [ '"' ]},
  51. {'A' => [ 'aaaaaaaaaaaa' ]},
  52. {'E' => [ 'e', 'E', 'e~']},
  53. {'L' => [ 'l' ]},
  54. );
  55. my @letter_entries;
  56. foreach my $letter (@{$sorted_index_entries_by_letter->{'cp'}}) {
  57. #my $letter_entry = {'letter' => $letter->{'letter'}};
  58. my $letter_entry = {};
  59. push @letter_entries, $letter_entry;
  60. foreach my $entry (@{$letter->{'entries'}}) {
  61. push @{$letter_entry->{$letter->{'letter'}}}, $entry->{'key'};
  62. }
  63. }
  64. {
  65. local $Data::Dumper::Purity = 1;
  66. local $Data::Dumper::Maxdepth = 2;
  67. local $Data::Dumper::Indent = 1;
  68. #print STDERR "".Data::Dumper->Dump([$sorted_index_entries_by_letter])."\n";
  69. #foreach my $letter (@{$sorted_index_entries_by_letter->{'cp'}}) {
  70. # print STDERR "AAA $letter->{'letter'} ".join('|',keys(%$letter))."\n";
  71. #}
  72. # print STDERR "".Data::Dumper->Dump([\@letter_entries])."\n";
  73. }
  74. cmp_deeply (\@letter_entries, \@letter_entries_ref, 'by letter index entries');