# coding: utf-8 # 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/.
class TestHashing(unittest.TestCase): def test_hash_file_known_hash(self): """Ensure a known hash value is recreated."""
data = b"The quick brown fox jumps over the lazy cog"
expected = "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3"
def test_invalid_exports_append(self): with self.assertRaises(ValueError) as ve:
self.EXPORTS += "foo.h"
six.assertRegex(
self,
str(ve.exception), "Expected a list of strings, not <(?:type|class) '%s'>" % str_type,
)
def test_invalid_exports_set(self): with self.assertRaises(ValueError) as ve:
self.EXPORTS.foo = "foo.h"
six.assertRegex(
self,
str(ve.exception), "Expected a list of strings, not <(?:type|class) '%s'>" % str_type,
)
def test_invalid_exports_append_base(self): with self.assertRaises(ValueError) as ve:
self.EXPORTS += "foo.h"
six.assertRegex(
self,
str(ve.exception), "Expected a list of strings, not <(?:type|class) '%s'>" % str_type,
)
def test_invalid_exports_bool(self): with self.assertRaises(ValueError) as ve:
self.EXPORTS += [True]
six.assertRegex(
self,
str(ve.exception), "Expected a list of strings, not an element of ""<(?:type|class) 'bool'>",
)
def test_del_exports(self): with self.assertRaises(MozbuildDeletionError):
self.EXPORTS.foo += ["bar.h"] del self.EXPORTS.foo
def test_unsorted(self): with self.assertRaises(UnsortedError):
self.EXPORTS += ["foo.h", "bar.h"]
with self.assertRaises(UnsortedError):
self.EXPORTS.foo = ["foo.h", "bar.h"]
with self.assertRaises(UnsortedError):
self.EXPORTS.foo += ["foo.h", "bar.h"]
with self.assertRaises(UnsortedError):
l[:] = ["b", "a"]
self.assertEqual(len(l), 2)
def test_add(self):
l = StrictOrderingOnAppendList()
l2 = l + ["a", "b"]
self.assertEqual(len(l), 0)
self.assertEqual(len(l2), 2)
self.assertIsInstance(l2, StrictOrderingOnAppendList)
with self.assertRaises(UnsortedError):
l2 = l + ["b", "a"]
self.assertEqual(len(l), 0)
def test_iadd(self):
l = StrictOrderingOnAppendList()
l += ["a", "b"]
self.assertEqual(len(l), 2)
self.assertIsInstance(l, StrictOrderingOnAppendList)
with self.assertRaises(UnsortedError):
l += ["b", "a"]
self.assertEqual(len(l), 2)
def test_add_after_iadd(self):
l = StrictOrderingOnAppendList(["b"])
l += ["a"]
l2 = l + ["c", "d"]
self.assertEqual(len(l), 2)
self.assertEqual(len(l2), 4)
self.assertIsInstance(l2, StrictOrderingOnAppendList) with self.assertRaises(UnsortedError):
l2 = l + ["d", "c"]
self.assertEqual(len(l), 2)
def test_add_StrictOrderingOnAppendList(self):
l = StrictOrderingOnAppendList()
l += ["c", "d"]
l += ["a", "b"]
l2 = StrictOrderingOnAppendList() with self.assertRaises(UnsortedError):
l2 += list(l) # Adding a StrictOrderingOnAppendList to another shouldn't throw
l2 += l
class TestStrictOrderingOnAppendListWithAction(unittest.TestCase): def setUp(self):
self.action = lambda a: (a, id(a))
def assertSameList(self, expected, actual):
self.assertEqual(len(expected), len(actual)) for idx, item in enumerate(actual):
self.assertEqual(item, expected[idx])
def test_init(self):
l = StrictOrderingOnAppendListWithAction(action=self.action)
self.assertEqual(len(l), 0)
original = ["a", "b", "c"]
l = StrictOrderingOnAppendListWithAction(["a", "b", "c"], action=self.action)
expected = [self.action(i) for i in original]
self.assertSameList(expected, l)
with self.assertRaises(ValueError):
StrictOrderingOnAppendListWithAction("abc", action=self.action)
with self.assertRaises(ValueError):
StrictOrderingOnAppendListWithAction()
def test_extend(self):
l = StrictOrderingOnAppendListWithAction(action=self.action)
original = ["a", "b"]
l.extend(original)
expected = [self.action(i) for i in original]
self.assertSameList(expected, l)
with self.assertRaises(ValueError):
l.extend("ab")
def test_slicing(self):
l = StrictOrderingOnAppendListWithAction(action=self.action)
original = ["a", "b"]
l[:] = original
expected = [self.action(i) for i in original]
self.assertSameList(expected, l)
with self.assertRaises(ValueError):
l[:] = "ab"
def test_add(self):
l = StrictOrderingOnAppendListWithAction(action=self.action)
original = ["a", "b"]
l2 = l + original
expected = [self.action(i) for i in original]
self.assertSameList(expected, l2)
with self.assertRaises(ValueError):
l + "abc"
def test_iadd(self):
l = StrictOrderingOnAppendListWithAction(action=self.action)
original = ["a", "b"]
l += original
expected = [self.action(i) for i in original]
self.assertSameList(expected, l)
# Don't allow extending lists with different flag definitions.
BarList = StrictOrderingOnAppendListWithFlagsFactory(
{"foo": six.text_type, "baz": bool}
)
bar = BarList(["d", "e", "f"])
bar["d"].foo = "foo"
bar["e"].baz = True with self.assertRaises(ValueError):
foo + bar with self.assertRaises(ValueError):
bar + foo
# It's not obvious what to do with duplicate list items with possibly # different flag values, so don't allow that case. with self.assertRaises(ValueError):
foo + foo
# Memoization of methods is expected to not keep references to # instances, so the refcount shouldn't have changed after executing the # memoized method.
self.assertEqual(refcount, sys.getrefcount(instance))
def test_memoized_property(self): class foo(object): def __init__(self):
self._count = 0
with self.assertRaises(ValueError):
l[:] = [3, "c"]
self.assertEqual(len(l), 2)
def test_add(self):
cls = TypedList(int)
l = cls()
l2 = l + [1, 2]
self.assertEqual(len(l), 0)
self.assertEqual(len(l2), 2)
self.assertIsInstance(l2, cls)
with self.assertRaises(ValueError):
l2 = l + [3, "c"]
self.assertEqual(len(l), 0)
def test_iadd(self):
cls = TypedList(int)
l = cls()
l += [1, 2]
self.assertEqual(len(l), 2)
self.assertIsInstance(l, cls)
with self.assertRaises(ValueError):
l += [3, "c"]
self.assertEqual(len(l), 2)
def test_add_coercion(self):
objs = []
class Foo(object): def __init__(self, obj):
objs.append(obj)
cls = TypedList(Foo)
l = cls()
l += [1, 2]
self.assertEqual(len(objs), 2)
self.assertEqual(type(l[0]), Foo)
self.assertEqual(type(l[1]), Foo)
# Adding a TypedList to a TypedList shouldn't trigger coercion again
l2 = cls()
l2 += l
self.assertEqual(len(objs), 2)
self.assertEqual(type(l2[0]), Foo)
self.assertEqual(type(l2[1]), Foo)
# Adding a TypedList to a TypedList shouldn't even trigger the code # that does coercion at all.
l2 = cls()
list.__setitem__(l, slice(0, -1), [1, 2])
l2 += l
self.assertEqual(len(objs), 2)
self.assertEqual(type(l2[0]), int)
self.assertEqual(type(l2[1]), int)
with self.assertRaises(TypeError):
FooBar("foo", "not integer") with self.assertRaises(TypeError):
FooBar(2, 4)
# Passing a tuple as the first argument is the same as passing multiple # arguments.
t1 = ("foo", 3)
t2 = FooBar(t1)
self.assertEqual(type(t2), FooBar)
self.assertEqual(FooBar(t1), FooBar("foo", 3))
class TestGroupUnifiedFiles(unittest.TestCase):
FILES = ["%s.cpp" % letter for letter in string.ascii_lowercase]
self.assertEqual(
expand_variables("$(a) and $(b)", {"a": "1", "b": "2"}), "1 and 2"
)
self.assertEqual(
expand_variables("$(a) and $(undefined)", {"a": "1", "b": "2"}), "1 and "
)
self.assertEqual(
expand_variables( "before $(string) between $(list) after",
{"string": "abc", "list": ["a", "b", "c"]},
), "before abc between a b c after",
)
class TestEnumString(unittest.TestCase): def test_string(self): class CompilerType(EnumString):
POSSIBLE_VALUES = ("gcc", "clang", "clang-cl")
def test_read_only_dict():
d = ReadOnlyDict(foo="bar") with pytest.raises(Exception):
d["foo"] = "baz"
with pytest.raises(Exception):
d.update({"foo": "baz"})
with pytest.raises(Exception): del d["foo"]
# ensure copy still works
d_copy = d.copy() assert d == d_copy # TODO Returning a dict here feels like a bug, but there are places in-tree # relying on this behaviour. assert isinstance(d_copy, dict)
d_copy = copy.copy(d) assert d == d_copy assert isinstance(d_copy, ReadOnlyDict)
d_copy = copy.deepcopy(d) assert d == d_copy assert isinstance(d_copy, ReadOnlyDict)
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.