# 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 test_conditional_if_0(self):
self.java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
[ "#if 0",
, "#else", "PASS", "#endif",
]
)
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", "",
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",
],
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_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 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")
= '//""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")
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",
]
)
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.