File Coverage

blib/lib/Test/C2FIT/ColumnFixture.pm
Criterion Covered Total %
statement 60 73 82.1
branch 13 20 65.0
condition n/a
subroutine 16 21 76.1
pod 2 11 18.1
total 91 125 72.8


line stmt bran cond sub pod time code
1             # $Id: ColumnFixture.pm,v 1.8 2006/06/16 15:20:56 tonyb Exp $
2             #
3             # Copyright (c) 2002-2005 Cunningham & Cunningham, Inc.
4             # Released under the terms of the GNU General Public License version 2 or later.
5             #
6             # Perl translation by Dave W. Smith
7             # Modified by Tony Byrne
8              
9             package Test::C2FIT::ColumnFixture;
10              
11 3     3   1600 use base 'Test::C2FIT::Fixture';
  3         7  
  3         228  
12 3     3   17 use strict;
  3         6  
  3         91  
13 3     3   1195 use Test::C2FIT::TypeAdapter;
  3         9  
  3         92  
14 3     3   24 use Error qw( :try );
  3         6  
  3         24  
15              
16             sub new {
17 2     2 0 9 my $pkg = shift;
18 2         25 return $pkg->SUPER::new( columnBindings => [], hasExecuted => 0, @_ );
19             }
20              
21             sub doRows {
22 1     1 0 2 my $self = shift;
23 1         8 my ($rows) = @_;
24 1         5 $self->bind( $rows->parts() );
25 1         3 $self->SUPER::doRows( $rows->more() );
26             }
27              
28             sub doRow {
29 2     2 0 3 my $self = shift;
30 2         3 my ($row) = @_;
31              
32 2         13 $self->{'hasExecuted'} = 0;
33             try {
34 2     2   36 $self->reset();
35 2         23 $self->SUPER::doRow($row);
36 2 50       9 $self->execute unless $self->{'hasExecuted'};
37             }
38             otherwise {
39 0     0   0 my $e = shift;
40 0         0 $self->exception( $row->leaf(), $e );
41 2         15 };
42             }
43              
44             sub doCell {
45 8     8 0 12 my $self = shift;
46 8         9 my ( $cell, $column ) = @_;
47              
48 8         13 my $adapter = $self->{'columnBindings'}->[$column];
49 8         13 eval {
50 8         19 my $string = $cell->text();
51 8 50       43 if ( $string eq "" ) {
    50          
    100          
    50          
52 0         0 $self->check( $cell, $adapter );
53             }
54             elsif ( not defined($adapter) ) {
55 0         0 $self->ignore($cell);
56             }
57             elsif ( $adapter->field() ) {
58 6         16 $adapter->set( $adapter->parse($string) );
59             }
60             elsif ( $adapter->method() ) {
61 2         9 $self->check( $cell, $adapter );
62             }
63             };
64 8 50       78 if ($@) {
65 0         0 $self->exception( $cell, $@ );
66             }
67             }
68              
69             sub check {
70 6     6 0 10 my $self = shift;
71 6         8 my ( $cell, $adapter ) = @_;
72              
73 6 100       24 if ( $self->{'hasExecuted'} ) {
    50          
74 3         10 $self->SUPER::check( $cell, $adapter );
75             }
76             elsif ( !$self->{'hasExecuted'} ) {
77 3         4 $self->{'hasExecuted'} = 1;
78             try {
79 3     3   61 $self->execute();
80 3         35 $self->SUPER::check( $cell, $adapter );
81             }
82             otherwise {
83 0     0   0 my $e = shift;
84 0         0 $self->exception( $cell, $e );
85 3         34 };
86             }
87             }
88              
89             sub reset {
90 0     0 1 0 my ($self) = @_;
91              
92             # about to process first cell of row
93             }
94              
95             sub execute {
96 1     1 1 3 my ($self) = @_;
97              
98             # about to process first method call of row
99             }
100              
101             sub bind {
102 2     2 0 4 my ( $self, $heads ) = @_;
103 2         4 my $column = 0;
104              
105 2         5 $self->{'columnBindings'} = [];
106 2         9 while ($heads) {
107 6         17 my $name = $heads->text();
108             try {
109 6 50   6   127 if ( $name eq "" ) {
    100          
110 0         0 $self->{'columnBindings'}->[$column] = undef;
111             }
112             elsif ( $name =~ /^(.*)\(\)$/ ) {
113 1         5 $self->{'columnBindings'}->[$column] =
114             $self->bindMethod( $self->camel($1) );
115             }
116             else {
117 5         35 $self->{'columnBindings'}->[$column] =
118             $self->bindField( $self->camel($name) );
119             }
120             }
121             otherwise {
122 0     0   0 my $e = shift;
123 0         0 $self->exception( $heads, $e );
124 6         56 };
125 6         94 $heads = $heads->more();
126 6         19 ++$column;
127             }
128             }
129              
130             sub bindMethod {
131 1     1 0 2 my $self = shift;
132 1         2 my ($name) = @_;
133 1         6 return Test::C2FIT::TypeAdapter->onMethod( $self, $name );
134             }
135              
136             sub bindField {
137 5     5 0 7 my $self = shift;
138 5         8 my ($name) = @_;
139 5         35 return Test::C2FIT::TypeAdapter->onField( $self, $name );
140             }
141              
142             sub getTargetClass {
143 0     0 0   my $self = shift;
144 0           ref($self);
145             }
146              
147             1;
148              
149             =pod
150              
151             =head1 NAME
152              
153             Test::C2FIT::ColumnFixture - A ColumnFixture maps columns in the test data to fields or methods of its subclasses.
154              
155             =head1 SYNOPSIS
156              
157             Normally, you subclass ColumnFixture.
158              
159             package MyColumnFixture;
160             use base 'Test::C2FIT::ColumnFixture;'
161              
162             sub getX {
163             my $self = shift;
164             return $self->{X};
165             }
166              
167             =head1 DESCRIPTION
168              
169             Column headings with braces (e.g. getX()) will get bound to methods, i.e. the data entered in your document
170             will be checked against the result of the respective method. A Column heading consisting of more words
171             will be concatened to a camel-case name ("get name ()" will be mapped to "getName()")
172              
173             Column headings without braces will be bound to instance variables (=fields).
174             In perl these need not to be predeclared. E.g. when column heading is "surname", then the ColumnFixture
175             puts the text of the respective cell to a variable which can be used by C<$self-E{surname}>.
176             A Column heading consisting of more words will be concatened to a camel-case name
177             ("given name" will be mapped to "givenName")
178              
179             When your data is not stored as string, then you'll propably need an TypeAdapter. See more in L.
180              
181             =head1 METHODS
182              
183             =over 4
184              
185             =item B
186              
187             Will be called before a row gets processed
188              
189             =item B
190              
191             Will be called either after a row has been processed or before the first usage of a method-column in the
192             row, depending upon which case occurs first.
193              
194             =back
195              
196             =head1 SEE ALSO
197              
198             Extensive and up-to-date documentation on FIT can be found at:
199             http://fit.c2.com/
200              
201              
202             =cut
203              
204             __END__