s[i] = repr(c)[1:-1]
self._value = "".join(s)
+ def multiline(self) -> str:
+ """
+ Lua multiline string is enclosed in double square brackets '[[ ]]'.
+ This method makes sure that double square brackets are escaped.
+ """
+
+ replace = {
+ "[[": r"\[\[",
+ "]]": r"\]\]",
+ }
+
+ ml = self._orig_value
+ for s, r in replace.items():
+ ml = ml.replace(s, r)
+ return ml
+
class EscapedStr32B(EscapedStr, StringLengthBase):
"""
str_template = Template("'{{ string }}'")
+str_multiline_template = Template(
+ """[[
+{{ string.multiline() }}
+]]"""
+)
+
+
+@pytest.mark.parametrize(
+ "val,exp",
+ [
+ ("\a\b\f\n\r\t\v\\", "\a\b\f\n\r\t\v\\"),
+ ("[[ test ]]", r"\[\[ test \]\]"),
+ ("[ [ test ] ]", r"[ [ test ] ]"),
+ ],
+)
+def test_escaped_str_multiline(val: Any, exp: str):
+ class TestSchema(ConfigSchema):
+ pattern: EscapedStr
+
+ d = TestSchema({"pattern": val})
+ assert (
+ str_multiline_template.render(string=d.pattern)
+ == f"""[[
+{exp}
+]]"""
+ )
+
+
@pytest.mark.parametrize(
"val,exp",
[