/
01_highlighting.t
149 lines (122 loc) · 4.01 KB
/
01_highlighting.t
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
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
use File::Find;
use File::Spec::Functions qw<catfile catdir>;
use Test::More;
use Test::Differences;
use Text::VimColor 0.25;
my %LANG_HIGHLIGHTERS = (
perl => [
construct_highlighter('perl', ['+let perl_include_pod=1']),
construct_highlighter('perl', [
'+let perl_include_pod=1',
'+let perl_fold=1',
]),
construct_highlighter('perl', [
'+let perl_include_pod=1',
'+let perl_fold=1',
'+let perl_fold_anonymous_subs=1',
]),
],
perl6 => [
construct_highlighter('perl6', []),
],
);
sub construct_highlighter {
my ( $lang, $option_set ) = @_;
my $syntax_file = catfile('syntax', "$lang.vim");
my $ftplugin_file = catfile('ftplugin', "$lang.vim");
my $css_file = catfile('t', 'vim_syntax.css');
my $css_url = join('/', '..', '..', 't', 'vim_syntax.css');
return Text::VimColor->new(
html_full_page => 1,
html_inline_stylesheet => 0,
html_stylesheet_url => $css_url,
all_syntax_groups => 1,
extra_vim_options => [
@$option_set,
'+set runtimepath=.',
"+source $ftplugin_file",
"+source $syntax_file",
"+syn sync fromstart",
],
);
}
sub extract_custom_options {
my ( $filename ) = @_;
my @options;
open my $fh, '<', $filename or return;
while(<$fh>) {
chomp;
if(/^\s*#\s*extra-option:\s*(.*)/) {
push @options, "+$1";
}
}
close $fh;
return @options;
}
sub create_custom_highlighter {
my ( $orig, @options ) = @_;
unshift @options, grep { /^\+let/ } $orig->vim_options;
my ($syntax) = grep { /^\+source syntax/ } $orig->vim_options;
my ($lang) = $syntax =~ /(\w+)\.vim/;
return construct_highlighter($lang, \@options);
}
sub get_language_for_file {
my ( $filename ) = @_;
return $filename =~ /perl6/ ? 'perl6' : 'perl';
}
sub test_source_file {
my ( $file, $highlighters ) = @_;
foreach my $hilite (@$highlighters) {
my @custom_options = extract_custom_options($file);
my $output;
if(@custom_options) {
my $custom_hilite = create_custom_highlighter($hilite, @custom_options);
$custom_hilite->syntax_mark_file($file);
$output = $custom_hilite->html();
} else {
$hilite->syntax_mark_file($file);
$output = $hilite->html();
}
my $html_file = $file;
$html_file .= '.html';
SKIP: {
# remove old failure output if present
my $fail = "${file}_fail.html";
unlink $fail;
# create the corresponding html file if it's missing
if (!-e $html_file) {
open my $markup, '>', $html_file or die "Can't open $html_file: $!\n";
print {$markup} $output;
close $markup;
skip("Created $html_file", 1);
}
open my $handle, '<', $html_file or die "Can't open $html_file: $!\n";
my $expected = do { local $/; scalar <$handle> };
eq_or_diff($output, $expected, "Correct output for $file");
# if the HTML is incorrect, write it out to a file for
# the user to inspect
if ($output ne $expected) {
open my $fh, '>', $fail or die "Can't open $fail: $!\n";
print $fh $output;
diag("You can inspect the incorrect output at $fail");
}
}
}
}
my @test_files;
if(@ARGV) {
@test_files = @ARGV;
} else {
find(sub {
return if !/\.(?:pl|pm|pod|t)$/;
push @test_files, $File::Find::name;
}, 't_source/perl', 't_source/perl6');
}
plan tests => scalar(map { @{ $LANG_HIGHLIGHTERS{get_language_for_file($_)} } } @test_files);
foreach my $test_file (@test_files) {
test_source_file($test_file, $LANG_HIGHLIGHTERS{ get_language_for_file($test_file) });
}