aboutsummaryrefslogtreecommitdiffstats
path: root/testing/lib/Test/Irssi/API.pm
blob: 3a659ed6496ce632be97a2987712db5bc90cbb75 (plain) (blame)
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
use strictures 1;
use MooseX::Declare;

class Test::Irssi::API {

    use POE;
    use Data::Dumper;

    has 'parent'
      => (
          is       => 'ro',
          isa      => 'Test::Irssi',
          required => 1,
         );


    has 'tests'
      => (
          traits => [qw/Hash/],
          is => 'rw',
          isa => 'HashRef',
          default => sub { {} },
          handles => {
                      test_names => 'keys',
                     },
         );


    sub create_test {
        my ($self, $name, $desc) = @_;
        $self->tests->{$name} = {desc => $desc, input => [], output => []};
    }

    sub simulate_input {
        my ($self, $name, $input) = @_;
        push @{ $self->tests->{$name}->{input} }, { input => $input };
    }

    sub simulate_delay {
        my ($self, $name, $delay) = @_;
        push @{ $self->tests->{$name}->{input} }, { delay => $delay };

    }

    sub expect_output {
        my ($self, $name, $regex, $line) = @_; # line is optional?
        push @{ $self->tests->{$name}->{output} }, { regex => $regex, line => $line };
    }

    sub run_test {
        my ($self, $test_name) = @_;
        my $data = $self->tests->{$test_name};
        foreach my $entry (@{ $data->{input} }) {
            if (exists $entry->{input}) {
                my $text = $entry->{input};
                $self->parent->inject_text($text);
            } elsif (exists $entry->{delay}) {
                my $delay = $entry->{delay};
                _do_delay($delay);
            } else {
                die "What: " . Dumper($entry);
            }
        }
    }

    sub run_tests {
        my ($self) = @_;
        foreach my $test_name ($self->test_names) {
            my $test = $self->tests->{$test_name};
            print "Going to prcess: $test_name";
            print Dumper($test);

        }
    }


    sub _do_delay {
        $poe_kernel->post('IrssiTestDriver' => create_delay => 5);
    }
}