Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
"""eeschema ground-truth parity for the connectivity core (SPEC §3.3/§3.4).
Fixtures are authored programmatically — through the writers API wherever the
writer can express them, and through a tiny textual builder for the one shape
the writer refuses to produce (a T-touch WITHOUT its junction dot;
``auto_junctions`` always inserts the dot). Each fixture is exported through a
real ``kicad-cli sch export netlist`` and the resulting net -> (ref, pin)
partition is asserted set-equal to what ``netbuild.build_nets`` derives from
the akcli reader. Tests needing the binary skip when no kicad-cli is
installed; the transform truth-table tests always run (the table itself was
established with kicad-cli 10.0.4 and is restated here as data).
Ground-truth verdicts these tests lock (kicad-cli 10.0.4):
* **Transform**: a file angle of +90 rotates a symbol counter-clockwise ON
SCREEN — ``(x, y) -> (y, -x)`` in the +Y-down frame — then the mirror is
applied (``x``: negate Y, ``y``: negate X). Neither of akcli's two former
transforms matched: the writer rotated the other way, the reader
mirror-then-rotated.
* **Label scope**: a local label DOES merge with a same-name global label or
power port anchored on the SAME sheet even when physically disconnected
(netbuild rule 5b), and NEVER merges across sheets (a child's local ``X``
netlists as ``/child/X``). Name comparison here is sheet-path-aware: an
eeschema net ``/X`` is the root-sheet-local X and is never collapsed onto a
global ``X``.
* **T-touch**: a wire END on another wire's mid-span connects ONLY through an
explicit junction node; shared endpoints (any count) connect bare.
* **Pin mid-span**: a pin tip on a wire's interior connects only via a
junction; a label connects anywhere along the wire.
"""
from __future__ import annotations
import json
import subprocess
import uuid as _uuid
from pathlib import Path
import pytest
from akcli import config as config_mod
from akcli import model, units
from akcli.checks import nets as netcheck
from akcli.drivers import kicad_cli as kicad_cli_driver
from akcli.readers import kicad as kreader
from akcli.readers import sexpr
from akcli.writers import geometry
from akcli.writers import kicad as kw
# --------------------------------------------------------------------------- #
# kicad-cli discovery — the PRODUCT ladder (``KICAD_CLI`` env -> PATH -> known
# install locations), not a private copy. A second, divergent probe here meant
# "the ground truth ran" and "akcli can find KiCad" were different questions:
# these tests could sit skipped on a machine the product happily drives, or
# (worse) run against a binary the product would never pick.
# --------------------------------------------------------------------------- #
KICAD_CLI = kicad_cli_driver.find()
needs_kicad = pytest.mark.skipif(
KICAD_CLI is None, reason="kicad-cli not installed (runs in the CI KiCad job)"
)
def _export_nets(path: Path) -> dict[str, frozenset[tuple[str, str]]]:
"""eeschema's netlist for ``path``: {net_name: {(ref, pin), ...}}."""
out = path.with_suffix(".net")
proc = subprocess.run(
[KICAD_CLI, "sch", "export", "netlist", "--output", str(out), str(path)],
capture_output=True, text=True, timeout=120,
)
assert proc.returncode == 0, f"kicad-cli failed: {proc.stderr}"
root = sexpr.parse(out.read_text(encoding="utf-8"))
nets: dict[str, frozenset[tuple[str, str]]] = {}
for net in root.find("nets").find_all("net"):
name = net.find("name").children[1].value
nodes = frozenset(
(n.find("ref").children[1].value, n.find("pin").children[1].value)
for n in net.find_all("node")
)
if nodes:
nets[name] = nodes
return nets
def _ee_partition(netmap: dict) -> set[frozenset]:
return set(netmap.values())
def _akcli_partition(sch: model.Schematic) -> set[frozenset]:
"""akcli nets as a membership partition, eeschema-comparable.
Power symbols (``#``-prefixed refs) are dropped — eeschema excludes them
from netlist export — and nets left empty by that drop vanish with them.
"""
out: set[frozenset] = set()
for net in sch.nets:
members = frozenset(m for m in net.members if not m[0].startswith("#"))
if members:
out.add(members)
return out
def _akcli_named(sch: model.Schematic) -> dict[str, frozenset]:
return {
n.name: frozenset(m for m in n.members if not m[0].startswith("#"))
for n in sch.nets if n.is_named
}
# --------------------------------------------------------------------------- #
# shared fixture library: XF (one asymmetric pin), RR (2-pin R), power:+3V3
# --------------------------------------------------------------------------- #
_LIB = """\
(kicad_symbol_lib (version 20231120) (generator akcli_test)
(symbol "XF" (pin_numbers (hide yes)) (pin_names (offset 0))
(exclude_from_sim no) (in_bom yes) (on_board yes)
(property "Reference" "X" (at 0 0 0) (effects (font (size 1.27 1.27))))
(property "Value" "XF" (at 0 0 0) (effects (font (size 1.27 1.27))))
(symbol "XF_1_1"
(pin passive line (at 2.54 5.08 270) (length 1.27)
(name "~" (effects (font (size 1.27 1.27))))
(number "1" (effects (font (size 1.27 1.27)))))))
(symbol "RR" (pin_numbers (hide yes)) (pin_names (offset 0))
(exclude_from_sim no) (in_bom yes) (on_board yes)
(property "Reference" "R" (at 0 0 0) (effects (font (size 1.27 1.27))))
(property "Value" "RR" (at 0 0 0) (effects (font (size 1.27 1.27))))
(symbol "RR_1_1"
(pin passive line (at 0 3.81 270) (length 1.27)
(name "~" (effects (font (size 1.27 1.27))))
(number "1" (effects (font (size 1.27 1.27)))))
(pin passive line (at 0 -3.81 90) (length 1.27)
(name "~" (effects (font (size 1.27 1.27))))
(number "2" (effects (font (size 1.27 1.27)))))))
(symbol "+3V3" (power) (pin_names (offset 0))
(exclude_from_sim no) (in_bom yes) (on_board yes)
(property "Reference" "#PWR" (at 0 0 0) (effects (font (size 1.27 1.27)) (hide yes)))
(property "Value" "+3V3" (at 0 0 0) (effects (font (size 1.27 1.27))))
(symbol "+3V3_1_1"
(pin power_in line (at 0 0 90) (length 0)
(name "+3V3" (effects (font (size 1.27 1.27))))
(number "1" (effects (font (size 1.27 1.27)))))))
)
"""
def _oplist(*ops):
return {"protocol_version": 1, "target_format": "kicad", "ops": list(ops)}
def _seed(d: Path, name: str) -> Path:
p = d / name
p.write_text(
f'(kicad_sch (version 20231120) (generator "akcli") '
f'(uuid "{_uuid.uuid4()}") (paper "A4"))\n'
)
return p
def _draw(d: Path, name: str, *ops) -> Path:
lib = d / "parity.kicad_sym"
if not lib.exists():
lib.write_text(_LIB)
tgt = _seed(d, name)
# add_sheet refuses a child file that does not exist yet (a dangling sheet
# bricks every later nets/check/render), so stub each child first — the
# fixtures that care overwrite it with real content afterwards.
for op in ops:
if op.get("op") == "add_sheet" and not (d / str(op["file"])).exists():
_seed(d, str(op["file"]))
results = kw.apply(_oplist(*ops), str(tgt), apply=True, sources=[str(lib)])
bad = [r for r in results if r.status != "ok"]
assert not bad, f"writer refused fixture ops: {bad[0].error_code}: {bad[0].message}"
return tgt
# --------------------------------------------------------------------------- #
# (2) transform truth table — kicad-cli-established, restated as data so the
# 12-combo lock runs even where KiCad is not installed.
# Library pin local (+100, +200) mil (+Y up); values are world offsets from the
# instance origin in the +Y-down frame.
# --------------------------------------------------------------------------- #
_XF_PIN_UP = (100, 200)
TRUTH = {
(0, "none"): (100, -200), (0, "x"): (100, 200), (0, "y"): (-100, -200),
(90, "none"): (-200, -100), (90, "x"): (-200, 100), (90, "y"): (200, -100),
(180, "none"): (-100, 200), (180, "x"): (-100, -200), (180, "y"): (100, 200),
(270, "none"): (200, 100), (270, "x"): (200, -100), (270, "y"): (-200, 100),
}
_COMBOS = sorted(TRUTH)
@pytest.mark.parametrize("rot,mirror", _COMBOS)
def test_transform_point_matches_eeschema_truth(rot, mirror):
flipped = (_XF_PIN_UP[0], -_XF_PIN_UP[1]) # +Y up -> +Y down
assert geometry.transform_point(flipped, rot, mirror) == TRUTH[(rot, mirror)]
@pytest.mark.parametrize("rot,mirror", _COMBOS)
def test_reader_pin_world_matches_eeschema_truth(rot, mirror):
ex, ey = TRUTH[(rot, mirror)]
wx, wy = kreader._pin_world(_XF_PIN_UP[0], _XF_PIN_UP[1], 5000, 4000, rot, mirror)
assert (wx, wy) == (5000 + ex, 4000 + ey)
@pytest.mark.parametrize("rot,mirror", _COMBOS)
def test_writer_pin_world_matches_eeschema_truth(rot, mirror):
sym = model.SymbolDef(
name="XF", lib_id="XF",
pins=[model.Pin(number="1", name=None, x_mil=100, y_mil=200)],
)
inst = model.Component(
designator="X1", library_ref="XF", x_mil=5000, y_mil=4000,
rotation=rot, mirror=mirror,
)
ex, ey = TRUTH[(rot, mirror)]
assert geometry.pin_world(sym, inst, sym.pins[0]) == (
units.mil_to_nm(5000 + ex), units.mil_to_nm(4000 + ey))
# --------------------------------------------------------------------------- #
# (a) pin world-coord matrix, arbitrated by a real eeschema netlist
# --------------------------------------------------------------------------- #
@pytest.fixture(scope="module")
def transform_parity(tmp_path_factory):
"""Writer-authored 12-combo sheet + its eeschema netlist."""
d = tmp_path_factory.mktemp("parity_xform")
ops = []
for i, (rot, mirror) in enumerate(_COMBOS):
cx, cy = 2000 + (i % 4) * 1500, 2000 + (i // 4) * 2000
ex, ey = TRUTH[(rot, mirror)]
op = {"op": "place_component", "lib_id": "XF", "designator": f"X{i}",
"x_mil": cx, "y_mil": cy, "rotation": rot}
if mirror != "none":
op["mirror"] = mirror
ops.append(op)
# anchor RR whose pin 1 sits 300 mil below the transformed XF pin
ops.append({"op": "place_component", "lib_id": "RR",
"designator": f"A{i}",
"x_mil": cx + ex, "y_mil": cy + ey + 450})
ops.append({"op": "add_wire", "vertices": [f"X{i}.1", f"A{i}.1"]})
tgt = _draw(d, "xform.kicad_sch", *ops)
return _export_nets(tgt), kreader.read_sch(tgt)
@needs_kicad
@pytest.mark.parametrize("rot,mirror", _COMBOS)
def test_pin_matrix_combo_connects_in_eeschema(transform_parity, rot, mirror):
"""eeschema must see the wire the writer drew to X{i}.1 land ON the pin."""
ee, _sch = transform_parity
i = _COMBOS.index((rot, mirror))
expected = frozenset({(f"X{i}", "1"), (f"A{i}", "1")})
assert expected in _ee_partition(ee), (
f"rot={rot} mirror={mirror}: eeschema did not connect the pin "
f"(transform diverges from eeschema)")
@needs_kicad
def test_pin_matrix_partition_parity(transform_parity):
ee, sch = transform_parity
assert _akcli_partition(sch) == _ee_partition(ee)
# --------------------------------------------------------------------------- #
# (b) label scope matrix
# --------------------------------------------------------------------------- #
@pytest.fixture(scope="module")
def scope_parity(tmp_path_factory):
d = tmp_path_factory.mktemp("parity_scope")
def stub(i, ref):
"""R at column i with a wire stub up from pin 1."""
x = 1000 * i
return (
{"op": "place_component", "lib_id": "RR", "designator": ref,
"x_mil": x, "y_mil": 2000},
{"op": "add_wire", "vertices": [f"{ref}.1", [x, 1700]]},
)
ops = [
# SCOPE1: DISCONNECTED local vs global, same sheet, same name
*stub(1, "R1"),
{"op": "add_net_label", "name": "SCOPE1", "at": [1000, 1700],
"scope": "local", "orientation": 90},
*stub(2, "R2"),
{"op": "add_net_label", "name": "SCOPE1", "at": [2000, 1700],
"scope": "global", "orientation": 90},
# +3V3: DISCONNECTED local label vs power port, same sheet
*stub(3, "R3"),
{"op": "add_net_label", "name": "+3V3", "at": [3000, 1700],
"scope": "local", "orientation": 90},
{"op": "place_component", "lib_id": "RR", "designator": "R4",
"x_mil": 4000, "y_mil": 2000},
{"op": "place_power_port", "lib_id": "+3V3", "net_name": "+3V3",
"at": "R4.1"},
# SCOPE2: two DISCONNECTED locals, same sheet, same name
*stub(5, "R5"),
{"op": "add_net_label", "name": "SCOPE2", "at": [5000, 1700],
"scope": "local", "orientation": 90},
*stub(6, "R6"),
{"op": "add_net_label", "name": "SCOPE2", "at": [6000, 1700],
"scope": "local", "orientation": 90},
# SCOPE3: two DISCONNECTED globals
*stub(7, "R7"),
{"op": "add_net_label", "name": "SCOPE3", "at": [7000, 1700],
"scope": "global", "orientation": 90},
*stub(8, "R8"),
{"op": "add_net_label", "name": "SCOPE3", "at": [8000, 1700],
"scope": "global", "orientation": 90},
# SCOPE4: CONNECTED local (mid-span!) + global on one wire
{"op": "place_component", "lib_id": "RR", "designator": "R9",
"x_mil": 9000, "y_mil": 2000},
{"op": "add_wire", "vertices": ["R9.1", [9000, 1600]]},
{"op": "add_net_label", "name": "SCOPE4", "at": [9000, 1700],
"scope": "local", "orientation": 90},
{"op": "add_net_label", "name": "SCOPE4", "at": [9000, 1600],
"scope": "global", "orientation": 90},
]
tgt = _draw(d, "scope.kicad_sch", *ops)
return _export_nets(tgt), kreader.read_sch(tgt)
@needs_kicad
def test_scope_partition_parity(scope_parity):
ee, sch = scope_parity
assert _akcli_partition(sch) == _ee_partition(ee)
@needs_kicad
def test_scope_local_joins_same_sheet_global_even_disconnected(scope_parity):
"""Rule 5b's ground truth: eeschema merges, so must akcli."""
ee, sch = scope_parity
assert ee["SCOPE1"] == {("R1", "1"), ("R2", "1")}
assert "/SCOPE1" not in ee # the local name was absorbed
assert _akcli_named(sch)["SCOPE1"] == ee["SCOPE1"]
@needs_kicad
def test_scope_local_joins_same_sheet_power_net_even_disconnected(scope_parity):
ee, sch = scope_parity
assert ee["+3V3"] == {("R3", "1"), ("R4", "1")}
assert "/+3V3" not in ee
assert _akcli_named(sch)["+3V3"] == ee["+3V3"]
@needs_kicad
def test_scope_two_locals_merge_and_stay_sheet_local(scope_parity):
"""Same-sheet same-name locals are ONE net — and it is a LOCAL net:
eeschema names it '/SCOPE2'; normalization must never collapse that
onto a global 'SCOPE2'."""
ee, sch = scope_parity
assert ee["/SCOPE2"] == {("R5", "1"), ("R6", "1")}
assert "SCOPE2" not in ee
assert _akcli_named(sch)["SCOPE2"] == ee["/SCOPE2"]
@needs_kicad
def test_scope_two_globals_merge(scope_parity):
ee, sch = scope_parity
assert ee["SCOPE3"] == {("R7", "1"), ("R8", "1")}
assert _akcli_named(sch)["SCOPE3"] == ee["SCOPE3"]
@needs_kicad
def test_scope_connected_local_plus_global_takes_global_name(scope_parity):
"""Also locks the mid-span label connect: the local label sits on the
wire's interior, not an endpoint."""
ee, sch = scope_parity
assert ee["SCOPE4"] == {("R9", "1")}
assert "/SCOPE4" not in ee
assert _akcli_named(sch)["SCOPE4"] == ee["SCOPE4"]
def test_local_label_does_not_merge_across_sheets():
"""Cross-sheet half of the 5b verdict (kicad-cli-verified on a hierarchy:
a child's local '+3V3' netlists as '/child/+3V3', separate from the root
power net). Locked at the netbuild layer — the writer cannot author
hierarchical sheets."""
prims = model.NetPrimitives(
wires=[model.WireSeg(a=(1000, 1000), b=(2000, 1000), sheet="/child")],
pins=[
model.PinHandle(ref=("R21", "1"), at=(1000, 1000), sheet="/child"),
model.PinHandle(ref=("R11", "1"), at=(1000, 1000), sheet=""),
],
labels=[
model.NetLabel(at=(1500, 1000), text="+3V3", scope="local",
sheet="/child"),
model.NetLabel(at=(1000, 1000), text="+3V3", scope="power",
sheet=""),
],
)
from akcli.netbuild import build_nets
nets = build_nets(prims, t_midspan_connects=False)
assert sorted(sorted(n.members) for n in nets) == [
[("R11", "1")], [("R21", "1")]]
# --------------------------------------------------------------------------- #
# (c) junction / X-cross / T patterns
# --------------------------------------------------------------------------- #
@pytest.fixture(scope="module")
def junction_parity(tmp_path_factory):
d = tmp_path_factory.mktemp("parity_junction")
def r_at(ref, x, y, rot=0):
op = {"op": "place_component", "lib_id": "RR", "designator": ref,
"x_mil": x, "y_mil": y}
if rot:
op["rotation"] = rot
return op
# The two upper resistors are flipped 180 so their pin-2 body ends point
# AWAY from the vertical wire; a pin-2 tip on the wire's span would earn
# an auto-junction (eeschema-faithful) and legitimately join the net.
ops = [
# X-cross WITH an explicit junction: all four pins one net
r_at("R1", 1000, 2150), r_at("R2", 3000, 2150),
r_at("R3", 2000, 850, rot=180), r_at("R4", 2000, 3150),
{"op": "add_wire", "vertices": ["R1.1", "R2.1"]}, # y = 2000
{"op": "add_wire", "vertices": ["R3.1", "R4.1"]}, # x = 2000
{"op": "add_junction", "at": [2000, 2000]},
# X-cross WITHOUT a junction: two independent nets
r_at("R5", 5000, 2150), r_at("R6", 7000, 2150),
r_at("R7", 6000, 850, rot=180), r_at("R8", 6000, 3150),
{"op": "add_wire", "vertices": ["R5.1", "R6.1"]},
{"op": "add_wire", "vertices": ["R7.1", "R8.1"]},
# T: arm wire ends on the trunk's mid-span; auto_junctions inserts the
# dot the way eeschema's editor would -> all three pins one net
r_at("R9", 1000, 6150), r_at("R10", 3000, 6150),
r_at("R11", 2000, 7150),
{"op": "add_wire", "vertices": ["R9.1", "R10.1"]}, # y = 6000
{"op": "add_wire", "vertices": [[2000, 6000], "R11.1"]},
# no_connect on a pin: stays a single-pin net on both sides
r_at("R12", 5000, 6150),
{"op": "add_no_connect", "pin": "R12.1"},
]
tgt = _draw(d, "junction.kicad_sch", *ops)
return _export_nets(tgt), kreader.read_sch(tgt)
@needs_kicad
def test_junction_partition_parity(junction_parity):
ee, sch = junction_parity
assert _akcli_partition(sch) == _ee_partition(ee)
@needs_kicad
def test_x_cross_with_junction_merges(junction_parity):
ee, _ = junction_parity
assert frozenset({("R1", "1"), ("R2", "1"), ("R3", "1"), ("R4", "1")}) \
in _ee_partition(ee)
@needs_kicad
def test_x_cross_without_junction_does_not_merge(junction_parity):
ee, _ = junction_parity
part = _ee_partition(ee)
assert frozenset({("R5", "1"), ("R6", "1")}) in part
assert frozenset({("R7", "1"), ("R8", "1")}) in part
@needs_kicad
def test_t_with_auto_junction_merges(junction_parity):
ee, _ = junction_parity
assert frozenset({("R9", "1"), ("R10", "1"), ("R11", "1")}) \
in _ee_partition(ee)
@needs_kicad
def test_no_connect_pin_stays_single(junction_parity):
ee, sch = junction_parity
assert frozenset({("R12", "1")}) in _ee_partition(ee)
# and the reader carried the no-connect point for ERC suppression
nc = {(round(x), round(y)) for x, y in sch.no_erc_points}
assert (5000, 6000) in nc
# --------------------------------------------------------------------------- #
# (c') junction-less T / pin-mid-span — the shapes the writer refuses to
# author (auto_junctions would insert the dot), hand-rolled textually.
# --------------------------------------------------------------------------- #
_ROOT_UUID = "ab000000-0000-4000-8000-000000000000"
def _mm(mil: float) -> str:
s = f"{mil * 0.0254:.4f}".rstrip("0").rstrip(".")
return s or "0"
def _raw_sch(body: list[str]) -> str:
lib = "\n".join(
"\t" + line for line in _LIB.splitlines()[1:-1]
)
return (
f'(kicad_sch\n\t(version 20231120)\n\t(generator "eeschema")\n'
f'\t(uuid "{_ROOT_UUID}")\n\t(paper "A4")\n'
f"\t(lib_symbols\n{lib})\n" + "\n".join(body) + "\n)\n"
)
def _raw_symbol(n: int, ref: str, x: float, y: float) -> str:
return f"""\
\t(symbol (lib_id "RR") (at {_mm(x)} {_mm(y)} 0) (unit 1)
\t\t(exclude_from_sim no) (in_bom yes) (on_board yes) (dnp no)
\t\t(uuid "ab000000-0000-4000-8000-{n:012d}")
\t\t(property "Reference" "{ref}" (at 0 0 0) (effects (font (size 1.27 1.27))))
\t\t(property "Value" "RR" (at 0 0 0) (effects (font (size 1.27 1.27))))
\t\t(pin "1" (uuid "ab000000-0000-4000-8000-{n + 1:012d}"))
\t\t(pin "2" (uuid "ab000000-0000-4000-8000-{n + 2:012d}"))
\t\t(instances (project "parity"
\t\t\t(path "/{_ROOT_UUID}" (reference "{ref}") (unit 1)))))"""
def _raw_wire(n: int, a, b) -> str:
return (f'\t(wire (pts (xy {_mm(a[0])} {_mm(a[1])}) '
f'(xy {_mm(b[0])} {_mm(b[1])}))\n'
f'\t\t(stroke (width 0) (type default))\n'
f'\t\t(uuid "ab000000-0000-4000-8000-{n:012d}"))')
@pytest.fixture(scope="module")
def bare_t_parity(tmp_path_factory):
d = tmp_path_factory.mktemp("parity_bare_t")
body = [
# T WITHOUT a junction node: trunk R1.1--R2.1, arm ends mid-span
_raw_symbol(100, "R1", 1000, 1150), # pin1 (1000, 1000)
_raw_symbol(200, "R2", 3000, 1150), # pin1 (3000, 1000)
_raw_symbol(300, "R3", 2000, 2150), # pin1 (2000, 2000)
_raw_wire(400, (1000, 1000), (3000, 1000)),
_raw_wire(500, (2000, 1000), (2000, 2000)),
# pin tip on a wire's INTERIOR, no junction: R5.1 stays floating
_raw_symbol(600, "R4", 5000, 1150), # pin1 (5000, 1000) = wire end
_raw_symbol(700, "R5", 6000, 1150), # pin1 (6000, 1000) = mid-span
_raw_wire(800, (5000, 1000), (7000, 1000)),
]
p = d / "bare_t.kicad_sch"
p.write_text(_raw_sch(body))
return _export_nets(p), kreader.read_sch(p)
@needs_kicad
def test_bare_t_partition_parity(bare_t_parity):
ee, sch = bare_t_parity
assert _akcli_partition(sch) == _ee_partition(ee)
@needs_kicad
def test_bare_t_does_not_connect_in_eeschema(bare_t_parity):
"""The verdict that retired unconditional netbuild rule 3 for KiCad."""
ee, _ = bare_t_parity
part = _ee_partition(ee)
assert frozenset({("R1", "1"), ("R2", "1")}) in part
assert frozenset({("R3", "1")}) in part
@needs_kicad
def test_pin_midspan_without_junction_floats(bare_t_parity):
ee, _ = bare_t_parity
part = _ee_partition(ee)
assert frozenset({("R4", "1")}) in part
assert frozenset({("R5", "1")}) in part
# --------------------------------------------------------------------------- #
# grid config -> NET_OFF_GRID (metric grids first-class; exact integer nm)
# --------------------------------------------------------------------------- #
def _one_pin_sch(x_mil: float, y_mil: float) -> model.Schematic:
comp = model.Component(
designator="U1", library_ref="X", x_mil=x_mil, y_mil=y_mil,
pins=[model.Pin(number="1", name=None, x_mil=x_mil, y_mil=y_mil)],
)
return model.Schematic(
source_path="<mem>", source_format="kicad",
components=[comp], nets=[],
)
def test_grid_config_parses_mil_and_mm(tmp_path):
p = tmp_path / config_mod.CONFIG_FILENAME
p.write_text('[project]\ngrid = "1.27mm"\n')
assert config_mod.load_config(p).grid_nm == 1_270_000
p.write_text('[project]\ngrid = 25\n')
assert config_mod.load_config(p).grid_nm == 25 * units.NM_PER_MIL
p.write_text('[project]\ngrid = "0.5mm"\n')
assert config_mod.load_config(p).grid_nm == 500_000
def test_grid_config_rejects_junk(tmp_path):
from akcli.errors import AkcliError
p = tmp_path / config_mod.CONFIG_FILENAME
p.write_text('[project]\ngrid = "fifty"\n')
with pytest.raises(AkcliError):
config_mod.load_config(p)
p.write_text('[project]\ngrid = 0\n')
with pytest.raises(AkcliError):
config_mod.load_config(p)
def test_metric_grid_pin_off_default_but_on_metric_grid(tmp_path):
# 1.5 mm = 59.0551... mil: off the 50-mil grid, exactly on a 0.5 mm grid.
mil = units.nm_to_mil(units.mm_to_nm(1.5))
sch = _one_pin_sch(mil, mil)
assert any(f.code == netcheck.NET_OFF_GRID for f in netcheck.run(sch, None))
p = tmp_path / config_mod.CONFIG_FILENAME
p.write_text('[project]\ngrid = "0.5mm"\n')
cfg = config_mod.load_config(p)
assert not netcheck.run(sch, cfg)
def test_default_grid_pin_on_50mil_grid_is_clean():
assert not netcheck.run(_one_pin_sch(1050, 2500), None)
# --------------------------------------------------------------------------- #
# (d) hierarchical sheet authoring (add_sheet) — cross-sheet membership parity
# --------------------------------------------------------------------------- #
_CHILD_LIB = """\
(lib_symbols
(symbol "RR" (pin_numbers (hide yes)) (pin_names (offset 0))
(exclude_from_sim no) (in_bom yes) (on_board yes)
(property "Reference" "R" (at 0 0 0) (effects (font (size 1.27 1.27))))
(property "Value" "RR" (at 0 0 0) (effects (font (size 1.27 1.27))))
(symbol "RR_1_1"
(pin passive line (at 0 3.81 270) (length 1.27)
(name "~" (effects (font (size 1.27 1.27))))
(number "1" (effects (font (size 1.27 1.27)))))
(pin passive line (at 0 -3.81 90) (length 1.27)
(name "~" (effects (font (size 1.27 1.27))))
(number "2" (effects (font (size 1.27 1.27))))))))"""
def _write_child(d: Path, root_uuid: str, sheet_uuid: str) -> None:
"""Child sheet: R2 wired to a hierarchical_label 'NET1' (parent-facing)."""
child_path = f"/{root_uuid}/{sheet_uuid}"
(d / "child.kicad_sch").write_text(
'(kicad_sch (version 20231120) (generator "akcli")\n'
f' (uuid "{_uuid.uuid4()}") (paper "A4")\n'
+ _CHILD_LIB + "\n"
' (symbol (lib_id "RR") (at 50.8 50.8 0) (unit 1)\n'
f' (uuid "{_uuid.uuid4()}")\n'
' (property "Reference" "R2" (at 53 49 0) (effects (font (size 1.27 1.27))))\n'
' (property "Value" "RR" (at 53 51 0) (effects (font (size 1.27 1.27))))\n'
f' (pin "1" (uuid "{_uuid.uuid4()}"))\n'
f' (pin "2" (uuid "{_uuid.uuid4()}"))\n'
f' (instances (project "noname" (path "{child_path}" (reference "R2") (unit 1)))))\n'
' (wire (pts (xy 50.8 46.99) (xy 50.8 40.64)) (stroke (width 0) (type default))\n'
f' (uuid "{_uuid.uuid4()}"))\n'
' (hierarchical_label "NET1" (shape bidirectional) (at 50.8 40.64 90)\n'
' (effects (font (size 1.27 1.27)))\n'
f' (uuid "{_uuid.uuid4()}"))\n'
')\n'
)
@pytest.fixture(scope="module")
def sheet_parity(tmp_path_factory):
"""A root authored with add_sheet + a hand-written child; R1.1 crosses the
sheet-pin<->hierarchical-label boundary to reach the child's R2.1."""
d = tmp_path_factory.mktemp("parity_sheet")
tgt = _draw(
d, "root.kicad_sch",
{"op": "place_component", "lib_id": "RR", "designator": "R1",
"x_mil": 1000, "y_mil": 1200},
{"op": "add_sheet", "name": "child", "file": "child.kicad_sch",
"at": [2000, 1000], "size": [1000, 800],
"pins": [{"name": "NET1", "type": "bidirectional", "side": "left",
"offset_mil": 200}]},
{"op": "add_wire", "vertices": ["R1.1", [2000, 1200]]},
)
doc = sexpr.parse(tgt.read_text())
root_uuid = doc.find("uuid").children[1].value
sheet_uuid = doc.find("sheet").find("uuid").children[1].value
_write_child(d, root_uuid, sheet_uuid)
return tgt
@needs_kicad
def test_add_sheet_partition_parity(sheet_parity):
"""The full (ref, pin) partition matches eeschema across the sheet boundary."""
assert _ee_partition(_export_nets(sheet_parity)) == _akcli_partition(
kreader.read_sch(sheet_parity))
@needs_kicad
def test_add_sheet_crosses_sheet_pin_to_hier_label(sheet_parity):
"""R1.1 (root) and R2.1 (child) share one net through the sheet pin."""
ee = _export_nets(sheet_parity)
crossing = next(nodes for nodes in ee.values()
if ("R1", "1") in nodes)
assert ("R2", "1") in crossing
sch = kreader.read_sch(sheet_parity)
net = next(n for n in sch.nets if ("R1", "1") in n.members)
assert ("R2", "1") in net.members
# --------------------------------------------------------------------------- #
# (e) bus semantics, single sheet — writer-authored, kicad-cli-arbitrated.
# Verdicts locked (KiCad 10.x): a labeled rip joins the member named by the
# WIRE's label; an unlabeled rip floats; a plain label ON the bus selects
# nothing; a (bus_entry) conducts between its two ends when wires END there,
# but an entry end on a wire's MID-SPAN does not attach (no junction).
# --------------------------------------------------------------------------- #
def _bus_rip(ops, bus_x, y, ref, label=None):
"""entry at (bus_x, y) -> wire right -> RR pin 1 at the far end."""
fx, fy = bus_x + 100, y + 100
ops.append({"op": "add_bus_entry", "at": [bus_x, y]})
ops.append({"op": "add_wire", "vertices": [[fx, fy], [fx + 600, fy]]})
ops.append({"op": "place_component", "lib_id": "RR", "designator": ref,
"x_mil": fx + 600, "y_mil": fy + 150})
if label:
ops.append({"op": "add_net_label", "name": label, "at": [fx + 300, fy]})
@pytest.fixture(scope="module")
def bus_parity(tmp_path_factory):
d = tmp_path_factory.mktemp("parity_bus")
ops = []
# labeled bus D[0..7]: rips D3 (R1), D3 (R2), unlabeled (R3), D5 (R4)
ops.append({"op": "add_bus", "vertices": [[4000, 2000], [4000, 5000]]})
ops.append({"op": "add_net_label", "name": "D[0..7]", "at": [4000, 2200]})
_bus_rip(ops, 4000, 2500, "R1", "D3")
_bus_rip(ops, 4000, 3000, "R2", "D3")
_bus_rip(ops, 4000, 3500, "R3", None)
_bus_rip(ops, 4000, 4000, "R4", "D5")
# detached wire labeled D5 (R5) — merges with the D5 rip by name
ops.append({"op": "add_wire", "vertices": [[6000, 4100], [6600, 4100]]})
ops.append({"op": "add_net_label", "name": "D5", "at": [6000, 4100]})
ops.append({"op": "place_component", "lib_id": "RR", "designator": "R5",
"x_mil": 6600, "y_mil": 4250})
# entry between two wires, NO bus: conducts end<->end
ops.append({"op": "place_component", "lib_id": "RR", "designator": "R11",
"x_mil": 16000, "y_mil": 2750})
ops.append({"op": "add_wire", "vertices": [[16000, 2600], [16500, 2600]]})
ops.append({"op": "add_bus_entry", "at": [16500, 2600]})
ops.append({"op": "add_wire", "vertices": [[16600, 2700], [17100, 2700]]})
ops.append({"op": "place_component", "lib_id": "RR", "designator": "R12",
"x_mil": 17100, "y_mil": 2850})
# entry end on a wire's MID-SPAN (no junction): the rip floats
ops.append({"op": "place_component", "lib_id": "RR", "designator": "R21",
"x_mil": 18000, "y_mil": 2750})
ops.append({"op": "add_wire", "vertices": [[18000, 2600], [19000, 2600]]})
ops.append({"op": "place_component", "lib_id": "RR", "designator": "R23",
"x_mil": 19000, "y_mil": 2750})
ops.append({"op": "add_bus_entry", "at": [18500, 2600]})
ops.append({"op": "add_wire", "vertices": [[18600, 2700], [19100, 2700]]})
ops.append({"op": "place_component", "lib_id": "RR", "designator": "R22",
"x_mil": 19100, "y_mil": 2850})
tgt = _draw(d, "bus.kicad_sch", *ops)
# the mid-span verdict depends on NO junction at (18500, 2600)
assert "18500" not in "".join(
str(j) for j in sexpr.parse(tgt.read_text()).find_all("junction"))
return tgt
@needs_kicad
def test_bus_partition_parity(bus_parity):
assert _ee_partition(_export_nets(bus_parity)) == _akcli_partition(
kreader.read_sch(bus_parity))
@needs_kicad
def test_bus_labeled_rips_share_member_net(bus_parity):
ee = _export_nets(bus_parity)
assert ee["/D3"] == {("R1", "1"), ("R2", "1")}
assert ee["/D5"] == {("R4", "1"), ("R5", "1")}
named = _akcli_named(kreader.read_sch(bus_parity))
assert named["D3"] == ee["/D3"] and named["D5"] == ee["/D5"]
@needs_kicad
def test_bus_unlabeled_rip_floats(bus_parity):
part = _ee_partition(_export_nets(bus_parity))
assert frozenset({("R3", "1")}) in part
assert frozenset({("R3", "1")}) in _akcli_partition(kreader.read_sch(bus_parity))
@needs_kicad
def test_bus_entry_conducts_wire_to_wire(bus_parity):
expected = frozenset({("R11", "1"), ("R12", "1")})
assert expected in _ee_partition(_export_nets(bus_parity))
assert expected in _akcli_partition(kreader.read_sch(bus_parity))
@needs_kicad
def test_bus_entry_end_on_wire_midspan_floats(bus_parity):
for part in (_ee_partition(_export_nets(bus_parity)),
_akcli_partition(kreader.read_sch(bus_parity))):
assert frozenset({("R21", "1"), ("R23", "1")}) in part
assert frozenset({("R22", "1")}) in part
# --------------------------------------------------------------------------- #
# (f) bus semantics across sheets — raw-text hierarchy (the writer cannot put
# labels/buses on a child sheet). Verdicts locked (KiCad 10.x): a GLOBAL bus
# label merges same-member rips across sheets (D3), a LOCAL bus label never
# does (E1), a sheet-pin<->hierarchical-label bus port stitches parent and
# child (P1), and vector expansion is inclusive in either order (K[3..0]:
# both K3 and K0 resolve). Continuation: the root's labeled bus reaches the
# rip through a second, endpoint-joined segment.
# --------------------------------------------------------------------------- #
_BUS_ROOT_UUID = "cd000000-0000-4000-8000-000000000000"
_BUS_SHEET_UUID = "cd000000-0000-4000-8000-000000000999"
def _bus_uuid_counter():
n = [0]
def nxt():
n[0] += 1
return f"cd000000-0000-4000-8000-{n[0]:012d}"
return nxt
def _bus_hier_fixture(d: Path) -> Path:
U = _bus_uuid_counter()
def wire(a, b, tag="wire"):
return (f'({tag} (pts (xy {_mm(a[0])} {_mm(a[1])}) '
f'(xy {_mm(b[0])} {_mm(b[1])}))'
f' (stroke (width 0) (type default)) (uuid "{U()}"))')
def label(text, x, y, tag="label", extra=""):
return (f'({tag} "{text}" {extra}(at {_mm(x)} {_mm(y)} 0)'
f' (effects (font (size 1.27 1.27))) (uuid "{U()}"))')
def entry(x, y):
return (f'(bus_entry (at {_mm(x)} {_mm(y)}) (size 2.54 2.54)'
f' (stroke (width 0) (type default)) (uuid "{U()}"))')
def sym(ref, x, y, path):
return (
f'(symbol (lib_id "RR") (at {_mm(x)} {_mm(y)} 0) (unit 1)\n'
f' (exclude_from_sim no) (in_bom yes) (on_board yes) (dnp no)\n'
f' (uuid "{U()}")\n'
f' (property "Reference" "{ref}" (at 0 0 0)'
f' (effects (font (size 1.27 1.27))))\n'
f' (property "Value" "RR" (at 0 0 0)'
f' (effects (font (size 1.27 1.27))))\n'
f' (pin "1" (uuid "{U()}"))\n (pin "2" (uuid "{U()}"))\n'
f' (instances (project "busparity"'
f' (path "{path}" (reference "{ref}") (unit 1)))))'
)
def rip(body, bus_x, y, ref, lbl, path):
fx, fy = bus_x + 100, y + 100
body.append(entry(bus_x, y))
body.append(wire((fx, fy), (fx + 600, fy)))
if lbl:
body.append(label(lbl, fx + 300, fy))
body.append(sym(ref, fx + 600, fy + 150, path))
libblock = "(lib_symbols\n" + "\n".join(_LIB.splitlines()[1:-1]) + ")"
rootpath = f"/{_BUS_ROOT_UUID}"
childpath = f"/{_BUS_ROOT_UUID}/{_BUS_SHEET_UUID}"
rb: list[str] = []
# continuation: two endpoint-joined bus segments; global label on the
# FIRST, rip D3 off the SECOND
rb.append(wire((4000, 2000), (4000, 3000), tag="bus"))
rb.append(wire((4000, 3000), (4000, 5000), tag="bus"))
rb.append(label("D[0..7]", 4000, 2200, tag="global_label",
extra="(shape input) "))
rip(rb, 4000, 3500, "R1", "D3", rootpath)
rb.append(wire((8000, 2000), (8000, 4000), tag="bus"))
rb.append(label("E[0..3]", 8000, 2200))
rip(rb, 8000, 2500, "R5", "E1", rootpath)
rb.append(wire((11000, 2200), (11000, 3200), tag="bus"))
rb.append(wire((11000, 2200), (12000, 2200), tag="bus"))
rip(rb, 11000, 2700, "R3", "P1", rootpath)
rb.append(
f'(sheet (at {_mm(12000)} {_mm(2000)}) (size {_mm(1000)} {_mm(800)})\n'
f' (stroke (width 0.1524) (type solid)) (fill (color 0 0 0 0))\n'
f' (uuid "{_BUS_SHEET_UUID}")\n'
f' (property "Sheetname" "child" (at 0 0 0)'
f' (effects (font (size 1.27 1.27))))\n'
f' (property "Sheetfile" "child.kicad_sch" (at 0 0 0)'
f' (effects (font (size 1.27 1.27))))\n'
f' (pin "P[0..3]" bidirectional (at {_mm(12000)} {_mm(2200)} 180)\n'
f' (effects (font (size 1.27 1.27))) (uuid "{U()}"))\n'
f' (instances (project "busparity"'
f' (path "/{_BUS_ROOT_UUID}" (page "2")))))'
)
rb.append(wire((20000, 2000), (20000, 4000), tag="bus"))
rb.append(label("K[3..0]", 20000, 2200, tag="global_label",
extra="(shape input) "))
rip(rb, 20000, 2500, "R7", "K3", rootpath)
rip(rb, 20000, 3200, "R9", "K0", rootpath)
root = (f'(kicad_sch (version 20231120) (generator "eeschema")'
f' (uuid "{_BUS_ROOT_UUID}") (paper "A4")\n{libblock}\n'
+ "\n".join(rb) + "\n)\n")
(d / "bus_root.kicad_sch").write_text(root)
cb: list[str] = []
cb.append(wire((4000, 2000), (4000, 4000), tag="bus"))
cb.append(label("D[0..7]", 4000, 2200, tag="global_label",
extra="(shape input) "))
rip(cb, 4000, 2500, "R2", "D3", childpath)
cb.append(wire((8000, 2000), (8000, 4000), tag="bus"))
cb.append(label("E[0..3]", 8000, 2200))
rip(cb, 8000, 2500, "R6", "E1", childpath)
cb.append(wire((11000, 2200), (11000, 3200), tag="bus"))
cb.append(label("P[0..3]", 11000, 2200, tag="hierarchical_label",
extra="(shape bidirectional) "))
rip(cb, 11000, 2700, "R4", "P1", childpath)
cb.append(wire((20000, 2000), (20000, 4000), tag="bus"))
cb.append(label("K[3..0]", 20000, 2200, tag="global_label",
extra="(shape input) "))
rip(cb, 20000, 2500, "R8", "K3", childpath)
rip(cb, 20000, 3200, "R10", "K0", childpath)
child = (f'(kicad_sch (version 20231120) (generator "eeschema")'
f' (uuid "{U()}") (paper "A4")\n{libblock}\n'
+ "\n".join(cb) + "\n)\n")
(d / "child.kicad_sch").write_text(child)
return d / "bus_root.kicad_sch"
@pytest.fixture(scope="module")
def bus_hier_parity(tmp_path_factory):
d = tmp_path_factory.mktemp("parity_bus_hier")
return _bus_hier_fixture(d)
@needs_kicad
def test_bus_hier_partition_parity(bus_hier_parity):
assert _ee_partition(_export_nets(bus_hier_parity)) == _akcli_partition(
kreader.read_sch(bus_hier_parity))
@needs_kicad
def test_global_bus_label_merges_members_across_sheets(bus_hier_parity):
ee = _export_nets(bus_hier_parity)
assert ee["D3"] == {("R1", "1"), ("R2", "1")}
named = _akcli_named(kreader.read_sch(bus_hier_parity))
assert named["D3"] == ee["D3"]
@needs_kicad
def test_local_bus_label_never_crosses_sheets(bus_hier_parity):
ee = _export_nets(bus_hier_parity)
assert ee["/E1"] == {("R5", "1")}
assert ee["/child/E1"] == {("R6", "1")}
part = _akcli_partition(kreader.read_sch(bus_hier_parity))
assert frozenset({("R5", "1")}) in part
assert frozenset({("R6", "1")}) in part
@needs_kicad
def test_sheet_pin_bus_port_stitches_parent_child(bus_hier_parity):
ee = _export_nets(bus_hier_parity)
assert ee["/child/P1"] == {("R3", "1"), ("R4", "1")}
net = next(n for n in kreader.read_sch(bus_hier_parity).nets
if ("R3", "1") in n.members)
assert sorted(net.members) == [("R3", "1"), ("R4", "1")]
@needs_kicad
def test_reversed_vector_range_inclusive_both_ends(bus_hier_parity):
ee = _export_nets(bus_hier_parity)
assert ee["K3"] == {("R7", "1"), ("R8", "1")}
assert ee["K0"] == {("R9", "1"), ("R10", "1")}
named = _akcli_named(kreader.read_sch(bus_hier_parity))
assert named["K3"] == ee["K3"] and named["K0"] == ee["K0"]
# --------------------------------------------------------------------------- #
# (g) bus aliases — raw-text hierarchy (the writer cannot emit bus_alias or put
# labels/buses on a child sheet). Verdict locked (kicad-cli 10.0.4): a
# (bus_alias "NAME" (members ...)) has NO effect on the exported netlist. A bus
# labeled with the alias name behaves like a plain, member-less bus — the
# netlist is identical whether or not the alias is declared:
# * a rip labeled with a declared member keeps its own label's SCOPE (a local
# "EN" stays sheet-local; it does NOT globalize the way a group "{...}" or
# vector "A[0..3]" bus member does), so cross-sheet member rips never merge;
# * when the alias NAME is itself a vector ("A[0..3]"), the vector wins and
# the declared members are ignored.
# akcli matches for free: it expands vector labels and treats every non-vector
# label (alias names included) as member-less, and never reads (bus_alias ...).
# --------------------------------------------------------------------------- #
def _bus_alias_fixture(d: Path, alias_name: str, members: list[str],
root_rips, child_rips, *, declare_alias: bool = True) -> Path:
U = _bus_uuid_counter()
def wire(a, b, tag="wire"):
return (f'({tag} (pts (xy {_mm(a[0])} {_mm(a[1])}) '
f'(xy {_mm(b[0])} {_mm(b[1])}))'
f' (stroke (width 0) (type default)) (uuid "{U()}"))')
def label(text, x, y, tag="label", extra=""):
return (f'({tag} "{text}" {extra}(at {_mm(x)} {_mm(y)} 0)'
f' (effects (font (size 1.27 1.27))) (uuid "{U()}"))')
def entry(x, y):
return (f'(bus_entry (at {_mm(x)} {_mm(y)}) (size 2.54 2.54)'
f' (stroke (width 0) (type default)) (uuid "{U()}"))')
def sym(ref, x, y, path):
return (
f'(symbol (lib_id "RR") (at {_mm(x)} {_mm(y)} 0) (unit 1)\n'
f' (exclude_from_sim no) (in_bom yes) (on_board yes) (dnp no)\n'
f' (uuid "{U()}")\n'
f' (property "Reference" "{ref}" (at 0 0 0)'
f' (effects (font (size 1.27 1.27))))\n'
f' (property "Value" "RR" (at 0 0 0)'
f' (effects (font (size 1.27 1.27))))\n'
f' (pin "1" (uuid "{U()}"))\n (pin "2" (uuid "{U()}"))\n'
f' (instances (project "busalias"'
f' (path "{path}" (reference "{ref}") (unit 1)))))'
)
def rip(body, bus_x, y, ref, lbl, path):
fx, fy = bus_x + 100, y + 100
body.append(entry(bus_x, y))
body.append(wire((fx, fy), (fx + 600, fy)))
if lbl:
body.append(label(lbl, fx + 300, fy))
body.append(sym(ref, fx + 600, fy + 150, path))
def alias():
mem = " ".join(f'"{m}"' for m in members)
return f'(bus_alias "{alias_name}" (members {mem}))'
libblock = "(lib_symbols\n" + "\n".join(_LIB.splitlines()[1:-1]) + ")"
rootpath = f"/{_BUS_ROOT_UUID}"
childpath = f"/{_BUS_ROOT_UUID}/{_BUS_SHEET_UUID}"
rb: list[str] = []
if declare_alias:
rb.append(alias())
rb.append(wire((4000, 2000), (4000, 8000), tag="bus"))
rb.append(label(alias_name, 4000, 2200, tag="global_label",
extra="(shape input) "))
y = 2500
for ref, lbl in root_rips:
rip(rb, 4000, y, ref, lbl, rootpath)
y += 600
rb.append(
f'(sheet (at {_mm(14000)} {_mm(2000)}) (size {_mm(1000)} {_mm(800)})\n'
f' (stroke (width 0.1524) (type solid)) (fill (color 0 0 0 0))\n'
f' (uuid "{_BUS_SHEET_UUID}")\n'
f' (property "Sheetname" "child" (at 0 0 0)'
f' (effects (font (size 1.27 1.27))))\n'
f' (property "Sheetfile" "child.kicad_sch" (at 0 0 0)'
f' (effects (font (size 1.27 1.27))))\n'
f' (instances (project "busalias"'
f' (path "/{_BUS_ROOT_UUID}" (page "2")))))'
)
root = (f'(kicad_sch (version 20231120) (generator "eeschema")'
f' (uuid "{_BUS_ROOT_UUID}") (paper "A4")\n{libblock}\n'
+ "\n".join(rb) + "\n)\n")
(d / "busalias_root.kicad_sch").write_text(root)
cb: list[str] = []
if declare_alias:
cb.append(alias())
cb.append(wire((4000, 2000), (4000, 8000), tag="bus"))
cb.append(label(alias_name, 4000, 2200, tag="global_label",
extra="(shape input) "))
y = 2500
for ref, lbl in child_rips:
rip(cb, 4000, y, ref, lbl, childpath)
y += 600
child = (f'(kicad_sch (version 20231120) (generator "eeschema")'
f' (uuid "{U()}") (paper "A4")\n{libblock}\n'
+ "\n".join(cb) + "\n)\n")
(d / "child.kicad_sch").write_text(child)
return d / "busalias_root.kicad_sch"
@pytest.fixture(scope="module")
def bus_alias_parity(tmp_path_factory):
d = tmp_path_factory.mktemp("parity_bus_alias")
return _bus_alias_fixture(
d, "CTRL", ["EN", "RST", "D[0..3]"],
[("R1", "EN"), ("R3", "D2"), ("R5", "XX")],
[("R2", "EN"), ("R4", "D2"), ("R6", "XX")],
)
@needs_kicad
def test_bus_alias_partition_parity(bus_alias_parity):
assert _ee_partition(_export_nets(bus_alias_parity)) == _akcli_partition(
kreader.read_sch(bus_alias_parity))
@needs_kicad
def test_bus_alias_members_stay_sheet_local(bus_alias_parity):
# A group/vector bus would merge EN and D2 across sheets; the alias never
# does — its members inherit each rip label's local scope.
ee = _export_nets(bus_alias_parity)
assert ee["/EN"] == {("R1", "1")}
assert ee["/child/EN"] == {("R2", "1")}
assert ee["/D2"] == {("R3", "1")}
assert ee["/child/D2"] == {("R4", "1")}
part = _akcli_partition(kreader.read_sch(bus_alias_parity))
assert frozenset({("R1", "1")}) in part
assert frozenset({("R2", "1")}) in part
assert frozenset({("R1", "1"), ("R2", "1")}) not in part
@needs_kicad
def test_bus_alias_declaration_has_no_netlist_effect(tmp_path):
# THE verdict: declaring the alias changes nothing in the exported netlist.
d1 = tmp_path / "with"
d1.mkdir()
with_alias = _export_nets(_bus_alias_fixture(
d1, "CTRL", ["EN", "RST", "D[0..3]"],
[("R1", "EN"), ("R3", "D2")], [("R2", "EN"), ("R4", "D2")],
declare_alias=True))
d0 = tmp_path / "without"
d0.mkdir()
without = _export_nets(_bus_alias_fixture(
d0, "CTRL", ["EN", "RST", "D[0..3]"],
[("R1", "EN"), ("R3", "D2")], [("R2", "EN"), ("R4", "D2")],
declare_alias=False))
assert _ee_partition(with_alias) == _ee_partition(without)
@pytest.fixture(scope="module")
def bus_alias_vector_collision(tmp_path_factory):
# alias NAME "A[0..3]" also parses as a vector; members X, Y are decoys.
d = tmp_path_factory.mktemp("parity_bus_alias_vec")
return _bus_alias_fixture(
d, "A[0..3]", ["X", "Y"],
[("R1", "A2"), ("R3", "X")],
[("R2", "A2"), ("R4", "X")],
)
@needs_kicad
def test_bus_alias_vector_name_collision_vector_wins(bus_alias_vector_collision):
ee = _export_nets(bus_alias_vector_collision)
# the vector member A2 is a real global bus member and merges cross-sheet;
# the alias member X is ignored and stays sheet-local.
assert ee["A2"] == {("R1", "1"), ("R2", "1")}
assert ee["/X"] == {("R3", "1")}
assert ee["/child/X"] == {("R4", "1")}
assert _ee_partition(ee) == _akcli_partition(
kreader.read_sch(bus_alias_vector_collision))
97c6f2bbeThis file is pinned to the selected release. Download the original to open it in your design tool.