forked from zhisme/copy_with_context.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_spec.lua
More file actions
217 lines (191 loc) · 5.2 KB
/
config_spec.lua
File metadata and controls
217 lines (191 loc) · 5.2 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
-- Define a global vim table before requiring the module
_G.vim = {
tbl_deep_extend = function(_, default, user_opts)
local function merge(t1, t2)
for k, v in pairs(t2) do
if type(v) == "table" and type(t1[k] or false) == "table" then
merge(t1[k], v)
else
t1[k] = v
end
end
return t1
end
return merge(default, user_opts)
end,
}
-- Ensure fresh module loading
package.loaded["copy_with_context.config"] = nil
package.loaded["copy_with_context.user_config_validation"] = nil
local config = require("copy_with_context.config")
describe("Config Module", function()
before_each(function()
-- Reset config.options to defaults before each test
config.options = {
mappings = {
relative = "<leader>cy",
absolute = "<leader>cY",
},
formats = {
default = "# {filepath}:{line}",
},
trim_lines = false,
}
end)
it("has default options", function()
assert.same({
mappings = {
relative = "<leader>cy",
absolute = "<leader>cY",
},
formats = {
default = "# {filepath}:{line}",
},
trim_lines = false,
}, config.options)
end)
it("can be called without arguments", function()
config.setup()
-- Should not error and keep default options
assert.is_not_nil(config.options.mappings)
assert.is_not_nil(config.options.formats)
end)
it("merges user options with defaults", function()
config.setup({
mappings = { relative = "<leader>new" },
trim_lines = true,
})
assert.same({
mappings = {
relative = "<leader>new",
absolute = "<leader>cY",
},
formats = {
default = "# {filepath}:{line}",
},
trim_lines = true,
}, config.options)
end)
it("validates configuration on setup", function()
local success = pcall(config.setup, {
mappings = {
custom = "<leader>cc",
},
formats = {
default = "# {filepath}:{line}",
-- missing 'custom' format
},
})
assert.is_false(success)
end)
it("validates format strings on setup", function()
local success = pcall(config.setup, {
mappings = {
relative = "<leader>cy",
},
formats = {
default = "# {invalid_variable}",
},
})
assert.is_false(success)
end)
it("validates custom format strings with invalid variables", function()
-- This test covers the error on line 33 of config.lua
local success = pcall(config.setup, {
mappings = {
relative = "<leader>cy",
custom = "<leader>cc",
},
formats = {
default = "# {filepath}:{line}",
custom = "# {invalid_custom_var}", -- Invalid variable in custom format
},
})
assert.is_false(success)
end)
it("handles missing formats gracefully", function()
-- Manually reset config to have no formats
config.options = {
mappings = {
relative = "<leader>cy",
},
trim_lines = false,
}
-- Setup without providing formats - should fail because no default format
local success = pcall(config.setup, {
mappings = {
relative = "<leader>cy",
},
})
-- Should fail validation because no default format
assert.is_false(success)
end)
it("validates multiple format strings", function()
local success = pcall(config.setup, {
mappings = {
relative = "<leader>cy",
custom1 = "<leader>c1",
custom2 = "<leader>c2",
},
formats = {
default = "# {filepath}:{line}",
custom1 = "# {remote_url}",
custom2 = "# {filepath}",
},
})
-- All formats are valid, should succeed
assert.is_true(success)
end)
it("validates output_formats with valid variables", function()
local success = pcall(config.setup, {
mappings = {
relative = "<leader>cy",
},
output_formats = {
default = "{copied_text}\n\n# {filepath}:{line}",
},
})
assert.is_true(success)
end)
it("validates output_formats with invalid variables", function()
local success = pcall(config.setup, {
mappings = {
relative = "<leader>cy",
},
output_formats = {
default = "{copied_text}\n# {invalid_var}",
},
})
assert.is_false(success)
end)
it("validates custom output_format with invalid variables", function()
local success = pcall(config.setup, {
mappings = {
relative = "<leader>cy",
markdown = "<leader>cm",
},
formats = {
default = "# {filepath}:{line}",
},
output_formats = {
markdown = "{copied_text}\n# {bad_variable}",
},
})
assert.is_false(success)
end)
it("validates multiple output_formats", function()
local success = pcall(config.setup, {
mappings = {
relative = "<leader>cy",
markdown = "<leader>cm",
full = "<leader>cf",
},
output_formats = {
default = "{copied_text}\n# {filepath}:{line}",
markdown = "```\n{copied_text}\n```\n\n_{filepath}:{line}_",
full = "{copied_text}\n\n# {filepath}:{line}\n# {remote_url}",
},
})
assert.is_true(success)
end)
end)