summaryrefslogtreecommitdiff
path: root/tests/test_misc.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_misc.py')
-rw-r--r--tests/test_misc.py41
1 files changed, 39 insertions, 2 deletions
diff --git a/tests/test_misc.py b/tests/test_misc.py
index 939b1c98..c8c2c9e4 100644
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -1,12 +1,13 @@
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
-# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
+# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
"""Tests of miscellaneous stuff."""
import pytest
from coverage.misc import contract, dummy_decorator_with_args, file_be_gone
-from coverage.misc import format_lines, Hasher, one_of
+from coverage.misc import format_lines, Hasher, one_of, substitute_variables
+from coverage.misc import CoverageException
from tests.coveragetest import CoverageTest
@@ -47,6 +48,13 @@ class HasherTest(CoverageTest):
h2.update({'b': 23, 'a': 17})
self.assertEqual(h1.hexdigest(), h2.hexdigest())
+ def test_dict_collision(self):
+ h1 = Hasher()
+ h1.update({'a': 17, 'b': {'c': 1, 'd': 2}})
+ h2 = Hasher()
+ h2.update({'a': 17, 'b': {'c': 1}, 'd': 2})
+ self.assertNotEqual(h1.hexdigest(), h2.hexdigest())
+
class RemoveFileTest(CoverageTest):
"""Tests of misc.file_be_gone."""
@@ -128,3 +136,32 @@ class ContractTest(CoverageTest):
])
def test_format_lines(statements, lines, result):
assert format_lines(statements, lines) == result
+
+
+VARS = {
+ 'FOO': 'fooey',
+ 'BAR': 'xyzzy',
+}
+
+@pytest.mark.parametrize("before, after", [
+ ("Nothing to do", "Nothing to do"),
+ ("Dollar: $$", "Dollar: $"),
+ ("Simple: $FOO is fooey", "Simple: fooey is fooey"),
+ ("Braced: X${FOO}X.", "Braced: XfooeyX."),
+ ("Missing: x${NOTHING}y is xy", "Missing: xy is xy"),
+ ("Multiple: $$ $FOO $BAR ${FOO}", "Multiple: $ fooey xyzzy fooey"),
+ ("Ill-formed: ${%5} ${{HI}} ${", "Ill-formed: ${%5} ${{HI}} ${"),
+ ("Strict: ${FOO?} is there", "Strict: fooey is there"),
+ ("Defaulted: ${WUT-missing}!", "Defaulted: missing!"),
+])
+def test_substitute_variables(before, after):
+ assert substitute_variables(before, VARS) == after
+
+@pytest.mark.parametrize("text", [
+ "Strict: ${NOTHING?} is an error",
+])
+def test_substitute_variables_errors(text):
+ with pytest.raises(CoverageException) as exc_info:
+ substitute_variables(text, VARS)
+ assert text in str(exc_info.value)
+ assert "Variable NOTHING is undefined" in str(exc_info.value)