Blame t/author-lib-convert-string-_to_bin.t

Packit dcde0c
#!perl
Packit dcde0c
Packit dcde0c
BEGIN {
Packit dcde0c
    unless ($ENV{AUTHOR_TESTING}) {
Packit dcde0c
        require Test::More;
Packit dcde0c
        Test::More::plan(skip_all =>
Packit dcde0c
                         'these tests are for testing by the author');
Packit dcde0c
    }
Packit dcde0c
}
Packit dcde0c
Packit dcde0c
use strict;
Packit dcde0c
use warnings;
Packit dcde0c
Packit dcde0c
use Test::More tests => 3625;
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
# Read and load configuration file and backend library.
Packit dcde0c
Packit dcde0c
use Config::Tiny ();
Packit dcde0c
Packit dcde0c
my $config_file = 't/author-lib.ini';
Packit dcde0c
my $config = Config::Tiny -> read('t/author-lib.ini')
Packit dcde0c
  or die Config::Tiny -> errstr();
Packit dcde0c
Packit dcde0c
# Read the library to test.
Packit dcde0c
Packit dcde0c
our $LIB = $config->{_}->{lib};
Packit dcde0c
Packit dcde0c
die "No library defined in file '$config_file'"
Packit dcde0c
  unless defined $LIB;
Packit dcde0c
die "Invalid library name '$LIB' in file '$config_file'"
Packit dcde0c
  unless $LIB =~ /^[A-Za-z]\w*(::\w+)*\z/;
Packit dcde0c
Packit dcde0c
# Load the library.
Packit dcde0c
Packit dcde0c
eval "require $LIB";
Packit dcde0c
die $@ if $@;
Packit dcde0c
Packit dcde0c
###############################################################################
Packit dcde0c
Packit dcde0c
can_ok($LIB, '_to_bin');
Packit dcde0c
Packit dcde0c
my @data;
Packit dcde0c
Packit dcde0c
# Small numbers.
Packit dcde0c
Packit dcde0c
for (my $x = 0; $x <= 255 ; ++ $x) {
Packit dcde0c
    push @data, [ $x, sprintf("%b", $x) ];
Packit dcde0c
}
Packit dcde0c
Packit dcde0c
# Add data in data file.
Packit dcde0c
Packit dcde0c
(my $datafile = $0) =~ s/\.t/.dat/;
Packit dcde0c
open DATAFILE, $datafile or die "$datafile: can't open file for reading: $!";
Packit dcde0c
while (<DATAFILE>) {
Packit dcde0c
    s/\s+\z//;
Packit dcde0c
    next if /^#/ || ! /\S/;
Packit dcde0c
    push @data, [ split /:/ ];
Packit dcde0c
}
Packit dcde0c
close DATAFILE or die "$datafile: can't close file after reading: $!";
Packit dcde0c
Packit dcde0c
# List context.
Packit dcde0c
Packit dcde0c
for (my $i = 0 ; $i <= $#data ; ++ $i) {
Packit dcde0c
    my ($in0, $out0) = @{ $data[$i] };
Packit dcde0c
Packit dcde0c
    my ($x, @got);
Packit dcde0c
Packit dcde0c
    my $test = qq|\$x = $LIB->_new("$in0"); |
Packit dcde0c
             . qq|\@got = $LIB->_to_bin(\$x)|;
Packit dcde0c
Packit dcde0c
    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
Packit dcde0c
Packit dcde0c
    eval $test;
Packit dcde0c
    is($@, "", "'$test' gives emtpy \$\@");
Packit dcde0c
Packit dcde0c
    subtest "_to_bin() in list context: $test", sub {
Packit dcde0c
        plan tests => 3,
Packit dcde0c
Packit dcde0c
        cmp_ok(scalar @got, '==', 1,
Packit dcde0c
               "'$test' gives one output arg");
Packit dcde0c
Packit dcde0c
        is(ref($got[0]), "",
Packit dcde0c
           "'$test' output arg is a scalar");
Packit dcde0c
Packit dcde0c
        is($got[0], $out0,
Packit dcde0c
           "'$test' output arg has the right value");
Packit dcde0c
    };
Packit dcde0c
}
Packit dcde0c
Packit dcde0c
# Scalar context.
Packit dcde0c
Packit dcde0c
for (my $i = 0 ; $i <= $#data ; ++ $i) {
Packit dcde0c
    my ($in0, $out0) = @{ $data[$i] };
Packit dcde0c
Packit dcde0c
    my ($x, $got);
Packit dcde0c
Packit dcde0c
    my $test = qq|\$x = $LIB->_new("$in0"); |
Packit dcde0c
             . qq|\$got = $LIB->_to_bin(\$x)|;
Packit dcde0c
Packit dcde0c
    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
Packit dcde0c
Packit dcde0c
    eval $test;
Packit dcde0c
    is($@, "", "'$test' gives emtpy \$\@");
Packit dcde0c
Packit dcde0c
    subtest "_to_bin() in scalar context: $test", sub {
Packit dcde0c
        plan tests => 2,
Packit dcde0c
Packit dcde0c
        is(ref($got), "",
Packit dcde0c
           "'$test' output arg is a scalar");
Packit dcde0c
Packit dcde0c
        is($got, $out0,
Packit dcde0c
           "'$test' output arg has the right value");
Packit dcde0c
    };
Packit dcde0c
}