test_pcb_parser.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from __future__ import annotations
  2. import math
  3. import sys
  4. from pathlib import Path
  5. import pytest
  6. ROOT = Path(__file__).resolve().parents[2]
  7. VIEWER_ROOT = ROOT / "tools" / "board-viewer"
  8. FIXTURE = Path(__file__).parent / "fixtures" / "parser_edge_cases.kicad_pcb"
  9. sys.path.insert(0, str(VIEWER_ROOT))
  10. from board_viewer.pcb import parse_kicad_pcb, select_touchpoints # noqa: E402
  11. def test_parser_ignores_hidden_reference_placeholder_and_non_footprint_text() -> None:
  12. board = parse_kicad_pcb(FIXTURE)
  13. references = {footprint.reference for footprint in board.footprints}
  14. touchpoints = {footprint.reference for footprint in select_touchpoints(board)}
  15. assert references == {"CON1", "TP1"}
  16. assert "${REFERENCE}" not in references
  17. assert touchpoints == {"CON1", "TP1"}
  18. assert "CON5" not in touchpoints
  19. def test_board_outline_bounds_are_extracted_from_edge_cuts() -> None:
  20. board = parse_kicad_pcb(FIXTURE)
  21. assert board.bounds == {"x": 0.0, "y": -10.0, "width": 30.0, "height": 10.0}
  22. def test_front_pad_coordinates_include_footprint_rotation_and_negative_y() -> None:
  23. board = parse_kicad_pcb(FIXTURE)
  24. connector = board.footprint_by_reference("CON1")
  25. pin = connector.pad_by_number("1")
  26. assert connector.component_value == "Fixture:Front"
  27. assert connector.x == 10
  28. assert connector.y == -5
  29. assert connector.rotation == 90
  30. assert connector.side == "front"
  31. assert pin.x == pytest.approx(11)
  32. assert pin.y == pytest.approx(-7)
  33. assert pin.rotation == pytest.approx(30)
  34. assert pin.net == "RS232_TX2"
  35. def test_back_pad_coordinates_follow_kicad_board_transform_without_extra_mirroring() -> None:
  36. board = parse_kicad_pcb(FIXTURE)
  37. testpoint = board.footprint_by_reference("TP1")
  38. pin = testpoint.pad_by_number("1")
  39. assert testpoint.side == "back"
  40. assert pin.x == pytest.approx(23)
  41. assert pin.y == pytest.approx(8)
  42. assert pin.rotation == pytest.approx(30)
  43. def test_unnumbered_zero_size_np_thru_hole_is_mechanical_and_finite() -> None:
  44. board = parse_kicad_pcb(FIXTURE)
  45. connector = board.footprint_by_reference("CON1")
  46. hole = next(pad for pad in connector.pads if pad.number == "")
  47. assert hole.mechanical is True
  48. assert hole.electrical is False
  49. assert hole.width == 0
  50. assert hole.height == 0
  51. assert hole.drill_width == 1
  52. assert hole.drill_height == 1
  53. assert all(math.isfinite(value) for value in connector.bounds.values())
  54. assert connector.bounds["width"] > 0
  55. assert connector.bounds["height"] > 0