| 1 | package t::TestNyarlax;
|
|---|
| 2 |
|
|---|
| 3 | use strict;
|
|---|
| 4 | use warnings;
|
|---|
| 5 |
|
|---|
| 6 | use Test::Base -Base;
|
|---|
| 7 | use Text::Nyarlax;
|
|---|
| 8 | use FindBin;
|
|---|
| 9 | use File::Spec;
|
|---|
| 10 | use FileHandle;
|
|---|
| 11 | use DirHandle;
|
|---|
| 12 |
|
|---|
| 13 | our @EXPORT = qw(
|
|---|
| 14 | set_parser
|
|---|
| 15 | set_test_elements
|
|---|
| 16 | run_test_parse
|
|---|
| 17 | );
|
|---|
| 18 |
|
|---|
| 19 | our $nyarlax = Text::Nyarlax->new;
|
|---|
| 20 |
|
|---|
| 21 | our ( $BaseDir, $SpecDir );
|
|---|
| 22 | {
|
|---|
| 23 | my @path = File::Spec->splitdir( $FindBin::Bin );
|
|---|
| 24 | while ( defined( my $dir = pop @path ) ) {
|
|---|
| 25 | if ( $dir eq 't' ) {
|
|---|
| 26 | $BaseDir = File::Spec->catdir( @path );
|
|---|
| 27 | $SpecDir = File::Spec->catdir( $BaseDir, qw( t parser spec ) );
|
|---|
| 28 | }
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | sub set_parser() {
|
|---|
| 33 | my ( $parser, $config ) = @_;
|
|---|
| 34 | $nyarlax->add_parser( { module => $parser, config => $config } );
|
|---|
| 35 | $nyarlax->set_parser( $parser );
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | sub set_test_elements() {
|
|---|
| 39 | my ( @elements ) = @_;
|
|---|
| 40 |
|
|---|
| 41 | if ( @elements > 0 ) {
|
|---|
| 42 | $_ = "$_.tb" for @elements;
|
|---|
| 43 | }
|
|---|
| 44 | else {
|
|---|
| 45 | my $dh = DirHandle->new( $SpecDir ) or croak $!;
|
|---|
| 46 | @elements = grep { $_ =~ m{\.tb$} } $dh->read;
|
|---|
| 47 | $dh->close;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | my $spec_data = q{};
|
|---|
| 51 | my $fh = FileHandle->new;
|
|---|
| 52 | for my $name ( @elements ) {
|
|---|
| 53 | my $file = File::Spec->catfile( $SpecDir, $name );
|
|---|
| 54 | $fh->open( $file, '<' );
|
|---|
| 55 | $spec_data .= do { local $/; <$fh> } . "\n\n";
|
|---|
| 56 | $fh->close;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | spec_string( $spec_data );
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | sub run_test_parse() {
|
|---|
| 63 | my ( $input, $expected ) = @_;
|
|---|
| 64 |
|
|---|
| 65 | filters {
|
|---|
| 66 | $input => [qw( parse )],
|
|---|
| 67 | $expected => [qw( yaml element )],
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | run_compare( $input => $expected );
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | 1;
|
|---|
| 74 | package t::TestNyarlax::Filter;
|
|---|
| 75 |
|
|---|
| 76 | use base qw( Test::Base::Filter );
|
|---|
| 77 |
|
|---|
| 78 | use Text::Nyarlax::Element;
|
|---|
| 79 |
|
|---|
| 80 | sub element {
|
|---|
| 81 | my $data = shift;
|
|---|
| 82 | $data ||= [];
|
|---|
| 83 | $data = [ $data ] if ( ref $data ne 'ARRAY' );
|
|---|
| 84 |
|
|---|
| 85 | my $elements = [];
|
|---|
| 86 |
|
|---|
| 87 | for my $item ( @{ $data } ) {
|
|---|
| 88 | if ( ref $item eq 'HASH' ) {
|
|---|
| 89 | my $name = $item->{'name'};
|
|---|
| 90 | my $attr = $item->{'attr'};
|
|---|
| 91 | my $content = $self->element( $item->{'content'} );
|
|---|
| 92 | my $element = Text::Nyarlax::Element->new(
|
|---|
| 93 | name => $name,
|
|---|
| 94 | attr => $attr,
|
|---|
| 95 | content => $content,
|
|---|
| 96 | );
|
|---|
| 97 | push @{ $elements }, $element;
|
|---|
| 98 | }
|
|---|
| 99 | else {
|
|---|
| 100 | push @{ $elements }, $item;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | return $elements;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | sub parse {
|
|---|
| 108 | my $text = shift;
|
|---|
| 109 | return $t::TestNyarlax::nyarlax->parse( $text );
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | 1;
|
|---|
| 113 | __END__
|
|---|