Skip to content

Commit

Permalink
Add tests for window arrangement code
Browse files Browse the repository at this point in the history
The output of the GetWindowDimensions function is hard to understand just by looking at it,
so I've added a helper function in the tests to render the window layout as text, so that
in order to create a new test you just come up with some args and paste the output as the
expected output.

This has the same downsides that any snapshot-based testing has: it's more brittle than
targeted assertions. But it is much easier to make sense of these snapshots than it is
to make sense of more fine-grained assertions, and I like the fact that these tests can
serve as documentation.
  • Loading branch information
jesseduffield committed Dec 5, 2023
1 parent b50ea9b commit 1df9348
Show file tree
Hide file tree
Showing 2 changed files with 368 additions and 22 deletions.
45 changes: 23 additions & 22 deletions pkg/gui/controllers/helpers/window_arrangement_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,6 @@ func GetWindowDimensions(args WindowArrangementArgs) map[string]boxlayout.Dimens
sidePanelsDirection = boxlayout.ROW
}

mainPanelsDirection := boxlayout.ROW
if splitMainPanelSideBySide(args) {
mainPanelsDirection = boxlayout.COLUMN
}

extrasWindowSize := getExtrasWindowSize(args)

showInfoSection := args.UserConfig.Gui.ShowBottomLine ||
args.InSearchPrompt ||
args.IsAnyModeActive ||
Expand All @@ -156,17 +149,7 @@ func GetWindowDimensions(args WindowArrangementArgs) map[string]boxlayout.Dimens
{
Direction: boxlayout.ROW,
Weight: mainSectionWeight,
Children: []*boxlayout.Box{
{
Direction: mainPanelsDirection,
Children: mainSectionChildren(args),
Weight: 1,
},
{
Window: "extras",
Size: extrasWindowSize,
},
},
Children: mainPanelChildren(args),
},
},
},
Expand All @@ -184,6 +167,28 @@ func GetWindowDimensions(args WindowArrangementArgs) map[string]boxlayout.Dimens
return MergeMaps(layerOneWindows, limitWindows)
}

func mainPanelChildren(args WindowArrangementArgs) []*boxlayout.Box {
mainPanelsDirection := boxlayout.ROW
if splitMainPanelSideBySide(args) {
mainPanelsDirection = boxlayout.COLUMN
}

result := []*boxlayout.Box{
{
Direction: mainPanelsDirection,
Children: mainSectionChildren(args),
Weight: 1,
},
}
if args.ShowExtrasWindow {
result = append(result, &boxlayout.Box{
Window: "extras",
Size: getExtrasWindowSize(args),
})
}
return result
}

func MergeMaps[K comparable, V any](maps ...map[K]V) map[K]V {
result := map[K]V{}
for _, currMap := range maps {
Expand Down Expand Up @@ -368,10 +373,6 @@ func splitMainPanelSideBySide(args WindowArrangementArgs) bool {
}

func getExtrasWindowSize(args WindowArrangementArgs) int {
if !args.ShowExtrasWindow {
return 0
}

var baseSize int
// The 'extras' window contains the command log context
if args.CurrentStaticWindow == "extras" {
Expand Down
Loading

0 comments on commit 1df9348

Please sign in to comment.