File: 55-wrapping.t

package info (click to toggle)
libpostscript-file-perl 2.20%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 808 kB
  • ctags: 274
  • sloc: perl: 7,065; sh: 48; makefile: 2
file content (275 lines) | stat: -rw-r--r-- 6,392 bytes parent folder | download | duplicates (4)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#! /usr/bin/perl
#---------------------------------------------------------------------
# Copyright 2011 Christopher J. Madsen
#
# Author: Christopher J. Madsen <perl@cjmweb.net>
# Created: 6 Oct 2011
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either the
# GNU General Public License or the Artistic License for more details.
#
# Test word wrapping in PostScript::File::Metrics
#---------------------------------------------------------------------

use 5.008;
use strict;
use warnings;

use Test::More 0.88;            # want done_testing
use List::Util qw(sum);

use PostScript::File::Metrics ();

#=====================================================================
sub iName     () { 0 }
sub iText     () { 1 }
sub iExpected () { 2 }
sub iWarnings () { 3 }

my @tests = (
  [
    "basic",
    "Basic test",
    [
      "Basic test"
    ]
  ],
  [
    "long",
    "This is a long text to be wrapped on four lines",
    [
      "This is a long",
      "text to be",
      "wrapped on",
      "four lines"
    ]
  ],
  [
    "long hyphens",
    "This is-a long-text to be-wrapped with-hyphens",
    [
      "This is\x{ad}a long\x{ad}",
      "text to be\x{ad}",
      "wrapped with\x{ad}",
      "hyphens"
    ]
  ],
  [
    "long dashes",
    "This is\x{2013}a long\x{2014}text to be\x{2013}wrapped with\x{2014}dashes",
    [
      "This is\x{2013}a long\x{2014}",
      "text to be\x{2013}",
      "wrapped with\x{2014}",
      "dashes"
    ]
  ],
  [
    "long slashes",
    "This is/a long/text to be/wrapped with/slashes",
    [
      "This is/a long/",
      "text to be/",
      "wrapped with/",
      "slashes"
    ]
  ],
  [
    "newlines",
    "This is\na long text\nwith\nnewlines to mark most of the places\n\nit should be wrapped.",
    [
      "This is",
      "a long text",
      "with",
      "newlines to",
      "mark most of",
      "the places",
      "",
      "it should be",
      "wrapped."
    ]
  ],
  [
    "non-breaking spaces",
    "This is\240a long\240text to be\240wrapped with non-breaking spaces",
    [
      "This is\x{a0}a",
      "long\x{a0}text to",
      "be\x{a0}wrapped",
      "with non\x{ad}",
      "breaking",
      "spaces"
    ]
  ],
  [
    "negative numbers",
    "This is -123456789 -987654321 -12\x{2212} -34\x{2212} -56\x{2212} -78\x{2212} -90\x{2212} -1234567890",
    [
      "This is",
      "\x{2212}123456789",
      "\x{2212}987654321",
      "\x{2212}12\x{2212} \x{2212}34\x{2212}",
      "\x{2212}56\x{2212} \x{2212}78\x{2212}",
      "\x{2212}90\x{2212}",
      "\x{2212}1234567890"
    ]
  ],
  [
    "zero-width space",
    "This\x{200b}is\x{200b}a\x{200b}long\x{200b}text\x{200b}containing\x{200b}only\x{200b}zero\x{200b}width\x{200b}spaces",
    [
      "Thisisalongtext",
      "containingonly",
      "zerowidth",
      "spaces"
    ]
  ],
  [
    "maxlines 1",
    [
      "This is a long text to be wrapped on only one line",
      {
        maxlines => 1,
        quiet => 1
      }
    ],
    [
      "This is a long text to be wrapped on only one line"
    ]
  ],
  [
    "maxlines 3",
    [
      "This is a long text to be wrapped on only three lines",
      {
        maxlines => 3,
        quiet => 1
      }
    ],
    [
      "This is a long",
      "text to be",
      "wrapped on only three lines"
    ]
  ],
  [
    "maxlines 3 not quiet",
    [
      "This is a long text to be wrapped on only three lines",
      {
        maxlines => 3
      }
    ],
    [
      "This is a long",
      "text to be",
      "wrapped on only three lines"
    ],
    [
      "'wrapped on only three lines' is too wide (122.28) for field width 72"
    ]
  ],
  [
    "ends with newline",
    "newline after\n",
    [
      "newline after"
    ]
  ],
  [
    "only at whitespace",
    [
      "This is-a long-text to be-wrapped with-hyphens",
      {
        chars => ""
      }
    ],
    [
      "This is\x{ad}a",
      "long\x{ad}text to",
      "be\x{ad}wrapped",
      "with\x{ad}hyphens"
    ]
  ],
  [
    "word too long",
    "ThisWordIsTooLongToWrapAnywhere so it warns.",
    [
      "ThisWordIsTooLongToWrapAnywhere",
      "so it warns."
    ],
    [
      "ThisWordIsTooLongToWrapAnywhere is too wide (170.05) for field width 72"
    ]
  ],
); # end @tests

#---------------------------------------------------------------------
# Capture warning messages:

my @warnings;

$SIG{__WARN__} = sub { push @warnings, $_[0] };

#---------------------------------------------------------------------
my $generateResults;

if (@ARGV and $ARGV[0] eq 'gen') {
  # Just output the actual results, so they can be diffed against this file
  $generateResults = 1;
} else {
  my $warnings = sum(map { $_ ? scalar @$_ : 0 } map { $_->[iWarnings] } @tests);
  plan tests => 2 * @tests + $warnings;
}

my $metrics = PostScript::File::Metrics->new(qw(Helvetica 10 cp1252));

for my $test (@tests) {
  @warnings = ();
  my @got = (ref $test->[iText]
             ? $metrics->wrap(72, @{ $test->[iText] })
             : $metrics->wrap(72,    $test->[iText] ));

  if ($generateResults) {
    $test->[iExpected] = \@got;
    if (@warnings) {
      s/ at \S*55-wrapping\.t.*//s or die for @warnings;
      $test->[iWarnings] = [ @warnings ];
    } else {
      pop @$test if $test->[iWarnings];
    }
  } else {
    is_deeply(\@got, @$test[iExpected, iName]);
    if (my $w = $test->[iWarnings]) {
      is(scalar @warnings, scalar @$w, "$test->[iName] warnings");
      for my $i (0 .. $#$w) {
        like($warnings[$i], qr/^\Q$w->[$i] at /, "$test->[iName] warning $i");
      }
    } else {
      is(scalar @warnings, 0, "$test->[iName] no warnings");
    }
  }
} # end for each $test in @tests

#---------------------------------------------------------------------
if ($generateResults) {
  require Data::Dumper;

  my $d = Data::Dumper->new([ \@tests ], ['*tests'])
            ->Indent(1)->Useqq(1)->Quotekeys(0)->Sortkeys(1)->Dump;
  $d =~ s/\]\n\);\n\z/],\n); # end \@tests\n/;

  open(my $out, '>:utf8', '/tmp/55-wrapping.t') or die $!;
  print $out "my $d";
} else {
  done_testing;
}

# Local Variables:
# compile-command: "perl 55-wrapping.t gen"
# End: