]> git.donarmstrong.com Git - perltidy.git/blob - t/snippets24.t
New upstream version 20210717
[perltidy.git] / t / snippets24.t
1 # Created with: ./make_t.pl
2
3 # Contents:
4 #1 git54.def
5 #2 git54.git54
6 #3 fpva.def
7 #4 fpva.fpva1
8 #5 fpva.fpva2
9 #6 lpxl.def
10 #7 lpxl.lpxl1
11 #8 lpxl.lpxl3
12 #9 lpxl.lpxl4
13 #10 lpxl.lpxl5
14 #11 git63.def
15 #12 align35.def
16 #13 rt136417.def
17 #14 rt136417.rt136417
18 #15 numbers.def
19 #16 code_skipping.def
20
21 # To locate test #13 you can search for its name or the string '#13'
22
23 use strict;
24 use Test::More;
25 use Carp;
26 use Perl::Tidy;
27 my $rparams;
28 my $rsources;
29 my $rtests;
30
31 BEGIN {
32
33     ###########################################
34     # BEGIN SECTION 1: Parameter combinations #
35     ###########################################
36     $rparams = {
37         'def'   => "",
38         'fpva1' => "-sfp",
39         'fpva2' => "-sfp -nfpva",
40         'git54' => "-bbp=3 -bbpi=2 -ci=4 -lp",
41         'lpxl1' => "-lp",
42         'lpxl3' => <<'----------',
43 -lp -lpxl='{ [ ('
44 ----------
45         'lpxl4' => <<'----------',
46 -lp -lpxl='{ [ W(1'
47 ----------
48         'lpxl5' => <<'----------',
49 -lp -lpxl='{ [ F(2'
50 ----------
51         'rt136417' => "-vtc=3",
52     };
53
54     ############################
55     # BEGIN SECTION 2: Sources #
56     ############################
57     $rsources = {
58
59         'align35' => <<'----------',
60 # different module names, do not align commas (fixes rt136416)
61 use File::Spec::Functions 'catfile', 'catdir';
62 use Mojo::Base 'Mojolicious', '-signatures';
63
64 # same module names, align fat commas
65 use constant PI => 4 * atan2 1, 1;
66 use constant TWOPI => 2 * PI;
67 use constant FOURPI => 4 * PI;
68
69 # same module names, align commas
70 use TestCounter '3rd-party', 0, '3rd-party no longer visible';
71 use TestCounter 'replace', 1, 'replacement now visible';
72 use TestCounter 'root';
73
74 # same module name, align fat commas but not commas
75 use constant COUNTDOWN => scalar reverse 1, 2, 3, 4, 5;
76 use constant COUNTUP => reverse 1, 2, 3, 4, 5;
77 use constant COUNTDOWN => scalar reverse 1, 2, 3, 4, 5;
78 ----------
79
80         'code_skipping' => <<'----------',
81 %Hdr=%U2E=%E2U=%Fallback=();
82 $in_charmap=$nerror=$nwarning=0;
83 $.=0;
84 #<<V  code skipping: perltidy will pass this verbatim without error checking
85
86     }}} {{{
87
88 #>>V
89 my $self=shift;
90 my $cloning=shift;
91 ----------
92
93         'fpva' => <<'----------',
94 log_something_with_long_function( 'This is a log message.', 2 );
95 Coro::AnyEvent::sleep( 3, 4 );
96 use Carp ();
97 use File::Spec ();
98 use File::Path ();
99 ----------
100
101         'git54' => <<'----------',
102 # testing sensitivity to excess commas
103 my $definition =>
104     (
105     {
106         key1 => value1
107     },
108     {
109         key2 => value2
110     },
111     );
112
113 my $definition =>
114     (
115     {
116         key => value
117     }
118     );
119
120 my $definition =>
121     (
122     {
123         key => value
124     },
125     );
126
127 my $definition =>
128     (
129     {
130         key => value,
131     },
132     );
133
134 my $list =
135     (
136       {
137         key => $value,
138         key => $value,
139         key => $value,
140         key => $value,
141         key => $value,
142       },
143     ) ;
144
145 my $list =
146     (
147       {
148         key => $value,
149         key => $value,
150         key => $value,
151         key => $value,
152         key => $value,
153       }
154     ) ;
155 ----------
156
157         'git63' => <<'----------',
158 my $fragment = $parser-> #parse_html_string
159   parse_balanced_chunk($I);
160 ----------
161
162         'lpxl' => <<'----------',
163 # simple function call
164 my $loanlength = getLoanLength(
165                                 $borrower->{'categorycode'},    # sc1
166                                 $iteminformation->{'itemtype'},
167                                 $borrower->{'branchcode'}       # sc3
168 );
169
170 # function call, more than one level deep
171 my $o = very::long::class::name->new(
172     {
173         propA => "a",
174         propB => "b",
175         propC => "c",
176     }
177 );
178
179 # function call with sublist
180 debug(
181       "Connecting to DB.",
182       "Extra-Parameters: " . join("<->", $extra_parms),
183       "Config: " . join("<->", %config)
184      );
185
186 # simple function call with code block
187 $m->command(-label   => 'Save',
188             -command => sub { print "DOS\n"; save_dialog($win); });
189
190 # function call, ternary in list
191 return
192   OptArgs2::Result->usage(
193     $style == OptArgs2::STYLE_FULL ? 'FullUsage' : 'NormalUsage',
194     'usage: ' . $usage . "\n" );
195
196 # not a function call
197 %blastparam = (
198     -run            => \%runparam,
199     -file           => '',
200     -parse          => 1,
201     -signif         => 1e-5,
202 );
203
204 # 'local' is a keyword, not a user function
205     local (
206         $len,    $pts,      @colspec, $char, $cols,
207         $repeat, $celldata, $at_text, $after_text
208     );
209
210 # square bracket with sublists
211 $data = [
212          ListElem->new(id => 0, val => 100),
213          ListElem->new(id => 2, val => 50),
214          ListElem->new(id => 1, val => 10),
215         ];
216
217 # curly brace with sublists
218 $behaviour = {
219               cat   => {nap    => "lap",   eat  => "meat"},
220               dog   => {prowl  => "growl", pool => "drool"},
221               mouse => {nibble => "kibble"},
222              };
223 ----------
224
225         'numbers' => <<'----------',
226 # valid numbers
227 my @vals = (
228
229     12345,
230     12345.67,
231     .23E-10,
232     3.14_15_92,
233     4_294_967_296,
234     0xff,
235     0xdead_beef,
236     0377,
237     0b011011,
238     0x1.999ap-4,
239     1e34,
240     1e+34,
241     1e+034,
242     -1e+034,
243     0.00000000000000000000000000000000000000000000000000000000000000000001,
244     0Xabcdef,
245     0B1101,
246     0o12_345,  # optional 'o' and 'O' added in perl v5.33.5
247     0O12_345,
248 );
249 ----------
250
251         'rt136417' => <<'----------',
252 function(
253   #
254   a, b, c);
255
256 %hash = (
257   a => b,
258   c => d,
259 );
260 ----------
261     };
262
263     ####################################
264     # BEGIN SECTION 3: Expected output #
265     ####################################
266     $rtests = {
267
268         'git54.def' => {
269             source => "git54",
270             params => "def",
271             expect => <<'#1...........',
272 # testing sensitivity to excess commas
273 my $definition => (
274     {
275         key1 => value1
276     },
277     {
278         key2 => value2
279     },
280 );
281
282 my $definition => (
283     {
284         key => value
285     }
286 );
287
288 my $definition => (
289     {
290         key => value
291     },
292 );
293
294 my $definition => (
295     {
296         key => value,
297     },
298 );
299
300 my $list = (
301     {
302         key => $value,
303         key => $value,
304         key => $value,
305         key => $value,
306         key => $value,
307     },
308 );
309
310 my $list = (
311     {
312         key => $value,
313         key => $value,
314         key => $value,
315         key => $value,
316         key => $value,
317     }
318 );
319 #1...........
320         },
321
322         'git54.git54' => {
323             source => "git54",
324             params => "git54",
325             expect => <<'#2...........',
326 # testing sensitivity to excess commas
327 my $definition =>
328     (
329       {
330          key1 => value1
331       },
332       {
333          key2 => value2
334       },
335     );
336
337 my $definition =>
338     (
339       {
340         key => value
341       }
342     );
343
344 my $definition =>
345     (
346       {
347          key => value
348       },
349     );
350
351 my $definition =>
352     (
353       {
354          key => value,
355       },
356     );
357
358 my $list =
359     (
360       {
361          key => $value,
362          key => $value,
363          key => $value,
364          key => $value,
365          key => $value,
366       },
367     );
368
369 my $list =
370     (
371       {
372         key => $value,
373         key => $value,
374         key => $value,
375         key => $value,
376         key => $value,
377       }
378     );
379 #2...........
380         },
381
382         'fpva.def' => {
383             source => "fpva",
384             params => "def",
385             expect => <<'#3...........',
386 log_something_with_long_function( 'This is a log message.', 2 );
387 Coro::AnyEvent::sleep( 3, 4 );
388 use Carp       ();
389 use File::Spec ();
390 use File::Path ();
391 #3...........
392         },
393
394         'fpva.fpva1' => {
395             source => "fpva",
396             params => "fpva1",
397             expect => <<'#4...........',
398 log_something_with_long_function ( 'This is a log message.', 2 );
399 Coro::AnyEvent::sleep            ( 3, 4 );
400 use Carp       ();
401 use File::Spec ();
402 use File::Path ();
403 #4...........
404         },
405
406         'fpva.fpva2' => {
407             source => "fpva",
408             params => "fpva2",
409             expect => <<'#5...........',
410 log_something_with_long_function ( 'This is a log message.', 2 );
411 Coro::AnyEvent::sleep ( 3, 4 );
412 use Carp ();
413 use File::Spec ();
414 use File::Path ();
415 #5...........
416         },
417
418         'lpxl.def' => {
419             source => "lpxl",
420             params => "def",
421             expect => <<'#6...........',
422 # simple function call
423 my $loanlength = getLoanLength(
424     $borrower->{'categorycode'},    # sc1
425     $iteminformation->{'itemtype'},
426     $borrower->{'branchcode'}       # sc3
427 );
428
429 # function call, more than one level deep
430 my $o = very::long::class::name->new(
431     {
432         propA => "a",
433         propB => "b",
434         propC => "c",
435     }
436 );
437
438 # function call with sublist
439 debug(
440     "Connecting to DB.",
441     "Extra-Parameters: " . join( "<->", $extra_parms ),
442     "Config: " . join( "<->", %config )
443 );
444
445 # simple function call with code block
446 $m->command(
447     -label   => 'Save',
448     -command => sub { print "DOS\n"; save_dialog($win); }
449 );
450
451 # function call, ternary in list
452 return OptArgs2::Result->usage(
453     $style == OptArgs2::STYLE_FULL ? 'FullUsage' : 'NormalUsage',
454     'usage: ' . $usage . "\n" );
455
456 # not a function call
457 %blastparam = (
458     -run    => \%runparam,
459     -file   => '',
460     -parse  => 1,
461     -signif => 1e-5,
462 );
463
464 # 'local' is a keyword, not a user function
465 local (
466     $len,    $pts,      @colspec, $char, $cols,
467     $repeat, $celldata, $at_text, $after_text
468 );
469
470 # square bracket with sublists
471 $data = [
472     ListElem->new( id => 0, val => 100 ),
473     ListElem->new( id => 2, val => 50 ),
474     ListElem->new( id => 1, val => 10 ),
475 ];
476
477 # curly brace with sublists
478 $behaviour = {
479     cat   => { nap    => "lap",   eat  => "meat" },
480     dog   => { prowl  => "growl", pool => "drool" },
481     mouse => { nibble => "kibble" },
482 };
483 #6...........
484         },
485
486         'lpxl.lpxl1' => {
487             source => "lpxl",
488             params => "lpxl1",
489             expect => <<'#7...........',
490 # simple function call
491 my $loanlength = getLoanLength(
492                                 $borrower->{'categorycode'},    # sc1
493                                 $iteminformation->{'itemtype'},
494                                 $borrower->{'branchcode'}       # sc3
495 );
496
497 # function call, more than one level deep
498 my $o = very::long::class::name->new(
499                                       {
500                                         propA => "a",
501                                         propB => "b",
502                                         propC => "c",
503                                       }
504 );
505
506 # function call with sublist
507 debug(
508        "Connecting to DB.",
509        "Extra-Parameters: " . join( "<->", $extra_parms ),
510        "Config: " . join( "<->", %config )
511 );
512
513 # simple function call with code block
514 $m->command( -label   => 'Save',
515              -command => sub { print "DOS\n"; save_dialog($win); } );
516
517 # function call, ternary in list
518 return
519   OptArgs2::Result->usage(
520                    $style == OptArgs2::STYLE_FULL ? 'FullUsage' : 'NormalUsage',
521                    'usage: ' . $usage . "\n" );
522
523 # not a function call
524 %blastparam = (
525                 -run    => \%runparam,
526                 -file   => '',
527                 -parse  => 1,
528                 -signif => 1e-5,
529 );
530
531 # 'local' is a keyword, not a user function
532 local (
533         $len,    $pts,      @colspec, $char, $cols,
534         $repeat, $celldata, $at_text, $after_text
535 );
536
537 # square bracket with sublists
538 $data = [
539           ListElem->new( id => 0, val => 100 ),
540           ListElem->new( id => 2, val => 50 ),
541           ListElem->new( id => 1, val => 10 ),
542 ];
543
544 # curly brace with sublists
545 $behaviour = {
546                cat   => { nap    => "lap",   eat  => "meat" },
547                dog   => { prowl  => "growl", pool => "drool" },
548                mouse => { nibble => "kibble" },
549 };
550 #7...........
551         },
552
553         'lpxl.lpxl3' => {
554             source => "lpxl",
555             params => "lpxl3",
556             expect => <<'#8...........',
557 # simple function call
558 my $loanlength = getLoanLength(
559     $borrower->{'categorycode'},    # sc1
560     $iteminformation->{'itemtype'},
561     $borrower->{'branchcode'}       # sc3
562 );
563
564 # function call, more than one level deep
565 my $o = very::long::class::name->new(
566     {
567         propA => "a",
568         propB => "b",
569         propC => "c",
570     }
571 );
572
573 # function call with sublist
574 debug(
575     "Connecting to DB.",
576     "Extra-Parameters: " . join( "<->", $extra_parms ),
577     "Config: " . join( "<->", %config )
578 );
579
580 # simple function call with code block
581 $m->command(
582     -label   => 'Save',
583     -command => sub { print "DOS\n"; save_dialog($win); } );
584
585 # function call, ternary in list
586 return
587   OptArgs2::Result->usage(
588     $style == OptArgs2::STYLE_FULL ? 'FullUsage' : 'NormalUsage',
589     'usage: ' . $usage . "\n" );
590
591 # not a function call
592 %blastparam = (
593     -run    => \%runparam,
594     -file   => '',
595     -parse  => 1,
596     -signif => 1e-5,
597 );
598
599 # 'local' is a keyword, not a user function
600 local (
601     $len,    $pts,      @colspec, $char, $cols,
602     $repeat, $celldata, $at_text, $after_text
603 );
604
605 # square bracket with sublists
606 $data = [
607     ListElem->new( id => 0, val => 100 ),
608     ListElem->new( id => 2, val => 50 ),
609     ListElem->new( id => 1, val => 10 ),
610 ];
611
612 # curly brace with sublists
613 $behaviour = {
614     cat   => { nap    => "lap",   eat  => "meat" },
615     dog   => { prowl  => "growl", pool => "drool" },
616     mouse => { nibble => "kibble" },
617 };
618 #8...........
619         },
620
621         'lpxl.lpxl4' => {
622             source => "lpxl",
623             params => "lpxl4",
624             expect => <<'#9...........',
625 # simple function call
626 my $loanlength = getLoanLength(
627                                 $borrower->{'categorycode'},    # sc1
628                                 $iteminformation->{'itemtype'},
629                                 $borrower->{'branchcode'}       # sc3
630 );
631
632 # function call, more than one level deep
633 my $o = very::long::class::name->new(
634     {
635         propA => "a",
636         propB => "b",
637         propC => "c",
638     }
639 );
640
641 # function call with sublist
642 debug(
643     "Connecting to DB.",
644     "Extra-Parameters: " . join( "<->", $extra_parms ),
645     "Config: " . join( "<->", %config )
646 );
647
648 # simple function call with code block
649 $m->command( -label   => 'Save',
650              -command => sub { print "DOS\n"; save_dialog($win); } );
651
652 # function call, ternary in list
653 return
654   OptArgs2::Result->usage(
655                    $style == OptArgs2::STYLE_FULL ? 'FullUsage' : 'NormalUsage',
656                    'usage: ' . $usage . "\n" );
657
658 # not a function call
659 %blastparam = (
660     -run    => \%runparam,
661     -file   => '',
662     -parse  => 1,
663     -signif => 1e-5,
664 );
665
666 # 'local' is a keyword, not a user function
667 local (
668         $len,    $pts,      @colspec, $char, $cols,
669         $repeat, $celldata, $at_text, $after_text
670 );
671
672 # square bracket with sublists
673 $data = [
674     ListElem->new( id => 0, val => 100 ),
675     ListElem->new( id => 2, val => 50 ),
676     ListElem->new( id => 1, val => 10 ),
677 ];
678
679 # curly brace with sublists
680 $behaviour = {
681     cat   => { nap    => "lap",   eat  => "meat" },
682     dog   => { prowl  => "growl", pool => "drool" },
683     mouse => { nibble => "kibble" },
684 };
685 #9...........
686         },
687
688         'lpxl.lpxl5' => {
689             source => "lpxl",
690             params => "lpxl5",
691             expect => <<'#10...........',
692 # simple function call
693 my $loanlength = getLoanLength(
694                                 $borrower->{'categorycode'},    # sc1
695                                 $iteminformation->{'itemtype'},
696                                 $borrower->{'branchcode'}       # sc3
697 );
698
699 # function call, more than one level deep
700 my $o = very::long::class::name->new(
701     {
702         propA => "a",
703         propB => "b",
704         propC => "c",
705     }
706 );
707
708 # function call with sublist
709 debug(
710     "Connecting to DB.",
711     "Extra-Parameters: " . join( "<->", $extra_parms ),
712     "Config: " . join( "<->", %config )
713 );
714
715 # simple function call with code block
716 $m->command(
717     -label   => 'Save',
718     -command => sub { print "DOS\n"; save_dialog($win); } );
719
720 # function call, ternary in list
721 return
722   OptArgs2::Result->usage(
723     $style == OptArgs2::STYLE_FULL ? 'FullUsage' : 'NormalUsage',
724     'usage: ' . $usage . "\n" );
725
726 # not a function call
727 %blastparam = (
728     -run    => \%runparam,
729     -file   => '',
730     -parse  => 1,
731     -signif => 1e-5,
732 );
733
734 # 'local' is a keyword, not a user function
735 local (
736     $len,    $pts,      @colspec, $char, $cols,
737     $repeat, $celldata, $at_text, $after_text
738 );
739
740 # square bracket with sublists
741 $data = [
742     ListElem->new( id => 0, val => 100 ),
743     ListElem->new( id => 2, val => 50 ),
744     ListElem->new( id => 1, val => 10 ),
745 ];
746
747 # curly brace with sublists
748 $behaviour = {
749     cat   => { nap    => "lap",   eat  => "meat" },
750     dog   => { prowl  => "growl", pool => "drool" },
751     mouse => { nibble => "kibble" },
752 };
753 #10...........
754         },
755
756         'git63.def' => {
757             source => "git63",
758             params => "def",
759             expect => <<'#11...........',
760 my $fragment = $parser->    #parse_html_string
761   parse_balanced_chunk($I);
762 #11...........
763         },
764
765         'align35.def' => {
766             source => "align35",
767             params => "def",
768             expect => <<'#12...........',
769 # different module names, do not align commas (fixes rt136416)
770 use File::Spec::Functions 'catfile', 'catdir';
771 use Mojo::Base 'Mojolicious', '-signatures';
772
773 # same module names, align fat commas
774 use constant PI     => 4 * atan2 1, 1;
775 use constant TWOPI  => 2 * PI;
776 use constant FOURPI => 4 * PI;
777
778 # same module names, align commas
779 use TestCounter '3rd-party', 0, '3rd-party no longer visible';
780 use TestCounter 'replace',   1, 'replacement now visible';
781 use TestCounter 'root';
782
783 # same module name, align fat commas but not commas
784 use constant COUNTDOWN => scalar reverse 1, 2, 3, 4, 5;
785 use constant COUNTUP   => reverse 1, 2, 3, 4, 5;
786 use constant COUNTDOWN => scalar reverse 1, 2, 3, 4, 5;
787 #12...........
788         },
789
790         'rt136417.def' => {
791             source => "rt136417",
792             params => "def",
793             expect => <<'#13...........',
794 function(
795     #
796     a, b, c
797 );
798
799 %hash = (
800     a => b,
801     c => d,
802 );
803 #13...........
804         },
805
806         'rt136417.rt136417' => {
807             source => "rt136417",
808             params => "rt136417",
809             expect => <<'#14...........',
810 function(
811     #
812     a, b, c );
813
814 %hash = (
815     a => b,
816     c => d,
817 );
818 #14...........
819         },
820
821         'numbers.def' => {
822             source => "numbers",
823             params => "def",
824             expect => <<'#15...........',
825 # valid numbers
826 my @vals = (
827
828     12345,
829     12345.67,
830     .23E-10,
831     3.14_15_92,
832     4_294_967_296,
833     0xff,
834     0xdead_beef,
835     0377,
836     0b011011,
837     0x1.999ap-4,
838     1e34,
839     1e+34,
840     1e+034,
841     -1e+034,
842     0.00000000000000000000000000000000000000000000000000000000000000000001,
843     0Xabcdef,
844     0B1101,
845     0o12_345,    # optional 'o' and 'O' added in perl v5.33.5
846     0O12_345,
847 );
848 #15...........
849         },
850
851         'code_skipping.def' => {
852             source => "code_skipping",
853             params => "def",
854             expect => <<'#16...........',
855 %Hdr        = %U2E    = %E2U      = %Fallback = ();
856 $in_charmap = $nerror = $nwarning = 0;
857 $.          = 0;
858 #<<V  code skipping: perltidy will pass this verbatim without error checking
859
860     }}} {{{
861
862 #>>V
863 my $self    = shift;
864 my $cloning = shift;
865 #16...........
866         },
867     };
868
869     my $ntests = 0 + keys %{$rtests};
870     plan tests => $ntests;
871 }
872
873 ###############
874 # EXECUTE TESTS
875 ###############
876
877 foreach my $key ( sort keys %{$rtests} ) {
878     my $output;
879     my $sname  = $rtests->{$key}->{source};
880     my $expect = $rtests->{$key}->{expect};
881     my $pname  = $rtests->{$key}->{params};
882     my $source = $rsources->{$sname};
883     my $params = defined($pname) ? $rparams->{$pname} : "";
884     my $stderr_string;
885     my $errorfile_string;
886     my $err = Perl::Tidy::perltidy(
887         source      => \$source,
888         destination => \$output,
889         perltidyrc  => \$params,
890         argv        => '',             # for safety; hide any ARGV from perltidy
891         stderr      => \$stderr_string,
892         errorfile   => \$errorfile_string,    # not used when -se flag is set
893     );
894     if ( $err || $stderr_string || $errorfile_string ) {
895         print STDERR "Error output received for test '$key'\n";
896         if ($err) {
897             print STDERR "An error flag '$err' was returned\n";
898             ok( !$err );
899         }
900         if ($stderr_string) {
901             print STDERR "---------------------\n";
902             print STDERR "<<STDERR>>\n$stderr_string\n";
903             print STDERR "---------------------\n";
904             ok( !$stderr_string );
905         }
906         if ($errorfile_string) {
907             print STDERR "---------------------\n";
908             print STDERR "<<.ERR file>>\n$errorfile_string\n";
909             print STDERR "---------------------\n";
910             ok( !$errorfile_string );
911         }
912     }
913     else {
914         if ( !is( $output, $expect, $key ) ) {
915             my $leno = length($output);
916             my $lene = length($expect);
917             if ( $leno == $lene ) {
918                 print STDERR
919 "#> Test '$key' gave unexpected output.  Strings differ but both have length $leno\n";
920             }
921             else {
922                 print STDERR
923 "#> Test '$key' gave unexpected output.  String lengths differ: output=$leno, expected=$lene\n";
924             }
925         }
926     }
927 }