File: generate-section.t

package info (click to toggle)
libpod-weaver-perl 4.020-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 496 kB
  • sloc: perl: 1,428; makefile: 2
file content (184 lines) | stat: -rw-r--r-- 3,661 bytes parent folder | download | duplicates (2)
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
#!perl
use strict;
use warnings;
use utf8;

use Test::More;

use Pod::Elemental;
use Pod::Weaver;
use Pod::Weaver::Config::Assembler;
use PPI;

sub make_weaver {
  my ($config) = @_;

  my $assembler = Pod::Weaver::Config::Assembler->new;

  my $root = $assembler->section_class->new({ name => '_' });
  $assembler->sequence->add_section($root);

  $assembler->change_section('GenerateSection');

  # XXX This is junky. -- rjbs, 2021-04-05
  for my $key (keys %$config) {
    if ($key eq 'text') {
      my @lines = split /\n/, $config->{$key};
      $assembler->add_value($key => $_) for @lines;
    } else {
      $assembler->add_value($key => $config->{$key});
    }
  }

  $assembler->end_section;

  return Pod::Weaver->new_from_config_sequence($assembler->sequence, {});
}

sub weave_ok {
  my ($section_config, $extra_input, $expect) = @_;

  my $in_pod   = q{};
  my $document = Pod::Elemental->read_string($in_pod); # wants octets

  my $ppi_document  = PPI::Document->new(\"");

  my $weaver = make_weaver($section_config);

  my $woven = $weaver->weave_document({
    pod_document => $document,
    ppi_document => $ppi_document,

    %$extra_input,
  });

  my $pod_string = $woven->as_pod_string;

  local $Test::Builder::Level = $Test::Builder::Level + 1;

  if (ref $expect) {
    return like($pod_string, $expect, "woven document is okay");
  }

  $pod_string =~ s/\A=pod\v+//;

  return is(
    $pod_string,
    $expect,
    "woven document contains expected substring",
  );
}

weave_ok(
  { title => 'Header Title' },
  {},
  "=head1 Header Title\n\n=cut\n",
);

weave_ok(
  {
    title => 'Whatever',
    text  => 'This is package {{$name}} v{{$version}}',
  },
  {
    zilla    => 1, # to trigger zilla-like operation
    distmeta => { name => "Foo", version => "1.2.3" },
  },
  "=head1 Whatever\n\nThis is package Foo v1.2.3\n\n=cut\n",
);

{
  my $template = <<'END';
Name {{$name}}
v{{$version}}
Homepage {{$homepage}}
repo web {{$repository_web}}
repo url {{$repository_url}}
bugtracker url {{$bugtracker_web}}
bugtracker email {{$bugtracker_email}}
END

  my $expect = <<'END';
=head1 Gorp

Name Foo-Bar
v0.010
Homepage https://foo-bar.xyz/
repo web https://hg.xyz.com/hgweb/pro
repo url https://there.com/dude/project
bugtracker url https://foo-bar.xyz/bugs/
bugtracker email bug-project@foo-bar-example

=cut
END

  weave_ok(
    {
      title => 'Gorp',
      text  => $template,
    },
    {
      zilla    => 1, # to trigger zilla-like operation
      distmeta => {
        name    => 'Foo-Bar',
        version => '0.010',
        resources => {
          homepage   => 'https://foo-bar.xyz/',
          repository => {
            url => 'https://there.com/dude/project',
            web => 'https://hg.xyz.com/hgweb/pro',
          },
          bugtracker => {
            web     => 'https://foo-bar.xyz/bugs/',
            mailto  => 'bug-project@foo-bar-example',
          },
        },
      },
    },
    $expect,
  );
}

weave_ok(
  {
    title => 'Whatever',
    text  => 'This is package {{$name}} v{{$version}}',
    is_template => 0,
  },
  {
    zilla    => 1, # to trigger zilla-like operation
    distmeta => { name => "Foo", version => "1.2.3" },
  },
  "=head1 Whatever\n\nThis is package {{\$name}} v{{\$version}}\n\n=cut\n",
);

weave_ok(
  {
    head  => 2,
    title => 'Whatever',
    text  => 'Xyz',
  },
  {},
  "=head2 Whatever\n\nXyz\n\n=cut\n",
);

weave_ok(
  {
    head  => 0,
    title => 'Whatever',
    text  => 'Xyz',
  },
  {},
  "Xyz\n\n=cut\n",
);

weave_ok(
  {
    title => 'Foo',
    text  => "This\n\n\nrocks.",
  },
  {},
  "=head1 Foo\n\nThis\n\n\nrocks.\n\n=cut\n",
);

done_testing;