"""Tests para format_table_to_markdown."""

import sys
import os

sys.path.insert(0, os.path.dirname(__file__))
from format_table_to_markdown import format_table_to_markdown


def test_tabla_normal():
    rows = [["Name", "Age", "City"], ["Alice", "30", "Madrid"], ["Bob", "25", "Berlin"]]
    result = format_table_to_markdown(rows)
    assert "| Name  | Age | City   |" in result
    assert "| ---   | --- | ---    |" in result or "---" in result
    assert "| Alice | 30  | Madrid |" in result
    assert "| Bob   | 25  | Berlin |" in result


def test_tabla_con_celdas_vacias():
    rows = [["A", "B"], ["", "x"], ["y", ""]]
    result = format_table_to_markdown(rows)
    assert "|" in result
    lines = result.split("\n")
    assert len(lines) == 4  # header + separator + 2 data rows


def test_tabla_con_1_fila():
    rows = [["Solo", "Row"]]
    result = format_table_to_markdown(rows)
    lines = result.split("\n")
    # header + separator (no data rows)
    assert len(lines) == 2
    assert "Solo" in lines[0]
    assert "---" in lines[1]


def test_tabla_vacia():
    result = format_table_to_markdown([])
    assert result == ""


def test_celdas_con_pipes():
    rows = [["Header"], ["cell|with|pipes"]]
    result = format_table_to_markdown(rows)
    assert "\\|" in result


def test_sin_header():
    rows = [["A", "B"], ["C", "D"]]
    result = format_table_to_markdown(rows, has_header=False)
    assert "---" not in result
    lines = result.split("\n")
    assert len(lines) == 2


if __name__ == "__main__":
    test_tabla_normal()
    test_tabla_con_celdas_vacias()
    test_tabla_con_1_fila()
    test_tabla_vacia()
    test_celdas_con_pipes()
    test_sin_header()
    print("All tests passed.")
