Skip to content

Commit

Permalink
710 Bugfix escape char func (#772)
Browse files Browse the repository at this point in the history
* Fix escape_char for quote

The `"` need no escaping. The old version, which was introduced in this
[commit](ea03412#diff-5a72a6018b10915bef41b81020b59395527ba5b4b844d222b4e97192b5450565R369),
has unexpected behaviour of adding a `\` in the resulting strings.

This change fixes this issue.

* Add plenary test for escape_cahr()

Not sure about the style guide for plenary tests. So added it here.

Further, I think one should perhaps add more special cahracters to the
test for not changing characters, but not sure what the expected
coverage of the test should be.

* Fix stylua formatting

---------

Co-authored-by: Will Dean <[email protected]>
  • Loading branch information
phdah and wd60622 authored Jan 3, 2025
1 parent 20a6f69 commit ec0d2b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lua/octo/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,7 @@ end

--- Escapes a characters on a string to be used as a JSON string
function M.escape_char(string)
return string.gsub(string, '["\\]', {
['"'] = '\\"',
return string.gsub(string, "[\\]", {
["\\"] = "\\\\",
})
end
Expand Down
20 changes: 20 additions & 0 deletions lua/tests/plenary/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,23 @@ describe("Utils module:", function()
end)
end)
end)

describe("Utils escape_char(): ", function()
it("escapes backslash characters in a string", function()
local input = [[hello \\ world]]
local expected = [[hello \\\\ world]]
eq(expected, this.escape_char(input))
end)

it("returns the same string if no escape characters", function()
local input = [['hello/ ~^'*$"world%]]
local expected = [['hello/ ~^'*$"world%]]
eq(expected, this.escape_char(input))
end)

it("handles an empty string", function()
local input = ""
local expected = ""
eq(expected, this.escape_char(input))
end)
end)

0 comments on commit ec0d2b5

Please sign in to comment.