Skip to content

Commit

Permalink
expression: fix SimpleString's quote method
Browse files Browse the repository at this point in the history
  • Loading branch information
zzl0 committed Jun 17, 2022
1 parent 6f28c79 commit a7208c0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
12 changes: 9 additions & 3 deletions libcst/_nodes/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,14 +656,20 @@ def quote(self) -> StringQuoteLiteral:
if len(quote) == 2:
# Let's assume this is an empty string.
quote = quote[:1]
elif len(quote) == 6:
# Let's assume this is an empty triple-quoted string.
elif 3 < len(quote) <= 6:
# Let's assume this can be one of the following:
# >>> """"foo"""
# '"foo'
# >>> """""bar"""
# '""bar'
# >>> """"""
# ''
quote = quote[:3]

if len(quote) not in {1, 3}:
# We shouldn't get here due to construction validation logic,
# but handle the case anyway.
raise Exception("Invalid string {self.value}")
raise Exception(f"Invalid string {self.value}")

# pyre-ignore We know via the above validation that we will only
# ever return one of the four string literals.
Expand Down
31 changes: 31 additions & 0 deletions libcst/_nodes/tests/test_simple_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import unittest
import libcst as cst


class TestSimpleString(unittest.TestCase):

def test_quote(self) -> None:
test_cases = [
('"a"', '"'),
("'b'", "'"),

('""', '"'),
("''", "'"),

('"""c"""', '"""'),
("'''d'''", "'''"),

('""""e"""', '"""'),
("''''f'''", "'''"),

('"""""g"""', '"""'),
("'''''h'''", "'''"),

('""""""', '"""'),
("''''''", "'''"),
]

for s, expected_quote in test_cases:
simple_string = cst.SimpleString(s)
actual = simple_string.quote
self.assertEqual(expected_quote, actual)

0 comments on commit a7208c0

Please sign in to comment.