Skip to content

Commit 457093c

Browse files
committed
test(f-string): add tests for inserts and not insterting fstrings
1 parent c256211 commit 457093c

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

lua/python/treesitter/commands.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function PythonTreeSitterCommands.ts_wrap_at_cursor(subtitute_option)
209209
end)
210210
end
211211

212-
---
212+
---
213213
---@param node TSNode the current ts node we are checking for parents
214214
---@return string callText check if this node has a "call" type node 3 parents up
215215
--- this is used for checking on "".format() calls for strings.
@@ -263,7 +263,7 @@ function PythonTreeSitterCommands.pythonFStr()
263263

264264
local isFormatString = callText:find([[^.*["']%.format%(]])
265265
local isRString = text:find("^r")
266-
local isFString = text:find("^r?f") -- rf -> raw-formatted-string
266+
local isFString = text:find("^r?f") -- rf -> raw-formatted-string
267267
local hasBraces = text:find("{.-[^%d,%s].-}") -- nonRegex-braces, see #12 and #15
268268

269269
if (not isFString and not isFormatString and not isRString) and hasBraces then

tests/test_treesitter.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,31 @@ T["enumerate"] = function()
6363
eq(get_lines(), { "for idx, i in enumerate([1, 2, 3]):" })
6464
end
6565

66+
T["f-string"] = MiniTest.new_set({
67+
hooks = {
68+
pre_case = function()
69+
child.cmd("e _not_existing_new_buffer.py")
70+
child.type_keys("cc", [["TEST"]], "<Esc>", "0")
71+
end,
72+
},
73+
})
74+
75+
T["f-string"]["insert f string"] = function()
76+
child.cmd("e! _not_existing_new_buffer.py")
77+
child.type_keys("cc", [["{foo}"]], "<Esc>", "hh", "i", "<Esc>")
78+
eq(get_lines(), { [[f"{foo}"]] })
79+
end
80+
81+
T["f-string"]["skip on r"] = function()
82+
child.cmd("e! _not_existing_new_buffer.py")
83+
child.type_keys("cc", [[r"{foo}"]], "<Esc>", "hh", "i", "<Esc>")
84+
eq(get_lines(), { [[r"{foo}"]] })
85+
end
86+
87+
T["f-string"]["skip on format"] = function()
88+
child.cmd("e! _not_existing_new_buffer.py")
89+
child.type_keys("cc", [["{foo}".format()]], "<Esc>", "0lll", "i", "<Esc>")
90+
eq(get_lines(), { [["{foo}".format()]] })
91+
end
6692
-- Return test set which will be collected and execute inside `MiniTest.run()`
6793
return T

0 commit comments

Comments
 (0)