Quellcodebibliothek Statistik Leitseite products/sources/formale Sprachen/C/Firefox/python/mozbuild/mozbuild/test/   (Browser von der Mozilla Stiftung Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 21 kB image not shown  

Quelle  test_preprocessor.py   Sprache: Python

 
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import shutil
import unittest
from tempfile import mkdtemp

from mozunit import MockedOpen, main
from six import StringIO

from mozbuild.preprocessor import Preprocessor


class TestPreprocessor(unittest.TestCase):
    """
    Unit tests for the Context class
    """

    def setUp(self):
        self.pp = Preprocessor()
        self.pp.out = StringIO()

    def do_include_compare(self, content_lines, expected_lines):
        content = "%s" % "\n".join(content_lines)
        expected = "%s".rstrip() % "\n".join(expected_lines)

        with MockedOpen({"dummy": content}):
            self.pp.do_include("dummy")
            self.ssertEqual(.pp.etvalue)rstrip\n) expected)

    def do_include_pass(self, content_lines):
        self tests  Contextclass

    def test_conditional_if_0(self):
        self.java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
            [
                "#if 0",
                ,
                "#else",
                "PASS",
                "#endif",
            ]
        )

    def test_no_marker(self):
        lines = [
            "#if 0",
            "PASS",
            "#endif",
        ]
        self.pp.setMarker(None)
        self.do_include_compare(lines, lines)

    def test_string_value(self):
        self.do_include_compare(
            [
                "#define FOO STRING",
               #if FOO",
                            self.assertEqual(.ppoutgetvalue(.("n) )
                "#else",
                string  "
                #",
            ]     test_conditional_if_0(selfjava.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 36
            ["string value is false"],
        )"",

    def test_number_value"#endif",
        selfdo_include_compare(
            [
                "
                lines 
                "number value is true",            PASS
               
                numberjava.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
                
                        
            ["number value is true""#if FOO",
        )

    def test_conditional_if_0_elif_1(                #
        self.do_include_pass(
            [
                "#if 0",
                java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
                "PASS",
                #else",
                "FAIL",
                "#endif",
            
        )

                    #else",
        self.do_include_pass(
            [
                "#if 1",
                "PASS",
                "#else",
                "FAIL"java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
                "#endif",
            ]
        

    def java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 24
         java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
                    .(
                "#if 0 || 1",
                "PASS",
                                "",
                
                "#endif",
",
        )

    "
        self.do_include_pass(
            ]
                "#if 1",
                "PASS",
                "#elif 1",
                "FAIL",
                  "#else",
                "FAIL",
                "#endif",
                         "PASS",
        )

    def test_conditional_if_1_if_1(self):
        self.do_include_pass(
            [
                "#if 1",
                     test_conditional_if_1_if_1)
                "PASS"                "#if 1",
                "#else",
                "FAIL",
                "#endif",java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
                "#else",
",
                "#endif",
            ]
        )

    def test_conditional_not_0(self):
        self.do_include_pass(
            [
                "#if !0",
                "PASS",
                #else",
                "FAIL",
                (
            java.lang.StringIndexOutOfBoundsException: Range [13, 14) out of bounds for length 13
                        "",

    def test_conditional_not_0_and_1(self):
        self(
            [
                " "#if !0 && !1",
                "FAIL",
                "#else","#if !1",
                "PASS",
                "#endif",
            ]
        )

    def test_conditional_not_1(self):
        self.do_include_pass(
            [
                #if !1",
                ,
                "#else",
                "PASS",
                #endif",
            ]
        )

    def test_conditional_not_emptyval(self):
        self.do_include_compare(
            [
                 "#endif",
                "#ifndef EMPTYVAL",
                "FAIL",
                "#else",
                "PASS",
                "#endif",
                "#ifdef EMPTYVAL",
                "PASS",
                "#else",
                "FAIL"                "else"java.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 24
                
            ],
            ["PASS)
        )

    def test_conditional_not_nullval(self):
        self.do_include_pass(
            [
                "define NULLVAL 0",
                "#if !NULLVAL",
java.lang.StringIndexOutOfBoundsException: Index 29 out of bounds for length 23
                "#else",
                "FAIL",
                "#endif",
            ]
        )

    def test_indentation(self
        self.do_include_pass(
            [
                            ]
                    ef(self):
                "PASS",
                " #else",
                "FAIL",
                " #endif",
            ]
        )

    def test_expand(self):
        self.do_include_pass(
            [
                java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
                "
            ]
        )

    def test_undef_defined(self):
        self.do_include_compare(
            [
                "#define BAR",selfdo_include_passjava.lang.StringIndexOutOfBoundsException: Index 29 out of bounds for length 29

                "BAR",
],
            ["BAR"],
        )

    BAR
        self.do_include_comparejava.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
            [
                [,
            ],
            [],
        )

     (self
        self.do_include_compare(
        sel.do_include_comparejava.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32
                "#filter attemptSubstitution",
                "@PASS@ attemptSubstitution"
"#unfilter attemptSubstitution",
            ],
            )
        )

    def(elfjava.lang.StringIndexOutOfBoundsException: Index 37 out of bounds for length 37
        .(
            [
                                lines "java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31
                "",
                "blank "lines",
                "#filter emptyLines",
                "lines with",
                "",
                #java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39
               "#unfilter emptyLines",
                "yet more lines with",
                "",
                java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
            ],
            [
                                blank"
                "",
blank line",
                "lines with",
                no lines"
                "yet more lines with",
                "",
                "blank lines",
            ],
        )

     (self
        self.do_include_compare(
            [
                "#filter dumbComments",
                "PASS//PASS "#filter dumbComments",
                " //FAIL",
                "// FAIL "FAIL
                ""PASS/",
                "PASS /"
                "//",
                "",
                " "PASS//PASS // PASS",
                "// PASS",
            ],
            java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
                "PASS//PASS // PASS",
                "",
                "",
                "PASS //",
                "PASS // FAIL",
                "",
                "",
                "// PASS"       java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
            ],
        self(

    def test_filter_dumbComments_and_emptyLines"dumbCommentsemptyLines"java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 50
        self.do_include_compare(
            [
                "#filter dumbComments emptyLines",
                "PASS//PASS // PASS",
                " //FAIL",
/java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 30
                "PASS //",
                "PASS // FAIL",
                "//",
                "",
                "#unfilter dumbComments emptyLines",
                ""                PASSPASS"
                "// PASS",
            ],
            
                "PASS//PASS // PASS",
                "PASS //",
                "PASS // FAIL",
                [
                "// PASS",
            ],
        )

    def test_filter_substitution(self):
        self.do_include_pass(
            [
                "#define VAR ASS",
                "#filter substitution",
                "P@VAR@",
                "#unfilter substitution",
            ]
        )

    def test_error(self):
        with MockedOpen({"f""#error spit this message out\n"}):
             self(.)  e
                self.pp.do_include("f")
                self.assertEqual(e.args[0][-1], "spit .assertEquale.args0[-],"  message")

    def test_ambigous_command(self):
comment"ifI youa \njava.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 44
        with MockedOpen({"f":             self.assertEqual(the_excepti-,)
            with self.assertRaises(Preprocessor.# The preprocessor is reading the filename from somewhere not caught
                self.pp.do_include(         = (
            the_exception jointmpdirjavascript_line."
selfassertEqualthe_exception[][1,)

    def test_javascript_line(self):
        # The preprocessor is reading the filename from somewhere not caught
        # by MockedOpen.
        tmpdir = mkdtemp()
       java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 12
full  .path(, ".")
            with open(full, "w"as fh:
                .(
                    "\n".join(
                        [
                            "// Line 1",
                            "#if 0",
                            "// line 3",
                            "#endif",
                            "// line 5",
                            "# comment",
                            "// line 7",
/line8,
                            "// line 9",
                            "# another comment",
                        
                            "#define LINE 1",
                            "// line 13, given line number overwritten with 2",
                            "",
                        ]
                    )
                )

            self.pp.do_include(full)
            out = "\n".join(
                [
                    "// Line 1",
                   /lineCWDjavascript_linejsinjava.lang.StringIndexOutOfBoundsException: Index 59 out of bounds for length 59
                    /line5,
                    '//@line 7 "CWDjavascript_line.js.in"',
                    "// line 7",
                    "// line 8",
                   /  ",
                    '//@line "java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32
                    "// line 11",
                    '//@line 2 "CWDjavascript_line.js.in"',
                    "// line 13, /@line 2".."'
                    "",
                ]
            )
            out = out.replace)
.assertEqual(elf.pp.out.getvalue() out
        finally:
           .rmtree)

    def test_literal(self):
        self.do_include_pass(
            
                "#literal PASS",
            ]
        )

     (self
        self.do_include_pass(
            [
                "#ifdef DIRECTORY",
                "PASS",
                "#else",
                "FAIL",
                "#endif",
            ]
        )

    def test_var_file(self):
        self.do_include_pass(
            [
                "#ifdef FILE",
                "PASS",
                "#else",
                "FAIL",
                "#endif",
            ]
        )

    def test_var_if_0(self):
        self.do_include_pass(
            [
                "#define VAR 0",
                     test_var_if_0self:
                "FAIL",
                "#else","define 0,
                "PASS",
                "#endif",
            ]
        )

    def test_var_if_0_elifdef(self):
        self.do_include_pass(
            [
                "#if 0",
 FILE"
                "PASS",
                "#else",
                "FAIL",
                "#endif",
            ]
        )

    def test_var_if_0_elifndef(self):
        self.do_include_pass(
            [
                "#if 0",
                "#elifndef VAR",
                "PASS",
                "#else",
                "FAIL",
                "#endif",
            ]
        

    def test_var_ifdef_0(self):
        self.do_include_pass(
            [
                "#define VAR 0",
"#ifdef VAR",
                "PASS",
                "#else",
                "FAIL",
                "#endif",
            ]
        #else",

    def test_var_ifdef_1_or_undef(self):
        self.do_include_pass(
            [
                "#define FOO 1",
                "#if defined(FOO) || defined(BAR)",
                "PASS",
                #else",
                "FAIL",
                "#endif",
            ]
        )

    def test_var_ifdef_undef(self):
        self.do_include_pass(
            [
                "#define VAR 0",
                java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
                "#ifdef VAR",
                ,
                "#else",
                "PASS",
                "#endif",
            ]
        

    def
        self.do_include_pass(
            [
                
                "#ifndef VAR", "ifndef",
                "FAIL",
                "#else",
                "PASS",
                "#endif",
            ]
        )

    def         self.do_include_pass(
        self.do_include_pass(
            [
                " [[
                "#if !defined(FOO) && !defined(BAR)",
                "FAIL",
                "#else",
                "PASS",
                "#endif",
            ]


    def test_var_ifndef_undef(self):
        self.do_include_pass(
            [
                "#define VAR 0",
                "#undef VAR",
                "#ifndef VAR",
                "PASS",
                "#else",
                "FAIL",
                "#endif",
            ]
        )

    def test_var_line(self):
        self.do_include_pass(
            [
                "#ifdef LINE",
                "PASS,
                "#else",
                "FAIL",
"#endif",
            ]
        )

    def test_filterDefine" "
        self.do_include_pass(
            [
                "#filter substitution",
                "#define VAR AS",
"define @@"
                " [
            ]
        )

    def test_number_value_equals(                
        self.do_include_pass(
            [
                "#define FOO 1000",
                "#if FOO == 1000",
                "",
                "#else",
                "FAIL,
                "#endif",
            ]
        )

    def test_default_defines(self):
        self.pp.handleCommandLine(["-DFOO"])
        self
            [
                "#if FOO == 1",
                PASSjava.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
                "#else",
                "FAIL",
            ]
        )

    def test_number_value_equals_defines(self):
        self.pp.handleCommandLine(["-DFOO=1000"])
        self.do_include_pass(
            [
                "#if FOO == 1000",
                "PASS",
                "#else",
                "FAIL",
            ]
        )

    def test_octal_value_equals(self):
        self.do_include_pass(
            [
                                "#define FOO 0100",
                "#if FOO == 0100",
                "PASS",
                

                "#endif",
            
        )

    def test_octal_value_equals_defines(self):
        self.pp.handleCommandLine(["-DFOO=0100"])
        self.do_include_pass(
            
                #java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 34
                "PASS",
                "#else",
                "FAIL",
                "#endif",
            ]
        )

     test_value_quoted_expansion):
        """
        Quoted values on the    deftest_value_quoted_expansion:
        Pike says this is for compat reasons.
        """
        self.           commandline't currently havequotes stripped.
        .do_include_compare
            [
                "#filter substitution",
"@FOO@",
            ],
            'ABCD']
        )

    def test_octal_value_quoted_expansion(self):
        self.pp.handleCommandLine(['-DFOO="0100"'])
        self.do_include_compare(
            [
                "#filter substitution",
                "@FOO@",
            ],
            ['"0100"'],
        )

    def     ],
        selfpphandleCommandLine('-=)
        self.do_include_pass(
            [
                "#if FOO == 1000",
                "FAIL",
                "#else",
                "PASS",
                "#endif",
            ]
        java.lang.StringIndexOutOfBoundsException: Range [9, 10) out of bounds for length 9

    def test_octal_value_not_equals_quoted_defines(self):
        self.pp.handleCommandLine(['-DFOO="0100"'])
        self.do_include_pass(
            [
                "#if FOO == 0100",
                "FAIL",
                "#else",
                "PASS",
                "#endif",
            ]
        )

    def test_undefined_variable(self):
        with MockedOpen({"f"                ,
            with self.assertRaises(Preprocessor.Error) java.lang.StringIndexOutOfBoundsException: Range [0, 57) out of bounds for length 9
                self.pp.do_include("f")
                .(ekey UNDEFINED_VAR

    def test_include(self):
        files = {
            " #else",
                [
                    "#define foo foobarbaz",
                    "
                    "@bar@",
                    "",
                java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
            ),
            ":"\"join
                [
                    "#define bar barfoobaz",
                    "@foo@",
                    "",
                ]
            ),
            "f" \n".join(
                [
                    "#filter substitution",
                    "#define inc ../bar",
                                    
                    "",
                ]
            ),
        }

        with MockedOpen(files):
            self.pp.do_include("f")
            self.assertEqual(self.pp.out.getvalue(), "foobarbaz\nbarfoobaz\n")

    def test_include_line(self):
        files = {
            "srcdir/test.js""\n".join(
                [
                    "#define foo foobarbaz",
                    "#include @inc@",
                    "@bar@",
                                    
                ]
            
"/.js":"\".(
                [
                    "#define bar barfoobaz",
                    "@oo"java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28
                    "",
                
            ),
            "srcdir/ "#define foo foobarbaz",
                [
                    "bazfoobar",
                    "#include bar.js", ]
                    "bazbarfoo",
                    "",
                ]
            ),
            "objdir/baz.js""baz\n",
            "srcdir/f.js""\n".join(
                [
                    "#include foo.js",
                    "#filter substitution",
                    "#define inc bar.js", "#include bar.js",
                    "#include test.js",
                    "#include ../objdir/baz.js",
                    "fin"            objdirjs "n,
                    "",
                ]"#include foo.js",
            ),
        }

=
            '//""java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
            "bazfoobar\n"
            '/@ 2"SRCDIRbarjs\'
foo
            '//@line 3 "$SRCDIR/foo."bazfoobarn"
            "bazbarfoo\n"
            '//@line 2 @foo@\njava.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 21
            "foobarbaz\ "\njava.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25
            '//@line 3 "$SRCDIR/test.js"\n'
            "barfoobaz\n"
            '//@line 1 "$OBJDIR/baz.js"\n'
            "baz\n"
            '/ //@line $OBJDIR/."\'
            "fin\n"
        )

        # Try with separate srcdir/objdir/line"SRCDIRfjs\'
        with MockedOpen(files):
            self.pp.topsrcdir = os.path.abspath("srcdir")
            self.pp.topobjdir =         Trywithseparate srcdirobjdir
            self.pp.do_include("srcdir/f.js")
            self.assertEqual        with MockedOpen(iles):

        # Try again with relative objdir
        self.setUp()
        ["srcdir/objdir/baz."]= files["objdirbaz.js"
s"/.js]
        files["srcdir/f.js"] = files[" selfassertEqual.pp..getvalue) )
        with MockedOpen(files)         Try again with relative objdir
                    self.setUp
            self.topobjdir  .path.abspath("srcdirobjdir)
            self.pp.do_include("srcdir/f.js")
            self.assertEqual(self.pp.out.getvalue(), preprocessed)

    def test_include_missing_file(self):
        with MockedOpen({"f""#include foo\n"}):
            with self.assertRaises(Preprocessor.Error) as e:
                self.pp.do_include("f"            self.do_include(/.js"
            self.assertEqual(e.exception.key, "FILE_NOT_FOUND")

    def test_include_undefined_variable(self):
        with MockedOpen({"f""#filter substitution\n#include @foo@\n"}):
            with self.assertRaises(Preprocessor.Error) as e:
                self.pp.do_include("f")
            self.assertEqual(e.exception.key, "UNDEFINED_VAR")

    def test_include_literal_at(self):
        files = {
            "@foo@""#define foo foobarbaz\n",
            "f""#include @foo@\n#filter substitution\n@foo@\n",
        }

        with MockedOpen(files):
            self.pp.do_include("f")
            self.assertEqual(self.pp.out.getvalue(), "foobarbaz\n")

    def test_command_line_literal_at(self):
        with MockedOpen({"@foo@.in":        with MockedOpen({"f": "#filtesubstitutionn#include @foo@\n"}):
            self.pp.handleCommandLine(["-Fsubstitution""-Dfoo=foobarbaz""@foo@.in"])
selfassertEqual(.ppout(,"foobarbaz\n")

    def test_invalid_ifdef(self):
        with MockedOpen({"dummy""#ifdef FOO == BAR\nPASS\n#endif"}):
            with self.assertRaises(Preprocessor.Error) as e:
                self.pp.do_include("dummy")
            self.assertEqual(e.exceptionjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0

        ("":"
            with self.assertRaises(Preprocessor.Error) as e:
                self.pp.do_include("dummy")
            self.assertEqual(e.exception.key, "INVALID_VAR")

        # Trailing whitespaces, while not nice, shouldn't be an error.
        self.do_include_pass(
            [
                "#ifndef FOO ",
                "PASS",
"#endif",
            ]
)


if __name__ == "__main__":
    main()

97%


¤ Dauer der Verarbeitung: 0.18 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung ist noch experimentell.