| 1 | #----------------------------------------------------------------- | ||||||||||||
| 2 | # MOBY::Client::MobyUnitTest | ||||||||||||
| 3 | # Author: Edward Kawas <edward.kawas@gmail.com>, | ||||||||||||
| 4 | # For copyright and disclaimer see below. | ||||||||||||
| 5 | # | ||||||||||||
| 6 | # $Id: MobyUnitTest.pm,v 1.5 2009/02/03 21:56:19 kawas Exp $ | ||||||||||||
| 7 | #----------------------------------------------------------------- | ||||||||||||
| 8 | |||||||||||||
| 9 | package MOBY::Client::MobyUnitTest; | ||||||||||||
| 10 | |||||||||||||
| 11 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 5 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 5 | ▶ | use XML::SemanticCompare; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 5 | ▶ | use vars qw /$VERSION/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 5 | $VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/; | |||||||||||
| 17 | |||||||||||||
| 18 | 5 | ▶ | use vars qw($AUTOLOAD); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | #----------------------------------------------------------------- | ||||||||||||
| 21 | # load all modules needed | ||||||||||||
| 22 | #----------------------------------------------------------------- | ||||||||||||
| 23 | 5 | ▶ | use XML::LibXML; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 5 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | |||||||||||||
| 26 | =head1 NAME | ||||||||||||
| 27 | |||||||||||||
| 28 | MOBY::Client::MobyUnitTest - Create Unit Tests and test your service | ||||||||||||
| 29 | |||||||||||||
| 30 | =head1 SYNOPSIS | ||||||||||||
| 31 | |||||||||||||
| 32 | use MOBY::Client::MobyUnitTest; | ||||||||||||
| 33 | my $x = MOBY::Client::MobyUnitTest->new; | ||||||||||||
| 34 | |||||||||||||
| 35 | # set expected output | ||||||||||||
| 36 | $x->expected_output($control_xml); | ||||||||||||
| 37 | # test expected output with XML output | ||||||||||||
| 38 | my $success = $x->test_output_xml($test_file); | ||||||||||||
| 39 | print "XML matches!\n" if $success; | ||||||||||||
| 40 | |||||||||||||
| 41 | # set xpath statement | ||||||||||||
| 42 | $x->xpath($some_xpath); | ||||||||||||
| 43 | # test xpath statement | ||||||||||||
| 44 | $success = $x->test_xpath($test_xml); | ||||||||||||
| 45 | print "xpath success!\n" if $success; | ||||||||||||
| 46 | |||||||||||||
| 47 | # set regex statement | ||||||||||||
| 48 | $x->regex($some_regex); | ||||||||||||
| 49 | # test regex statement | ||||||||||||
| 50 | $success = $x->test_regex($test_xml); | ||||||||||||
| 51 | print "regex success!\n" if $success; | ||||||||||||
| 52 | |||||||||||||
| 53 | # get XML differences if any | ||||||||||||
| 54 | my $differences = $x->get_xml_differences($test_xml); | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 DESCRIPTION | ||||||||||||
| 57 | |||||||||||||
| 58 | This module is used for providing unit test case information for any particular service, as well as actually performing the tests on the service. | ||||||||||||
| 59 | |||||||||||||
| 60 | =cut | ||||||||||||
| 61 | |||||||||||||
| 62 | =head1 AUTHORS | ||||||||||||
| 63 | |||||||||||||
| 64 | Edward Kawas (edward.kawas [at] gmail [dot] com) | ||||||||||||
| 65 | |||||||||||||
| 66 | =cut | ||||||||||||
| 67 | |||||||||||||
| 68 | #----------------------------------------------------------------- | ||||||||||||
| 69 | # AUTOLOAD | ||||||||||||
| 70 | #----------------------------------------------------------------- | ||||||||||||
| 71 | 0 | ▶ | sub AUTOLOAD { | ||||||||||
|
Subroutine
AUTOLOAD: not called CC=10 SCAR=47.0
| |||||||||||||
| 72 | 0 | my $self = shift; | |||||||||||
| 73 | 0 | ▶ | my $type = ref($self) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | or croak("$self is not an object"); | ||||||||||||
| 75 | |||||||||||||
| 76 | 0 | my $name = $AUTOLOAD; | |||||||||||
| 77 | 0 | $name =~ s/.*://; # strip fully-qualified portion | |||||||||||
| 78 | 0 | ▶ | unless ( exists $self->{_permitted}->{$name} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 0 | croak("Can't access '$name' field in class $type"); | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 0 | my $is_func = $self->{_permitted}->{$name}[1] =~ m/subroutine/i; | |||||||||||
| 83 | |||||||||||||
| 84 | 0 | ▶ | unless ($is_func) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 0 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 0 | my $val = shift; | |||||||||||
| 87 | 0 | ▶ | $val = $val || ""; | ||||||||||
|
Condition0%0/2
$val || ''
MC/DC0%0/1
$val || ''
$val
Truth table0%0/2
A
A= $val
| |||||||||||||
| 88 | return $self->{$name} = $val | ||||||||||||
| 89 | 0 | ▶ | if $self->{_permitted}->{$name}[1] =~ m/write/i; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | croak("Can't write to '$name' field in class $type"); | |||||||||||
| 91 | } else { | ||||||||||||
| 92 | return $self->{$name} | ||||||||||||
| 93 | 0 | ▶ | if $self->{_permitted}->{$name}[1] =~ m/read/i; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 0 | croak("Can't read '$name' field in class $type"); | |||||||||||
| 95 | } | ||||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | # call a function | ||||||||||||
| 99 | 0 | ▶ | if ($is_func) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 0 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | |||||||||||||
| 102 | # parameterized call | ||||||||||||
| 103 | 0 | my $x = $self->{_permitted}->{$name}[0]; | |||||||||||
| 104 | 0 | return $self->$x(shift); | |||||||||||
| 105 | } else { | ||||||||||||
| 106 | |||||||||||||
| 107 | # un-parameterized call | ||||||||||||
| 108 | 0 | my $x = $self->{_permitted}->{$name}[0]; | |||||||||||
| 109 | 0 | return $self->$x(); | |||||||||||
| 110 | } | ||||||||||||
| 111 | } | ||||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | #----------------------------------------------------------------- | ||||||||||||
| 115 | # new | ||||||||||||
| 116 | #----------------------------------------------------------------- | ||||||||||||
| 117 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 118 | 0 | my ( $class, %options ) = @_; | |||||||||||
| 119 | |||||||||||||
| 120 | # permitted fields | ||||||||||||
| 121 | 0 | my %fields = ( | |||||||||||
| 122 | |||||||||||||
| 123 | # attribute => [default, accessibility], | ||||||||||||
| 124 | example_input => [ "", 'read/write' ], | ||||||||||||
| 125 | expected_output => [ "", 'read/write' ], | ||||||||||||
| 126 | regex => [ "", 'read/write' ], | ||||||||||||
| 127 | xpath => [ "", 'read/write' ], | ||||||||||||
| 128 | test_output_xml => [ "_test_xml", 'subroutine' ], | ||||||||||||
| 129 | get_xml_differences => [ "_get_xml_differences", 'subroutine' ], | ||||||||||||
| 130 | test_regex => [ "_test_regex_statement", 'subroutine' ], | ||||||||||||
| 131 | test_xpath => [ "_test_xpath_statement", 'subroutine' ], | ||||||||||||
| 132 | ); | ||||||||||||
| 133 | |||||||||||||
| 134 | # create an object | ||||||||||||
| 135 | 0 | my $self = { _permitted => \%fields }; | |||||||||||
| 136 | |||||||||||||
| 137 | # set user values if they exist | ||||||||||||
| 138 | 0 | ▶ | $self->{example_input} = $options{example_input} || ''; | ||||||||||
|
Condition0%0/2
$options{'example_input'} || ''
MC/DC0%0/1
$options{'example_input'} || ''
$options{'example_input'}
Truth table0%0/2
A
A= $options{'example_input'}
| |||||||||||||
| 139 | 0 | ▶ | $self->{expected_output} = $options{expected_output} || ''; | ||||||||||
|
Condition0%0/2
$options{'expected_output'} || ''
MC/DC0%0/1
$options{'expected_output'} || ''
$options{'expected_output'}
Truth table0%0/2
A
A= $options{'expected_output'}
| |||||||||||||
| 140 | 0 | ▶ | $self->{regex} = $options{regex} || ''; | ||||||||||
|
Condition0%0/2
$options{'regex'} || ''
MC/DC0%0/1
$options{'regex'} || ''
$options{'regex'}
Truth table0%0/2
A
A= $options{'regex'}
| |||||||||||||
| 141 | 0 | ▶ | $self->{xpath} = $options{xpath} || ''; | ||||||||||
|
Condition0%0/2
$options{'xpath'} || ''
MC/DC0%0/1
$options{'xpath'} || ''
$options{'xpath'}
Truth table0%0/2
A
A= $options{'xpath'}
| |||||||||||||
| 142 | |||||||||||||
| 143 | 0 | bless $self, $class; | |||||||||||
| 144 | 0 | return $self; | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | #----------------------------------------------------------------- | ||||||||||||
| 148 | # _test_xml: semantically compare $xml to $self->expected_output | ||||||||||||
| 149 | #----------------------------------------------------------------- | ||||||||||||
| 150 | 0 | ▶ | sub _test_xml { | ||||||||||
|
Subroutine
_test_xml: not called CC=2 SCAR=17.9
| |||||||||||||
| 151 | 0 | my ( $self, $xml ) = @_; | |||||||||||
| 152 | 0 | ▶ | return undef if $self->expected_output =~ m//g; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 153 | # compare the docs | ||||||||||||
| 154 | 0 | my $sc = XML::SemanticCompare->new(); | |||||||||||
| 155 | 0 | return $sc->compare($self->expected_output, $xml); | |||||||||||
| 156 | } | ||||||||||||
| 157 | |||||||||||||
| 158 | #----------------------------------------------------------------- | ||||||||||||
| 159 | # _test_xpath_statement: apply xpath to $xml | ||||||||||||
| 160 | #----------------------------------------------------------------- | ||||||||||||
| 161 | 0 | ▶ | sub _test_xpath_statement { | ||||||||||
|
Subroutine
_test_xpath_statement: not called CC=3 SCAR=24.8
| |||||||||||||
| 162 | 0 | my ( $self, $xml ) = @_; | |||||||||||
| 163 | # no xpath expression, nothing to test | ||||||||||||
| 164 | 0 | ▶ | return undef if $self->xpath =~ m//g; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 165 | # empty xml, nothing to test | ||||||||||||
| 166 | 0 | ▶ | return undef if $xml =~ m//g; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 167 | #instantiate a parser | ||||||||||||
| 168 | 0 | my $sc = XML::SemanticCompare->new(); | |||||||||||
| 169 | 0 | return $sc->test_xpath($self->xpath, $xml); | |||||||||||
| 170 | } | ||||||||||||
| 171 | |||||||||||||
| 172 | #----------------------------------------------------------------- | ||||||||||||
| 173 | # _test_regex_statement: apply regex to $xml | ||||||||||||
| 174 | #----------------------------------------------------------------- | ||||||||||||
| 175 | 0 | ▶ | sub _test_regex_statement { | ||||||||||
|
Subroutine
_test_regex_statement: not called CC=2 SCAR=17.9
| |||||||||||||
| 176 | 0 | my ( $self, $xml ) = @_; | |||||||||||
| 177 | 0 | my $regex = $self->regex; | |||||||||||
| 178 | 0 | ▶ | return undef unless $xml =~ m/$regex/g; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 179 | 0 | return 1; | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | #----------------------------------------------------------------- | ||||||||||||
| 183 | # _get_xml_differences: | ||||||||||||
| 184 | # get the differences between $xml and expected xml | ||||||||||||
| 185 | # and return them | ||||||||||||
| 186 | #----------------------------------------------------------------- | ||||||||||||
| 187 | 0 | ▶ | sub _get_xml_differences { | ||||||||||
|
Subroutine
_get_xml_differences: not called CC=1 SCAR=6.9
| |||||||||||||
| 188 | 0 | my ( $self, $xml ) = @_; | |||||||||||
| 189 | 0 | croak "not yet implemented ...\n"; | |||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | 0 | ▶ | sub DESTROY { } | ||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||
| 193 | |||||||||||||
| 194 | 5 | 1; | |||||||||||
| 195 | |||||||||||||
| 196 | __END__ | ||||||||||||