summaryrefslogtreecommitdiff
path: root/gerbonara/tests
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2023-04-15 17:09:20 +0200
committerjaseg <git@jaseg.de>2023-04-15 17:09:20 +0200
commit2400ff8e5fea41c1f8c6251d37a02209ec253f93 (patch)
tree395968d05156c094709fda605a9fe572aed32b1d /gerbonara/tests
parentb43e4e2eec99b92a1e87f6388703db1ca33518d1 (diff)
downloadgerbonara-2400ff8e5fea41c1f8c6251d37a02209ec253f93.tar.gz
gerbonara-2400ff8e5fea41c1f8c6251d37a02209ec253f93.tar.bz2
gerbonara-2400ff8e5fea41c1f8c6251d37a02209ec253f93.zip
cad: Add KiCad symbol/footprint parser
Diffstat (limited to 'gerbonara/tests')
-rw-r--r--gerbonara/tests/conftest.py28
-rw-r--r--gerbonara/tests/test_kicad_footprints.py57
-rw-r--r--gerbonara/tests/test_kicad_sexpr.py26
-rw-r--r--gerbonara/tests/test_kicad_symbols.py59
4 files changed, 170 insertions, 0 deletions
diff --git a/gerbonara/tests/conftest.py b/gerbonara/tests/conftest.py
index b999027..bd89901 100644
--- a/gerbonara/tests/conftest.py
+++ b/gerbonara/tests/conftest.py
@@ -1,4 +1,5 @@
+import os
from pathlib import Path
import pytest
@@ -33,3 +34,30 @@ def pytest_sessionstart(session):
run_cargo_cmd('resvg', '--help')
except FileNotFoundError:
pytest.exit('resvg binary not found, aborting test.', 2)
+
+def pytest_addoption(parser):
+ parser.addoption('--kicad-symbol-library', nargs='*', help='Run symbol library tests on given symbol libraries. May be given multiple times.')
+ parser.addoption('--kicad-footprint-files', nargs='*', help='Run footprint library tests on given footprint files. May be given multiple times.')
+
+def pytest_generate_tests(metafunc):
+ if 'kicad_library_file' in metafunc.fixturenames:
+ if not (library_files := metafunc.config.getoption('symbol_library', None)):
+ if (lib_dir := os.environ.get('KICAD_SYMBOLS')):
+ lib_dir = Path(lib_dir).expanduser()
+ if not lib_dir.is_dir():
+ raise ValueError(f'Path "{lib_dir}" given by KICAD_SYMBOLS environment variable does not exist or is not a directory.')
+ library_files = list(lib_dir.glob('*.kicad_sym'))
+ else:
+ raise ValueError('Either --kicad-symbol-library command line parameter or KICAD_SYMBOLS environment variable must be given.')
+ metafunc.parametrize('kicad_library_file', library_files, ids=list(map(str, library_files)))
+
+ if 'kicad_mod_file' in metafunc.fixturenames:
+ if not (mod_files := metafunc.config.getoption('footprint_files', None)):
+ if (lib_dir := os.environ.get('KICAD_FOOTPRINTS')):
+ lib_dir = Path(lib_dir).expanduser()
+ if not lib_dir.is_dir():
+ raise ValueError(f'Path "{lib_dir}" given by KICAD_FOOTPRINTS environment variable does not exist or is not a directory.')
+ mod_files = list(lib_dir.glob('**/*.kicad_mod'))
+ else:
+ raise ValueError('Either --kicad-footprint-files command line parameter or KICAD_FOOTPRINTS environment variable must be given.')
+ metafunc.parametrize('kicad_mod_file', mod_files, ids=list(map(str, mod_files)))
diff --git a/gerbonara/tests/test_kicad_footprints.py b/gerbonara/tests/test_kicad_footprints.py
new file mode 100644
index 0000000..a238e1c
--- /dev/null
+++ b/gerbonara/tests/test_kicad_footprints.py
@@ -0,0 +1,57 @@
+
+from itertools import zip_longest
+import re
+
+from ..cad.kicad.sexp import build_sexp
+from ..cad.kicad.sexp_mapper import sexp
+from ..cad.kicad.footprints import Footprint
+
+def test_parse(kicad_mod_file):
+ Footprint.open(kicad_mod_file)
+
+def test_round_trip(kicad_mod_file):
+ print('========== Stage 1 load ==========')
+ orig_fp = Footprint.open(kicad_mod_file)
+ print('========== Stage 1 save ==========')
+ stage1_sexp = build_sexp(orig_fp.sexp())
+ with open('/tmp/foo.sexp', 'w') as f:
+ f.write(stage1_sexp)
+
+ print('========== Stage 2 load ==========')
+ reparsed_fp = Footprint.parse(stage1_sexp)
+ print('========== Stage 2 save ==========')
+ stage2_sexp = build_sexp(reparsed_fp.sexp())
+ print('========== Checks ==========')
+
+ for stage1, stage2 in zip_longest(stage1_sexp.splitlines(), stage2_sexp.splitlines()):
+ assert stage1 == stage2
+
+ return
+
+ original = re.sub(r'\(', '\n(', re.sub(r'\s+', ' ', kicad_mod_file.read_text()))
+ original = re.sub(r'\) \)', '))', original)
+ original = re.sub(r'\) \)', '))', original)
+ original = re.sub(r'\) \)', '))', original)
+ original = re.sub(r'\) \)', '))', original)
+ stage1 = re.sub(r'\(', '\n(', re.sub(r'\s+', ' ', stage1_sexp))
+ for original, stage1 in zip_longest(original.splitlines(), stage1.splitlines()):
+ if original.startswith('(version'):
+ continue
+
+ original, stage1 = original.strip(), stage1.strip()
+ if original != stage1:
+ if any(original.startswith(f'({foo}') for foo in ['arc', 'circle', 'rectangle', 'polyline', 'text']):
+ # These files have symbols with graphic primitives in non-standard order
+ return
+
+ if original.startswith('(symbol') and stage1.startswith('(symbol'):
+ # Re-export can change symbol order. This is ok.
+ return
+
+ if original.startswith('(at') and stage1.startswith('(at'):
+ # There is some disagreement as to whether rotation angles are ints or floats, and the spec doesn't say.
+ return
+
+ assert original == stage1
+
+
diff --git a/gerbonara/tests/test_kicad_sexpr.py b/gerbonara/tests/test_kicad_sexpr.py
new file mode 100644
index 0000000..b2c60b1
--- /dev/null
+++ b/gerbonara/tests/test_kicad_sexpr.py
@@ -0,0 +1,26 @@
+
+from ..cad.kicad.sexp import parse_sexp, build_sexp
+
+def test_sexp_round_trip():
+ test_sexp = '''(()() (foo) (23)\t(foo 23) (foo 23 bar baz) (foo bar baz) ("foo bar") (" foo " bar) (23 " baz ")
+ (foo ( bar ( baz 23) 42) frob) (() (foo) ()()) foo 23 23.0 23.000001 "foo \\"( ))bar" "foo\\"bar\\"baz" "23" "23foo"
+ "" "" ("") ("" 0 0.0)
+ "lots of data" "lots of data" "lots of data" "lots of data" "lots of data" "lots of data"
+ "lots of data" "lots of data" "lots of data" "lots of data" "lots of data" "lots of data"
+ "lots of data" "lots of data" "lots of data" "lots of data" "lots of data" "lots of data"
+ "lots of data" "lots of data" "lots of data" "lots of data" "lots of data" "lots of data"
+ "lots of data" "lots of data" "lots of data" "lots of data" "lots of data" "lots of data"
+ "lots of data" "lots of data" "lots of data" "lots of data" "lots of data" "lots of data"
+ "lots of data" "lots of data" "lots of data" "lots of data" "lots of data" "lots of data"
+ "lots of data" "lots of data" "lots of data" "lots of data" "lots of data" "lots of data"
+ "lots of data" "lots of data" "lots of data" "lots of data" "lots of data" "lots of data"
+ "lots of data" "lots of data" "lots of data" "lots of data" "lots of data" "lots of data")
+ '''
+ parsed = parse_sexp(test_sexp)
+ sexp1 = build_sexp(parsed)
+ re_parsed = parse_sexp(sexp1)
+ sexp2 = build_sexp(parsed)
+
+ assert re_parsed == parsed
+ assert sexp1 == sexp2
+
diff --git a/gerbonara/tests/test_kicad_symbols.py b/gerbonara/tests/test_kicad_symbols.py
new file mode 100644
index 0000000..0a6c595
--- /dev/null
+++ b/gerbonara/tests/test_kicad_symbols.py
@@ -0,0 +1,59 @@
+
+from itertools import zip_longest
+import re
+
+from ..cad.kicad.sexp import build_sexp
+from ..cad.kicad.sexp_mapper import sexp
+from ..cad.kicad.symbols import Library
+
+
+def test_parse(kicad_library_file):
+ Library.open(kicad_library_file)
+
+
+def test_round_trip(kicad_library_file):
+ print('========== Stage 1 load ==========')
+ orig_lib = Library.open(kicad_library_file)
+ print('========== Stage 1 save ==========')
+ stage1_sexp = build_sexp(orig_lib.sexp())
+
+ print('========== Stage 2 load ==========')
+ reparsed_lib = Library.parse(stage1_sexp)
+ print('========== Stage 2 save ==========')
+ stage2_sexp = build_sexp(reparsed_lib.sexp())
+ print('========== Checks ==========')
+
+ for stage1, stage2 in zip_longest(stage1_sexp.splitlines(), stage2_sexp.splitlines()):
+ assert stage1 == stage2
+
+ original = re.sub(r'\(', '\n(', re.sub(r'\s+', ' ', kicad_library_file.read_text()))
+ original = re.sub(r'\) \)', '))', original)
+ original = re.sub(r'\) \)', '))', original)
+ original = re.sub(r'\) \)', '))', original)
+ original = re.sub(r'\) \)', '))', original)
+ stage1 = re.sub(r'\(', '\n(', re.sub(r'\s+', ' ', stage1_sexp))
+ for original, stage1 in zip_longest(original.splitlines(), stage1.splitlines()):
+ if original.startswith('(version'):
+ continue
+
+ original, stage1 = original.strip(), stage1.strip()
+ if original != stage1:
+ if any(original.startswith(f'({foo}') for foo in ['arc', 'circle', 'rectangle', 'polyline', 'text']):
+ # These files have symbols with graphic primitives in non-standard order
+ return
+
+ if original.startswith('(offset') and stage1.startswith('(offset'):
+ # Some symbol files contain ints where floats should be.
+ return
+
+ if original.startswith('(symbol') and stage1.startswith('(symbol'):
+ # Re-export can change symbol order. This is ok.
+ return
+
+ if original.startswith('(at') and stage1.startswith('(at'):
+ # There is some disagreement as to whether rotation angles are ints or floats, and the spec doesn't say.
+ return
+
+ assert original == stage1
+
+