Skip to content

Commit

Permalink
Use raw strings in CBot unit tests
Browse files Browse the repository at this point in the history
use v5.35;

my $code = "";

while(<>) {
    if (/ExecuteTest\(\n$/) {  # First line of the test
        die "nested?" if $code ne "";
        $code = $_;
    } elsif (/^ {4}\);/) {  # Last line of the test
        $code .= $_;

        if ($code !~ /\)"/ and $code !~ /\)\\"/ and $code =~ /
            ^
            (?<head>
                \ *(auto\ publicProgram\ =\ )?ExecuteTest\(
            )
            \n
            (?<body>
                (?&CBotOrComment)
                (\n (?&CBotOrComment) )*
            )
            (?<tail>
                # Error code
                (
                    ,\n
                    \s*[a-zA-Z0-9]+
                )?
                \s*
                \n\ {4}\);\n
            )
            $

            (?(DEFINE)
                (?<CBotOrComment>  # A quoted line of CBot code or a C++ comment
                    [ ]{8}"
                        ([^\\]|\\"|\\'|\\\\)*  # Allow only the following escapes: \", \' and \\
                    (\\n)?"
                    |
                    [ ]{8}\/\/.*
                )
            )
        /x) {
            my $head = $+{head} . "R\"(";
            my $body = $+{body};
            my $tail = $+{tail};

            if ($body =~ "\n") {
                $body =~ s/^([ ]{8})"/$1/mg;  # Strip the quote at the begining
                $body =~ s/(\\n)?"(\n?)$/$2/mg;  # Strip the quote at the end

                # Implement escape seqences
                $body =~ s/\\"/"/g;
                $body =~ s/\\'/'/g;
                $body =~ s/\\\\/\\/g;

                if ( $tail eq "\n    );\n" ) {
                    $tail = "\n    )\");\n";
                } else {
                    $body .= "\n        )\"";
                }
                $code = $head . "\n" . $body . $tail;
            }
        } else {
            #print $code; # DEBUG
        }
        print $code;
        $code = "";
    } else {
        if ($code eq "") {
            print $_;
        } else {
            $code .= $_;
        }
    }
}
  • Loading branch information
hexagonrecursion committed Sep 2, 2024
1 parent f8f5253 commit f5ca83d
Showing 1 changed file with 2,471 additions and 2,364 deletions.
Loading

0 comments on commit f5ca83d

Please sign in to comment.