File: test_coverup_1.py

package info (click to toggle)
scalene 1.5.51-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,528 kB
  • sloc: cpp: 22,930; python: 13,403; javascript: 11,769; ansic: 817; makefile: 196; sh: 45
file content (39 lines) | stat: -rw-r--r-- 1,519 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
# file scalene/scalene_analysis.py:69-99
# lines [69, 70, 82, 83, 84, 87, 88, 90, 91, 92, 94, 95, 96, 97, 99]
# branches ['87->88', '87->99', '88->90', '88->94', '90->87', '90->91', '91->90', '91->92', '94->87', '94->95', '96->87', '96->97']

import pytest
from scalene.scalene_analysis import ScaleneAnalysis
from unittest.mock import patch

@pytest.fixture
def cleanup_imports():
    # Fixture to clean up sys.modules after the test
    import sys
    before = set(sys.modules.keys())
    yield
    after = set(sys.modules.keys())
    for extra in after - before:
        del sys.modules[extra]

def test_get_native_imported_modules(cleanup_imports):
    # Mock the is_native method to control which modules are considered native
    with patch.object(ScaleneAnalysis, 'is_native', return_value=True):
        source_code = """
import math
import os
from sys import path
"""
        expected_imports = ['import math', 'import os', 'from sys import path']
        actual_imports = ScaleneAnalysis.get_native_imported_modules(source_code)
        assert set(actual_imports) == set(expected_imports), "The list of native imports does not match the expected list."

    with patch.object(ScaleneAnalysis, 'is_native', return_value=False):
        source_code = """
import math
import os
from sys import path
"""
        expected_imports = []
        actual_imports = ScaleneAnalysis.get_native_imported_modules(source_code)
        assert actual_imports == expected_imports, "The list of native imports should be empty."