├── .circleci └── config.yml ├── .gitignore ├── META6.json ├── README.md ├── lib └── Data │ └── Dump.rakumod └── t ├── 00-use.t ├── 01-hash.t ├── 02-obj.t ├── 03-array.t ├── 04-gist.t ├── 05-skipmethods.t ├── 06-match.t ├── 07-obj-no-postfix.t └── 08-override.t /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | - image: tonyodell/rakudo-nightly:latest 6 | 7 | working_directory: ~ 8 | 9 | steps: 10 | - checkout 11 | - run: 12 | name: install build deps 13 | command: | 14 | zef install --deps-only . 15 | - run: 16 | name: test 17 | command: | 18 | zef test . 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | blib/* 2 | .precomp 3 | *.swp 4 | -------------------------------------------------------------------------------- /META6.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "Data::Dump", 3 | "support" : { }, 4 | "auth": "zef:tony-o", 5 | "perl" : "6", 6 | "provides" : { 7 | "Data::Dump" : "lib/Data/Dump.rakumod" 8 | }, 9 | "license": "Artistic-2.0", 10 | "depends": [ ], 11 | "test-depends" : [ ], 12 | "description" : "a colorful? data dumper for perl6", 13 | "source-url" : "git://github.com/tony-o/perl6-data-dump.git", 14 | "version" : "0.0.16" 15 | } 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data::Dump 2 | 3 | ## for perl6 4 | 5 | that's right folks, here's a quicky for your data dump needs. if you have Terminal::ANSIColor installed then the output will be so colorful your eyes might bleed. 6 | 7 | feel free to submit bugs or make suggestions, if you submit a bug please provide a concise example that replicates the problem and i'll add some tests and make this thing better. 8 | 9 | ## options 10 | 11 | all of these options can be overridden in the `DATA_DUMP` environment variable in the format: `indent=4,color=false` 12 | 13 | ### `indent` 14 | 15 | default: `2` 16 | 17 | ```perl6 18 | <...> 19 | say Dump({ some => object }, :indent(4)); 20 | <...> 21 | ``` 22 | 23 | ### `max-recursion` 24 | 25 | default: `50` 26 | 27 | ```perl6 28 | <...> 29 | say Dump({ some => object }, :max-recursion(3)); 30 | <...> 31 | ``` 32 | 33 | ### `color` 34 | 35 | default: `True` 36 | 37 | This will override the default decision to use color on the output if `Terminal::ANSIColor` is installed. Passing a value of `False` will ensure that the output is vanilla. 38 | 39 | ```perl6 40 | <...> 41 | say Dump({ some => object }, :color(False)); 42 | <...> 43 | ``` 44 | 45 | ### `gist` 46 | 47 | default: `False` 48 | 49 | This will override the default object determination and output and use the output of `.gist` 50 | 51 | ```perl6 52 | <...> 53 | say Dump({ some => object}, :gist); 54 | <...> 55 | ``` 56 | 57 | ### `no-postfix` 58 | 59 | default: `False` 60 | 61 | This will shorten `Str|Int|Rat|Numeric` output from `5.Int|"H".Str` to simply `5|"H"` 62 | 63 | ### `skip-methods` 64 | 65 | default: `False` 66 | 67 | This will skip the methods if you dump custom classes. 68 | 69 | ```perl6 70 | <...> 71 | say Dump($object, :skip-methods(True)); 72 | <...> 73 | ``` 74 | 75 | ### `overrides` 76 | 77 | default: `{}` 78 | 79 | This will allow you to override how DD dumps certain types of objects. 80 | 81 | ```perl6 82 | Dump($object, overrides => { 83 | Int => sub ($int) { return $int * 2; }, 84 | Str => sub ($str) { return "'$str'"; }, 85 | # etc. 86 | }); 87 | ``` 88 | 89 | ## usage 90 | 91 | ```perl6 92 | use Data::Dump; 93 | 94 | say Dump(%( 95 | key1 => 'value1', 96 | key256 => 1, 97 | )); 98 | ``` 99 | 100 | output: 101 | ``` 102 | { 103 | key1 => "value1".Str, 104 | key256 => 1.Int, 105 | } 106 | ``` 107 | note: if you have Terminal::ANSIColor installed then it's going to be amazing. so, prepare yourself. 108 | 109 | ## oh you want to ```Dump``` your custom class? 110 | 111 | here you go, dude 112 | 113 | ```perl6 114 | use Data::Dump; 115 | 116 | class E { 117 | has $.public; 118 | has Int $!private = 5; 119 | method r(Str $a) { }; 120 | method s($b, :$named? = 5) { }; 121 | method e returns Int { say $!private; }; 122 | }; 123 | 124 | say Dump(E.new); 125 | ``` 126 | 127 | output: 128 | ``` 129 | E :: ( 130 | $!private => 5.Int, 131 | $!public => (Any), 132 | 133 | method e () returns Int {...}, 134 | method public () returns Mu {...}, 135 | method r (Str $a) returns Mu {...}, 136 | method s (Any $b, Any :named($named) = 5) returns Mu {...}, 137 | ) 138 | ``` 139 | 140 | -------------------------------------------------------------------------------- /lib/Data/Dump.rakumod: -------------------------------------------------------------------------------- 1 | unit module Data::Dump; 2 | my %provides-cache; 3 | my $colorizor = (try require ::('Terminal::ANSIColor')) === Nil 4 | && {''} || ::('Terminal::ANSIColor::EXPORT::DEFAULT::&color'); 5 | 6 | sub re-o ($o) { 7 | $o // 'undef'; 8 | } 9 | 10 | sub key ($o) { 11 | return $colorizor("red") ~ re-o($o) ~ $colorizor("reset"); 12 | } 13 | 14 | sub sym ($o) { 15 | return $colorizor("bold white") ~ re-o($o) ~ $colorizor("reset"); 16 | } 17 | 18 | sub val ($o) { 19 | return $colorizor("blue") ~ re-o($o) ~ $colorizor("reset"); 20 | } 21 | 22 | sub what ($o) { 23 | return $colorizor("yellow") ~ re-o($o) ~ $colorizor("reset"); 24 | } 25 | 26 | sub method-gist ($o) { 27 | sym($o ~~ Method ?? 'method ' !! 'sub ') ~ 28 | $o.name ~ ' (' ~ 29 | $o.signature.params[($o ~~ Method ?? 1 !! 0) .. *-2].map({.gist}).join(', ') ~ 30 | ') returns ' ~ $o.returns.WHAT.^name ~ 31 | ' {...}'; 32 | } 33 | 34 | sub pseudo-cache($obj) { #provide list of methods provided by parents 35 | my %r; 36 | for $obj.^mro[1..*] -> $x { 37 | if %provides-cache{$x.^name} { 38 | %provides-cache{$x.^name}.map({ %r{$_}.push($x.^name); }); 39 | } else { 40 | $x.^methods.map({ 41 | %provides-cache{$x.^name}.push($_.gist.Str); 42 | %r{$_.gist.Str}.push($x.^name); 43 | }); 44 | } 45 | } 46 | %r; 47 | } 48 | 49 | my %defaults = (:color(True), :indent(2), :ilevel(0), :max-recursion(50), :gist(False), :skip-methods(False), :no-postfix(False)); 50 | 51 | if %*ENV{'DATA_DUMP'} { 52 | for %*ENV{'DATA_DUMP'}.split(',', :skip-empty) -> $i { 53 | my ($k,$v) = $i.split('=').map(*.lc.trim); 54 | try { 55 | CATCH { default { say "WARN(DATA_DUMP): $_"; } } 56 | %defaults{$k} = %defaults{$k}.WHAT ~~ Bool 57 | ?? ($v.lc eq 'true' ?? True !! ($v.lc eq 'false' ?? False !! die "$k can only be set to 'true' or 'false', got: '$v'")) 58 | !! %defaults{$k}.WHAT.new($v); 59 | }; 60 | } 61 | } 62 | 63 | multi Dump ( 64 | Mu $obj, 65 | Int :$indent? = %defaults, 66 | Int :$ilevel? = %defaults, 67 | Bool :$color? = %defaults, 68 | Int :$max-recursion? = %defaults, 69 | Bool :$gist = %defaults, 70 | Bool :$skip-methods = %defaults, 71 | Bool :$no-postfix = %defaults, 72 | :%overrides where { !$_.values.grep: * !~~ Sub } = {}, 73 | ) is export { 74 | return $obj.gist; 75 | } 76 | 77 | multi Dump ( 78 | Any $obj, 79 | Int :$indent? = %defaults, 80 | Int :$ilevel? = %defaults, 81 | Bool :$color? = %defaults, 82 | Int :$max-recursion? = %defaults, 83 | Bool :$gist = %defaults, 84 | Bool :$skip-methods = %defaults, 85 | Bool :$no-postfix = %defaults, 86 | :%overrides where { !$_.values.grep: * !~~ Sub } = {}, 87 | ) is export { 88 | return '...' if $max-recursion == $ilevel; 89 | temp $colorizor = sub (Str $s) { '' } unless $color; 90 | try { 91 | require 'Terminal::ANSIColor'; 92 | }; 93 | my Str $out = ''; 94 | my Str $space = (' ' x $indent) x $ilevel; 95 | my Str $spac2 = (' ' x $indent) x ($ilevel+1); 96 | %overrides.map({ %overrides{$_.key.^name} //= $_.value; }); 97 | if %overrides{$obj.^name}.defined { 98 | my %options; 99 | warn 'Overrides must contain only one positional parameter' if %overrides{$obj.^name}.signature.params.grep(!*.named).elems != 1; 100 | for %overrides{$obj.^name}.signature.params -> $param { 101 | next unless $param.named; 102 | next unless $param.named ~~ (qw<$indent $ilevel $color $max-recursion $gist $skip-methods $no-postfix %overrides>); 103 | %options{$param.substr(1)} = $::($param.substr(1)); 104 | } 105 | $out ~= %overrides{$obj.^name}($obj, |%options) ~ "\n"; 106 | } elsif $obj.WHAT ~~ Hash|Map && !$gist { 107 | my @keys = $obj.keys.sort; 108 | my $spacing = @keys.map({ .chars }).max; 109 | $out ~= "{$space}{sym('{')}" ~ (@keys.elems > 0 ?? "\n" !! ""); 110 | for @keys -> $key { 111 | my $chars = $key.chars; 112 | $out ~= $spac2 ~ "{key($chars ?? $key !! '""')}{ ' ' x ($spacing - $key.chars)} {sym('=>')} "; 113 | $out ~= (try { Dump($obj{$key}, :%overrides, :$no-postfix, :$gist, :$color, :$max-recursion, :$indent, :$skip-methods, ilevel => $ilevel+1).trim; } // 'failure') ~ ",\n"; 114 | } 115 | $out ~= "{@keys.elems > 0 ?? $space !! ' '}{sym('}')}\n"; 116 | } elsif $obj.WHAT ~~ Pair && !$gist { 117 | my $key = $obj.key.WHAT ~~ Str 118 | ?? key($obj.key eq '' ?? '""' !! $obj.key) 119 | !! Dump($obj.key, :%overrides, :$gist, :$max-recursion, :$indent, :$skip-methods, :$color, :$no-postfix); 120 | $out ~= $key ~ ' => ' ~ Dump($obj.value, :%overrides, :$gist, :$max-recursion, :$indent, :$skip-methods, :$color, :$no-postfix); 121 | } elsif $obj.WHAT ~~ List && !$gist { 122 | $out ~= "{$space}{sym('[')}" ~ (@($obj).elems > 0 ?? "\n" !! ""); 123 | for @($obj) -> $o { 124 | $out ~= Dump($o, :%overrides, :$no-postfix, :$color, :$gist, :$max-recursion, :$indent, :$skip-methods, ilevel => $ilevel+1).trim-trailing ~ ",\n"; 125 | } 126 | $out ~= "{@($obj).elems > 0 ?? $space !! ' '}{sym(']')}\n"; 127 | } elsif $obj.WHAT ~~ any(Int, Str, Rat, Numeric) && !$gist { 128 | my $what = $obj.WHAT.^name; 129 | $out ~= "{$space}{$obj.defined ?? val($obj.perl) ~ ($no-postfix ?? '' !! '.'~what($what)) !! what($what) ~ ':U' }\n"; 130 | } elsif (Nil|Any) ~~ $obj.WHAT && !$gist { 131 | $out ~= $space ~ "({Nil ~~ $obj.WHAT ?? 'Nil' !! 'Any'})\n"; 132 | } elsif (Sub|Method) ~~ $obj.WHAT && !$gist { 133 | $out ~= $space ~ "{$obj.perl.subst(/'{' .+? $/, '')}\n"; 134 | } elsif Range ~~ $obj.WHAT && !$gist { 135 | $out ~= "{$space}{$obj.min}{$obj.excludes-min??'^'!!''}..{$obj.excludes-max??'^'!!''}{$obj.max}"; 136 | } elsif $obj ~~ IO::Path && !$gist { 137 | my $what = $obj.WHAT.^name; 138 | $out ~= “{$space}{val($obj.perl // '')}{$no-postfix ?? '' !! '.'~what($what)} :absolute("{$obj.absolute}")\n”; 139 | } elsif $obj ~~ Match|Grammar && !$gist { 140 | $out ~= $space ~ sym("{$obj.^name} :: (") ~ "\n"; 141 | my @props = qw.grep({ $obj.^can($_) }); 142 | my $asp = @props.map({ .chars }).max; 143 | for @props -> $p { 144 | $out ~= "{$spac2}{key($p)}{ ' ' x ($asp - $p.chars) } => "; 145 | $out ~= (try { 146 | CATCH { .say; } 147 | Dump($obj.^can($p)[0].($obj), :%overrides, :$no-postfix, :$color, :$gist, :$max-recursion, :$indent, :$skip-methods, ilevel => $ilevel+1).trim; 148 | } // 'Failure') ~ ",\n"; 149 | } 150 | $out ~= "{$space}{sym(')')}\n"; 151 | } else { 152 | $out ~= $space ~ sym("{$obj.^name} :: (") ~ "\n"; 153 | if $gist { 154 | $out ~= "{$spac2}{$obj.gist},\n"; 155 | } else { 156 | my @attrs = try { $obj.^attributes.sort({ $^x.Str cmp $^y.Str }) } // @(); 157 | my @meths = try { $obj.^methods.grep({ 158 | $obj ~~ $_.^mro[0] && $_ ~~ Method; 159 | }).sort({ $^x.^name.Str cmp $^y.^name.Str }) } // @(); 160 | my @attr-len = @attrs.map({ next unless .so && .^can('Str'); .Str.chars }); 161 | my @meth-len = @meths.map({ method-gist($_) }).map({ next unless .^can('chars'); .chars }); 162 | my $spacing = (@attr-len, @meth-len).max; 163 | 164 | for @attrs.sort -> $attr { 165 | next unless $attr; 166 | $out ~= "{$spac2}{key($attr)}{ ' ' x ($spacing - ($attr.so ?? $attr.Str.chars !! 0)) } => "; 167 | $out ~= ( try { Dump($attr.get_value($obj), :%overrides, :$no-postfix, :$color, :$gist, :$max-recursion, :$indent, :$skip-methods, ilevel => $ilevel+1).trim; } // 168 | try { Dump($attr.hash, :%overrides, :$no-postfix, :$color, :$gist, :$max-recursion, :$indent, :$skip-methods, ilevel => $ilevel+1).trim; } // 169 | 'undefined') ~ ",\n"; 170 | } 171 | 172 | $out ~= "\n" if @attrs.elems > 0; 173 | if !$skip-methods { 174 | my %parent-methods = pseudo-cache($obj); 175 | for @meths.sort({$^a cmp $^b}) -> $meth { 176 | next if %parent-methods{$meth.^name}; 177 | if %overrides{Method.^name} { 178 | my %options; 179 | warn 'Overrides must contain only one positional parameter' if %overrides{Method.^name}.signature.params.grep(!*.named).elems != 1; 180 | for %overrides{Method.^name}.signature.params -> $param { 181 | next unless $param.named; 182 | next unless $param.named ~~ (qw<$indent $ilevel $color $max-recursion $gist $skip-methods $no-postfix %overrides>); 183 | %options{$param.substr(1)} = $::($param.substr(1)); 184 | } 185 | $out ~= $spac2 ~ %overrides{Method.^name}($meth, |%options) ~ "\n"; 186 | } else { 187 | $out ~= "{$spac2}{method-gist($meth)},\n"; 188 | } 189 | } 190 | } 191 | } 192 | 193 | $out ~= "{$space}{sym(')')}\n"; 194 | } 195 | $out .=trim if ($ilevel == 0); 196 | return $out; 197 | } 198 | 199 | =begin pod 200 | 201 | =head1 Data::Dump for perl6 202 | 203 | that's right folks, here's a quicky for your data dump needs. if you have 204 | Term::ANSIColor installed then the output will be so colorful your eyes 205 | might bleed. 206 | 207 | feel free to submit bugs or make suggestions, if you submit a bug please 208 | provide a concise example that replicates the problem and i'll add some 209 | tests and make this thing better. 210 | 211 | =head2 options 212 | 213 | all of these options can be overriden with the appropriate types in the 214 | DATA_DUMP environment variable in the format: ident=4,color=false 215 | 216 | =item C 217 | 218 | default: C<2> 219 | 220 | perl6 221 | <...> 222 | say Dump({ some => object }, :indent(4)); 223 | <...> 224 | 225 | =item C 226 | 227 | default: C<50> 228 | 229 | perl6 230 | <...> 231 | say Dump({ some => object }, :max-recursion(3)); 232 | <...> 233 | 234 | =item C 235 | 236 | default: C 237 | 238 | This will override the default decision to use color on the output if 239 | C is installed. Passing a value of C will ensure 240 | that the output is vanilla. 241 | 242 | perl6 243 | <...> 244 | say Dump({ some => object }, :color(False)); 245 | <...> 246 | 247 | =head3 C 248 | 249 | default: C 250 | 251 | This will override the default object determination and output and use the output of C<.gist> 252 | 253 | perl6 254 | <...> 255 | say Dump({ some => object }, :gist); 256 | <...> 257 | 258 | =head3 C 259 | 260 | default: C 261 | 262 | This will shorten C output from C<5.Int|"H".Str> to simply C<5|"H"> 263 | 264 | =head3 C 265 | 266 | default: C 267 | 268 | This will skip the methods if you dump custom classes. 269 | 270 | =head3 C 271 | 272 | default: C<{}> 273 | 274 | This will allow you to override how DD dumps certain types of objects. 275 | 276 | perl6 277 | Dump($object, overrides => { 278 | Int => sub ($int) { return $int * 2; }, 279 | Str => sub ($str) { return "'$str'"; }, 280 | # etc. 281 | }); 282 | 283 | =head2 usage 284 | 285 | use Data::Dump; 286 | 287 | say Dump(%( 288 | key1 => 'value1', 289 | key256 => 1, 290 | )); 291 | 292 | output: 293 | 294 | { 295 | key1 => "value1".Str, 296 | key256 => 1.Int, 297 | } 298 | 299 | note: if you have Term::ANSIColor installed then it's going to be amazing. 300 | so, prepare yourself. 301 | 302 | =head2 oh you want to C your custom class? 303 | 304 | here you go, dude 305 | 306 | use Data::Dump; 307 | 308 | class E { 309 | has $.public; 310 | has Int $!private = 5; 311 | method r(Str $a) { }; 312 | method s($b, :$named? = 5) { }; 313 | method e returns Int { say $!private; }; 314 | }; 315 | 316 | say Dump(E.new); 317 | 318 | output: 319 | 320 | E :: ( 321 | $!private => 5.Int, 322 | $!public => (Any), 323 | 324 | method e () returns Int {...}, 325 | method public () returns Mu {...}, 326 | method r (Str $a) returns Mu {...}, 327 | method s (Any $b, Any :named($named) = 5) returns Mu {...}, 328 | ) 329 | 330 | =end pod 331 | -------------------------------------------------------------------------------- /t/00-use.t: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl6 2 | 3 | use Test; 4 | use Data::Dump; 5 | 6 | plan 1; 7 | 8 | ok True, 'can use'; 9 | 10 | -------------------------------------------------------------------------------- /t/01-hash.t: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl6 2 | 3 | BEGIN { %*ENV = ''; } 4 | use Test; 5 | use Data::Dump; 6 | plan 5; 7 | 8 | my $out = Dump(%( 9 | a => 'a', 10 | b => 'b', 11 | hash => %( 12 | a => 'a', 13 | b => 'b', 14 | reallylongkey => 'key', 15 | hash => %( 16 | a => 'a', 17 | b => 5, 18 | ), 19 | ), 20 | ), :color(False)); 21 | 22 | 23 | ok $out eq "\{\n a => \"a\".Str,\n b => \"b\".Str,\n hash => \{\n a => \"a\".Str,\n b => \"b\".Str,\n hash => \{\n a => \"a\".Str,\n b => 5.Int,\n },\n reallylongkey => \"key\".Str,\n },\n}"; 24 | 25 | my $expected-hash = Q:to/END/; 26 | { 27 | "" => "bar".Str, 28 | } 29 | END 30 | my %hash = "" => 'bar'; 31 | is Dump(%hash, :color(False)), $expected-hash.chomp, "Hash with a key which is an empty string"; 32 | my $expected = 'foo => "bar".Str'; 33 | my $expected2 = '11.Int => "bar".Str'; 34 | my $expected3 = '"" => "bar".Str'; 35 | 36 | my $set = Pair.new('foo', 'bar'); 37 | my $set1 = Pair.new(11, 'bar'); 38 | my $set2 = Pair.new('', 'bar'); 39 | 40 | is Dump($set, :color(False)), $expected, "Pairs with Str keys"; 41 | is Dump($set1, :color(False)), $expected2, "Pairs with Int keys"; 42 | is Dump($set2, :color(False)), $expected3, "Pair with an empty string as the key"; 43 | 44 | -------------------------------------------------------------------------------- /t/02-obj.t: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl6 2 | 3 | BEGIN { %*ENV = ''; } 4 | use Test; 5 | use Data::Dump; 6 | 7 | plan 3; 8 | 9 | class E { 10 | has $.public; 11 | has Int $!private = 5; 12 | 13 | method r(Str $a) { }; 14 | method s($b, :$named? = 5) { }; 15 | method e returns Int { say $!private; }; 16 | method !p(Int $x) { }; 17 | }; 18 | 19 | my $out = Dump(E.new, :color(False)); 20 | 21 | # Autogenerated method BUILDALL is listed starting with Rakudo 2017.10 22 | my $expected = "E :: (\n \$!private => 5.Int,\n \$!public => (Nil),\n\n method e () returns Int \{...},\n method public () returns Mu \{...},\n method r (Str \$a) returns Mu \{...},\n method s (\$b, :\$named = 5) returns Mu \{...},\n)"; 23 | 24 | ok $out eq $expected, "got expected data structure" or die $out; 25 | 26 | is Dump(Mu), '(Mu)', 'Can dump an undefined Mu type object'; 27 | is Dump(Nil), '(Nil)', 'Can dump an undefined Any type object';; 28 | 29 | # vi:syntax=perl6 30 | -------------------------------------------------------------------------------- /t/03-array.t: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl6 2 | 3 | BEGIN { %*ENV = ''; } 4 | use Test; 5 | use Data::Dump; 6 | plan 1; 7 | 8 | my $out = Dump(@( 9 | { hello => 'world' }, 10 | { one => 2 }, 11 | ), :color(False)); 12 | 13 | say $out; 14 | 15 | ok $out eq "[\n \{\n hello => \"world\".Str,\n },\n \{\n one => 2.Int,\n },\n]"; 16 | -------------------------------------------------------------------------------- /t/04-gist.t: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl6 2 | 3 | BEGIN { %*ENV = ''; } 4 | use Test; 5 | use Data::Dump; 6 | plan 3; 7 | 8 | my $out = Dump(%( 9 | a => 'a', 10 | b => %( b => 'b' ), 11 | ), :color(False), :gist); 12 | 13 | ok $out eq "Hash :: (\n \{a => a, b => \{b => b\}\},\n)", 'weird hash'; 14 | 15 | $out = Dump('foobar' ~~ m/foo(bar)/, :color(False), :gist); 16 | 17 | ok $out eq "Match :: (\n 「foobar」\n 0 => 「bar」,\n)", 'Match object'; 18 | 19 | $out = Dump(Pair.new(Mu, Mu), :color(False), :gist); 20 | 21 | is-deeply $out, "Pair :: (\n (Mu) => (Mu),\n)", "Pair with undefined keys"; 22 | -------------------------------------------------------------------------------- /t/05-skipmethods.t: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl6 2 | 3 | BEGIN { %*ENV = ''; } 4 | use Test; 5 | use Data::Dump; 6 | 7 | plan 3; 8 | 9 | class E { 10 | has $.public; 11 | has Int $!private = 5; 12 | 13 | method r(Str $a) { }; 14 | method s($b, :$named? = 5) { }; 15 | method e returns Int { say $!private; }; 16 | }; 17 | 18 | my $out = Dump(E.new, :color(False), :skip-methods); 19 | 20 | my $expected = "E :: (\n \$!private => 5.Int,\n \$!public => (Nil),\n\n)"; 21 | 22 | ok $out eq $expected, "got expected data structure" or die $out; 23 | 24 | class F { 25 | has E $.e; 26 | method x(Str $a) { }; 27 | } 28 | 29 | $out = Dump(F.new(:e(E.new)), :color(False), :skip-methods); 30 | 31 | $expected = "F :: (\n \$!e => E :: (\n \$!private => 5.Int,\n \$!public => (Nil),\n\n ),\n\n)"; 32 | 33 | ok $out eq $expected, "got expected nested data structure" or die $out; 34 | 35 | role G { 36 | has $!g; 37 | } 38 | 39 | class H does G { 40 | } 41 | 42 | $expected = "H :: (\n \$!g => undefined,\n\n)"; 43 | $out = Dump(H, :!color, :skip-methods); 44 | 45 | ok $out eq $expected, "role stuff still is recognized in class" or die $out.perl; 46 | 47 | # vi:syntax=perl6 48 | -------------------------------------------------------------------------------- /t/06-match.t: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl6 2 | 3 | BEGIN { %*ENV = ''; } 4 | use Test; 5 | use Data::Dump; 6 | 7 | plan 1; 8 | 9 | my $match = 'hello world' ~~ /'o w'/; 10 | 11 | my $out = Dump($match, :color(False), :skip-methods); 12 | my $expected = "Match :: (\n made => (Nil),\n pos => 7.Int,\n hash => \{ \},\n from => 4.Int,\n list => [ ],\n orig => \"hello world\".Str,\n)"; 13 | 14 | ok $out eq $expected, "special handling for Match" or die $out.perl; 15 | 16 | # vi:syntax=perl6 17 | -------------------------------------------------------------------------------- /t/07-obj-no-postfix.t: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl6 2 | 3 | BEGIN { %*ENV = ''; } 4 | use Test; 5 | use Data::Dump; 6 | 7 | plan 3; 8 | 9 | class E { 10 | has $.public; 11 | has Int $!private = 5; 12 | has Str $!private2 = 'hello'; 13 | 14 | method r(Str $a) { }; 15 | method s($b, :$named? = 5) { }; 16 | method e returns Int { say $!private; }; 17 | method !p(Int $x) { }; 18 | }; 19 | 20 | my $out = Dump(E.new, :color(False), :no-postfix); 21 | 22 | # Autogenerated method BUILDALL is listed starting with Rakudo 2017.10 23 | my $expected = "E :: (\n \$!private => 5,\n \$!private2 => \"hello\",\n \$!public => (Nil),\n\n method e () returns Int \{...},\n method public () returns Mu \{...},\n method r (Str \$a) returns Mu \{...},\n method s (\$b, :\$named = 5) returns Mu \{...},\n)"; 24 | 25 | Dump(E.new).say; 26 | 27 | ok $out eq $expected, "got expected data structure" or die $out; 28 | 29 | is Dump(Mu), '(Mu)', 'Can dump an undefined Mu type object'; 30 | is Dump(Nil), '(Nil)', 'Can dump an undefined Any type object';; 31 | 32 | # vi:syntax=perl6 33 | -------------------------------------------------------------------------------- /t/08-override.t: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl6 2 | 3 | BEGIN { %*ENV = ''; } 4 | use Test; 5 | use Data::Dump; 6 | 7 | plan 3; 8 | 9 | class E { 10 | has $.public; 11 | has Int $!private = 5; 12 | has Str $!private2 = 'hello'; 13 | 14 | method r(Str $a) { }; 15 | method s($b, :$named? = 5) { }; 16 | method e returns Int { say $!private; }; 17 | method !p(Int $x) { }; 18 | }; 19 | 20 | 21 | my %overrides = ( 22 | Method => sub ($meth) { 23 | $meth.name ~ ' ()'; 24 | }, 25 | Int => sub ($val) { $val * 2; }, 26 | Str => sub ($val) { "Str:$val"; }, 27 | ); 28 | my $out = Dump(E.new, :color(False), :no-postfix, :%overrides) ; 29 | 30 | # Autogenerated method BUILDALL is listed starting with Rakudo 2017.10 31 | my $expected = "E :: (\n \$!private => 10,\n \$!private2 => Str:hello,\n \$!public => (Nil),\n\n e ()\n public ()\n r ()\n s ()\n)"; 32 | 33 | Dump(E.new, :no-postfix, :%overrides).say; 34 | 35 | ok $out eq $expected, "got expected data structure" or die $out; 36 | 37 | is Dump(Mu), '(Mu)', 'Can dump an undefined Mu type object'; 38 | is Dump(Nil), '(Nil)', 'Can dump an undefined Any type object';; 39 | 40 | # vi:syntax=perl6 41 | --------------------------------------------------------------------------------