def _inject_cell_into_test(cell_source, test_source): """Inserts cell_source into test. The cell_source is inserted wherever %cell appears at the stripped start of a line. All lines of cell_source are prefixed with the same leading whitespace as %cell. Example: test: if x > 0: %cell # end of %cell assert x == 10 assert z == 15 cell: x*=5 z = x + \ 5 print(x) celltest: if x > 0: x*=5 z = x + \ 5 print(x) # end of %cell assert x == 10 assert z == 15 """ celltest_lines = [] for test_line in source2lines(test_source): cell_inj_span = get_cell_inj_span(test_line) if cell_inj_span is not None: prefix = test_line[0 : cell_inj_span[0]] for cell_line in source2lines(cell_source): celltest_lines.append(prefix + cell_line) suffix = test_line[cell_inj_span[1] : :] if len(suffix) > 0: if len(celltest_lines) == 0: celltest_lines.append("") celltest_lines[-1] += suffix else: celltest_lines.append(test_line) return lines2source(celltest_lines)
def test_get_cell_inj_span(test_line, expected): assert get_cell_inj_span(test_line) == expected