File: basic.t

package info (click to toggle)
libstring-truncate-perl 1.100603-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: perl: 357; makefile: 2
file content (178 lines) | stat: -rw-r--r-- 3,759 bytes parent folder | download | duplicates (5)
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
167
168
169
170
171
172
173
174
175
176
177
178
use strict;
use warnings;

use Test::More 0.88;

BEGIN { use_ok('String::Truncate', qw(elide trunc)); }

my $brain = "this is your brain";
# len: 18 -- 123456789012345678

my %code = (
  elide   => \&elide,
  trunc   => \&trunc,
  'S:T:e' => String::Truncate->can('elide'),
  'S:T:t' => String::Truncate->can('trunc'),
);

while (my ($name, $code) = each %code) {
  is(
    $code->($brain, 50),
    "this is your brain",
    "don't $name short strings",
  );

  is(
    $code->($brain, 18),
    $brain,
    "don't $name exact-length strings",
  );
}

is(
  elide($brain, 16),
  "this is your ...",
  "right-side elide example",
);

is(
  trunc($brain, 16),
  "this is your bra",
  "right-side trunc example",
);

is(
  elide($brain, 16, { truncate => 'left' }),
  "...is your brain",
  "left-side elide example",
);

is(
  trunc($brain, 16, { truncate => 'left' }),
  "is is your brain",
  "left-side trunc example",
);

is(
  elide($brain, 16, { truncate => 'middle' }),
  "this is... brain",
  "middle-side elide example",
);

is(
  trunc($brain, 16, { truncate => 'middle' }),
  "this is ur brain",
  "middle-side trunc example",
);

is(
  elide($brain, 16, { truncate => 'middle', marker => '..' }),
  "this is..r brain",
  "middle-side elide example with short marker",
);

is(
  elide($brain, 15, { truncate => 'middle', marker => '..' }),
  "this is.. brain",
  "middle-side example with short marker and side-length mismatch",
);

eval { elide($brain, 2) };
like($@, qr/longer/, "can't truncate to less than marker");

eval { elide($brain, 20, { truncate => 'backside' }) };
like($@, qr/invalid/, "only left|right|middle are valid");

eval { trunc($brain, 16, { truncate => 'middle', marker => '..' }); };
like($@, qr/marker may not be passed/, "can't pass marker to trunc");

is(
  elide($brain, 10, { marker => ' &c.' }),
  "this i &c.",
  "custom marker",
);

is(
  elide("foobar", 6),
  "foobar",
  "we don't elide anything if length == maxlength",
);

is(
  elide("!!", 2),
  "!!",
  "we don't care about marker length if marker isn't needed",
);

is(
  elide("foobar", 5),
  "fo...",
  "keep-left elision of a very short string",
);

is(
  #      12345678901234567890123456789012
  trunc("This should break between words.", 14, { at_space => 1 }),
  "This should",
  "at_space lets us break betwen words (at right)",
);

{
  my $s = 'a ' x 20 . "\n\n" . 'b ' x 20;

  like(
    elide($s, 50, { at_space => 1 }),
    qr/b/,
    "newlines don't break at_space",
  );
}

is(
  #      12345678901234567890123456789012
  trunc("This should break between words.", 14, { at_space => 1 }),
  "This should",
  "at_space lets us break betwen words (at right)",
);

is(
  #      21098765432109876543210987654321
  trunc("This should break between words.", 14,
    { truncate => 'left', at_space => 1 }
  ),
  "between words.",
  "at_space lets us break betwen words (at left)",
);

is(
  #      12345678901234567890123456789012
  elide("This should break between words.", 14, { at_space => 1 }),
  "This should...",
  "at_space lets us break betwen words (elide, at right)",
);

is(
  #      21098765432109876543210987654321
  elide("This should break between words.", 14,
    { truncate => 'left', at_space => 1 }
  ),
  "...words.",
  "at_space lets us break betwen words (elide, at left)",
);

is(
  #      12345678901234567890123456789012
  elide("This should break between words.", 20,
    { truncate => 'middle', at_space => 1 }
  ),
  "This...words.",
  "at_space lets us break betwen words (elide, at middle)",
);

is(
  #      123456789012345678901234567890123
  elide("Thisisonereallylongstringnospace.", 20, { at_space => 1 }),
  "Thisisonereallylo...",
  "if it can't break at a word boundary, it breaks as late as possible",
);

done_testing;