2 # Copyright (C) 2006-2009, Parrot Foundation.
8 use lib qw( . lib ../lib ../../lib );
9 use Test::More tests => 1;
10 use Parrot::Distribution;
15 t/codingstd/c_operator.t - checks spacing around operators in C source
20 % prove t/codingstd/c_operator.t
23 % perl t/codingstd/c_operator.t src/foo.c include/parrot/bar.h
27 Checks that all C language source files have the proper use of spacing
32 Paul Cochrane <paultcochrane at gmail dot com>
36 L<docs/pdds/pdd07_codingstd.pod>
40 my $DIST = Parrot::Distribution->new;
41 my @files = @ARGV ? <@ARGV> : $DIST->get_c_language_files();
42 check_operators(@files);
48 my $parser = Pod::Simple->new();
50 $parser->output_string( \$non_pod_buf );
51 # set up a code handler to get at the non-pod
52 # thanks to Thomas Klausner's Pod::Strip for the inspiration
53 $parser->code_handler(
55 print {$_[2]{output_fh}} $_[0], "\n";
57 $parser->parse_string_document( $buf );
65 foreach my $file (@_) {
66 my $path = @ARGV ? $file : $file->path();
69 next if $path =~ m/\.l$/;
71 my $buf = $DIST->slurp($path);
73 # only strip pod from .ops files
74 if ( $path =~ m/\.ops$/ ) {
75 $buf = strip_pod($buf);
78 # strip ', ", and C comments #'
80 (?: (') (?: \\\\ | \\' | [^'] )* (') ) # rm ' string #'
81 | (?: (") (?: \\\\ | \\" | [^"] )* (") ) # rm " string #"
82 | /(\*) .*? (\*)/ # rm C comment
84 }{defined $1 ? "$1$2" : defined $3 ? "$3$4" : "$5$6"}egsx;
86 my @lines = split( /\n/, $buf );
87 $comma_space{$path} = [];
88 for (my $i=0; $i <= $#lines; $i++) {
89 # after a comma there should be one space or a newline
90 if ( $lines[$i] =~ m{ ( (?:,) (?! \s ) (?= .+) ) }gx ) {
91 push @{ $comma_space{$path} }, $lines[$i];
96 ## L<PDD07/Code Formatting"there should be one space or a newline after a comma">/
97 my @comma_space_files;
98 for my $path ( sort keys %comma_space ) {
99 if (my $cnt = scalar @{ $comma_space{$path} }) {
100 push @comma_space_files, <<"END_ERROR";
101 $path [$cnt line@{[ ($cnt >1) ? 's': '' ]}] at :
102 @{[ join("\n--\n", @{$comma_space{$path}}) ]}
106 is(join("\n",@comma_space_files),
108 "there should be one space or a newline after a comma");
113 # cperl-indent-level: 4
116 # vim: expandtab shiftwidth=4: