Skip to content

Commit

Permalink
Make escaping strings more consistent.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnwpark committed Mar 15, 2024
1 parent d7515df commit c1d90b4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
12 changes: 5 additions & 7 deletions edb/edgeql/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@


def escape_string(s: str) -> str:
split = re.split(r"(\n|\\\\|\\')", s)

if len(split) == 1:
return s.replace(r"'", r"\'")

return ''.join((r if i % 2 else r.replace(r"'", r"\'"))
for i, r in enumerate(split))
result = s
result = result.replace('\\', '\\\\')
result = result.replace("'", "\\'")
result = result.replace('"', '\\"')
return result


def quote_literal(string: str) -> str:
Expand Down
26 changes: 26 additions & 0 deletions tests/edgeql/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# This source file is part of the EdgeDB open source project.
#
# Copyright 2016-present MagicStack Inc. and the EdgeDB authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


import unittest


def suite():
test_loader = unittest.TestLoader()
test_suite = test_loader.discover('.', pattern='test_*.py')
return test_suite
44 changes: 44 additions & 0 deletions tests/edgeql/test_quote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# This source file is part of the EdgeDB open source project.
#
# Copyright 2018-present MagicStack Inc. and the EdgeDB authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import unittest

import edb.edgeql.quote as qlquote

class QuoteTests(unittest.TestCase):

def test_escape_string(self):
self.assertEqual(qlquote.escape_string(''), '')
self.assertEqual(qlquote.escape_string('abc'), 'abc')
self.assertEqual(qlquote.escape_string('\n'), '\n')
self.assertEqual(qlquote.escape_string('\t'), '\t')
self.assertEqual(qlquote.escape_string('\\'), '\\\\')
self.assertEqual(qlquote.escape_string('\\n'), '\\\\n')
self.assertEqual(qlquote.escape_string('\\t'), '\\\\t')
self.assertEqual(qlquote.escape_string('\\\\'), '\\\\\\\\')
self.assertEqual(qlquote.escape_string('"'), '\\"')
self.assertEqual(qlquote.escape_string("'"), "\\'")
self.assertEqual(qlquote.escape_string('\\"'), '\\\\\\"')
self.assertEqual(qlquote.escape_string("\\'"), "\\\\\\'")
self.assertEqual(qlquote.escape_string(
'abc"efg\nhij\'klm\\nop"'),
'abc\\"efg\nhij\\\'klm\\\\nop\\"')

def test_quote_string(self):
self.assertEqual(qlquote.quote_literal("abc"), "'abc'")
self.assertEqual(qlquote.quote_literal("abc\\\n"), "'abc\\\\\n'")

0 comments on commit c1d90b4

Please sign in to comment.