
"""
Comprehensive tests for Python table formatter.
Tests cover all formatting methods, Python-specific features, and edge cases.
"""

import pytest
from typing import Any, Dict, List

from tree_sitter_analyzer.formatters.python_formatter import PythonTableFormatter


class TestPythonTableFormatterBasic:
    """Test basic Python table formatter functionality."""

    @pytest.fixture
    def formatter(self):
        """Create a Python table formatter instance."""
        return PythonTableFormatter()

    @pytest.fixture
    def sample_python_data(self):
        """Sample Python analysis data."""
        return {
            "file_path": "src/calculator.py",
            "language": "python",
            "classes": [
                {
                    "name": "Calculator",
                    "type": "class",
                    "visibility": "public",
                    "line_range": {"start": 10, "end": 50},
                    "docstring": "A simple calculator class for basic arithmetic operations."
                }
            ],
            "functions": [
                {
                    "name": "add",
                    "visibility": "public",
                    "line_range": {"start": 15, "end": 20},
                    "parameters": [
                        {"name": "self", "type": "Calculator"},
                        {"name": "a", "type": "int"},
                        {"name": "b", "type": "int"}
                    ],
                    "return_type": "int",
                    "complexity_score": 1,
                    "docstring": "Add two numbers together.",
                    "is_async": False,
                    "decorators": []
                },
                {
                    "name": "divide",
                    "visibility": "public",
                    "line_range": {"start": 25, "end": 35},
                    "parameters": [
                        {"name": "self", "type": "Calculator"},
                        {"name": "a", "type": "float"},
                        {"name": "b", "type": "float"}
                    ],
                    "return_type": "float",
                    "complexity_score": 3,
                    "docstring": "Divide two numbers with error handling.",
                    "is_async": False,
                    "decorators": []
                }
            ],
            "methods": [
                {
                    "name": "add",
                    "visibility": "public",
                    "line_range": {"start": 15, "end": 20},
                    "parameters": [
                        {"name": "self", "type": "Calculator"},
                        {"name": "a", "type": "int"},
                        {"name": "b", "type": "int"}
                    ],
                    "return_type": "int",
                    "complexity_score": 1,
                    "docstring": "Add two numbers together.",
                    "is_async": False,
                    "decorators": []
                }
            ],
            "fields": [
                {
                    "name": "precision",
                    "type": "int",
                    "visibility": "private",
                    "line_range": {"start": 12, "end": 12},
                    "modifiers": ["class_variable"],
                    "javadoc": "Precision for calculations"
                }
            ],
            "imports": [
                {
                    "name": "math",
                    "module_name": "",
                    "raw_text": "import math"
                },
                {
                    "name": "sqrt",
                    "module_name": "math",
                    "raw_text": "from math import sqrt"
                }
            ],
            "statistics": {
                "method_count": 2,
                "field_count": 1,
                "class_count": 1
            },
            "source_code": '"""Calculator module for basic arithmetic operations."""\n\nimport math\nfrom math import sqrt\n\nclass Calculator:\n    """A simple calculator class."""\n    precision = 2\n    \n    def add(self, a: int, b: int) -> int:\n        """Add two numbers."""\n        return a + b'
        }

    def test_formatter_initialization(self, formatter):
        """Test formatter initialization."""
        assert isinstance(formatter, PythonTableFormatter)
        assert hasattr(formatter, 'format')
        assert hasattr(formatter, '_format_full_table')
        assert hasattr(formatter, '_format_compact_table')

    def test_format_method_delegates_to_format_structure(self, formatter, sample_python_data):
        """Test that format method delegates to format_structure."""
        result = formatter.format(sample_python_data)
        
        assert isinstance(result, str)
        assert len(result) > 0
        assert "Calculator" in result

    def test_format_with_empty_data(self, formatter):
        """Test formatting with empty data."""
        empty_data = {}
        result = formatter.format(empty_data)
        
        assert isinstance(result, str)
        # Should handle empty data gracefully


