PrePAN

Sign in to PrePAN

Regex::Wrapper OO Perl Regex Wrapper

Author
snovakov@github
Date
URL
Status
In Review
Good

Synopsis

INPUT

#!/usr/bin/perl -w

use strict;

use Test::More tests => 20;

BEGIN {
  use_ok('Wrapper', 'use_Wrapper');
}

can_ok('Wrapper', ('new'));

BEGIN {
  use_ok('Match', 'use_Match');
}

can_ok('Match', ('new'));

my $original = "cie";
my $regex = Wrapper->new($original)
	or die("Error creating Wrapper object!");

isa_ok($regex, 'Wrapper');

can_ok('Wrapper', ('get_pattern'));

# Wrapper object check
my $expected = bless( {
                 '_regex' => qr/cie/,
                 '_pattern' => 'cie'
               }, 'Wrapper' );
is_deeply($regex, $expected, 'wrapper_object_check');

# regex match
$expected = qr/cie/;
ok($regex->get_regex() eq $expected, 'object_ok');

# full match
$expected = "cie";
ok($regex->match("cie")->get_match eq $expected, 'full_match');

# from
$expected = 0;
ok($regex->match("cie")->from() eq $expected, 'from');

# to
$expected = 2;
ok($regex->match("cie")->to() eq $expected, 'to');

# partial match
$expected = "ci";
ok($regex->match("ci")->get_match() eq $expected, 'partial_match');

# from
$expected = 0;
ok($regex->match("ci")->from() eq $expected, 'from');

# to
$expected = 1;
ok($regex->match("ci")->to() eq $expected, 'to');

# no match
unless($regex->match("xyz")) {
	ok(1 eq 1, 'no_match');
}else{
	fail('no_match');
}

# substitute (match)
$expected = "me";
ok($regex->substitute("ci","m")->get_result() eq $expected, 'substitute_match');

# substitute (no match)
unless($regex->substitute("ty","w")) {
	ok(1 eq 1, 'substitute_no_match');	
}else{
	fail('substitute_no_match');
}

# substitute (match)
$expected = "xe";
ok($regex->substitute("ci","x")->get_result() eq $expected, 'substitute_match');

# substitute all (match)
my $last = "I batted 4 for 4!";
$expected = "I batted four for four!";
my $new_regex = Wrapper->new("4")
	or die("Error creating Wrapper object!");
ok($new_regex->substitute_all($last,"four")->get_result() eq $expected, 'substitute_all');

# substitute all (no match, attribute doesn't exist)
unless($new_regex->substitute_all($last,"four")->get_hemans() eq '') {
	ok(1 eq 1, "substitute_all_attribute_doesn't_exist")
}else{
	fail("substitute_all_attribute_doesn't_exist")
}

OUTPUT

1..20
ok 1 - use Wrapper;
ok 2 - use Match;
ok 3 - Wrapper->can('new')
ok 4 - Match->can('new')
ok 5 - An object of class 'Wrapper' isa 'Wrapper'
ok 6 - Wrapper->can('get_pattern')
ok 7 - wrapper_object_check
ok 8 - object_ok
ok 9 - full_match
ok 10 - from
ok 11 - to
ok 12 - partial_match
ok 13 - from
ok 14 - to
ok 15 - no_match
ok 16 - substitute_match
ok 17 - substitute_no_match
ok 18 - substitute_match
ok 19 - substitute_all
ok 20 - substitute_all_attribute_doesn't_exist

Description

Small, 2-file module providing practical, useful Perl regex functionality. Pattern matching, obtain 'to' and 'from' match indices, substitute part of a pattern match, and substitute all matching patterns with a new pattern.

Comments

Please sign up to post a review.