test_source.py

Overview

The `test_source.py` file contains an extensive set of unit tests and utility functions designed to verify and demonstrate the functionality of the `Source` and related classes from the `_pytest._code` module. These classes provide sophisticated mechanisms for retrieving, parsing, and manipulating Python source code programmatically.

Primarily, the file tests how source code can be extracted from functions, methods, classes, and raw code strings, and how these sources can be indexed, stripped, deindented, and segmented into logical code statements. It also tests edge cases such as multiline strings, comments, decorators, single-line constructs, and error handling scenarios.

This test suite is critical for ensuring that pytest's internal source code handling features work correctly, which in turn supports accurate traceback reporting, source code introspection, and test reporting.


Detailed Explanation of Classes and Functions

Functions

test_source_str_function() -> None

test_source_from_function() -> None

test_source_from_method() -> None

test_source_from_lines() -> None

test_source_from_inner_function() -> None

test_source_strips() -> None

test_source_strip_multiline() -> None


Class TestAccesses


Class TestSourceParsing


Other Key Functional Tests


Classes Testing Specific Python Constructs


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Class and Function Structure

flowchart TD
    A[Tests in test_source.py]

    A --> B[test_source_str_function()]
    A --> C[test_source_from_function()]
    A --> D[test_source_from_method()]
    A --> E[test_source_from_lines()]
    A --> F[test_source_from_inner_function()]
    A --> G[test_source_strips()]
    A --> H[test_source_strip_multiline()]
    A --> I[TestAccesses]
    A --> J[TestSourceParsing]
    A --> K[test_getstartingblock_singleline()]
    A --> L[test_getline_finally()]
    A --> M[test_getfuncsource_dynamic()]
    A --> N[test_getfuncsource_with_multiline_string()]
    A --> O[test_deindent()]
    A --> P[test_source_of_class_at_eof_without_newline()]
    A --> Q[test_source_fallback()]
    A --> R[test_findsource_fallback()]
    A --> S[test_findsource()]
    A --> T[test_getfslineno()]
    A --> U[test_code_of_object_instance_with_call()]
    A --> V[getstatement()]
    A --> W[TestTry]
    A --> X[TestTryFinally]
    A --> Y[TestIf]
    A --> Z[Other test functions (comments, semicolon, decorators, etc.)]

    subgraph TestAccesses
        I1[setup_class()]
        I2[test_getrange()]
        I3[test_getrange_step_not_supported()]
        I4[test_getline()]
        I5[test_len()]
        I6[test_iter()]
    end

    subgraph TestSourceParsing
        J1[setup_class()]
        J2[test_getstatement()]
        J3[test_getstatementrange_triple_quoted()]
        J4[test_getstatementrange_within_constructs()]
        J5[test_getstatementrange_bug()]
        J6[test_getstatementrange_bug2()]
        J7[test_getstatementrange_ast_issue58()]
        J8[test_getstatementrange_out_of_bounds_py3()]
        J9[test_getstatementrange_with_syntaxerror_issue7()]
    end

    I --> I1
    I --> I2
    I --> I3
    I --> I4
    I --> I5
    I --> I6

    J --> J1
    J --> J2
    J --> J3
    J --> J4
    J --> J5
    J --> J6
    J --> J7
    J --> J8
    J --> J9

Summary

`test_source.py` is a highly comprehensive test suite validating the extraction, parsing, and handling of Python source code by pytest's internal utilities. It covers a wide range of real-world scenarios and edge cases, ensuring that pytest can reliably introspect and manipulate source code for improved test reporting, debugging, and dynamic behavior. The tests leverage Python introspection capabilities and robust fallback mechanisms to maximize reliability across diverse environments and code structures.