File: test_bytecode.py

package info (click to toggle)
python-coverage 7.8.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,188 kB
  • sloc: python: 31,123; ansic: 1,184; javascript: 773; makefile: 304; sh: 107; xml: 48
file content (49 lines) | stat: -rw-r--r-- 1,490 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

"""Tests for coverage.py's bytecode analysis."""

from __future__ import annotations

import dis

from textwrap import dedent

from tests.coveragetest import CoverageTest

from coverage import env
from coverage.bytecode import code_objects, op_set


class BytecodeTest(CoverageTest):
    """Tests for bytecode.py"""

    def test_code_objects(self) -> None:
        code = compile(
            dedent("""\
                def f(x):
                    def g(y):
                        return {z for z in range(10)}
                    def j():
                        return [z for z in range(10)]
                    return g(x)
                def h(x):
                    return x+1
                """),
            "<string>",
            "exec"
        )

        objs = list(code_objects(code))
        assert code in objs

        expected = {"<module>", "f", "g", "j", "h"}
        if env.PYVERSION < (3, 12):
            # Comprehensions were compiled as implicit functions in earlier
            # versions of Python.
            expected.update({"<setcomp>", "<listcomp>"})
        assert {c.co_name for c in objs} == expected

    def test_op_set(self) -> None:
        opcodes = op_set("LOAD_CONST", "NON_EXISTENT_OPCODE", "RETURN_VALUE")
        assert opcodes == {dis.opmap["LOAD_CONST"], dis.opmap["RETURN_VALUE"]}