Home | Mirror | Search | 雜文 | ITEYE 博客 | OSChina 博客 | 51CTO 博客

6.3. Test::Simple

過程 6.3. setp by setp

  1. My::Test::English

    $ mkdir -p lib/My/Test/
    $ vim lib/My/Test/English.pm
    				

    package My::Test::English;
    
    # 返回單詞的複數
    sub plural {
    
        my $word = shift;
    
        # 以s/x/sh/ch結尾的情況
        if ( $word =~ /(s|x|sh|ch)$/ ) {
            $word .= 'es';
        }
    
        # 以輔音+y結尾的情況
        elsif ( $word =~ /[^aeiou]y$/ ) {
            $word =~ s/y$/ies/;
        }
    
        # 以f結尾的情況
        elsif ( $word =~ /f$/ ) {
            $word =~ s/f$/ves/;
        }
    
        # 其他情況
        else {
            $word .= 's';
        }
    
        return $word;
    }
    
    1;
    				
  2. plural.t

    $ vim t/plural.t

    #!/usr/bin/perl
    
    use My::Test::English;
    use Test::Simple qw(no_plan);
    
    BEGIN{ use lib qw(My/Test) }
    
    sub plural { My::Test::English::plural(shift) }
    
    ok (plural('diff')      eq 'difves',    plural('diff'));
    ok (plural('try')       eq 'tries',     plural('try'));
    ok (plural('book')      eq 'books',     plural('book'));
    ok (plural('finish')eq 'finish',        plural('finish'));
    ok (plural('work') eq 'works',          'works');
    				
  3. Build test

    $ ./Build test --test_files t/plural.t --verbose 1
    				
comments powered by Disqus