Difference between revisions of "Perl Script Snippets"

From STRIDE Wiki
Jump to: navigation, search
(Created page with '== Canonical Module Format == <source lang="perl"> use strict; use warnings; package MyTests; use base qw(STRIDE::Test); use STRIDE::Test; sub test_one : Test { ASSERT_TRU…')
 
Line 1: Line 1:
== Canonical Module Format ==
+
== Canonical module format ==
  
 
<source lang="perl">
 
<source lang="perl">
Line 47: Line 47:
 
</source>
 
</source>
  
== Test Module Including POD Documentation ==
+
== Test module including POD documentation ==
  
 
<source lang="perl">
 
<source lang="perl">
Line 89: Line 89:
  
 
1;
 
1;
 +
</source>
 +
 +
== Simple expectation test ==
 +
 +
<source lang="perl">
 +
sub sync_exact : Test {   
 +
    my $h = TestPointSetup(
 +
        order => TEST_POINT_EXPECT_ORDERED,
 +
        expected => [
 +
            "point a",
 +
            "point b",
 +
            "point c"
 +
        ],
 +
        unexpected => [ TEST_POINT_EVERYTHING_ELSE ]
 +
    );
 +
   
 +
    #...start source under test, if necessary
 +
 +
    # use Check if the events have all happened by the time
 +
    # you validate the test points. Otherwise use Wait with
 +
    # a reasonable timeout value.
 +
    $h->Check();
 +
}
 +
</source>
 +
 +
== Invoking a function on the target ==
 +
 +
=== Blocking syntax (blocks until function returns) ===
 +
 +
<source lang="perl">
 +
my $retval = Functions->foo(1, "input string");
 +
Functions->bar();
 +
</source>
 +
 +
=== Non-blocking syntax (return immediately, function continues to execute on device) ===
 +
 +
<source lang="perl">
 +
Functions->{non_blocking} = 1;
 +
Functions->foo(1, "input string");
 +
my $retval = Functions->WaitReturn('foo', 1000); #waits up to one second for function to return
 +
</source>
 +
 +
== Accessing compiler macro values (constants) at the time of source compilation ==
 +
<source lang="c">
 +
/* some compilation unit included in the STRIDE compilation process */
 +
#define SOME_DEFINED_VALUE 42
 +
</source>
 +
 +
<source lang="perl">
 +
my $value = Constants->{SOME_DEFINED_VALUE};
 
</source>
 
</source>

Revision as of 12:31, 28 January 2010

Canonical module format

use strict;
use warnings;

package MyTests;
use base qw(STRIDE::Test);
use STRIDE::Test;

sub test_one : Test
{
    ASSERT_TRUE(1);
}

sub test_two : Test
{
    ASSERT_TRUE(0);
}

1;

Subroutine attributes to declare test methods and fixtures

sub a_test : Test {
    # test method
}

sub startup_method : Test(startup) {
    # startup fixturing
}

sub shutdown_method : Test(shutdown) {
    # shutdown fixturing
}

sub setup_method : Test(setup) {
    # setup fixturing
}

sub teardown_method : Test(teardown) {
    # teardown fixture
}

Test module including POD documentation

package MyTests;
use base qw(STRIDE::Test);
use STRIDE::Test;

=head1 NAME

MyTests - example tests

=head1 DESCRIPTION

This is MyTests_1, a deeply funky piece of Perl code.

=head1 METHODS

=cut


=head2 test_one

this is a simple passing test.

=cut
sub test_one : Test
{
    ASSERT_TRUE(1);
}


=head2 test_two

this is a simple passing test.

=cut
sub test_two : Test
{
    ASSERT_TRUE(0);
}

1;

Simple expectation test

sub sync_exact : Test {    
    my $h = TestPointSetup(
        order => TEST_POINT_EXPECT_ORDERED,
        expected => [
            "point a",
            "point b",
            "point c"
        ],
        unexpected => [ TEST_POINT_EVERYTHING_ELSE ]
    );
    
    #...start source under test, if necessary

    # use Check if the events have all happened by the time 
    # you validate the test points. Otherwise use Wait with 
    # a reasonable timeout value.
    $h->Check(); 
}

Invoking a function on the target

Blocking syntax (blocks until function returns)

my $retval = Functions->foo(1, "input string");
Functions->bar();

Non-blocking syntax (return immediately, function continues to execute on device)

Functions->{non_blocking} = 1;
Functions->foo(1, "input string");
my $retval = Functions->WaitReturn('foo', 1000); #waits up to one second for function to return

Accessing compiler macro values (constants) at the time of source compilation

/* some compilation unit included in the STRIDE compilation process */
#define SOME_DEFINED_VALUE 42
my $value = Constants->{SOME_DEFINED_VALUE};