summaryrefslogtreecommitdiff
path: root/lua/tests/automated/layout_strategies_spec.lua
blob: 3315f635d9ce9e1d5b47538d97d0a1d633dcb2ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
local config = require "telescope.config"
local resolve = require "telescope.config.resolve"
local layout_strats = require "telescope.pickers.layout_strategies"

local validate_layout_config = layout_strats._validate_layout_config

local eq = assert.are.same

describe("layout_strategies", function()
  it("should have validator", function()
    assert(validate_layout_config, "Has validator")
  end)

  local test_height = function(should, output, input, opts)
    opts = opts or {}

    local max_columns, max_lines = opts.max_columns or 100, opts.max_lines or 100
    it(should, function()
      local layout_config = validate_layout_config("horizontal", { height = true }, { height = input })

      eq(output, resolve.resolve_height(layout_config.height)({}, max_columns, max_lines))
    end)
  end

  test_height("should handle numbers", 10, 10)

  test_height("should handle percentage: 100", 10, 0.1, { max_lines = 100 })
  test_height("should handle percentage: 110", 11, 0.1, { max_lines = 110 })

  test_height("should call functions: simple", 5, function()
    return 5
  end)
  test_height("should call functions: percentage", 15, function(_, _, lines)
    return 0.1 * lines
  end, {
    max_lines = 150,
  })

  local test_defaults_key = function(should, key, strat, output, ours, theirs, override)
    ours = ours or {}
    theirs = theirs or {}
    override = override or {}

    it(should, function()
      config.clear_defaults()
      config.set_defaults({ layout_config = theirs }, { layout_config = { ours, "description" } })
      local layout_config = validate_layout_config(strat, layout_strats._configurations[strat], override)
      eq(output, layout_config[key])
    end)
  end

  test_defaults_key(
    "should use ours if theirs and override don't give the key",
    "height",
    "horizontal",
    50,
    { height = 50 },
    { width = 100 },
    { width = 120 }
  )

  test_defaults_key(
    "should use ours if theirs and override don't give the key for this strategy",
    "height",
    "horizontal",
    50,
    { height = 50 },
    { vertical = { height = 100 } },
    { vertical = { height = 120 } }
  )

  test_defaults_key(
    "should use theirs if override doesn't give the key",
    "height",
    "horizontal",
    100,
    { height = 50 },
    { height = 100 },
    { width = 120 }
  )

  test_defaults_key(
    "should use override if key given",
    "height",
    "horizontal",
    120,
    { height = 50 },
    { height = 100 },
    { height = 120 }
  )

  test_defaults_key(
    "should use override if key given for this strategy",
    "height",
    "horizontal",
    120,
    { height = 50 },
    { height = 100 },
    { horizontal = { height = 120 } }
  )

  test_defaults_key(
    "should use theirs if override doesn't give key (even if ours has strategy specific)",
    "height",
    "horizontal",
    100,
    { horizontal = { height = 50 } },
    { height = 100 },
    { width = 120 }
  )

  test_defaults_key(
    "should use override (even if ours has strategy specific)",
    "height",
    "horizontal",
    120,
    { horizontal = { height = 50 } },
    { height = 100 },
    { height = 120 }
  )

  test_defaults_key(
    "should use override (even if theirs has strategy specific)",
    "height",
    "horizontal",
    120,
    { height = 50 },
    { horizontal = { height = 100 } },
    { height = 120 }
  )

  test_defaults_key(
    "should use override (even if ours and theirs have strategy specific)",
    "height",
    "horizontal",
    120,
    { horizontal = { height = 50 } },
    { horizontal = { height = 100 } },
    { height = 120 }
  )

  test_defaults_key(
    "should handle user config overriding a table with a number",
    "height",
    "horizontal",
    120,
    { height = { padding = 5 } },
    { height = 120 },
    {}
  )

  test_defaults_key(
    "should handle user oneshot overriding a table with a number",
    "height",
    "horizontal",
    120,
    {},
    { height = { padding = 5 } },
    { height = 120 }
  )
end)