class TestPythonTableFormatterFullFormat:
    """Test Python table formatter full format functionality."""

    @pytest.fixture
    def formatter(self):
        """Create a Python table formatter instance."""
        return PythonTableFormatter()

    def test_full_format_module_header(self, formatter):
        """Test full format module header generation."""
        data = {
            "file_path": "src/utils.py",
            "classes": [],
            "functions": [],
            "imports": []
        }
        
        result = formatter._format_full_table(data)
        
        assert "# Module: utils" in result

    def test_full_format_package_header(self, formatter):
        """Test full format package header generation."""
        data = {
            "file_path": "src/__init__.py",
            "classes": [],
            "functions": [],
            "imports": []
        }
        
        result = formatter._format_full_table(data)
        
        assert "# Package:" in result

    def test_full_format_script_header(self, formatter):
        """Test full format script header generation."""
        data = {
            "file_path": "main.py",
            "classes": [],
            "functions": [
                {
                    "name": "main",
                    "raw_text": "if __name__ == '__main__':\n    main()"
                }
            ],
            "imports": []
        }
        
        result = formatter._format_full_table(data)
        
        assert "# Script: main" in result

    def test_full_format_module_docstring(self, formatter):
        """Test full format module docstring extraction."""
        data = {
            "file_path": "calculator.py",
            "source_code": '"""Calculator module for basic arithmetic operations."""\n\nclass Calculator:\n    pass',
            "classes": [],
            "functions": [],
            "imports": []
        }
        
        result = formatter._format_full_table(data)
        
        assert "## Description" in result
        assert "Calculator module for basic arithmetic operations." in result

    def test_full_format_imports_section(self, formatter):
        """Test full format imports section."""
        data = {
            "file_path": "test.py",
            "classes": [],
            "functions": [],
            "imports": [
                {"raw_text": "import os"},
                {"raw_text": "from typing import List"},
                {"name": "json", "module_name": "", "raw_text": ""}  # Fallback case
            ]
        }
        
        result = formatter._format_full_table(data)
        
        assert "## Imports" in result
        assert "```python" in result
        assert "import os" in result
        assert "from typing import List" in result
        assert "import json" in result  # Fallback construction

    def test_full_format_multiple_classes(self, formatter):
        """Test full format with multiple classes."""
        data = {
            "file_path": "models.py",
            "classes": [
                {
                    "name": "User",
                    "type": "class",
                    "visibility": "public",
                    "line_range": {"start": 10, "end": 30}
                },
                {
                    "name": "Admin",
                    "type": "class",
                    "visibility": "public",
                    "line_range": {"start": 35, "end": 50}
                }
            ],
            "methods": [
                {
                    "name": "get_name",
                    "line_range": {"start": 15, "end": 18}
                },
                {
                    "name": "set_permissions",
                    "line_range": {"start": 40, "end": 45}
                }
            ],
            "fields": [
                {
                    "name": "username",
                    "line_range": {"start": 12, "end": 12}
                }
            ],
            "functions": [],
            "imports": []
        }
        
        result = formatter._format_full_table(data)
        
        assert "## Classes" in result
        assert "| Class | Type | Visibility | Lines | Methods | Fields |" in result
        assert "| User | class | public | 10-30 | 1 | 1 |" in result
        assert "| Admin | class | public | 35-50 | 1 | 0 |" in result

    def test_full_format_single_class(self, formatter):
        """Test full format with single class."""
        data = {
            "file_path": "calculator.py",
            "classes": [
                {
                    "name": "Calculator",
                    "type": "class",
                    "visibility": "public",
                    "line_range": {"start": 10, "end": 50}
                }
            ],
            "statistics": {
                "method_count": 3,
                "field_count": 2
            },
            "functions": [],
            "imports": []
        }
        
        result = formatter._format_full_table(data)
        
        assert "## Class Info" in result
        assert "| Property | Value |" in result
        assert "| Type | class |" in result
        assert "| Total Methods | 3 |" in result
        assert "| Total Fields | 2 |" in result

    def test_full_format_fields_section(self, formatter):
        """Test full format fields section."""
        data = {
            "file_path": "model.py",
            "classes": [],
            "functions": [],
            "imports": [],
            "fields": [
                {
                    "name": "username",
                    "type": "str",
                    "visibility": "private",
                    "modifiers": ["instance"],
                    "line_range": {"start": 15, "end": 15},
                    "javadoc": "User's login name"
                },
                {
                    "name": "MAX_USERS",
                    "type": "int",
                    "visibility": "public",
                    "modifiers": ["class", "constant"],
                    "line_range": {"start": 10, "end": 10},
                    "javadoc": "Maximum number of users allowed in the system"
                }
            ]
        }
        
        result = formatter._format_full_table(data)
        
        assert "## Fields" in result
        assert "| Name | Type | Vis | Modifiers | Line | Doc |" in result
        assert "| username | str |" in result
        assert "| MAX_USERS | int |" in result

    def test_full_format_methods_section(self, formatter):
        """Test full format methods section."""
        data = {
            "file_path": "service.py",
            "classes": [],
            "functions": [],
            "imports": [],
            "methods": [
                {
                    "name": "process_data",
                    "visibility": "public",
                    "line_range": {"start": 20, "end": 35},
                    "parameters": [
                        {"name": "self", "type": "Service"},
                        {"name": "data", "type": "List[str]"}
                    ],
                    "return_type": "Dict[str, Any]",
                    "complexity_score": 5,
                    "docstring": "Process input data and return results.",
                    "is_async": True,
                    "decorators": ["property", "cached"]
                }
            ]
        }
        
        result = formatter._format_full_table(data)
        
        assert "## Methods" in result
        assert "| Method | Signature | Vis | Lines | Cols | Cx | Decorators | Doc |" in result
        assert "process_data🔄" in result  # Async indicator
        assert "@property" in result


