~ubuntu-branches/ubuntu/trusty/libtext-aligner-perl/trusty

« back to all changes in this revision

Viewing changes to t/10_Aligner.t

  • Committer: Bazaar Package Importer
  • Author(s): Zed Pobre
  • Date: 2011-06-10 12:26:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110610122648-exkawt5o0xre4w5v
Tags: 0.07-1
* New upstream version
  * Color support added
* Standards-Version: 3.9.2
* Switch from CDBS to DH

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
use Test;
 
1
use Test::More;
2
2
use strict;
3
3
 
4
4
my $n_tests;
5
5
use Text::Aligner;
6
6
 
 
7
use Term::ANSIColor;
 
8
use constant HAVE_COLORSTRIP => defined &Term::ANSIColor::colorstrip;
 
9
 
7
10
# MaxKeeper
8
11
BEGIN { $n_tests += 7 }
9
12
 
10
13
my $mk = Text::Aligner::MaxKeeper->new;
11
 
ok( $mk->max, undef);
 
14
is( $mk->max, undef);
12
15
$mk->remember( $_) for 0, 5, 3, -1, 5, 1;
13
 
ok( $mk->max, 5);
 
16
is( $mk->max, 5);
14
17
$mk->forget( -1);
15
 
ok( $mk->max, 5);
16
 
$mk->forget( 5);
17
 
ok( $mk->max, 5);
18
 
$mk->forget( 5);
19
 
ok( $mk->max, 3);
 
18
is( $mk->max, 5);
 
19
$mk->forget( 5);
 
20
is( $mk->max, 5);
 
21
$mk->forget( 5);
 
22
is( $mk->max, 3);
20
23
$mk->remember( 3);
21
24
$mk->remember( 2);
22
25
$mk->forget( 3);
23
 
ok( $mk->max, 3);
 
26
is( $mk->max, 3);
24
27
$mk->forget( 3);
25
 
ok( $mk->max, 2);
 
28
is( $mk->max, 2);
26
29
 
27
30
# _compile_alispec
28
31
BEGIN { $n_tests += 6 }
29
32
 
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 {};
33
36
 
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");
60
63
    }
61
64
}
62
65
 
63
66
# Text::Aligner class
64
67
BEGIN { $n_tests += 1 }
65
68
 
 
69
# number recognition
 
70
BEGIN { $n_tests += 2 }
 
71
ok(Text::Aligner::_is_number(12.3));
 
72
ok(!Text::Aligner::_is_number('abc'));
 
73
# moved to SKIP block
 
74
# ok(Text::Aligner::_is_number(colored('123', 'red')));
 
75
 
 
76
 
66
77
my $ali = Text::Aligner->new;
67
 
ok( ref $ali, 'Text::Aligner');
 
78
is( ref $ali, 'Text::Aligner');
68
79
 
69
 
use constant STRINGS =>
 
80
# full sample
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);
73
 
 
 
83
use constant STRINGS => undef, qw( Z xxxxZ 0 9.11 1119.111111);
74
84
use constant SPECS => qw( left center right num auto);
75
85
 
76
86
BEGIN {
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
80
90
}
81
91
 
82
92
for my $spec ( SPECS ) {
84
94
    for my $str ( STRINGS ) {
85
95
        my $res = $ali->justify( $str);
86
96
        my $diag = 'ok';
87
 
        $diag = "new $spec-aligner justifies '$str' to '$res'" unless
88
 
        $str eq $res;
89
 
        ok( $diag, 'ok');
 
97
        my $strout = defined $str ? $str : '';
 
98
        $diag = "new $spec-aligner justifies '$strout' to '$res'" unless
 
99
            $strout eq $res;
 
100
        is( $diag, 'ok');
90
101
    }
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)";
99
110
            }
100
 
            ok( $diag, '');
 
111
            is( $diag, '');
101
112
            $diag = '';
102
113
            defined $str or $str = '';
103
114
            if ( $spec =~ /num/ and $str =~ /[9Z]/ and $init =~ /[9Z]/ ) {
108
119
                $diag = ( $initloc != $resloc);
109
120
            }
110
121
            $diag = "$spec-aligner with '$init' justifies '$str' to '$res' (pos)" if $diag;
111
 
            ok( $diag, '');
 
122
            is( $diag, '');
112
123
        }
113
124
        $ali->_forget( $init);
114
125
    }
115
126
}
116
127
 
117
128
# align() function
118
 
BEGIN { $n_tests += 15 }
 
129
BEGIN { $n_tests += 21 }
119
130
use Text::Aligner qw( align);
120
131
ok( defined &align);
121
132
 
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 ');
127
 
ok( $res[ 1], 'a    ');
128
 
ok( $res[ 2], 'test!');
 
137
is( $res[ 0], 'just ');
 
138
is( $res[ 1], 'a    ');
 
139
is( $res[ 2], 'test!');
129
140
 
130
141
# scalar context
131
142
my $res = align( 'right', @col);
132
 
ok( $res, " just\n    a\ntest!\n");
 
143
is( $res, " just\n    a\ntest!\n");
133
144
 
134
145
# original unchanged?
135
 
ok( join( '|', @col), join( '|', @save_col));
 
146
is( join( '|', @col), join( '|', @save_col));
136
147
 
137
148
# in-place alignment
138
149
align( '', @col);
139
 
ok( $col[ 0], 'just ');
140
 
ok( $col[ 1], 'a    ');
141
 
ok( $col[ 2], 'test!');
 
150
is( $col[ 0], 'just ');
 
151
is( $col[ 1], 'a    ');
 
152
is( $col[ 2], 'test!');
142
153
 
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 ');
148
 
ok( $col[ 1], 'a    ');
149
 
ok( $col[ 2], 'test!');
150
 
ok( $scalar,  'now  ');
 
158
is( $col[ 0], 'just ');
 
159
is( $col[ 1], 'a    ');
 
160
is( $col[ 2], 'test!');
 
161
is( $scalar,  'now  ');
 
162
 
 
163
# color support
 
164
SKIP: {
 
165
    my $ver = $Term::ANSIColor::VERSION;
 
166
    skip(
 
167
        "Term::ANSIColor $ver doesn't support colorstrip",
 
168
        6,
 
169
    ) unless HAVE_COLORSTRIP;
 
170
    *colorstrip = \ &Term::ANSIColor::colorstrip;
 
171
 
 
172
    my @col = (
 
173
        'Just',
 
174
        colored('a', 'green'),
 
175
        colored('test!', 'bold'),
 
176
        colored(123.456, 'red'),
 
177
        colored( 12, 'red'),
 
178
    );
 
179
    my @res = align( 'auto', @col);
 
180
    my @ref = align( 'auto', map colorstrip($_), @col);
 
181
    my @check = map colorstrip($_), @res;
 
182
 
 
183
    is($check[$_], $ref[$_], "color support $_") for 0 .. $#col;
 
184
    ok(Text::Aligner::_is_number(colored('123', 'red')));
 
185
}
151
186
 
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/ );
155
190
 
156
191
BEGIN { plan tests => $n_tests }