|
| 1 | +"""Unit tests for process_constraints shared functions.""" |
| 2 | + |
| 3 | +from labview_fpga_hdl_tools.process_constraints import ( |
| 4 | + load_custom_constraints, |
| 5 | + replace_custom_constraints_in_xdc_folder, |
| 6 | +) |
| 7 | + |
| 8 | + |
| 9 | +class TestLoadCustomConstraints: |
| 10 | + """Tests for load_custom_constraints().""" |
| 11 | + |
| 12 | + def test_given_valid_file__when_loaded__then_returns_content(self, tmp_path): |
| 13 | + constraints_file = tmp_path / "custom.xdc" |
| 14 | + constraints_file.write_text("set_property BITSTREAM.CONFIG.SPI_BUSWIDTH 4 [current_design]") |
| 15 | + result = load_custom_constraints(str(constraints_file)) |
| 16 | + assert result == "set_property BITSTREAM.CONFIG.SPI_BUSWIDTH 4 [current_design]" |
| 17 | + |
| 18 | + def test_given_none_path__when_loaded__then_returns_empty_string(self): |
| 19 | + result = load_custom_constraints(None) |
| 20 | + assert result == "" |
| 21 | + |
| 22 | + def test_given_nonexistent_path__when_loaded__then_returns_empty_string(self): |
| 23 | + result = load_custom_constraints("/nonexistent/path/custom.xdc") |
| 24 | + assert result == "" |
| 25 | + |
| 26 | + def test_given_empty_string_path__when_loaded__then_returns_empty_string(self): |
| 27 | + result = load_custom_constraints("") |
| 28 | + assert result == "" |
| 29 | + |
| 30 | + |
| 31 | +class TestReplaceCustomConstraintsInXdcFolder: |
| 32 | + """Tests for replace_custom_constraints_in_xdc_folder().""" |
| 33 | + |
| 34 | + def test_given_xdc_with_macro__when_replaced__then_macro_is_substituted(self, tmp_path): |
| 35 | + xdc_file = tmp_path / "constraints.xdc" |
| 36 | + xdc_file.write_text( |
| 37 | + "# Header\n" "#LabVIEWFPGAHdlTools_Macro macro_GitHubCustomConstraints\n" "# Footer\n" |
| 38 | + ) |
| 39 | + custom_file = tmp_path / "custom.xdc" |
| 40 | + custom_file.write_text("set_property PACKAGE_PIN A1 [get_ports clk]") |
| 41 | + |
| 42 | + replace_custom_constraints_in_xdc_folder(str(tmp_path), str(custom_file)) |
| 43 | + |
| 44 | + result = xdc_file.read_text() |
| 45 | + assert "set_property PACKAGE_PIN A1 [get_ports clk]" in result |
| 46 | + assert "macro_GitHubCustomConstraints" not in result |
| 47 | + assert "# Header\n" in result |
| 48 | + assert "# Footer\n" in result |
| 49 | + |
| 50 | + def test_given_xdc_with_macro__when_no_custom_file__then_macro_is_removed(self, tmp_path): |
| 51 | + xdc_file = tmp_path / "constraints.xdc" |
| 52 | + xdc_file.write_text( |
| 53 | + "# Header\n" "#LabVIEWFPGAHdlTools_Macro macro_GitHubCustomConstraints\n" "# Footer\n" |
| 54 | + ) |
| 55 | + |
| 56 | + replace_custom_constraints_in_xdc_folder(str(tmp_path), None) |
| 57 | + |
| 58 | + result = xdc_file.read_text() |
| 59 | + assert "macro_GitHubCustomConstraints" not in result |
| 60 | + assert "# Header\n" in result |
| 61 | + |
| 62 | + def test_given_no_xdc_files__when_called__then_no_error(self, tmp_path): |
| 63 | + vhd_file = tmp_path / "design.vhd" |
| 64 | + vhd_file.write_text("entity design is end;") |
| 65 | + |
| 66 | + replace_custom_constraints_in_xdc_folder(str(tmp_path), None) |
| 67 | + # Should not raise, vhd file should be untouched |
| 68 | + assert vhd_file.read_text() == "entity design is end;" |
| 69 | + |
| 70 | + def test_given_nonexistent_folder__when_called__then_no_error(self): |
| 71 | + replace_custom_constraints_in_xdc_folder("/nonexistent/folder", None) |
| 72 | + # Should silently return |
| 73 | + |
| 74 | + def test_given_case_insensitive_macro__when_replaced__then_works(self, tmp_path): |
| 75 | + xdc_file = tmp_path / "test.xdc" |
| 76 | + xdc_file.write_text("#labviewfpgahdltools_macro macro_githubcustomconstraints\n") |
| 77 | + custom_file = tmp_path / "custom.xdc" |
| 78 | + custom_file.write_text("# custom content") |
| 79 | + |
| 80 | + replace_custom_constraints_in_xdc_folder(str(tmp_path), str(custom_file)) |
| 81 | + |
| 82 | + result = xdc_file.read_text() |
| 83 | + assert "# custom content" in result |
| 84 | + assert "macro_githubcustomconstraints" not in result |
| 85 | + |
| 86 | + def test_given_xdc_without_macro__when_called__then_file_unchanged(self, tmp_path): |
| 87 | + xdc_file = tmp_path / "plain.xdc" |
| 88 | + original = "# Just a normal constraint file\nset_property FOO BAR\n" |
| 89 | + xdc_file.write_text(original) |
| 90 | + |
| 91 | + replace_custom_constraints_in_xdc_folder(str(tmp_path), None) |
| 92 | + |
| 93 | + assert xdc_file.read_text() == original |
| 94 | + |
| 95 | + def test_given_multiple_xdc_files__when_replaced__then_all_processed(self, tmp_path): |
| 96 | + for name in ["a.xdc", "b.xdc", "c.xdc"]: |
| 97 | + (tmp_path / name).write_text( |
| 98 | + "#LabVIEWFPGAHdlTools_Macro macro_GitHubCustomConstraints\n" |
| 99 | + ) |
| 100 | + custom_file = tmp_path / "custom.xdc" |
| 101 | + custom_file.write_text("REPLACED") |
| 102 | + |
| 103 | + replace_custom_constraints_in_xdc_folder(str(tmp_path), str(custom_file)) |
| 104 | + |
| 105 | + for name in ["a.xdc", "b.xdc", "c.xdc"]: |
| 106 | + assert "REPLACED" in (tmp_path / name).read_text() |
0 commit comments