class TestPythonTableFormatterCompactFormat:
    """Test Python table formatter compact format functionality."""

    @pytest.fixture
    def formatter(self):
        """Create a Python table formatter instance."""
        return PythonTableFormatter()

    def test_compact_format_multiple_classes_header(self, formatter):
        """Test compact format header with multiple classes."""
        data = {
            "file_path": "models.py",
            "classes": [
                {"name": "User"},
                {"name": "Admin"}
            ],
            "statistics": {"method_count": 5, "field_count": 3}
        }
        
        result = formatter._format_compact_table(data)
        
        assert "# models.py" in result

    def test_compact_format_single_class_header(self, formatter):
        """Test compact format header with single class."""
        data = {
            "file_path": "calculator.py",
            "classes": [
                {"name": "Calculator"}
            ],
            "statistics": {"method_count": 3, "field_count": 1}
        }
        
        result = formatter._format_compact_table(data)
        
        assert "# Calculator" in result

    def test_compact_format_info_section(self, formatter):
        """Test compact format info section."""
        data = {
            "file_path": "test.py",
            "classes": [{"name": "Test"}],
            "statistics": {
                "method_count": 4,
                "field_count": 2
            }
        }
        
        result = formatter._format_compact_table(data)
        
        assert "## Info" in result
        assert "| Classes | 1 |" in result
        assert "| Methods | 4 |" in result
        assert "| Fields | 2 |" in result

    def test_compact_format_methods_section(self, formatter):
        """Test compact format methods section."""
        data = {
            "file_path": "service.py",
            "classes": [{"name": "Service"}],
            "statistics": {"method_count": 1, "field_count": 0},
            "methods": [
                {
                    "name": "execute",
                    "visibility": "public",
                    "line_range": {"start": 10, "end": 20},
                    "parameters": [
                        {"name": "self", "type": "Service"},
                        {"name": "command", "type": "str"}
                    ],
                    "return_type": "bool",
                    "complexity_score": 2,
                    "javadoc": "Execute a command and return success status."
                }
            ]
        }
        
        result = formatter._format_compact_table(data)
        
        assert "## Methods" in result
        assert "| Method | Sig | V | L | Cx | Doc |" in result
        assert "| execute |" in result
        assert "| 2 |" in result  # Complexity


