File: pod2markdown.t

package info (click to toggle)
libpod-markdown-perl 3.400000-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 456 kB
  • sloc: perl: 1,010; makefile: 2
file content (121 lines) | stat: -rw-r--r-- 2,665 bytes parent folder | download | duplicates (3)
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
# vim: set ts=2 sts=2 sw=2 expandtab smarttab:
use strict;
use warnings;
use lib 't/lib';
use MarkdownTests;

use File::Temp qw{ tempfile }; # core
use File::Spec::Functions qw( catfile ); # core

my ($lib, $bin) = scalar(grep { /\bblib\Wlib$/ } @INC)
  ? ('blib/lib', 'blib/script')
  : ('lib',      'bin');

$bin = '/usr/bin' if $ENV{AUTOPKGTEST_TMP};

my $script = catfile($bin, qw(pod2markdown));

my ($tmp_in,   $infile) = tempfile( 'pod2markdown-in.XXXXXX',  TMPDIR => 1, UNLINK => 1 );
print $tmp_in "=head1 Temp\n\nI<File>\n";
close $tmp_in;

my ($tmp_out, $outfile) = tempfile( 'pod2markdown-out.XXXXXX', TMPDIR => 1, UNLINK => 1 );
print $tmp_out "overwrite me\n";
close $tmp_out;

sub corpus {
  catfile( corpus => $_[0] );
}

# I tried this with IPC::Open2, but windows hangs waiting for more <STDIN>...

sub pod2markdown {
  my ($args, $exp, $desc) = @_;
  local $Test::Builder::Level = $Test::Builder::Level + 1;
  unshift @$args, $^X, "-I$lib", $script;
  {
    open(my $fh, '>', $outfile) or die "Failed to open $outfile: $!";
    print $fh "oops\n";
    close $fh;
  }
  is slurp_file($outfile), "oops\n", 'output file prepared';
  system(join ' ', map { length($_) > 1 ? qq["$_"] : $_ } @$args);
  is slurp_file($outfile), $exp, $desc;
}

{
  sub testp2m {
    splice @_, 1, 0, "# Temp\n\n_File_\n";
    goto &pod2markdown;
  }

  testp2m(
    ['<', $infile, '>', $outfile],
    'no args: < in > out',
  );

  testp2m(
    [$infile, '>', $outfile],
    '1 arg: input file, stdout',
  );

  testp2m(
    [$infile, $outfile],
    '2 args: input file, output file',
  );

  testp2m(
    ['-', $outfile, '<', $infile],
    '2 args: - (stdin), output file',
  );

  testp2m(
    ['-', '-', '<', $infile, '>', $outfile],
    'both dashes: - (stdin) - (stdout)',
  );

}

{
  my $in = corpus('copy.pod');
  my @args = ($in, $outfile);
  my $exp = sub { sprintf "# cr\n\n{ \\`%s\\` }\n", $_[0] };

  pod2markdown(
    [@args],
    $exp->("\xc2\xa9"),
    'no encoding specified returns UTF-8',
  );

  pod2markdown(
    ['--html-encode-chars=1', @args],
    $exp->("&copy;"),
    'html_encode_chars=1 encodes entities',
  );

  pod2markdown(
    ['-e', 'ascii', @args],
    $exp->("&copy;"),
    'ascii encoding returns ascii with html entities encoded',
  );

  pod2markdown(
    ['--output-encoding=utf-8', @args],
    $exp->("\xc2\xa9"),
    'specify utf-8 output encoding',
  );

  pod2markdown(
    ['--match-encoding', corpus('lit-cp1252-enc.pod'), $outfile],
    $exp->("\xa9"),
    'match input cp1252',
  );

  pod2markdown(
    ['-m', corpus('lit-utf8-enc.pod'), $outfile],
    $exp->("\xc2\xa9"),
    'match input utf-8',
  );
}

done_testing;