File: YAMLFrontMatter.pm

package info (click to toggle)
libcode-tidyall-plugin-yamlfrontmatter-perl 1.000001-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 268 kB
  • sloc: perl: 352; makefile: 2
file content (127 lines) | stat: -rw-r--r-- 3,412 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
122
123
124
125
126
127
package Test::Code::TidyAll::Plugin::YAMLFrontMatter;

use strict;
use warnings;

use Test::Class::Most;

use Code::TidyAll;

use Capture::Tiny qw( capture );
use Path::Tiny qw( tempdir );

sub tidyall {
    my $self = shift;
    my %args = @_;

    my $tidyall = Code::TidyAll->new(
        quiet    => 1,
        root_dir => tempdir(
            { realpath => 1 },
            TEMPLATE => 'Code-TidyAll-XXXX',
            CLEANUP  => 1,
        ),
        plugins => {
            YAMLFrontMatter => {
                select => '*',    # everything, since we're testing
                %{ $args{conf} || {} },
            },
        },
    );

    my $result;
    capture {
        $result = $tidyall->process_source( $args{source}, 'dummy.md' );
    };

    my $expect_error = $args{expect_error};
    unless ($expect_error) {
        is( $result->state, 'checked', "state=checked [$args{desc}]" );
        is( $result->error, undef,     "no error [$args{desc}]" );
        return;
    }

    is( $result->state, 'error', "state=error [$args{desc}]" );
    like(
        $result->error || q{}, $expect_error,
        "error message [$args{desc}]"
    );
    return;
}

sub test_main : Tests {
    my $self = shift;

    $self->tidyall(
        source => "---\n---\nThis should work",
        desc   => 'Empty is okay'
    );

    $self->tidyall(
        source => "---\none: line\n---\nThis should work",
        desc   => 'one line is okay'
    );

    $self->tidyall(
        source => "---\none: line\ntwo: lines\n---\nThis should work",
        desc   => 'two lines are okay'
    );

    $self->tidyall(
        source => 'fish',
        expect_error =>
            qr/dummy[.]md' does not start with valid YAML Front Matter/,
        desc => 'no front matter',
    );

    $self->tidyall(
        source       => qq{---\n!!!!!\n---\nThis should not work},
        expect_error => qr/Problem parsing YAML/,
        desc         => 'Bad YAML',
    );

    ## encoding handling

    $self->tidyall(
        source => "\x{EF}\x{BB}\x{BF}---\n---\nSomeone set us up the BOM",
        expect_error => qr/Starting document with UTF-8 BOM is not allowed/,
        desc         => 'BOM',
    );

    $self->tidyall(
        source =>
            "\x{fe}\x{ff}\x{00}-\x{00}-\x{00}-\x{00}\n\x{00}-\x{00}-\x{00}-\x{00}\n\x{00}!",
        conf => { encoding => 'UTF-16' },
        desc => 'UTF-16',
    );

    $self->tidyall(
        source       => "---\n---\nL\x{e9}on hates invalid UTF-8",
        expect_error => qr/File does not match encoding 'UTF-8'/,
        desc         => 'Invalid encoding',
    );

    ## required keys

    $self->tidyall(
        source       => "---\n---\nA rose by any other name",
        desc         => 'Missing one required key',
        conf         => { required_top_level_keys => 'title' },
        expect_error => qr/Missing required YAML Front Matter key: 'title'/,
    );

    $self->tidyall(
        source => "---\ntitle: B Piper\n---\nA rose by any other name",
        desc   => 'No missing key',
        conf   => { required_top_level_keys => 'title' },
    );

    $self->tidyall(
        source       => "---\ntitle: B Piper\n---\nA rose by any other name",
        desc         => 'Missing second key',
        conf         => { required_top_level_keys => 'title wibble' },
        expect_error => qr/Missing required YAML Front Matter key: 'wibble'/,
    );
}

1;