class TestPythonTableFormatterMethodFormatting:
    """Test Python table formatter method formatting functionality."""

    @pytest.fixture
    def formatter(self):
        """Create a Python table formatter instance."""
        return PythonTableFormatter()

    def test_format_method_row_basic(self, formatter):
        """Test basic method row formatting."""
        method = {
            "name": "calculate",
            "visibility": "public",
            "line_range": {"start": 10, "end": 15},
            "parameters": [
                {"name": "self", "type": "Calculator"},
                {"name": "value", "type": "int"}
            ],
            "return_type": "float",
            "complexity_score": 2,
            "docstring": "Calculate result from input value.",
            "is_async": False,
            "decorators": []
        }
        
        result = formatter._format_method_row(method)
        
        assert "calculate" in result
        assert "🔓" in result  # Public visibility
        assert "10-15" in result
        assert "2" in result  # Complexity

    def test_format_method_row_async(self, formatter):
        """Test async method row formatting."""
        method = {
            "name": "fetch_data",
            "visibility": "public",
            "line_range": {"start": 20, "end": 30},
            "parameters": [{"name": "self", "type": "Service"}],
            "return_type": "Dict[str, Any]",
            "complexity_score": 3,
            "docstring": "Fetch data asynchronously.",
            "is_async": True,
            "decorators": []
        }
        
        result = formatter._format_method_row(method)
        
        assert "fetch_data🔄" in result  # Async indicator

    def test_format_method_row_private(self, formatter):
        """Test private method row formatting."""
        method = {
            "name": "_helper_method",
            "visibility": "private",
            "line_range": {"start": 50, "end": 55},
            "parameters": [{"name": "self", "type": "Service"}],
            "return_type": "None",
            "complexity_score": 1,
            "docstring": "Internal helper method.",
            "is_async": False,
            "decorators": []
        }
        
        result = formatter._format_method_row(method)
        
        assert "_helper_method" in result
        assert "🔒" in result  # Private visibility

    def test_format_method_row_magic(self, formatter):
        """Test magic method row formatting."""
        method = {
            "name": "__init__",
            "visibility": "public",
            "line_range": {"start": 5, "end": 10},
            "parameters": [
                {"name": "self", "type": "Calculator"},
                {"name": "precision", "type": "int"}
            ],
            "return_type": "None",
            "complexity_score": 1,
            "docstring": "Initialize calculator with precision.",
            "is_async": False,
            "decorators": []
        }
        
        result = formatter._format_method_row(method)
        
        assert "__init__" in result
        assert "✨" in result  # Magic method visibility

    def test_format_method_row_with_decorators(self, formatter):
        """Test method row formatting with decorators."""
        method = {
            "name": "cached_result",
            "visibility": "public",
            "line_range": {"start": 30, "end": 40},
            "parameters": [{"name": "self", "type": "Service"}],
            "return_type": "str",
            "complexity_score": 1,
            "docstring": "Get cached result.",
            "is_async": False,
            "decorators": ["property", "lru_cache"]
        }
        
        result = formatter._format_method_row(method)
        
        assert "@property" in result

    def test_format_method_row_fallback_line_numbers(self, formatter):
        """Test method row formatting with fallback line numbers."""
        method = {
            "name": "legacy_method",
            "visibility": "public",
            "start_line": 100,
            "end_line": 110,
            "parameters": [],
            "return_type": "None",
            "complexity_score": 1,
            "docstring": "Legacy method with old line format.",
            "is_async": False,
            "decorators": []
        }
        
        result = formatter._format_method_row(method)
        
        assert "100-110" in result


