# 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 (self)java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42 """
Quotedvalues onthe don currently quotesstripped
Pike says this isfor 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_undefined_variable(self): with MockedOpen({"f": "#filter substitution\n@foo@"}): with self.assertRaises(Preprocessor.Error) as e ]
self.pp.do_include("f")
n.(
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"
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()
ength 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()
¤ 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.0.23Bemerkung:
(vorverarbeitet)
¤
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.