File: calculator_steps.pl

package info (click to toggle)
libtest-bdd-cucumber-perl 0.26-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 528 kB
  • sloc: perl: 3,436; makefile: 8
file content (94 lines) | stat: -rwxr-xr-x 2,256 bytes parent folder | download
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
#!perl

use strict;
use warnings;

use Test::More;
use Test::BDD::Cucumber::StepFile;

use lib 'examples/calculator/lib/';

Before sub {
    use_ok( 'Calculator' );
};

After sub {
    my $c = shift;
    # a bit contrived, as garbage collection would clear it out
    delete $c->stash->{'scenario'}->{'Calculator'};
    ok( not exists $c->stash->{'scenario'}->{'Calculator'} );
};

my %numbers_as_words = (
    __THE_NUMBER_ONE__ => 1,
    __THE_NUMBER_FOUR__ => 4,
    __THE_NUMBER_FIVE__ => 5,
    __THE_NUMBER_TEN__ => 10,
);

sub map_word_to_number
{
    my $word = shift;

    ok( $word );
    ok( exists $numbers_as_words{ $word } );

    return $numbers_as_words{ $word };
}

Transform qr/^(__THE_NUMBER_\w+__)$/, sub { map_word_to_number( $1 ) };

Transform qr/^table:number as word$/, sub {
    my ( $c, $data ) = @_;

    for my $row ( @{ $data } ) {
        $row->{'number'} = map_word_to_number( $row->{'number as word'} );
    }
};

Given 'a new Calculator object', sub {
    S->{'Calculator'} = Calculator->new()
};

Given qr/^having pressed (.+)/, sub {
    S->{'Calculator'}->press( $_ ) for split(/(,| and) /, C->matches->[0]);
};

Given qr/^having keyed (.+)/, sub {
    # Make this call the having pressed
    my ( $value ) = @{ C->matches };
    S->{'Calculator'}->key_in( $value );
};

Given 'having successfully performed the following calculations', sub {
    my $calculator = S->{'Calculator'};

    for my $row ( @{ C->data } ) {
        $calculator->key_in( $row->{'first'}    );
        $calculator->key_in( $row->{'operator'} );
        $calculator->key_in( $row->{'second'}   );
        $calculator->press( '=' );

        is( $calculator->display,
            $row->{'result'},
            $row->{'first'} .' '. $row->{'operator'} .' '. $row->{'second'} );
    }
};

Given 'having entered the following sequence', sub {
    S->{'Calculator'}->key_in( C->data );
};

Given 'having added these numbers', sub {
    for my $row ( @{ C->data } )
    {
        S->{'Calculator'}->key_in( $row->{number} );
        S->{'Calculator'}->key_in( '+' );
    }
};

Then qr/^the display should show (.+)/, sub {
    my ( $value ) = @{ C->matches };
    is( S->{'Calculator'}->display, $value,
        "Calculator display as expected" );
};