Skip to content

Commit 0e2f8f0

Browse files
f18mlovesegfault
authored andcommitted
Add new testcase about multiline lines (#38)
1 parent 60f8142 commit 0e2f8f0

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

beautysh/beautysh.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def beautify_string(self, data, path=''):
3535
"""Beautify string (file part)."""
3636
tab = 0
3737
case_level = 0
38-
continue_line = False
38+
prev_line_had_continue = None
39+
continue_line = None
3940
open_brackets = 0
4041
in_here_doc = False
4142
defer_ext_quote = False
@@ -75,7 +76,18 @@ def beautify_string(self, data, path=''):
7576
# remove '#' comments
7677
test_record = re.sub(r'(\A|\s)(#.*)', '', test_record, 1)
7778

78-
if(in_here_doc): # pass on with no changes
79+
# detect whether this line ends with line continuation character:
80+
prev_line_had_continue = continue_line
81+
continue_line = re.search(r'\\$', stripped_record)
82+
83+
if continue_line:
84+
# remove contents of strings initiated on current line but that continue on next line
85+
# (in particular we need to ignore brackets they may contain!)
86+
test_record = re.sub(r'"[^"]*?\\$', '', test_record)
87+
88+
inside_multiline_continuation = (continue_line is not None) and (prev_line_had_continue is not None)
89+
90+
if(in_here_doc) or (inside_multiline_continuation): # pass on with no changes
7991
output.append(record)
8092
# now test for here-doc termination string
8193
if(re.search(here_string, test_record) and not
@@ -153,8 +165,7 @@ def beautify_string(self, data, path=''):
153165
test_record) is not None]
154166
net = inc - outc
155167
tab += min(net, 0)
156-
extab = tab + else_case + choice_case + (
157-
1 if continue_line and not open_brackets else 0)
168+
extab = tab + else_case + choice_case
158169
extab = max(0, extab)
159170
output.append((self.tab_str * self.tab_size * extab) +
160171
stripped_record)
@@ -166,7 +177,6 @@ def beautify_string(self, data, path=''):
166177
# count open brackets for line continuation
167178
open_brackets += len(re.findall(r'\[', test_record))
168179
open_brackets -= len(re.findall(r'\]', test_record))
169-
continue_line = re.search(r'\\$', stripped_record)
170180
line += 1
171181
error = (tab != 0)
172182
if(error):

tests/indent_test1_beautified.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ function quote_escapes1()
2525
echo "command to indent"
2626
}
2727

28+
function quote_escapes_line_continuation2()
29+
{
30+
local EVIL_STRING="{\"time\":{\"last\":{\"value\":1,\"type\":\"days\",\"clockType\":\"days\",\"start\":${TEST_START_TIME}},\"to\":${TEST_END_TIME},\"from\":${TEST_START_TIME_PLUS_ONE}}, \
31+
\"categories\":{\"category\":[${CATEGORY_FOR_EMPIRIX_EXMS_QUERY}]},\"requestedRows\":1000,\
32+
\"advancedFilters\":[{\"groupOperator\":\"or\",\"advancedFilter\":[{\"filter\":[{\"name\":\"${PARAM}\",\"type\":\"string\",\"condition\":\"=\",\"value\":[\"${VALUE}\"]}],\
33+
\"filterOperator\":\"and\"}],\"asdr\":\"common\"}]}"
34+
echo "command to indent"
35+
}
36+
2837
function complex_mix1()
2938
{
3039
while true; do

tests/indent_test1_raw.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ function quote_escapes1()
2525
echo "command to indent"
2626
}
2727

28+
function quote_escapes_line_continuation2()
29+
{
30+
local EVIL_STRING="{\"time\":{\"last\":{\"value\":1,\"type\":\"days\",\"clockType\":\"days\",\"start\":${TEST_START_TIME}},\"to\":${TEST_END_TIME},\"from\":${TEST_START_TIME_PLUS_ONE}}, \
31+
\"categories\":{\"category\":[${CATEGORY_FOR_EMPIRIX_EXMS_QUERY}]},\"requestedRows\":1000,\
32+
\"advancedFilters\":[{\"groupOperator\":\"or\",\"advancedFilter\":[{\"filter\":[{\"name\":\"${PARAM}\",\"type\":\"string\",\"condition\":\"=\",\"value\":[\"${VALUE}\"]}],\
33+
\"filterOperator\":\"and\"}],\"asdr\":\"common\"}]}"
34+
echo "command to indent"
35+
}
36+
2837
function complex_mix1()
2938
{
3039
while true; do

0 commit comments

Comments
 (0)