1#!/usr/bin/perl -w
2# vim: set ft=perl:
3
4use strict;
5
6use Test::More;
7use Shell::Base;
8use File::Temp qw(tempfile);
9
10plan tests => 8;
11
12my ($tmpfh, $tmp) = tempfile("sbXXXXXX", UNLINK => 1);
13
14print $tmpfh <<'TMP';
15name ="Darren Chamberlain"
16email= "darren@cpan.org"
17Date = "Tue Mar 18 17:52:14 EST 2003"
18TMP
19
20close $tmpfh;   # Is closing this ok?
21
22my %config = Shell::Base->parse_rcfile($tmp);
23is(keys %config,     3,                                 "3 elements");
24is($config{'name'},  '"Darren Chamberlain"',            "\$config{name} => $config{name}");
25is($config{'email'}, '"darren@cpan.org"',               "\$config{email} => $config{email}");
26is($config{'date'},  '"Tue Mar 18 17:52:14 EST 2003"',  "\$config{date} => $config{date}");
27is($config{'Date'},  undef,                             "\$config{Date} => not defined");
28
29package New::Package;
30use base qw(Shell::Base);
31
32sub parse_rcfile {
33    my ($self, $rcfile) = @_;
34    my @lines = ();
35    my $c = 0;
36    local *F;
37
38    open F, $rcfile or die "Can't open $rcfile: $!";
39    while (defined(my $l = <F>)) {
40        chomp $l;
41        push @lines, $l;
42    }
43    close F or die "Can't close $rcfile: $!";
44
45    return map { ++$c => $_ } @lines;
46}
47
48package main;
49
50%config = New::Package->parse_rcfile($tmp);
51is($config{1} => 'name ="Darren Chamberlain"',           "$config{1} => 'name = \"Darren Chamberlain\"'");
52is($config{2} => 'email= "darren@cpan.org"',             "$config{2} => 'email = \"darren\@cpan.org\"'");
53is($config{3} => 'Date = "Tue Mar 18 17:52:14 EST 2003"', "$config{3} => 'Date = \"Tue Mar 18 17:52:14 EST 2003\"'");
54