155 lines
6.3 KiB
Python
155 lines
6.3 KiB
Python
"""Tests for lightgui.project_io (Phase 4): New / Open / Save /
|
|
Save As, dirty tracking, and the .fdj round-trip against the C
|
|
writer's layout (checked against the Rust fixtures, which the C and
|
|
Rust loaders share).
|
|
|
|
The legacy-type fixtures (control.fdj, hostile.fdj) carry pre-XFrame
|
|
types (Canvas, Button, Label, Entry): the XFrame-only loader drops
|
|
them by design, so those tests assert the drop + the surviving
|
|
window fields, not a byte-identical round-trip.
|
|
"""
|
|
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
from lightgui.model import Project
|
|
from lightgui.project_io import (
|
|
ProjectFile, ensure_fdj_path, is_fdj_path)
|
|
|
|
FIXTURES = "/opt/lightgui_rust/crates/lightgui-core/tests/fixtures"
|
|
|
|
|
|
class ProjectFileTests(unittest.TestCase):
|
|
def test_new_resets_in_place(self):
|
|
project = Project()
|
|
project.add_widget("GtkButton", 10, 10)
|
|
holder = ProjectFile(project)
|
|
holder.new()
|
|
self.assertIs(holder.project, project)
|
|
self.assertEqual(project.widgets, [])
|
|
self.assertEqual(project.title, "Main Window")
|
|
self.assertIsNone(holder.path)
|
|
self.assertFalse(holder.dirty)
|
|
|
|
def test_save_as_and_open_round_trip(self):
|
|
project = Project(title="Round Trip", class_name="RoundTrip")
|
|
button = project.add_widget("GtkButton", 10, 20, 150, 36)
|
|
button.set_prop("background", "#336699")
|
|
button.set_prop("hint", "tip")
|
|
from lightgui.inspector import set_event_handler
|
|
set_event_handler(button, "OnClick", "on_click")
|
|
project.add_widget("GtkLabel", 30, 40, 200, 30)
|
|
holder = ProjectFile(project)
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = os.path.join(folder, "roundtrip.fdj")
|
|
holder.save_as(path)
|
|
self.assertEqual(holder.path, path)
|
|
self.assertFalse(holder.dirty)
|
|
self.assertTrue(os.path.isfile(path))
|
|
|
|
other = ProjectFile()
|
|
other.open_path(path)
|
|
self.assertEqual(other.project.title, "Round Trip")
|
|
self.assertEqual(len(other.project.widgets), 2)
|
|
loaded = other.project.widgets[0]
|
|
self.assertEqual(loaded.name, "GtkButton_001")
|
|
self.assertEqual(loaded.kind, "GtkButton")
|
|
self.assertEqual((loaded.x, loaded.y), (10, 20))
|
|
self.assertEqual(loaded.get_prop("background"), "#336699")
|
|
self.assertEqual(loaded.get_prop("hint"), "tip")
|
|
self.assertEqual(loaded.signals, ["OnClick=on_click"])
|
|
# Save -> load -> save is byte-identical (C writer layout).
|
|
self.assertEqual(other.project.save_text(),
|
|
project.save_text())
|
|
|
|
def test_save_without_path_raises(self):
|
|
holder = ProjectFile()
|
|
with self.assertRaises(ValueError):
|
|
holder.save()
|
|
|
|
def test_open_missing_file_raises(self):
|
|
holder = ProjectFile()
|
|
with self.assertRaises(ValueError):
|
|
holder.open_path("/nonexistent/nope.fdj")
|
|
self.assertEqual(holder.project.widgets, [])
|
|
|
|
def test_open_bad_file_leaves_project_untouched(self):
|
|
holder = ProjectFile()
|
|
holder.project.add_widget("GtkButton", 10, 10)
|
|
with tempfile.NamedTemporaryFile(
|
|
"w", suffix=".fdj", delete=False) as handle:
|
|
handle.write("{not json")
|
|
path = handle.name
|
|
try:
|
|
with self.assertRaises(ValueError):
|
|
holder.open_path(path)
|
|
finally:
|
|
os.unlink(path)
|
|
self.assertEqual(len(holder.project.widgets), 1)
|
|
|
|
def test_dirty_flag(self):
|
|
holder = ProjectFile()
|
|
self.assertFalse(holder.dirty)
|
|
holder.mark_dirty()
|
|
self.assertTrue(holder.dirty)
|
|
holder.mark_clean()
|
|
self.assertFalse(holder.dirty)
|
|
|
|
def test_title(self):
|
|
holder = ProjectFile()
|
|
self.assertEqual(holder.title, "Untitled")
|
|
holder.path = "/some/dir/my_app.fdj"
|
|
self.assertEqual(holder.title, "my_app.fdj")
|
|
|
|
def test_fdj_path_helpers(self):
|
|
self.assertTrue(is_fdj_path("a.fdj"))
|
|
self.assertTrue(is_fdj_path("A.FDJ"))
|
|
self.assertFalse(is_fdj_path("a.py"))
|
|
self.assertFalse(is_fdj_path(None))
|
|
self.assertEqual(ensure_fdj_path("a"), "a.fdj")
|
|
self.assertEqual(ensure_fdj_path("a.fdj"), "a.fdj")
|
|
|
|
@unittest.skipUnless(os.path.isdir(FIXTURES), "Rust fixtures absent")
|
|
def test_empty_fixture_round_trips_byte_identical(self):
|
|
"""empty.fdj: the C writer's layout, reproduced exactly."""
|
|
path = os.path.join(FIXTURES, "empty.fdj")
|
|
with open(path, encoding="utf-8") as handle:
|
|
original = handle.read()
|
|
project = Project.load_file(path)
|
|
self.assertEqual(project.save_text(), original)
|
|
|
|
@unittest.skipUnless(os.path.isdir(FIXTURES), "Rust fixtures absent")
|
|
def test_control_fixture_window_fields_survive(self):
|
|
"""control.fdj: title with control chars + a legacy Canvas
|
|
widget (dropped by the XFrame-only loader, by design)."""
|
|
path = os.path.join(FIXTURES, "control.fdj")
|
|
project = Project.load_file(path)
|
|
self.assertEqual(project.title, "ctrl\x01\x08\x0c\x1f")
|
|
self.assertEqual(project.skipped, ["Canvas"])
|
|
self.assertEqual(project.widgets, [])
|
|
# The window block re-serialises exactly.
|
|
saved = project.save_text()
|
|
self.assertIn('"title": "ctrl\\u0001\\u0008\\u000c\\u001f"',
|
|
saved)
|
|
|
|
@unittest.skipUnless(os.path.isdir(FIXTURES), "Rust fixtures absent")
|
|
def test_hostile_fixture_window_fields_survive(self):
|
|
"""hostile.fdj: quotes/backslashes/newlines in strings,
|
|
menubar spec, background colour; legacy widgets dropped."""
|
|
path = os.path.join(FIXTURES, "hostile.fdj")
|
|
project = Project.load_file(path)
|
|
self.assertEqual(project.title, 'My "Quoted" \\ App\nSecond line')
|
|
self.assertEqual(project.class_name, "my cool app!")
|
|
self.assertEqual((project.win_width, project.win_height),
|
|
(1024, 768))
|
|
self.assertEqual(project.background, "#102030")
|
|
self.assertEqual(project.menubar,
|
|
"File: New | Open | - | Quit\nHelp: About")
|
|
self.assertEqual(project.skipped, ["Button", "Label", "Entry"])
|
|
self.assertEqual(project.widgets, [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|