Skip to content

Instantly share code, notes, and snippets.

@nkh
Created June 6, 2019 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkh/a33c213563534eb48606953a4c0853c9 to your computer and use it in GitHub Desktop.
Save nkh/a33c213563534eb48606953a4c0853c9 to your computer and use it in GitHub Desktop.
ddt using custom set
# content to be matched
my $contents = q:to/EOI/;
[passwords]
jack=password1
joy=muchmoresecure123
[quotas]
jack=123
joy=42
EOI
# define some regexp structure
my regex header { \s* ('[' \w+ ']') \h* \n+ }
my regex identifier { \w+ }
my regex kvpair { \s* <key=identifier> '=' <value=identifier> \n+ }
my regex section { <header> <kvpair>* }
# create a match object to dump
my $m = $contents ~~ /<section>*/ ;
use Data::Dump::Tree ;
use CustomSetup ;
ddt $m, :title<Custom setup>, :does[CustomSetup] ;
use MONKEY-SEE-NO-EVAL;
role CustomSetup
{
has Int $.thingy ;
method custom_setup
{
try
{
EVAL q:to/EOE/ ;
use Data::Dump::Tree ;
use Data::Dump::Tree::ExtraRoles ;
use Data::Dump::Tree::Enums ;
sub header_filter($dumper, \r, $s, ($depth, $path, $glyph, @renderings), (\k, \b, \v, \f, \final, \want_address))
{
# add text in the rendering
#@renderings.push: (|$glyph, ('***', "HEADER filter", '***')) ;
# <header> replaced by its match
if k eq "<header>"
{
my %caps = $s.caps ;
v = "%caps<0>" ;
f = %caps<0>.from ~ '..' ~ (%caps<0>.pos - 1) ;
final = DDT_FINAL ;
}
# <section> have not text but a range
if k eq "<section>" { v = '' ; }
# <kvpair> has neither text nor range
if k eq "<kvpair>" { v = '' ; f = '' ; }
}
sub elements_filter($dumper, $s, ($depth, $glyph, @renderings, $element), @sub_elements)
{
my ($k, $b) = $element ;
@sub_elements = @sub_elements.grep({$_[0] ne '<identifier>' }) if $k eq "<kvpair>" ;
}
sub ls(Str $s, $limit)
{
$limit.defined && $s.chars > $limit
?? $s.substr(0, $limit) ~ '(+' ~ $s.chars - $limit ~ ')'
!! $s
}
self does DDTR::FixedGlyphs ;
self does DDTR::MatchDetails ;
self does DDTR::SuperscribeType ;
self does DDTR::SuperscribeAddress ;
self does DDTR::PerlString ;
$.color_kbs = True ;
$.display_address = DDT_DISPLAY_NONE ;
$.header_filters.push: &header_filter ;
$.elements_filters.push: &elements_filter ;
EOE
} # try
$! and die "Can't load custom setup. $!" ;
} # custom_setup
} #role
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment