Commits

Fabrice Gabolde committed 71fe2de

Fix usage of given/when.

Comments (0)

Files changed (2)

 
                 my @parameters = @{$expected_output->{parameters}};
 
-                given ($expected_output->{command}) {
-                    when ('fastforward') {
-                        # handle params someday, for now assume "some"
-                        $self->test_builder->note('Fastforwarding...');
-                        $fastforwarding = 1;
-                        $self->test_builder->output(\$fastforwarding_buffer);
-                        $self->test_builder->failure_output(\$fastforwarding_sink);
-                        $self->test_builder->todo_output(\$fastforwarding_buffer);
-                    }
-                    when ('wait_less_than') {
-                        alarm $parameters[0];
-                    }
-                    default { die "unknown command '$_'" }
+                if ($expected_output->{command} eq 'fastforward') {
+                    # handle params someday, for now assume "some"
+                    $self->test_builder->note('Fastforwarding...');
+                    $fastforwarding = 1;
+                    $self->test_builder->output(\$fastforwarding_buffer);
+                    $self->test_builder->failure_output(\$fastforwarding_sink);
+                    $self->test_builder->todo_output(\$fastforwarding_buffer);
+                } elsif ($expected_output->{command} eq 'wait_less_than') {
+                    alarm $parameters[0];
+                } else {
+                    croak(sprintf(q{unknown command '%s'},
+                                  $expected_output->{command}));
                 }
 
                 next;
             my $output = $1;
             $output = $self->ansi_escape($output);
 
-            given ($expected_output->{match_type}) {
+            if ($expected_output->{match_type} eq 'literal') {
 
-                when ('literal') {
+                $self->test_builder->is_eq($output, $expected_output->{output},
+                                           sprintf(q{literal match of '%s'}, $expected_output->{output}));
 
-                    $self->test_builder->is_eq($output, $expected_output->{output},
-                                               sprintf(q{literal match of '%s'}, $expected_output->{output}));
+            } elsif ($expected_output->{match_type} eq 'regex') {
 
-                }
+                my $regex = $expected_output->{output};
+                use re 'eval'; # allow delayed interpolation trickery
+                $self->test_builder->like($output, qr/$regex/,
+                                          sprintf(q{regex match of '%s'}, $expected_output->{output}));
 
-                when ('regex') {
+            } else {
 
-                    my $regex = $expected_output->{output};
-                    use re 'eval'; # allow delayed interpolation trickery
-                    $self->test_builder->like($output, qr/$regex/,
-                                              sprintf(q{regex match of '%s'}, $expected_output->{output}));
-
-                }
-
-                default { die "unknown match type '$_'" }
+                croak(sprintf(q{unknown match type '%s'},
+                              $expected_output->{match_type}));
 
             }
 
 
         chomp($line);
 
-        given ($line) {
+        if ($line =~ /^#/) {
 
-            when (/^#/) {
+            # Comment, skip it... unless it's a shebang
 
-                # Comment, skip it... unless it's a shebang
+            if ($handle->input_line_number <= 1
+                and $line =~ m/^#!/) {
 
-                if ($handle->input_line_number <= 1
-                    and $line =~ m/^#!/) {
-
-                    $shell = $line;
-                    $shell =~ s/^#!//;
-                    $shell = [ shellwords($shell) ];
-
-                }
+                $shell = $line;
+                $shell =~ s/^#!//;
+                $shell = [ shellwords($shell) ];
 
             }
 
-            when (/^$/) {
+        } elsif ($line =~/^$/) {
 
-                # Empty line (no starting whitespace) is only for
-                # clarity
+            # Empty line (no starting whitespace) is only for clarity
+
+        } elsif ($line =~ /^ {4}\$ (.*)$/) {
+
+            # Shell command, push into the arrayref to create a new
+            # command/output couple
+
+            my $command = $1;
+            push @{$commands}, { shell => $command,
+                                 outputs => [] };
+            $plan_size++; # "no leftover output" test
+
+        } elsif ($line =~ /^($output_prefix_re)(.*)$/) {
+
+            # Output line with optional match type
+
+            my ($match_type, $output) = ($valid_output_prefixes{$1}, $2);
+
+            unless (grep { exists $_->{shell} } @{$commands}) {
+
+                push @errors, { line_number => $handle->input_line_number,
+                                error => 'Output before any shell commands' };
+                next;
 
             }
 
-            when (/^ {4}\$ (.*)$/) {
+            push @{$commands->[-1]->{outputs}}, { match_type => $match_type,
+                                                  output => $output };
+            $plan_size++; # a literal or regex match test
 
-                # Shell command, push into the arrayref to create a new
-                # command/output couple
+        } elsif ($line =~ /^($commands_re)\s*(.*)$/) {
 
-                my $command = $1;
-                push @{$commands}, { shell => $command,
-                                     outputs => [] };
-                $plan_size++; # "no leftover output" test
+            # Lembas command
+            my ($command, $parameters) = ($1, $2);
+
+            unless (exists $valid_commands{$command}) {
+
+                push @errors, { line_number => $handle->input_line_number,
+                                error => sprintf(q{Call to unknown command '%s'},
+                                                 $command) };
+                next;
 
             }
 
-            when (/^($output_prefix_re)(.*)$/) {
+            unless ($command eq 'preamble'
+                    or grep { exists $_->{shell} } @{$commands}) {
 
-                # Output line with optional match type
+                push @errors, { line_number => $handle->input_line_number,
+                                error => 'Call to command before any shell commands' };
+                next;
 
-                my ($match_type, $output) = ($valid_output_prefixes{$1}, $2);
+            }
 
-                unless (grep { exists $_->{shell} } @{$commands}) {
+            my @parameters = quotewords('\s+', 0, $parameters);
+
+            if ($parameters
+                and not @parameters) {
+
+                # parsing into quoted words failed, probably
+                # unbalanced delimiters.
+                push @errors, { line_number => $handle->input_line_number,
+                                error => 'Parameter list appears to contain unbalanced delimiters' };
+                next;
+
+            }
+
+            if ($command eq 'preamble') {
+                if (@{$commands}) {
 
                     push @errors, { line_number => $handle->input_line_number,
-                                    error => 'Output before any shell commands' };
+                                    error => 'Call to "preamble" command after shell commands' };
                     next;
 
                 }
 
-                push @{$commands->[-1]->{outputs}}, { match_type => $match_type,
-                                                      output => $output };
-                $plan_size++; # a literal or regex match test
+                push @{$commands}, { shell => undef,
+                                     outputs => [] };
+                # preamble command also ends up generating a "no
+                # leftover output" test
+                $plan_size++;
+
+            } elsif ($command eq 'wait_less_than') {
+
+                if (@parameters > 2
+                    or @parameters < 1) {
+                    push @errors, { line_number => $handle->input_line_number,
+                                    error => 'wait_less_than command accepts 1 or 2 parameters' };
+                    next;
+                } elsif (@parameters == 2) {
+                    my ($value, $unit) = @parameters;
+                    if ($unit =~ /second(?:s)?/) {
+                        @parameters = ($value);
+                    } elsif ($unit =~ /minute(?:s)?/) {
+                        @parameters = ($value * 60);
+                    } else {
+                        push @errors, { line_number => $handle->input_line_number,
+                                        error => "unknown time unit $unit" };
+                        next;
+                    }
+                }
+
+                push @{$commands->[-1]->{outputs}}, { command => $command,
+                                                      parameters => \@parameters };
+                # wait_less_than command generates a failure if the
+                # shell command timed out, then a BAIL_OUT occurs; or
+                # a success if there was no timeout
+                $plan_size++;
+
+            } else {
+
+                push @{$commands->[-1]->{outputs}}, { command => $command,
+                                                      parameters => \@parameters };
 
             }
 
-            when (/^($commands_re)\s*(.*)$/) {
+        } else {
 
-                # Lembas command
-                my ($command, $parameters) = ($1, $2);
-
-                unless (exists $valid_commands{$command}) {
-
-                    push @errors, { line_number => $handle->input_line_number,
-                                    error => sprintf(q{Call to unknown command '%s'},
-                                                     $command) };
-                    next;
-
-                }
-
-                unless ($command eq 'preamble'
-                        or grep { exists $_->{shell} } @{$commands}) {
-
-                    push @errors, { line_number => $handle->input_line_number,
-                                    error => 'Call to command before any shell commands' };
-                    next;
-
-                }
-
-                my @parameters = quotewords('\s+', 0, $parameters);
-
-                if ($parameters
-                    and not @parameters) {
-
-                    # parsing into quoted words failed, probably
-                    # unbalanced delimiters.
-                    push @errors, { line_number => $handle->input_line_number,
-                                    error => 'Parameter list appears to contain unbalanced delimiters' };
-                    next;
-
-                }
-
-                given ($command) {
-                    when ('preamble') {
-                        if (@{$commands}) {
-
-                            push @errors, { line_number => $handle->input_line_number,
-                                            error => 'Call to "preamble" command after shell commands' };
-                            next;
-
-                        }
-
-                        push @{$commands}, { shell => undef,
-                                             outputs => [] };
-                        # preamble command also ends up generating a "no
-                        # leftover output" test
-                        $plan_size++;
-
-                    }
-
-                    when ('wait_less_than') {
-
-                        if (@parameters > 2
-                            or @parameters < 1) {
-                            push @errors, { line_number => $handle->input_line_number,
-                                            error => 'wait_less_than command accepts 1 or 2 parameters' };
-                            next;
-                        } elsif (@parameters == 2) {
-                            my ($value, $unit) = @parameters;
-                            given ($unit) {
-                                when (/second(?:s)?/) {
-                                    @parameters = ($value);
-                                }
-                                when (/minute(?:s)?/) {
-                                    @parameters = ($value * 60);
-                                }
-                                default {
-                                    push @errors, { line_number => $handle->input_line_number,
-                                                    error => "unknown time unit $unit" };
-                                    next;
-                                }
-                            }
-                        }
-
-                        push @{$commands->[-1]->{outputs}}, { command => $command,
-                                                              parameters => \@parameters };
-                        # wait_less_than command generates a failure
-                        # if the shell command timed out, then a
-                        # BAIL_OUT occurs; or a success if there was
-                        # no timeout
-                        $plan_size++;
-
-                    }
-
-                    default {
-                        push @{$commands->[-1]->{outputs}}, { command => $command,
-                                                              parameters => \@parameters };
-                    }
-
-                }
-
-            }
-
-            default {
-
-                push @errors, { line_number => $handle->input_line_number,
-                                error => 'Syntax error' };
-
-            }
+            push @errors, { line_number => $handle->input_line_number,
+                            error => 'Syntax error' };
 
         }
 

t/00-construction.t

+use strict;
+use warnings;
+use 5.010;
+use Carp;
+
+use Test::More;
+use Test::Exception;
+use Test::Builder;
+
+use_ok 'Lembas';
+
+new_ok 'Lembas', [ test_builder => Test::Builder->create,
+                   shell => 'examples/ush',
+                   commands => [ { command => 'preamble' },
+                                 { outputs => { match_type => 're',
+                                                output => '' } } ] ];
+
+done_testing;