class TestPythonTableFormatterSignatures:
    """Test Python table formatter signature creation."""

    @pytest.fixture
    def formatter(self):
        """Create a Python table formatter instance."""
        return PythonTableFormatter()

    def test_create_compact_signature_basic(self, formatter):
        """Test basic compact signature creation."""
        method = {
            "parameters": [
                {"name": "self", "type": "Calculator"},
                {"name": "a", "type": "int"},
                {"name": "b", "type": "str"}
            ],
            "return_type": "bool"
        }
        
        result = formatter._create_compact_signature(method)
        
        assert result == "(Calculator,i,s):b"

    def test_create_compact_signature_complex_types(self, formatter):
        """Test compact signature with complex types."""
        method = {
            "parameters": [
                {"name": "data", "type": "List[str]"},
                {"name": "mapping", "type": "Dict[str, int]"},
                {"name": "optional", "type": "Optional[float]"}
            ],
            "return_type": "Union[str, None]"
        }
        
        result = formatter._create_compact_signature(method)
        
        assert "L[s]" in result
        assert "D[s,i]" in result
        assert "O[f]" in result

    def test_create_compact_signature_no_types(self, formatter):
        """Test compact signature with no type information."""
        method = {
            "parameters": [
                {"name": "param1"},
                {"name": "param2"}
            ],
            "return_type": "Any"
        }
        
        result = formatter._create_compact_signature(method)
        
        assert result == "(Any,Any):A"

    def test_format_python_signature_with_types(self, formatter):
        """Test Python signature formatting with types."""
        method = {
            "parameters": [
                {"name": "self", "type": "Calculator"},
                {"name": "value", "type": "int"},
                {"name": "precision", "type": "float"}
            ],
            "return_type": "Decimal"
        }
        
        result = formatter._format_python_signature(method)
        
        assert result == "(self: Calculator, value: int, precision: float) -> Decimal"

    def test_format_python_signature_without_types(self, formatter):
        """Test Python signature formatting without types."""
        method = {
            "parameters": [
                {"name": "self"},
                {"name": "value"}
            ],
            "return_type": ""
        }
        
        result = formatter._format_python_signature(method)
        
        assert result == "(self, value)"

    def test_format_python_signature_mixed_types(self, formatter):
        """Test Python signature formatting with mixed type information."""
        method = {
            "parameters": [
                {"name": "self", "type": "Service"},
                {"name": "data"},  # No type
                {"name": "callback", "type": "Callable[[str], bool]"}
            ],
            "return_type": "Any"
        }
        
        result = formatter._format_python_signature(method)
        
        assert "self: Service" in result
        assert "data," in result  # No type annotation
        assert "callback: Callable[[str], bool]" in result


class TestPythonTableFormatterTypeShortening:
    """Test Python table formatter type shortening functionality."""

    @pytest.fixture
    def formatter(self):
        """Create a Python table formatter instance."""
        return PythonTableFormatter()

    def test_shorten_basic_types(self, formatter):
        """Test shortening of basic Python types."""
        assert formatter._shorten_type("str") == "s"
        assert formatter._shorten_type("int") == "i"
        assert formatter._shorten_type("float") == "f"
        assert formatter._shorten_type("bool") == "b"
        assert formatter._shorten_type("None") == "N"
        assert formatter._shorten_type("Any") == "A"

    def test_shorten_collection_types(self, formatter):
        """Test shortening of collection types."""
        assert formatter._shorten_type("List") == "L"
        assert formatter._shorten_type("Dict") == "D"
        assert formatter._shorten_type("Optional") == "O"
        assert formatter._shorten_type("Union") == "U"

    def test_shorten_generic_types(self, formatter):
        """Test shortening of generic types."""
        assert formatter._shorten_type("List[str]") == "L[s]"
        assert formatter._shorten_type("List[int]") == "L[i]"
        assert formatter._shorten_type("Dict[str, int]") == "D[s,i]"
        assert formatter._shorten_type("Optional[str]") == "O[s]"

    def test_shorten_custom_types(self, formatter):
        """Test shortening of custom types."""
        assert formatter._shorten_type("CustomClass") == "Cus"
        assert formatter._shorten_type("VeryLongTypeName") == "Ver"
        assert formatter._shorten_type("ABC") == "ABC"  # Short enough

    def test_shorten_none_type(self, formatter):
        """Test shortening of None type."""
        assert formatter._shorten_type(None) == "Any"

    def test_shorten_non_string_type(self, formatter):
        """Test shortening of non-string types."""
        assert formatter._shorten_type(123) == "123"
        assert formatter._shorten_type([]) == "[]"


