import json import tempfile import unittest from pathlib import Path from textbook.build_definitions import build def write_json(path, data): path.write_text(json.dumps(data), encoding='utf-8') class BuildDefinitionsTests(unittest.TestCase): def test_only_entries_with_lean_code_are_emitted(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / 'defs.json' write_json(path, [ {"label": "Definition A", "concept": "has mathlib equivalent", "mathlib_ref": "Nat.Prime", "lean_code": None, "notes": ""}, {"label": "Definition B", "concept": "needs hand-written def", "mathlib_ref": None, "lean_code": "def Foo (n : Nat) : Prop := n = n", "notes": "some note"}, ]) text, emitted = build([path]) self.assertEqual(emitted, 1) self.assertIn('def Foo (n : Nat) : Prop := n = n', text) self.assertIn('Definition B', text) self.assertNotIn('Definition A', text) # has a Mathlib ref, nothing to assemble def test_multiple_manifests_are_concatenated(self): with tempfile.TemporaryDirectory() as directory: directory = Path(directory) path1 = directory / 'defs1.json' path2 = directory / 'defs2.json' write_json(path1, [{"label": "D1", "concept": "c1", "mathlib_ref": None, "lean_code": "def D1 := True", "notes": ""}]) write_json(path2, [{"label": "D2", "concept": "c2", "mathlib_ref": None, "lean_code": "def D2 := True", "notes": ""}]) text, emitted = build([path1, path2]) self.assertEqual(emitted, 2) self.assertIn('def D1 := True', text) self.assertIn('def D2 := True', text) if __name__ == '__main__': unittest.main()