1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
use Test::More;
use strict;
use t::Foo;
my $foo = Regexp::Log::Foo->new();
ok( ref($foo) eq 'Regexp::Log::Foo', "It's a Regexp::Log::Foo" );
# check defaults
ok( $foo->format eq '%d %c %b', "Default format" );
my @capture = $foo->capture;
ok( @capture == 1, "Default capture" );
ok( $capture[0] eq 'c', "Default captured field" );
ok( $foo->comments == 0, "Default comments" );
ok( $foo->anchor_line == 1, "Default anchor line");
ok( $foo->modifiers eq '', "Default modifiers" );
# check the anchor_line method
$foo = Regexp::Log::Foo->new( format => '%a' );
my $_xism = qr// =~ /^\(\?\^/ ? "^" : "-xism";
ok( $foo->regexp eq qq/(?$_xism\:^(?:\\d+)\$)/, "Ok for default anchors" );
ok( $foo->anchor_line(0) == 0, "Disabling anchors for line" );
ok( $foo->regexp eq qq/(?$_xism\:(?:\\d+))/ , "Ok for disabled anchors" );
# check modifiers
ok( $foo->modifiers('sim') eq q/sim/, "Ok to set modifiers" );
ok( $foo->regexp eq qq/(?$_xism\:(?sim:(?:\\d+)))/, "Modifiers configured" );
# check the format method
$foo = Regexp::Log::Foo->new();
ok( $foo->format('%a %b %c') eq '%a %b %c', "Format return new value" );
ok( $foo->format eq '%a %b %c', "new format value is set" );
my $r = $foo->regexp;
# check the format method with templates
$foo = Regexp::Log::Foo->new( format => ':default' );
is( $foo->regexp, $r, "Same regexp with ':default' and '%a %b %c'");
# check the fields method
my @fields = sort $foo->fields;
my $i = 0;
for (qw(a b c cn cs d)) {
ok( $fields[ $i++ ] eq $_, "Found field $_" );
}
# set the captures
@fields = $foo->capture(':none');
ok( @fields == 0, "Capture :none" );
@fields = sort $foo->capture(qw( b cs ));
ok( @fields == 2, "Capture only two fields" );
$i = 0;
for (qw( b cs )) {
ok( $fields[ $i++ ] eq $_, "Field $_ is captured" );
}
$foo->format('%d %c %b');
@fields = sort $foo->capture(':all');
$i = 0;
for (qw( b c cn cs d)) {
ok( $fields[ $i++ ] eq $_, "Field $_ is captured by :all" );
}
# the comments method
ok( $foo->comments(1) == 1, "comments old value" );
ok( $foo->comments == 1, "comments new value" );
# the regexp method
ok( $foo->regex eq $foo->regexp, "regexp() is aliased to regex()" );
$foo->comments(0);
my $regexp = $foo->regexp;
ok( $regexp !~ /\(\?\#.*?\)/, "No comment in regexp" );
$foo->comments(1);
$foo->format('%d');
ok( @{ [ $foo->regexp =~ /(\(\?\#.*?\))/g ] } == 2,
"2 comment for %d in regexp" );
$foo->format('%c');
ok( @{ [ $foo->regexp =~ /(\(\?\#.*?\))/g ] } == 6,
"6 comments for %c in regexp" );
$foo->comments(0);
# test the regex on real log lines
@ARGV = ('t/foo1.log');
$foo->format("%a %b %c %d");
@fields = $foo->capture(":all");
$regexp = $foo->regexp;
my %data;
my @data = (
{
a => 1,
b => 'this',
c => 'h4cker/31337',
cs => 'h4cker',
cn => 31337,
d => 'foo'
},
{
a => 2,
b => 'this',
c => 'cosmos/1999',
cs => 'cosmos',
cn => 1999,
d => 'foo'
},
{
a => 3,
b => 'that',
c => 'perec/11',
cs => 'perec',
cn => 11,
d => 'bar'
},
{
a => undef,
b => undef,
c => undef,
cs => undef,
cn => undef,
d => undef
},
{
a => 40,
b => 'this',
c => 'beast/666',
cs => 'beast',
cn => 666,
d => 'baz'
},
);
$i = 0;
while (<>) {
@data{@fields} = /$regexp/;
is_deeply( \%data, $data[ $i++ ], "foo1.log line " . ( $i + 1 ) );
}
# check that metacharacters are correctly handled
@ARGV = ( 't/foo2.log' );
$foo->format('%a (%c) $ %d? [%b]');
@fields = $foo->capture(":all");
$regexp = $foo->regexp;
@data = (
{ a => 1, c => 'jay/123', cs => 'jay', cn => 123, d => 'foo', b => 'this' },
{
a => 25,
c => 'garden/87',
cs => 'garden',
cn => 87,
d => 'bar',
b => 'that'
}
);
$i = 0;
while (<>) {
@data{@fields} = /$regexp/;
is_deeply( \%data, $data[ $i++ ], "foo2.log line " . ( $i + 1 ) );
}
BEGIN { plan tests => 43 }
|