import json import tempfile import unittest from pathlib import Path from copienator import read_json from copienator_gui.recorrection import prepare_selection_file class RecorrectionFileTests(unittest.TestCase): def test_examples_are_inactive_and_opening_preserves_selection(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) path = prepare_selection_file(root) self.assertEqual(read_json(path), []) self.assertIn('// Une copie entière', path.read_text()) selection = [["Copie46", ["II)9)"]]] path.write_text(json.dumps(selection)) prepare_selection_file(root) self.assertEqual(read_json(path), selection) before = path.read_bytes() prepare_selection_file(root) self.assertEqual(path.read_bytes(), before) def test_comments_do_not_affect_strings_or_other_json_files(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) path = root / "refaire.json" path.write_text('// Example\n[["Copie01", ["a//b"]]]\n') self.assertEqual(read_json(path), [["Copie01", ["a//b"]]]) other = root / "correction.json" other.write_text(path.read_text()) with self.assertRaises(json.JSONDecodeError): read_json(other)