class TestPythonTableFormatterUtilities:
    """Test Python table formatter utility functions."""

    @pytest.fixture
    def formatter(self):
        """Create a Python table formatter instance."""
        return PythonTableFormatter()

    def test_extract_module_docstring_single_line(self, formatter):
        """Test extracting single-line module docstring."""
        data = {
            "source_code": '"""Single line module docstring."""\n\nclass Test:\n    pass'
        }
        
        result = formatter._extract_module_docstring(data)
        
        assert result == "Single line module docstring."

    def test_extract_module_docstring_multi_line(self, formatter):
        """Test extracting multi-line module docstring."""
        data = {
            "source_code": '"""Multi-line\nmodule docstring\nwith details."""\n\nclass Test:\n    pass'
        }
        
        result = formatter._extract_module_docstring(data)
        
        assert "Multi-line" in result
        assert "module docstring" in result
        assert "with details." in result

    def test_extract_module_docstring_single_quotes(self, formatter):
        """Test extracting module docstring with single quotes."""
        data = {
            "source_code": "'''Module docstring with single quotes.'''\n\nclass Test:\n    pass"
        }
        
        result = formatter._extract_module_docstring(data)
        
        assert result == "Module docstring with single quotes."

    def test_extract_module_docstring_none(self, formatter):
        """Test extracting module docstring when none exists."""
        data = {
            "source_code": "import os\n\nclass Test:\n    pass"
        }
        
        result = formatter._extract_module_docstring(data)
        
        assert result is None

    def test_extract_module_docstring_no_source(self, formatter):
        """Test extracting module docstring with no source code."""
        data = {}
        
        result = formatter._extract_module_docstring(data)
        
        assert result is None

    def test_get_python_visibility_symbol(self, formatter):
        """Test Python visibility symbol mapping."""
        assert formatter._get_python_visibility_symbol("public") == "🔓"
        assert formatter._get_python_visibility_symbol("private") == "🔒"
        assert formatter._get_python_visibility_symbol("protected") == "🔐"
        assert formatter._get_python_visibility_symbol("magic") == "✨"
        assert formatter._get_python_visibility_symbol("unknown") == "🔓"  # Default

    def test_format_decorators_empty(self, formatter):
        """Test formatting empty decorators list."""
        result = formatter._format_decorators([])
        
        assert result == "-"

    def test_format_decorators_important(self, formatter):
        """Test formatting important decorators."""
        decorators = ["property", "staticmethod", "custom"]
        result = formatter._format_decorators(decorators)
        
        assert "@property" in result
        assert "@staticmethod" in result

    def test_format_decorators_single(self, formatter):
        """Test formatting single decorator."""
        decorators = ["custom_decorator"]
        result = formatter._format_decorators(decorators)
        
        assert result == "@custom_decorator"

    def test_format_decorators_multiple_non_important(self, formatter):
        """Test formatting multiple non-important decorators."""
        decorators = ["custom1", "custom2", "custom3"]
        result = formatter._format_decorators(decorators)
        
        assert result == "@custom1 (+2)"


