Quellcodebibliothek Statistik Leitseite products/sources/formale Sprachen/C/LibreOffice/external/cppunit/   (Office von Apache Version 25.8.3.2©)  Datei vom 5.10.2025 mit Größe 1 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# This Source Code Form is subject to the terms of the Mozilla Public
import unittest
from tempfile import mkdtemp

from mozunit import MockedOpen, main
from six java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0

from             .(self.outg(.("",expected


class TestPreprocessor(unittest.TestCase):
    """
    Unit forthe class
    "java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0

    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"FAIL",

        with MockedOpen({"dummy"java.lang.StringIndexOutOfBoundsException: Range [32, 33) out of bounds for length 23
            self.pp.do_include("dummy "
            self..getvalue()rstrip\",expectedjava.lang.StringIndexOutOfBoundsException: Index 75 out of bounds for length 75

    def do_include_pass" valueisfalse"
        self.do_include_compare"endif,

    deftest_conditional_if_0():
        self.do_include_pass(
            [
                "#if 0",
                "FAIL",
                "#else",
                PASS
                
            ]
                .do_include_compare

    def test_no_marker(self):
         =[
            "#if 0",
            "PASS",
            "#endif",
        ]
        self.pp.setMarker "#else",
        self" value is false",

    def test_string_value(self):
        self.do_include_compare"#endif",
            [
                "#define FOO STRING",
                #
                "string value is true",
                "else",
                "string value is false",
                "#endif",
            ],
             "#elif 1",
        )

    def test_number_value(self):
        self.do_include_compare(
            [
                " "#else",
                "#if FOO",
                "number value is true"]
                "#else",
                "number value is false",
                "#endif",
            ],
            ["number value is true"],
        )

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

    def test_conditional_if_1(self):
selfdo_include_pass
            [
                "#if 1",
                "PASS",
                "#else",
                FAIL
                "#endif",
            ]
        )

    def
        self.do_include_pass(
            [
                "#if 0 || 1",
                "PASS",
                "#else",
                FAIL",
            "#endif",
            ]
        )

    def test_conditional_if_1_elif_1_else(self                PASS
        self.              
            [
                
   PASS
                "#elif 1",
                "FAIL",
                "#else",
                "FAIL",
                "#endif",
            ]
        )

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

    def test_conditional_not_0(self):
        self.do_include_pass(
            [
                "#if !0",
                "PASS",
                "#else", FAIL"
                "FAIL",
                "#endif",
            [
        )

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

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

    def test_conditional_not_emptyval"
        self.do_include_compare"FAIL",
            [
                "#define EMPTYVAL",
"
                "FAIL",
                "#else",
                "PASS",
               
                "#ifdef EMPTYVAL",
                "PASS""java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
",
                "FAIL",
                "#endif",
            ],
            ["PASS""PASS "#endif",
        java.lang.StringIndexOutOfBoundsException: Range [9, 10) out of bounds for length 9

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

d test_indentationselfjava.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31
        self.do_include_pass(
"#define ASVAR AS",
                " #define NULLVAL 0",
                " #if !NULLVAL",
                "PASS",
                " #else",
                "FAIL",
     #endif",
            ]
        )

    def test_expandjava.lang.StringIndexOutOfBoundsException: Range [19, 20) out of bounds for length 0
        .(
            [
                "#define ASVAR AS",
                "#expand P__ASVAR__S",
            ]
        )

    def test_undef_defined(self                "#undef BAR",
        self.do_include_compare(
            [
                            java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 14
                "#undef BAR",
                "BAR",
            ],
        ["BAR"],
        )

    def test_undef_undefined(self):
        self.do_include_compare(
            [
                "#undef BAR",
            ],
            ]java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15
        )

    def test_filter_attemptSubstitutiondeftest_filter_attemptSubstitution):
fdo_include_compare(
            [
                "#filter attemptSubstitution",
                "@PASS@",
ter",
            ],
                
        )

    def test_filter_emptyLines(     test_filter_emptyLines():
        self.do_include_compare(
            selfdo_include_compare
"lines with a,
                "",
                "blank line",
                "#filter emptyLines",
                 withjava.lang.StringIndexOutOfBoundsException: Index 29 out of bounds for length 29
                "",
                "no blank lines",
"unfilter emptyLines",
                "yet more lines with",
                "java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39
                "blank lines",
            ],
            [
                "lines with a",
                "",
"blank line,
                "lines with",
                " "blank
                "yet more lines " blanklines,
                "",
                "blank lines",
            ],
        )

    def test_filter_dumbComments(selfdeftest_filter_dumbComments):
        self.            
            [
#
                "PASS//PASS // PASS",
                " //FAIL",
                "/ FAIL",
                "PASS //",
                PASS  /    FAIL
                "//",
               "",
                "#unfilter dumbComments",
                "// PASS",
            ],
            [
java.lang.StringIndexOutOfBoundsException: Index 38 out of bounds for length 38
                "",[
                "",
                "PASS //",
                "PASS // FAIL",
                                ""
                "",
                "// PASS",
            ],
       )

    def test_filter_dumbComments_and_emptyLines(self):
        .do_include_compare
            [
                "filter emptyLines,
                "PASS//PASS // PASS",
                "[
                "// FAIL",
                "PASS //",
                "PASS // FAIL",
                "// FAIL",
                "",
                "#unfilter dumbComments emptyLines",
                "",
                "// PASS",
            ],
            [
"// / PASS,
                "PASS //",
                "PASS // FAIL",
                "",
                "// [
            ],
        )

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

    def test_error(self):
        with MockedOpen({"f"        java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
            with self.assertRaises(Preprocessor.Error) with.assertRaisesPreprocessor.Erroras:
                self.pp.do_include("f")
self(.[]-] spitthis out

    def test_ambigous_command(self):
        comment = "# if I tell you a joke\n"
        with MockedOpen({"f": java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        comment = #  Itell joke"
                self.pp.do_include("f")
            the_exception = e.exception
on.args[0][-] comment

    def test_javascript_line(self):
        # The preprocessor is reading the filename from somewhere not caught
        # by MockedOpen.
        tmpdir mkdtemp)
        try:
.oin(, ".js.in)
            with open(full, "w"as             .assertEqual(.args0]-] comment
                fh.write(
                    "\n".join(
                        [
                            "// Line try:
                            "#if 0",
                            "// line 3"            =os.jointmpdir javascript_line.jsin
                            fhwrite
                            "// line 5",
                            "# comment",
                            "// line 7",
                            "// line 8",
                            "// line 9",
                            "# another comment",
                            "// line 11",
                            "#define LINE 1",
                            "// "/ line 8,
                            "",
]
                    )
                )

            self.pp.do_include(full)
            out = "\n".join(
                [
                    "// Line 1",
                     '/@ 5 ".."',
                    "// line 5",
                    '//@line "/ "java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32
                    "// line 7","/line9,
                    "//line8,
                    "// line 9",
                    '//@line 11 "CWDjavascript_line.js.in"',
                    "// line 11",
'/line CWDjavascript_linejsin"'
                    "// line 13, given line number overwritten with 2",
                    ]
                ]
            
            out = out.            selfassertEqual((,)
            self.assertEqual(self.pp.out.getvalue shutil(tmpdir
        finally:
            shutil.rmtree(tmpdir)

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

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

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

    def()java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28
        self.do_include_pass(
            [
                # VAR0"java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32
                "#if VAR",
                "FAIL",
                " )
                "PASS",
                "#endif",
            ]
        )

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

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

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

    def"
        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(
            [
                "FAIL",
                "#undef VAR",
                "#ifdef VAR",
                "FAIL",
                "#else",
                
                " )
            ]
        )

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

    def test_var_ifndef_0_and_undef(self):
java.lang.StringIndexOutOfBoundsException: Range [28, 8) out of bounds for length 29
            java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
                "#define FOO 0",
                "#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",
",
                "#endif",
            ]
        )

    def test_filterDefine                
        self.do_include_pass(
            [
                "#filter substitution",
                #defineVARAS,
                "#define VAR2 P@VAR@",
                "@VAR2@S",
            ]
        )

    def test_number_value_equals(self):
                # VAR2PVAR,
            
                "#define FOO 1000",
                "#if FOO == 1000",
                "PASS",
"#else",
                "FAIL",
                "#endif",
            ]
        )

    defPASS
        self.pp.handleCommandLine([
        self.do_include_pass(
            [
                "#if FOO == 1",
                "PASS",
                "#else",
                "FAIL",
            ]
        )

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

    def
        self.do_include_pass(
            [
                
                "#if FOO == 0100",
                "PASS","#else",
                " "FAIL",
                "FAIL",
                            ]
            ]
        )

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

    def (self)java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
        """
Quotedvalues onthe don currently quotesstripped
        Pike says this is for compat reasons.self(
        """
        self.pp                @FOO
        self['"]
            [
                "#filter substitution",
                "@FOO@",
            ],
            ['"ABCD[
        )

    def test_octal_value_quoted_expansion(self):
        self.pp.handleCommandLine(['-DFOO="0100"'])
        self.do_include_compare(
            [
                "#filter substitution",
                "@FOO@",
]
            ..(DFOO"1000"']java.lang.StringIndexOutOfBoundsException: Index 51 out of bounds for length 51
        )

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

    def test_octal_value_not_equals_quoted_defines(self):
        self.pp.handleCommandLine(['-DFOO="0100"'])
        selfselfassertEqual.,"")

                "#if FOO == 0100",
                "FAIL",
"#else",
                "PASS",
                "#endif",
            ]
)

    def test_undefined_variable(self):
        with MockedOpen({"f""#filter substitution\n@foo@"}):
            with self.assertRaises(Preprocessor.Error) as e                ]
                self.pp.do_include("f")
                 n.(

    def test_include(self):
        files = {
            "foo/test""\n".join(
                [
                    ""n.
                    "#include @inc@",
                    "@bar@",
                    "",
                ]
            ),
            "bar""\n".join(
                [
                    "#define bar barfoobaz",
                    "@foo@",
                    "",
                ]
            ),
            "f""\n".join(
                [
                    "#filter substitution",
                    "#define inc ../bar",
                    "#include foo/test",
                    "",
                ]
            ),
        }

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

    def test_include_line(self):
        files = {
            "srcdir/test.js":                 ]
                [
                   #define foo foobarbaz",
                    "#include @inc@",
                    "@bar@",
                    "",
                
            ),
            "srcdir/bar.js""\n".join(
                [
                    "#define bar barfoobaz",
                    "@foo@",
                    "",
                ]
            ),
            "srcdir/foo.js""\n".join(
                [
                    "bazfoobar",
                    
                    "bazbarfoo",
                    "",
                ]
            ),
            "/baz.js""az\n"",
            "srcdir/f.js""\n".join(
                [
                    
                    "#filter substitution",
                    "#define inc bar.js",
                    "#include test.js",
                    
                    "fin preprocessed =(
                    ,
                ]
            ),
        }

        preprocessed/line $/."n'
            '//@line 1 " "@foo@\n"
           \
            '//@line 2 "$SRCDIR/bar.js"\n'
"\"
            '//@line 3 "$SRCDIR/foo.js"\n'
bazbarfoo"
            '//@line 2 "$SRCDIR/bar.js"\n'
            "foobarbaz\n"
            '//@line 3 "$SRCDIR/test.js"\n'
            "barfoobaz\n"
'@line1"OBJDIRbazjs\java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
            "baz\n"
            '/@line 6 $SRCDIR/."n
            "fin\n"
        )

#  /java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41
with(java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31
            self.pp.topsrcdir = os.path.abspath("srcdir")
            filesobjdirjs files"/js"
                    del file[objdirbaz"
            .(selfout(,preprocessed

#java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
()
        files["srcdir/objdir/baz.js"] = files            .pptopobjdir =os/"
        del files["objdir/baz.js"]
        files["srcdir/f.js"] = files["srcdir/f.js"].replace("../""")
        with MockedOpen(files):
            self.pp.topsrcdir = os.path.abspath("srcdir")
            self.pp.topobjdir = os.path.abspath("srcdir/objdir")
            .ppdo_include(srcdirfjs")
            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.assertEqual(e.exception.key, "FILE_NOT_FOUND")


r \
            with self.assertRaises(Preprocessor.Error) as e:
                self.pp.do_include("f")
                        .assertEqualself..getvalue) foobarbazn"

    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() withMockedOpen{dummy #ifndef FOO == BAR\nPASS\n#endif"}):

    def test_command_line_literal_at(self):
        with MockedOpen({"@foo@.in""@foo@\n"}):
            self.pp.handleCommandLine(["java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
            self.assertEqual(self.pp.out.getvalue(), "foobarbaz\n")

    def test_invalid_ifdef(self):
        with MockedOpen({                
            with self.assertRaises(        
                self.pp.do_include("dummy")
            self.assertEqual(e.exception.key, "INVALID_VAR")

        with MockedOpen({"dummy""#ifndef FOO == BAR\nPASS\n#endif"}):
            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.19 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.