This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move several functions to Utilities.pm
[perl5.git] / dist / ExtUtils-ParseXS / t / 113-check_cond_preproc_statements.t
index dd2e7b9..a6cbd50 100644 (file)
@@ -5,74 +5,144 @@ use Carp;
 use Cwd;
 use File::Spec;
 use File::Temp qw( tempdir );
-use Test::More qw(no_plan); # tests =>  7;
+use Capture::Tiny qw( capture );
+use Test::More tests => 13;
 use lib qw( lib );
 use ExtUtils::ParseXS::Utilities qw(
+    check_conditional_preprocessor_statements
 );
-#    check_conditional_preprocessor_statements
 
 my $self = {};
 $self->{line} = [];
 $self->{XSStack} = [];
 $self->{XSStack}->[0] = {};
-my @capture = ();
-sub capture { push @capture, $_[0] };
 
-#{
-#    $self->{line} = [
-#        "#if this_is_an_if_statement",
-#        "Alpha this is not an if/elif/elsif/endif",
-#        "#elif this_is_an_elif_statement",
-#        "Beta this is not an if/elif/elsif/endif",
-#        "#else this_is_an_else_statement",
-#        "Gamma this is not an if/elif/elsif/endif",
-#        "#endif this_is_an_endif_statement",
-#    ];
-#    $self->{XSStack}->[-1]{type} = 'if';
-#
-#    @capture = ();
-#    local $SIG{__WARN__} = \&capture;
-#    is( check_conditional_preprocessor_statements($self), 0,
-#        "basic case: returned 0: all ifs resolved" );
-#    ok( ! @capture, "No warnings captured, as expected" );
-#}
-#
-#{
-#    $self->{line} = [
-#        "#if this_is_an_if_statement",
-#        "Alpha this is not an if/elif/elsif/endif",
-#        "#if this_is_a_different_if_statement",
-#        "Beta this is not an if/elif/elsif/endif",
-#        "#endif this_is_a_different_endif_statement",
-#        "Gamma this is not an if/elif/elsif/endif",
-#        "#endif this_is_an_endif_statement",
-#    ];
-#    $self->{XSStack}->[-1]{type} = 'if';
-#
-#    @capture = ();
-#    local $SIG{__WARN__} = \&capture;
-#    is( check_conditional_preprocessor_statements($self), 0,
-#        "one nested if case: returned 0: all ifs resolved" );
-#    ok( ! @capture, "No warnings captured, as expected" );
-#}
-#
-#{
-#    $self->{line} = [
-#        "Alpha this is not an if/elif/elsif/endif",
-#        "#elif this_is_an_elif_statement",
-#        "Beta this is not an if/elif/elsif/endif",
-#        "#else this_is_an_else_statement",
-#        "Gamma this is not an if/elif/elsif/endif",
-#        "#endif this_is_an_endif_statement",
-#    ];
-#    $self->{XSStack}->[-1]{type} = 'if';
-#
-#    @capture = ();
-#    local $SIG{__WARN__} = \&capture;
-#    is( check_conditional_preprocessor_statements($self), undef,
-#        "missing 'if' case: returned undef: all ifs resolved" );
-#    ok( @capture, "Warning captured, as expected" );
-#}
+{
+    $self->{line} = [
+        "#if this_is_an_if_statement",
+        "Alpha this is not an if/elif/elsif/endif",
+        "#elif this_is_an_elif_statement",
+        "Beta this is not an if/elif/elsif/endif",
+        "#else this_is_an_else_statement",
+        "Gamma this is not an if/elif/elsif/endif",
+        "#endif this_is_an_endif_statement",
+    ];
+    $self->{line_no} = [ 17 .. 23 ];
+    $self->{XSStack}->[-1]{type} = 'if';
+    $self->{filename} = 'myfile1';
 
+    my ($stdout, $stderr, $rv);
+    ($stdout, $stderr) = capture {
+        $rv = check_conditional_preprocessor_statements($self);
+    };
+        
+    is( $rv, 0, "Basic case: returned 0: all ifs resolved" );
+    ok( ! $stderr, "No warnings captured, as expected" );
+}
+
+{
+    $self->{line} = [
+        "#if this_is_an_if_statement",
+        "Alpha this is not an if/elif/elsif/endif",
+        "#if this_is_a_different_if_statement",
+        "Beta this is not an if/elif/elsif/endif",
+        "#endif this_is_a_different_endif_statement",
+        "Gamma this is not an if/elif/elsif/endif",
+        "#endif this_is_an_endif_statement",
+    ];
+    $self->{line_no} = [ 17 .. 23 ];
+    $self->{XSStack}->[-1]{type} = 'if';
+    $self->{filename} = 'myfile1';
+
+    my ($stdout, $stderr, $rv);
+    ($stdout, $stderr) = capture {
+        $rv = check_conditional_preprocessor_statements($self);
+    };
+    is( $rv, 0, "One nested if case: returned 0: all ifs resolved" );
+    ok( ! $stderr, "No warnings captured, as expected" );
+}
+
+{
+    $self->{line} = [
+        "Alpha this is not an if/elif/elsif/endif",
+        "#elif this_is_an_elif_statement",
+        "Beta this is not an if/elif/elsif/endif",
+        "#else this_is_an_else_statement",
+        "Gamma this is not an if/elif/elsif/endif",
+        "#endif this_is_an_endif_statement",
+    ];
+    $self->{line_no} = [ 17 .. 22 ];
+    $self->{XSStack}->[-1]{type} = 'if';
+    $self->{filename} = 'myfile1';
+
+    my ($stdout, $stderr, $rv);
+    ($stdout, $stderr) = capture {
+        $rv = check_conditional_preprocessor_statements($self);
+    };
+    is( $rv, undef,
+        "Missing 'if' case: returned undef: all ifs resolved" );
+    like( $stderr,
+        qr/Warning: #else\/elif\/endif without #if in this function/,
+        "Got expected warning: lack of #if"
+    );
+    like( $stderr,
+        qr/precede it with a blank line/s,
+        "Got expected warning: advice re blank line"
+    );
+}
+
+{
+    $self->{line} = [
+        "Alpha this is not an if/elif/elsif/endif",
+        "#elif this_is_an_elif_statement",
+        "Beta this is not an if/elif/elsif/endif",
+        "#else this_is_an_else_statement",
+        "Gamma this is not an if/elif/elsif/endif",
+        "#endif this_is_an_endif_statement",
+    ];
+    $self->{line_no} = [ 17 .. 22 ];
+    $self->{XSStack}->[-1]{type} = 'file';
+    $self->{filename} = 'myfile1';
+
+    my ($stdout, $stderr, $rv);
+    ($stdout, $stderr) = capture {
+        $rv = check_conditional_preprocessor_statements($self);
+    };
+    is( $rv, undef,
+        "Missing 'if' case: returned undef: all ifs resolved" );
+    like( $stderr,
+        qr/Warning: #else\/elif\/endif without #if in this function/,
+        "Got expected warning: lack of #if"
+    );
+    unlike( $stderr,
+        qr/precede it with a blank line/s,
+        "Did not get unexpected stderr"
+    );
+}
+
+{
+    $self->{line} = [
+        "#if this_is_an_if_statement",
+        "Alpha this is not an if/elif/elsif/endif",
+        "#elif this_is_an_elif_statement",
+        "Beta this is not an if/elif/elsif/endif",
+        "#else this_is_an_else_statement",
+        "Gamma this is not an if/elif/elsif/endif",
+    ];
+    $self->{line_no} = [ 17 .. 22 ];
+    $self->{XSStack}->[-1]{type} = 'if';
+    $self->{filename} = 'myfile1';
+
+    my ($stdout, $stderr, $rv);
+    ($stdout, $stderr) = capture {
+        $rv = check_conditional_preprocessor_statements($self);
+    };
+    isnt( $rv, 0,
+        "Missing 'endif' case: returned non-zero as expected" );
+    like( $stderr,
+        qr/Warning: #if without #endif in this function/s,
+        "Got expected warning: lack of #endif"
+    );
+}
 
 pass("Passed all tests in $0");