1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
################################################################################
#
# $Project: /Convert-Binary-C $
# $Author: mhx $
# $Date: 2009/03/15 04:11:00 +0100 $
# $Revision: 24 $
# $Source: /tests/001_init.t $
#
################################################################################
#
# Copyright (c) 2002-2009 Marcus Holland-Moritz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
################################################################################
use Test::More tests => 29;
use constant SUCCEED => 1;
use constant FAIL => 0;
#===================================================================
# try to require the modules (2 tests)
#===================================================================
require_ok('Convert::Binary::C');
require_ok('Convert::Binary::C::Cached');
#===================================================================
# check if we build the right object (4 tests)
#===================================================================
eval { $p = new Convert::Binary::C };
is($@, '', "create Convert::Binary::C object");
is(ref $p, 'Convert::Binary::C',
"blessed Convert::Binary::C reference");
eval { $p = new Convert::Binary::C::Cached };
is($@, '', "create Convert::Binary::C::Cached object");
is(ref $p, 'Convert::Binary::C::Cached',
"blessed Convert::Binary::C::Cached reference");
#===================================================================
# check initialization during construction (4 tests)
#===================================================================
eval {
$p = new Convert::Binary::C PointerSize => 4,
EnumSize => 4,
IntSize => 4,
Alignment => 2,
ByteOrder => 'BigEndian',
EnumType => 'Both';
};
is($@, '', "create Convert::Binary::C object with arguments");
is(ref $p, 'Convert::Binary::C',
"blessed Convert::Binary::C reference");
@warn = ();
eval {
local $SIG{__WARN__} = sub { push @warn, $_[0] };
$p = new Convert::Binary::C::Cached Cache => 'tests/cache.cbc',
PointerSize => 4,
EnumSize => 4,
IntSize => 4,
Alignment => 2,
ByteOrder => 'BigEndian',
EnumType => 'Both';
};
is($@, '', "create Convert::Binary::C::Cached object with arguments");
is(ref $p, 'Convert::Binary::C::Cached',
"blessed Convert::Binary::C::Cached reference");
if (@warn) {
my $ok = 1;
printf "# %d warning(s) issued:\n", scalar @warn;
for (@warn) {
diag($_);
/Cannot load (?:Data::Dumper|IO::File), disabling cache at $0/
or $ok = 0;
}
ok($ok, 'warnings');
}
else { pass('warnings') }
#===================================================================
# check unknown options in constructor (2 tests)
#===================================================================
eval {
$p = new Convert::Binary::C FOO => 123, ByteOrder => 'BigEndian', BAR => ['abc'];
};
like($@, qr/Invalid option 'FOO' at \Q$0/);
eval {
$p = new Convert::Binary::C::Cached FOO => 123, ByteOrder => 'BigEndian', BAR => ['abc'];
};
like($@, qr/Invalid option 'FOO' at \Q$0/);
#===================================================================
# check invalid construction (2 tests)
#===================================================================
eval {
$p = new Convert::Binary::C FOO;
};
like($@, qr/Number of configuration arguments to new must be even at \Q$0/);
eval {
$p = new Convert::Binary::C::Cached FOO;
};
like($@, qr/Number of configuration arguments to new must be even at \Q$0/);
#===================================================================
# check invalid construction (2 tests)
#===================================================================
eval {
$p = new Convert::Binary::C ByteOrder => 'FOO';
};
like($@, qr/ByteOrder must be.*not 'FOO' at \Q$0/);
eval {
$p = new Convert::Binary::C::Cached ByteOrder => 'FOO';
};
like($@, qr/ByteOrder must be.*not 'FOO' at \Q$0/);
#===================================================================
# check undefined feature (2 tests)
#===================================================================
eval {
$p = Convert::Binary::C::feature('foobar');
};
is($@, '');
ok(not defined $p);
#===================================================================
# check calling feature as method (2 tests)
#===================================================================
eval {
$p = new Convert::Binary::C;
$p = $p->feature('debug');
};
is($@, '');
ok(defined $p);
#===================================================================
# check object corruption (8 tests)
#===================================================================
for my $class (qw(Convert::Binary::C Convert::Binary::C::Cached)) {
eval { $p = $class->new };
is($@, '');
eval { $p->{''} = 0 };
like($@, qr/^Modification of a read-only value attempted/);
$tmp = delete $p->{''};
eval { $p->clean };
like($@, qr/THIS is corrupt/);
$p->{''} = $tmp;
$e = {'' => $tmp};
bless $e, ref $p;
eval { $e->clean };
like($@, qr/THIS->hv is corrupt/);
# don't forget to rebless to avoid warnings during cleanup
bless $e, 'main';
}
|