File Coverage

lib/Dist/Zilla/Util/ExpandINI/Writer.pm
Criterion Covered Total %
statement 56 58 96.5
branch 9 14 64.2
condition 5 9 55.5
subroutine 10 10 100.0
pod 3 4 75.0
total 83 95 87.3


line stmt bran cond sub pod time code
1 10     10   1163 use 5.006;
  10         37  
2 10     10   56 use strict;
  10         20  
  10         248  
3 10     10   54 use warnings;
  10         26  
  10         764  
4              
5             package Dist::Zilla::Util::ExpandINI::Writer;
6              
7             our $VERSION = '0.003002';
8              
9             # ABSTRACT: An order-preserving INI Writer
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 10     10   10664 use Config::INI::Writer 0.024;
  10         77637  
  10         364  
14 10     10   959 use parent 'Config::INI::Writer';
  10         349  
  10         131  
15 10     10   750 use Carp qw(croak);
  10         24  
  10         6569  
16              
17              
18              
19              
20              
21             sub is_valid_section_name {
22 145     145 0 225 my ( undef, $name ) = @_;
23 145         2298 return $name !~ m{
24             (?: # Dont capture
25             \n # Newlines may not occur in a section name
26             |
27             \s; # Comments may not occur in a section name
28             |
29             ^\s # Leading whitespace is illegal in a section name
30             |
31             \s$ # Trailing whitespace is illegal in a section name
32             )
33             }msx;
34             }
35              
36             sub preprocess_input {
37 9     9 1 39385 my ( undef, $input_data ) = @_;
38 9         24 my @out;
39 9         19 my $i = 0;
40 9         18 for my $ini_record ( @{$input_data} ) {
  9         34  
41 145         178 $i++;
42 145 100 66     868 if ( $ini_record->{name} and '_' eq $ini_record->{name} ) {
43 9         24 push @out, '_', $ini_record;
44 9         27 next;
45             }
46 136 50       326 if ( not $ini_record->{package} ) {
47 0         0 croak("Entry $i lacks package component");
48             }
49 136         221 my $kn = $ini_record->{package};
50 136 100 66     618 if ( $ini_record->{name} and $ini_record->{package} ne $ini_record->{name} ) {
51 135         330 $kn .= q[ / ] . $ini_record->{name};
52             }
53 136         245 push @out, $kn, $ini_record;
54 136         224 next;
55             }
56 9         38 return \@out;
57             }
58              
59             sub validate_input {
60 9     9 1 45 my ( $self, $input ) = @_;
61              
62 9         23 my %seen;
63              
64 9         20 my @input_copy = @{$input};
  9         70  
65              
66 9         38 while (@input_copy) {
67 145         516 my ( $name, $ini_record ) = splice @input_copy, 0, 2;
68              
69 145 50       553 if ( $seen{$name}++ ) {
70 0         0 Carp::croak "multiple declarations found of $name";
71             }
72              
73 145 50       302 Carp::croak "illegal section name '$name'"
74             if not $self->is_valid_section_name($name);
75              
76 145         227 my @props_copy = @{ $ini_record->{lines} };
  145         358  
77              
78 145         501 while (@props_copy) {
79 54         422 my ( $property, $value ) = splice @props_copy, 0, 2;
80              
81 54 50       190 Carp::croak "property name '$property' contains illegal character"
82             if not $self->is_valid_property_name($property);
83              
84 54 50 33     781 Carp::croak "value for $name.$property contains illegal character"
85             if defined $value and not $self->is_valid_value($value);
86              
87             }
88             }
89 9         164 return;
90             }
91              
92             sub stringify_section_data {
93 145     145 1 6558 my ( $self, $ini_record ) = @_;
94 145         203 my $output = q[];
95 145         207 $output .= q[;] . $_ . qq[\n] for @{ $ini_record->{comment_lines} };
  145         429  
96 145         450 $output .= $self->SUPER::stringify_section_data( $ini_record->{lines} );
97 145         1918 return $output;
98             }
99             1;
100              
101             __END__
102              
103             =pod
104              
105             =encoding UTF-8
106              
107             =head1 NAME
108              
109             Dist::Zilla::Util::ExpandINI::Writer - An order-preserving INI Writer
110              
111             =head1 VERSION
112              
113             version 0.003002
114              
115             =for Pod::Coverage is_valid_section_name
116              
117             =head1 AUTHOR
118              
119             Kent Fredric <kentnl@cpan.org>
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             This software is copyright (c) 2015 by Kent Fredric <kentfredric@gmail.com>.
124              
125             This is free software; you can redistribute it and/or modify it under
126             the same terms as the Perl 5 programming language system itself.
127              
128             =cut