Debian Codegraph @

dist/ExtUtils-ParseXS/t/113-check_cond_preproc_statements.t

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