root/lang/perl/Text-Nyarlax/trunk/t/TestNyarlax.pm @ 88

Revision 88, 2.4 kB (checked in by nyarla, 6 years ago)

lang/perl/Text-Nyarlax: I changed loading method of a parser module and changed the construction of the test file.

Line 
1package t::TestNyarlax;
2
3use strict;
4use warnings;
5
6use Test::Base -Base;
7use Text::Nyarlax;
8use FindBin;
9use File::Spec;
10use FileHandle;
11use DirHandle;
12
13our @EXPORT = qw(
14    set_parser
15    set_test_elements
16    run_test_parse
17);
18
19our $nyarlax = Text::Nyarlax->new;
20
21our ( $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
32sub set_parser() {
33    my ( $parser, $config ) = @_;
34    $nyarlax->add_parser( { module => $parser, config => $config } );
35    $nyarlax->set_parser( $parser );
36}
37
38sub 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
62sub 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
731;
74package t::TestNyarlax::Filter;
75
76use base qw( Test::Base::Filter );
77
78use Text::Nyarlax::Element;
79
80sub 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
107sub parse {
108    my $text = shift;
109    return $t::TestNyarlax::nyarlax->parse( $text );
110}
111
1121;
113__END__
Note: See TracBrowser for help on using the browser.