113-check_cond_preproc_statements.t revision 1.1.1.1
1#!/usr/bin/perl
2use strict;
3use warnings;
4use Carp;
5use Cwd;
6use File::Spec;
7use File::Temp qw( tempdir );
8use Test::More tests => 13;
9use lib qw( lib t/lib );
10use ExtUtils::ParseXS;
11use ExtUtils::ParseXS::Utilities qw(
12    check_conditional_preprocessor_statements
13);
14use PrimitiveCapture;
15
16my $self = bless({} => 'ExtUtils::ParseXS');
17$self->{line} = [];
18$self->{XSStack} = [];
19$self->{XSStack}->[0] = {};
20
21{
22    $self->{line} = [
23        "#if this_is_an_if_statement",
24        "Alpha this is not an if/elif/elsif/endif",
25        "#elif this_is_an_elif_statement",
26        "Beta this is not an if/elif/elsif/endif",
27        "#else this_is_an_else_statement",
28        "Gamma this is not an if/elif/elsif/endif",
29        "#endif this_is_an_endif_statement",
30    ];
31    $self->{line_no} = [ 17 .. 23 ];
32    $self->{XSStack}->[-1]{type} = 'if';
33    $self->{filename} = 'myfile1';
34
35    my $rv;
36    my $stderr = PrimitiveCapture::capture_stderr(sub {
37        $rv = check_conditional_preprocessor_statements($self);
38    });
39        
40    is( $rv, 0, "Basic case: returned 0: all ifs resolved" );
41    ok( ! $stderr, "No warnings captured, as expected" );
42}
43
44{
45    $self->{line} = [
46        "#if this_is_an_if_statement",
47        "Alpha this is not an if/elif/elsif/endif",
48        "#if this_is_a_different_if_statement",
49        "Beta this is not an if/elif/elsif/endif",
50        "#endif this_is_a_different_endif_statement",
51        "Gamma this is not an if/elif/elsif/endif",
52        "#endif this_is_an_endif_statement",
53    ];
54    $self->{line_no} = [ 17 .. 23 ];
55    $self->{XSStack}->[-1]{type} = 'if';
56    $self->{filename} = 'myfile1';
57
58    my $rv;
59    my $stderr = PrimitiveCapture::capture_stderr(sub {
60        $rv = check_conditional_preprocessor_statements($self);
61    });
62    is( $rv, 0, "One nested if case: returned 0: all ifs resolved" );
63    ok( ! $stderr, "No warnings captured, as expected" );
64}
65
66{
67    $self->{line} = [
68        "Alpha this is not an if/elif/elsif/endif",
69        "#elif this_is_an_elif_statement",
70        "Beta this is not an if/elif/elsif/endif",
71        "#else this_is_an_else_statement",
72        "Gamma this is not an if/elif/elsif/endif",
73        "#endif this_is_an_endif_statement",
74    ];
75    $self->{line_no} = [ 17 .. 22 ];
76    $self->{XSStack}->[-1]{type} = 'if';
77    $self->{filename} = 'myfile1';
78
79    my $rv;
80    my $stderr = PrimitiveCapture::capture_stderr(sub {
81        $rv = check_conditional_preprocessor_statements($self);
82    });
83    is( $rv, undef,
84        "Missing 'if' case: returned undef: all ifs resolved" );
85    like( $stderr,
86        qr/Warning: #else\/elif\/endif without #if in this function/,
87        "Got expected warning: lack of #if"
88    );
89    like( $stderr,
90        qr/precede it with a blank line/s,
91        "Got expected warning: advice re blank line"
92    );
93}
94
95{
96    $self->{line} = [
97        "Alpha this is not an if/elif/elsif/endif",
98        "#elif this_is_an_elif_statement",
99        "Beta this is not an if/elif/elsif/endif",
100        "#else this_is_an_else_statement",
101        "Gamma this is not an if/elif/elsif/endif",
102        "#endif this_is_an_endif_statement",
103    ];
104    $self->{line_no} = [ 17 .. 22 ];
105    $self->{XSStack}->[-1]{type} = 'file';
106    $self->{filename} = 'myfile1';
107
108    my $rv;
109    my $stderr = PrimitiveCapture::capture_stderr(sub {
110        $rv = check_conditional_preprocessor_statements($self);
111    });
112    is( $rv, undef,
113        "Missing 'if' case: returned undef: all ifs resolved" );
114    like( $stderr,
115        qr/Warning: #else\/elif\/endif without #if in this function/,
116        "Got expected warning: lack of #if"
117    );
118    unlike( $stderr,
119        qr/precede it with a blank line/s,
120        "Did not get unexpected stderr"
121    );
122}
123
124{
125    $self->{line} = [
126        "#if this_is_an_if_statement",
127        "Alpha this is not an if/elif/elsif/endif",
128        "#elif this_is_an_elif_statement",
129        "Beta this is not an if/elif/elsif/endif",
130        "#else this_is_an_else_statement",
131        "Gamma this is not an if/elif/elsif/endif",
132    ];
133    $self->{line_no} = [ 17 .. 22 ];
134    $self->{XSStack}->[-1]{type} = 'if';
135    $self->{filename} = 'myfile1';
136
137    my $rv;
138    my $stderr = PrimitiveCapture::capture_stderr(sub {
139        $rv = check_conditional_preprocessor_statements($self);
140    });
141    isnt( $rv, 0,
142        "Missing 'endif' case: returned non-zero as expected" );
143    like( $stderr,
144        qr/Warning: #if without #endif in this function/s,
145        "Got expected warning: lack of #endif"
146    );
147}
148
149pass("Passed all tests in $0");
150