|
| 1 | +local helpers = require("diffview.tests.helpers") |
| 2 | + |
| 3 | +local eq = helpers.eq |
| 4 | + |
| 5 | +describe("diffview.ui.panel", function() |
| 6 | + local Panel = require("diffview.ui.panel").Panel |
| 7 | + |
| 8 | + describe("interface contract", function() |
| 9 | + -- The tab_enter/tab_leave listeners in diff and file_history views |
| 10 | + -- depend on panel instances exposing a `winid` field and an `is_open` |
| 11 | + -- method. Verify these exist on the base class so that a future |
| 12 | + -- refactor cannot silently break the contract (see issue #611). |
| 13 | + |
| 14 | + it("has a winid field after init", function() |
| 15 | + local panel = Panel({ |
| 16 | + bufname = "TestPanel", |
| 17 | + config = Panel.default_config_split, |
| 18 | + }) |
| 19 | + -- winid starts nil (panel not yet opened). |
| 20 | + eq(nil, panel.winid) |
| 21 | + end) |
| 22 | + |
| 23 | + it("exposes is_open as a callable method", function() |
| 24 | + local panel = Panel({ |
| 25 | + bufname = "TestPanel", |
| 26 | + config = Panel.default_config_split, |
| 27 | + }) |
| 28 | + eq("function", type(panel.is_open)) |
| 29 | + end) |
| 30 | + |
| 31 | + it("is_open returns falsy when winid is nil", function() |
| 32 | + local panel = Panel({ |
| 33 | + bufname = "TestPanel", |
| 34 | + config = Panel.default_config_split, |
| 35 | + }) |
| 36 | + assert.falsy(panel:is_open()) |
| 37 | + end) |
| 38 | + |
| 39 | + it("does not expose a get_winid method", function() |
| 40 | + -- get_winid has never been part of the Panel API. Callers should |
| 41 | + -- access the winid field directly. This test guards against |
| 42 | + -- accidental re-introduction of calls to a non-existent method. |
| 43 | + local panel = Panel({ |
| 44 | + bufname = "TestPanel", |
| 45 | + config = Panel.default_config_split, |
| 46 | + }) |
| 47 | + eq(nil, panel.get_winid) |
| 48 | + end) |
| 49 | + end) |
| 50 | + |
| 51 | + describe("subclass contracts", function() |
| 52 | + -- The actual panels used by the two view types must inherit the same |
| 53 | + -- interface. |
| 54 | + |
| 55 | + local function assert_panel_interface(panel_class, name) |
| 56 | + it(name .. " inherits winid field", function() |
| 57 | + eq(nil, rawget(panel_class, "get_winid")) |
| 58 | + end) |
| 59 | + |
| 60 | + it(name .. " inherits is_open method", function() |
| 61 | + eq("function", type(panel_class.is_open)) |
| 62 | + end) |
| 63 | + end |
| 64 | + |
| 65 | + local FilePanel = require("diffview.scene.views.diff.file_panel").FilePanel |
| 66 | + local FileHistoryPanel = require("diffview.scene.views.file_history.file_history_panel").FileHistoryPanel |
| 67 | + |
| 68 | + assert_panel_interface(FilePanel, "FilePanel") |
| 69 | + assert_panel_interface(FileHistoryPanel, "FileHistoryPanel") |
| 70 | + end) |
| 71 | +end) |
0 commit comments