8
use constant HAVE_COLORSTRIP => defined &Term::ANSIColor::colorstrip;
8
11
BEGIN { $n_tests += 7 }
10
13
my $mk = Text::Aligner::MaxKeeper->new;
12
15
$mk->remember( $_) for 0, 5, 3, -1, 5, 1;
28
31
BEGIN { $n_tests += 6 }
30
33
BEGIN { *_compile_alispec = \ &Text::Aligner::_compile_alispec }
31
34
my @specs = qw( left center right num);
32
ok( ref( ( _compile_alispec( $_))[ 1]), 'CODE') for @specs, 0.5, 'num(,)', sub {};
35
is( ref( ( _compile_alispec( $_))[ 1]), 'CODE') for @specs, 0.5, 'num(,)', sub {};
34
37
# expected positions for combinations of string/specification
35
38
BEGIN { $n_tests += 12*7 } # number of strings * number of specs
56
59
my $wanted = shift @ans;
57
60
my $got = $code->( $str);
58
61
my $showstr = "'$str'";
59
ok( "($spec, $showstr) -> $got", "($spec, $showstr) -> $wanted");
62
is( "($spec, $showstr) -> $got", "($spec, $showstr) -> $wanted");
63
66
# Text::Aligner class
64
67
BEGIN { $n_tests += 1 }
70
BEGIN { $n_tests += 2 }
71
ok(Text::Aligner::_is_number(12.3));
72
ok(!Text::Aligner::_is_number('abc'));
74
# ok(Text::Aligner::_is_number(colored('123', 'red')));
66
77
my $ali = Text::Aligner->new;
67
ok( ref $ali, 'Text::Aligner');
78
is( ref $ali, 'Text::Aligner');
69
use constant STRINGS =>
70
81
# undef, '', ' ', qw( Z xxZ xxxxxxxxxZ 0 19 .1 9. 9.11 11119.1 1119.11111);
71
82
# reduced sample for distribution
72
undef, qw( Z xxxxZ 0 9.11 1119.111111);
83
use constant STRINGS => undef, qw( Z xxxxZ 0 9.11 1119.111111);
74
84
use constant SPECS => qw( left center right num auto);
77
87
my $nstr = @{ [ STRINGS ]};
78
88
my $nspec = @{ [ SPECS ]};
79
$n_tests += $nspec*( $nstr + 2*$nstr*$nstr); # according to proram below
89
$n_tests += $nspec*( $nstr + 2*$nstr*$nstr); # according to program below
82
92
for my $spec ( SPECS ) {
84
94
for my $str ( STRINGS ) {
85
95
my $res = $ali->justify( $str);
87
$diag = "new $spec-aligner justifies '$str' to '$res'" unless
97
my $strout = defined $str ? $str : '';
98
$diag = "new $spec-aligner justifies '$strout' to '$res'" unless
91
102
for my $init ( STRINGS ) {
92
103
$ali->alloc( $init);
97
108
if ( length( $res) != length( $init) ) {
98
109
$diag = "$spec-aligner with '$init' justifies '$str' to '$res' (length)";
102
113
defined $str or $str = '';
103
114
if ( $spec =~ /num/ and $str =~ /[9Z]/ and $init =~ /[9Z]/ ) {
123
134
my @col = qw( just a test!);
124
135
my @save_col = @col; # copy for later
125
136
my @res = align( '', @col);
126
ok( $res[ 0], 'just ');
128
ok( $res[ 2], 'test!');
137
is( $res[ 0], 'just ');
139
is( $res[ 2], 'test!');
131
142
my $res = align( 'right', @col);
132
ok( $res, " just\n a\ntest!\n");
143
is( $res, " just\n a\ntest!\n");
134
145
# original unchanged?
135
ok( join( '|', @col), join( '|', @save_col));
146
is( join( '|', @col), join( '|', @save_col));
137
148
# in-place alignment
138
149
align( '', @col);
139
ok( $col[ 0], 'just ');
141
ok( $col[ 2], 'test!');
150
is( $col[ 0], 'just ');
152
is( $col[ 2], 'test!');
143
154
# scalar deref (not sure i like this feature)
144
155
@col = @save_col;
145
156
my $scalar = 'now';
146
157
align( '', $col[ 0], \ $col[ 1], $col[ 2], \ $scalar);
147
ok( $col[ 0], 'just ');
149
ok( $col[ 2], 'test!');
150
ok( $scalar, 'now ');
158
is( $col[ 0], 'just ');
160
is( $col[ 2], 'test!');
161
is( $scalar, 'now ');
165
my $ver = $Term::ANSIColor::VERSION;
167
"Term::ANSIColor $ver doesn't support colorstrip",
169
) unless HAVE_COLORSTRIP;
170
*colorstrip = \ &Term::ANSIColor::colorstrip;
174
colored('a', 'green'),
175
colored('test!', 'bold'),
176
colored(123.456, 'red'),
179
my @res = align( 'auto', @col);
180
my @ref = align( 'auto', map colorstrip($_), @col);
181
my @check = map colorstrip($_), @res;
183
is($check[$_], $ref[$_], "color support $_") for 0 .. $#col;
184
ok(Text::Aligner::_is_number(colored('123', 'red')));
152
187
# fail as expected?
153
188
eval { align( '', 'wirdnix') };
154
ok( $@, qr/Modification of a read-only value/ );
189
like( $@, qr/^Modification of a read-only value/ );
156
191
BEGIN { plan tests => $n_tests }