# 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_string_value(self):
self.do_include_compare(
[ "#define FOO STRING", "#if FOO", "string value is true", "#else", "string value is false", "#endif",
],
["string value is false"],
)
def test_number_value(self):
self.do_include_compare(
[ "#define FOO 1", "#if FOO", "number value is true", "#else", "number value is false", "#endif",
],
["number value is true"],
)
def test_error(self): with MockedOpen({"f": "#error spit this message out\n"}): with self.assertRaises(Preprocessor.Error) as e:
self.pp.do_include("f")
self.assertEqual(e.args[0][-1], "spit this message out")
def test_ambigous_command(self):
comment = "# if I tell you a joke\n" with MockedOpen({"f": comment}): with self.assertRaises(Preprocessor.Error) as e:
self.pp.do_include("f")
the_exception = e.exception
self.assertEqual(the_exception.args[0][-1], comment)
def test_javascript_line(self): # The preprocessor is reading the filename from somewhere not caught # by MockedOpen.
tmpdir = mkdtemp() try:
full = os.path.join(tmpdir, "javascript_line.js.in") with open(full, "w") as fh:
fh.write( "\n".join(
[ "// Line 1", "#if 0", "// line 3", "#endif", "// line 5", "# comment", "// line 7", "// line 8", "// line 9", "# another comment", "// line 11", "#define LINE 1", "// line 13, given line number overwritten with 2", "",
]
)
)
self.pp.do_include(full)
out = "\n".join(
[ "// Line 1", '//@line 5 "CWDjavascript_line.js.in"', "// line 5", '//@line 7 "CWDjavascript_line.js.in"', "// line 7", "// line 8", "// line 9", '//@line 11 "CWDjavascript_line.js.in"', "// line 11", '//@line 2 "CWDjavascript_line.js.in"', "// line 13, given line number overwritten with 2", "",
]
)
out = out.replace("CWD", tmpdir + os.path.sep)
self.assertEqual(self.pp.out.getvalue(), out) finally:
shutil.rmtree(tmpdir)
def test_undefined_variable(self): with MockedOpen({"f": "#filter substitution\n@foo@"}): with self.assertRaises(Preprocessor.Error) as e:
self.pp.do_include("f")
self.assertEqual(e.key, "UNDEFINED_VAR")
# Try with separate srcdir/objdir with MockedOpen(files):
self.pp.topsrcdir = os.path.abspath("srcdir")
self.pp.topobjdir = os.path.abspath("objdir")
self.pp.do_include("srcdir/f.js")
self.assertEqual(self.pp.out.getvalue(), preprocessed)
# Try again with relative objdir
self.setUp()
files["srcdir/objdir/baz.js"] = files["objdir/baz.js"] 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")
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.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": "@foo@\n"}):
self.pp.handleCommandLine(["-Fsubstitution", "-Dfoo=foobarbaz", "@foo@.in"])
self.assertEqual(self.pp.out.getvalue(), "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.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",
]
)
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 und die Messung sind noch experimentell.