"Fossies" - the Fresh Open Source Software Archive

Member "sendpage-1.001001/t/01_Sendpage-KeesConf.t" (3 Jan 2008, 6331 Bytes) of package /linux/privat/old/sendpage-1.001001.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl
    2 # -*- Mode: CPerl -*-
    3 # Sendpage-KeesConf.t -- unit test for Sendpage::KeesConf
    4 # $Id: 01_Sendpage-KeesConf.t 223 2006-08-24 16:01:31Z keescook $
    5 #
    6 # Copyright (C)  2006  Zak B. Elep
    7 # zakame@spunge.org, http://zakame.spunge.org
    8 #
    9 # This program is free software; you can redistribute it and/or modify
   10 # it under the terms of the GNU General Public License as published by
   11 # the Free Software Foundation; either version 2 of the License,
   12 # or (at your option) any later version.
   13 #
   14 # This program is distributed in the hope that it will be useful, but
   15 # WITHOUT ANY WARRANTY; without even the implied warranty of
   16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17 # GNU General Public License for more details.
   18 #
   19 # You should have received a copy of the GNU General Public License
   20 # along with this program; if not, write to the Free Software Foundation, Inc.,
   21 # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
   22 #
   23 # After installation, run as `perl Sendpage-KeesConf.t'
   24 
   25 use strict;
   26 use warnings;
   27 
   28 use Test::More tests => 57;
   29 BEGIN { use_ok( 'Sendpage::KeesConf' ) or die }
   30 
   31 CONSTRUCTOR: {
   32     my $config = new Sendpage::KeesConf;
   33     isa_ok( $config, 'Sendpage::KeesConf' );
   34     my @functions = qw( dump define instance_exists ifset exists
   35                         fallbackget get instances file set breakdown );
   36     can_ok( $config, $_ ) foreach @functions;
   37 }
   38 
   39 CONFIG_WITH_DEFAULTS: {
   40     my %defaults =
   41         (
   42          boolean => { ARGCOUNT => 0, DEFAULT => "true", },
   43          scalar  => { ARGCOUNT => 1, DEFAULT => "foo" },
   44          list    => { ARGCOUNT => 2, DEFAULT => [ qw(bar baz) ], },
   45         );
   46 
   47     my $config = Sendpage::KeesConf->new();
   48     isa_ok( $config, 'Sendpage::KeesConf' );
   49     foreach (keys %defaults) {
   50         $config->define( $_, $defaults{ $_ } );
   51         isnt( $config->exists( $_ ),
   52               1,
   53               qq(exists("$_") as DEFAULT) );
   54         isnt( $config->ifset( $_ ),
   55               1,
   56               qq(ifset("$_") as DEFAULT) );
   57         is_deeply( $config->get( $_ ),
   58                    $defaults{ $_ }{DEFAULT},
   59                    qq(get("$_") as DEFAULT) );
   60         is_deeply( $config->fallbackget( $_ ),
   61                    $defaults{ $_ }{DEFAULT},
   62                    qq(fallbackget("$_") as DEFAULT) );
   63     }
   64 }
   65 
   66 CONFIG_FROM_FILE: {
   67     # populate our data
   68     my %settings =
   69         (
   70          modem =>
   71          {
   72           DEFAULT   => { baud    => 56000, flowctl => 'hardware', },
   73           sportster => { baud    => 115200 },
   74           hayes     => { flowctl => 'software' },
   75           dummy     => { },
   76          },
   77         );
   78 
   79     my $config = new Sendpage::KeesConf;
   80     isa_ok( $config, 'Sendpage::KeesConf' );
   81 
   82     # populate defaults
   83     for my $section (keys %settings) {
   84         for my $var (keys %{ $settings{$section}{DEFAULT} }) {
   85             $config->define( "$section:$var",
   86                              {
   87                               DEFAULT => $settings{$section}{DEFAULT}{$var}
   88                              } );
   89         }
   90     }
   91 
   92     # make the tempfile
   93     use File::Temp ();
   94     my $tfh = new File::Temp( UNLINK => 1 );
   95     isa_ok( $tfh, 'File::Temp', $tfh );
   96     my $fname = $tfh->filename;
   97 
   98     # populate the tempfile with sample config
   99     for my $section (keys %settings) {
  100         for my $instance (keys %{ $settings{$section} }) {
  101             next if $instance =~ /DEFAULT/;
  102             print $tfh "[$section:$instance]\n";
  103             for my $var (keys %{ $settings{$section}{$instance} }) {
  104                 print $tfh "$var = $settings{$section}{$instance}{$var}\n";
  105             }
  106             print $tfh "\n";
  107         }
  108     }
  109     close $tfh;
  110 
  111     is( $config->file($fname), 1, "file($fname) read" );
  112 
  113     # test settings from %settings
  114     for my $section (keys %settings) {
  115         is_deeply( [ "DEFAULT", $config->instances($section) ],
  116                    [ sort keys %{ $settings{$section} } ],
  117                    qq(instances("$section")) );
  118 
  119         # test instances
  120         for my $instance (keys %{ $settings{$section} }) {
  121             next if $instance =~ /DEFAULT/;
  122             is( $config->instance_exists("$section:$instance"),
  123                 1,
  124                 qq(instance_exists("$section:$instance") from $fname) );
  125 
  126             # test overriden settings
  127             for my $var (keys %{ $settings{$section}{$instance} }) {
  128                 is_deeply( [ $config->breakdown("$section:$instance\@$var") ],
  129                            [ $section, $instance, $var ],
  130                            qq(breakdown("$section:$instance\@$var")) );
  131                 is( $config->exists("$section:$instance\@$var"),
  132                     1,
  133                     qq(exists("$section:$instance\@$var") from $fname) );
  134                 is( $config->get("$section:$instance\@$var"),
  135                     $settings{$section}{$instance}{$var},
  136                     qq(get("$section:$instance\@$var") from $fname) );
  137                 is( $config->fallbackget("$section:$instance\@$var"),
  138                     $settings{$section}{$instance}{$var},
  139                     qq(fallbackget("$section:$instance\@$var") from $fname) );
  140                 is( $config->ifset("$section:$instance\@$var"),
  141                     $settings{$section}{$instance}{$var},
  142                     qq(ifset("$section:$instance\@$var") from $fname) );
  143             }
  144 
  145             # test defaults
  146             for my $default (keys %{ $settings{$section}{DEFAULT} }) {
  147                 my @vars = keys %{ $settings{$section}{$instance} };
  148                 next if "@vars" =~ /$default/;
  149                 isnt( $config->exists("$section:$instance\@$default"),
  150                       1,
  151                       qq(exists("$section:$instance\@$default") from DEFAULT) );
  152                 is( $config->fallbackget("$section:$instance\@$default"),
  153                     $settings{$section}{DEFAULT}{$default},
  154                     qq(fallbackget("$section:$instance\@$default") from DEFAULT) );
  155                 is( $config->ifset("$section:$instance\@$default"),
  156                     undef,
  157                     qq(ifset("$section:$instance\@$default") from DEFAULT) );
  158             }
  159         }
  160     }
  161 
  162     # cleanup
  163     is( unlink($fname),
  164         1,
  165         "Remove $tfh" );
  166     ok( !-e $fname, "$tfh gone" );
  167 }
  168 
  169 # /* vim: set filetype=perl : */ 
  170 # /* vi:set ai ts=4 sw=4 expandtab: */