class TestPythonTableFormatterEdgeCases:
    """Test Python table formatter edge cases."""

    @pytest.fixture
    def formatter(self):
        """Create a Python table formatter instance."""
        return PythonTableFormatter()

    def test_format_with_missing_data_fields(self, formatter):
        """Test formatting with missing data fields."""
        data = {
            "file_path": "incomplete.py"
            # Missing classes, functions, imports, etc.
        }
        
        result = formatter._format_full_table(data)
        
        assert isinstance(result, str)
        assert "# Module: incomplete" in result

    def test_format_with_malformed_line_ranges(self, formatter):
        """Test formatting with malformed line ranges."""
        data = {
            "file_path": "test.py",
            "classes": [],
            "functions": [],
            "imports": [],
            "methods": [
                {
                    "name": "broken_method",
                    "line_range": {},  # Empty line range
                    "parameters": [],
                    "complexity_score": 0
                }
            ]
        }
        
        result = formatter._format_full_table(data)
        
        assert "broken_method" in result
        assert "0-0" in result  # Default line range

    def test_format_with_unicode_content(self, formatter):
        """Test formatting with Unicode content."""
        data = {
            "file_path": "unicode_test.py",
            "classes": [
                {
                    "name": "测试类",
                    "type": "class",
                    "visibility": "public",
                    "line_range": {"start": 1, "end": 10}
                }
            ],
            "functions": [],
            "imports": [],
            "methods": [
                {
                    "name": "处理数据",
                    "visibility": "public",
                    "line_range": {"start": 5, "end": 8},
                    "parameters": [{"name": "数据", "type": "str"}],
                    "return_type": "bool",
                    "complexity_score": 1,
                    "docstring": "处理输入数据并返回结果。"
                }
            ]
        }
        
        result = formatter._format_full_table(data)
        
        # Unicode characters are escaped in output
        assert "\u5904\u7406\u6570\u636e" in result  # 处理数据 method name
        assert "\u6570\u636e" in result  # 数据 parameter name

    def test_format_with_very_long_names(self, formatter):
        """Test formatting with very long names."""
        data = {
            "file_path": "long_names.py",
            "classes": [
                {
                    "name": "VeryLongClassNameThatExceedsNormalLengthLimits",
                    "type": "class",
                    "visibility": "public",
                    "line_range": {"start": 1, "end": 50}
                }
            ],
            "functions": [],
            "imports": [],
            "methods": [
                {
                    "name": "extremely_long_method_name_that_should_be_handled_gracefully",
                    "visibility": "public",
                    "line_range": {"start": 10, "end": 20},
                    "parameters": [
                        {"name": "very_long_parameter_name", "type": "VeryLongTypeName"}
                    ],
                    "return_type": "AnotherVeryLongTypeName",
                    "complexity_score": 1,
                    "docstring": "This is a very long docstring that should be truncated appropriately."
                }
            ]
        }
        
        result = formatter._format_full_table(data)
        
        # Long names may be truncated in output, check for partial matches
        assert "VeryLongClassNameThatExceedsNormalLengthLimits" in result or "Ver" in result
        assert "extremely_long_method_name_that_should_be_handled_gracefully" in result

    def test_format_with_empty_collections(self, formatter):
        """Test formatting with empty collections."""
        data = {
            "file_path": "empty.py",
            "classes": [],
            "functions": [],
            "imports": [],
            "methods": [],
            "fields": [],
            "statistics": {
                "method_count": 0,
                "field_count": 0,
                "class_count": 0
            }
        }
        
        result = formatter._format_full_table(data)
        
        assert "# Module: empty" in result
        # Should handle empty collections gracefully

    def test_format_with_none_values(self, formatter):
        """Test formatting with None values in data."""
        data = {
            "file_path": "test.py",
            "classes": [
                {
                    "name": None,
                    "type": None,
                    "visibility": None,
                    "line_range": None
                }
            ],
            "functions": [],
            "imports": [],
            "methods": [
                {
                    "name": None,
                    "visibility": None,
                    "line_range": None,
                    "parameters": None,
                    "return_type": None,
                    "complexity_score": None,
                    "docstring": None
                }
            ]
        }
        
        result = formatter._format_full_table(data)
        
        assert isinstance(result, str)
        # Should handle None values gracefully without crashing

    def test_format_performance_with_large_data(self, formatter):
        """Test formatting performance with large datasets."""
        # Create large dataset
        large_methods = []
        for i in range(100):
            large_methods.append({
                "name": f"method_{i}",
                "visibility": "public",
                "line_range": {"start": i * 10, "end": i * 10 + 5},
                "parameters": [
                    {"name": "self", "type": "TestClass"}
                ] + [{"name": f"param_{j}", "type": "str"} for j in range(5)],
                "return_type": "bool",
                "complexity_score": i % 10,
                "docstring": f"Method {i} documentation.",
                "is_async": i % 2 == 0,
                "decorators": [f"decorator_{j}" for j in range(3)]
            })
        
        data = {
            "file_path": "large_file.py",
            "classes": [{"name": "LargeClass", "type": "class", "visibility": "public"}],
            "functions": [],
            "imports": [],
            "methods": large_methods,
            "statistics": {
                "method_count": 100,
                "field_count": 0,
                "class_count": 1
            }
        }
        
        result = formatter._format_full_table(data)
        
        assert isinstance(result, str)
        assert len(result) > 1000  # Should generate substantial output
        assert "method_0" in result
        assert "method_99" in result