def exercise_strip_spaces_separate_strings(): from fable.read import Error, source_line, strip_spaces_separate_strings import itertools global_line_index_generator = itertools.count() def slc(cmbnd): return [ source_line( global_line_index_generator=global_line_index_generator, file_name="str", line_number=i+1, text=" "+line) for i,line in enumerate(cmbnd.splitlines())] def check(cmbnd, expected_code, expected_strings): for cmbnd_work,expected_strings_work in [ (cmbnd, expected_strings), (cmbnd.replace("'", '"'), [s.replace("'", '"') for s in expected_strings])]: ssl = strip_spaces_separate_strings(source_line_cluster=slc(cmbnd_work)) assert not show_diff(ssl.code, expected_code) assert len(ssl.strings) == len(ssl.string_indices) assert ssl.strings == expected_strings_work expected_string_indices = [] for i,c in enumerate(expected_code): if (c == "'" or c == '"'): expected_string_indices.append(i) assert ssl.string_indices == expected_string_indices if (cmbnd.find('"') > 0): break check("a = 0", "a=0", []) check("a = ''", "a='", [""]) check("a = '\"'", "a='", ["\""]) check("a = \"'\"", "a='", ["'"]) check("a = 'x'", "a='", ["x"]) check("a = ' x ' ", "a='", [" x "]) check("a = ''''", "a='", ["'"]) check("call foo('')", "callfoo(')", [""]) check("call foo('''')", "callfoo(')", ["'"]) check("c a l l f o o ( '''' ) ", "callfoo(')", ["'"]) check(" c a l l f o o ( '''' ) ", "callfoo(')", ["'"]) check(" C A L LF O O ( ' abc '''' def ''' , ' g''h''i''' , X ) ", "callfoo(',',x)", [" abc '' def '", " g'h'i'"]) check("a = '\n'", "a='", [""]) check("a = 'x\n'", "a='", ["x"]) check("a = '\ny'", "a='", ["y"]) check("a = 'x\ny'", "a='", ["xy"]) check("a = '''\n'", "a='", ["'"]) check("a = '\n'''", "a='", ["'"]) check("a = '''\n'''", "a='", ["''"]) # for cmbnd,q,nd in [("'abc", "'", 9), ('x="', '"', 11)]: try: strip_spaces_separate_strings(source_line_cluster=slc(cmbnd)) except Error, e: assert not show_diff(str(e), """\ Missing terminating %s character: at str(1): | %s| %s^""" % (q, cmbnd, "-"*nd)) else: raise Exception_expected
def check(cmbnd, expected_code, expected_strings): for cmbnd_work,expected_strings_work in [ (cmbnd, expected_strings), (cmbnd.replace("'", '"'), [s.replace("'", '"') for s in expected_strings])]: ssl = strip_spaces_separate_strings(source_line_cluster=slc(cmbnd_work)) assert not show_diff(ssl.code, expected_code) assert len(ssl.strings) == len(ssl.string_indices) assert ssl.strings == expected_strings_work expected_string_indices = [] for i,c in enumerate(expected_code): if (c == "'" or c == '"'): expected_string_indices.append(i) assert ssl.string_indices == expected_string_indices if (cmbnd.find('"') > 0): break
def check(cmbnd, expected_code, expected_strings): for cmbnd_work, expected_strings_work in [ (cmbnd, expected_strings), (cmbnd.replace("'", '"'), [s.replace("'", '"') for s in expected_strings]) ]: ssl = strip_spaces_separate_strings( source_line_cluster=slc(cmbnd_work)) assert not show_diff(ssl.code, expected_code) assert len(ssl.strings) == len(ssl.string_indices) assert ssl.strings == expected_strings_work expected_string_indices = [] for i, c in enumerate(expected_code): if (c == "'" or c == '"'): expected_string_indices.append(i) assert ssl.string_indices == expected_string_indices if (cmbnd.find('"') > 0): break
def exercise_strip_spaces_separate_strings(): from fable.read import Error, source_line, strip_spaces_separate_strings import itertools global_line_index_generator = itertools.count() def slc(cmbnd): return [ source_line( global_line_index_generator=global_line_index_generator, file_name="str", line_number=i + 1, text=" " + line) for i, line in enumerate(cmbnd.splitlines()) ] def check(cmbnd, expected_code, expected_strings): for cmbnd_work, expected_strings_work in [ (cmbnd, expected_strings), (cmbnd.replace("'", '"'), [s.replace("'", '"') for s in expected_strings]) ]: ssl = strip_spaces_separate_strings( source_line_cluster=slc(cmbnd_work)) assert not show_diff(ssl.code, expected_code) assert len(ssl.strings) == len(ssl.string_indices) assert ssl.strings == expected_strings_work expected_string_indices = [] for i, c in enumerate(expected_code): if (c == "'" or c == '"'): expected_string_indices.append(i) assert ssl.string_indices == expected_string_indices if (cmbnd.find('"') > 0): break check("a = 0", "a=0", []) check("a = ''", "a='", [""]) check("a = '\"'", "a='", ["\""]) check("a = \"'\"", "a='", ["'"]) check("a = 'x'", "a='", ["x"]) check("a = ' x ' ", "a='", [" x "]) check("a = ''''", "a='", ["'"]) check("call foo('')", "callfoo(')", [""]) check("call foo('''')", "callfoo(')", ["'"]) check("c a l l f o o ( '''' ) ", "callfoo(')", ["'"]) check(" c a l l f o o ( '''' ) ", "callfoo(')", ["'"]) check(" C A L LF O O ( ' abc '''' def ''' , ' g''h''i''' , X ) ", "callfoo(',',x)", [" abc '' def '", " g'h'i'"]) check("a = '\n'", "a='", [""]) check("a = 'x\n'", "a='", ["x"]) check("a = '\ny'", "a='", ["y"]) check("a = 'x\ny'", "a='", ["xy"]) check("a = '''\n'", "a='", ["'"]) check("a = '\n'''", "a='", ["'"]) check("a = '''\n'''", "a='", ["''"]) # for cmbnd, q, nd in [("'abc", "'", 9), ('x="', '"', 11)]: try: strip_spaces_separate_strings(source_line_cluster=slc(cmbnd)) except Error as e: assert not show_diff( str(e), """\ Missing terminating %s character: at str(1): | %s| %s^""" % (q, cmbnd, "-" * nd)) else: raise Exception_expected