1#!./perl -w 2# t/trailing_comma.t - Test TrailingComma() 3 4use strict; 5use warnings; 6 7use Data::Dumper; 8use Test::More; 9use lib qw( ./t/lib ); 10use Testing qw( _dumptostr ); 11 12my @cases = ({ 13 input => [], 14 output => "[]", 15 desc => 'empty array', 16}, { 17 input => [17], 18 output => "[17]", 19 desc => 'single-element array, no indent', 20 conf => { Indent => 0 }, 21}, { 22 input => [17], 23 output => "[\n 17,\n]", 24 desc => 'single-element array, indent=1', 25 conf => { Indent => 1 }, 26}, { 27 input => [17], 28 output => "[\n 17,\n ]", 29 desc => 'single-element array, indent=2', 30 conf => { Indent => 2 }, 31}, { 32 input => [17, 18], 33 output => "[17,18]", 34 desc => 'two-element array, no indent', 35 conf => { Indent => 0 }, 36}, { 37 input => [17, 18], 38 output => "[\n 17,\n 18,\n]", 39 desc => 'two-element array, indent=1', 40 conf => { Indent => 1 }, 41}, { 42 input => [17, 18], 43 output => "[\n 17,\n 18,\n ]", 44 desc => 'two-element array, indent=2', 45 conf => { Indent => 2 }, 46}, { 47 input => {}, 48 output => "{}", 49 desc => 'empty hash', 50}, { 51 input => {foo => 17}, 52 output => "{'foo' => 17}", 53 desc => 'single-element hash, no indent', 54 conf => { Indent => 0 }, 55}, { 56 input => {foo => 17}, 57 output => "{\n 'foo' => 17,\n}", 58 desc => 'single-element hash, indent=1', 59 conf => { Indent => 1 }, 60}, { 61 input => {foo => 17}, 62 output => "{\n 'foo' => 17,\n }", 63 desc => 'single-element hash, indent=2', 64 conf => { Indent => 2 }, 65}, { 66 input => {foo => 17, quux => 18}, 67 output => "{'foo' => 17,'quux' => 18}", 68 desc => 'two-element hash, no indent', 69 conf => { Indent => 0 }, 70}, { 71 input => {foo => 17, quux => 18}, 72 output => "{\n 'foo' => 17,\n 'quux' => 18,\n}", 73 desc => 'two-element hash, indent=1', 74 conf => { Indent => 1 }, 75}, { 76 input => {foo => 17, quux => 18}, 77 output => "{\n 'foo' => 17,\n 'quux' => 18,\n }", 78 desc => 'two-element hash, indent=2', 79 conf => { Indent => 2 }, 80}); 81 82my $xs_available = !$Data::Dumper::Useperl; 83my $tests_per_case = $xs_available ? 2 : 1; 84 85plan tests => $tests_per_case * @cases; 86 87for my $case (@cases) { 88 run_case($case, $xs_available ? 'XS' : 'PP'); 89 if ($xs_available) { 90 local $Data::Dumper::Useperl = 1; 91 run_case($case, 'PP'); 92 } 93} 94 95sub run_case { 96 my ($case, $mode) = @_; 97 my ($input, $output, $desc, $conf) = @$case{qw<input output desc conf>}; 98 my $obj = Data::Dumper->new([$input]); 99 $obj->Trailingcomma(1); # default to on for these tests 100 $obj->Sortkeys(1); 101 for my $k (sort keys %{ $conf || {} }) { 102 $obj->$k($conf->{$k}); 103 } 104 chomp(my $got = _dumptostr($obj)); 105 is($got, "\$VAR1 = $output;", "$desc (in $mode mode)"); 106} 107