def from_string(new_feature, string, with_file=None, language=None): """Creates a new feature from string""" lines = strings.get_stripped_lines(string, ignore_lines_starting_with='#') tags = [] if not language: language = Language() strings.consume_tags_lines(lines, tags) found = len(re.findall(r'%s:[ ]*\w+' % language.feature, "\n".join(lines), re.U)) if found > 1: raise LettuceSyntaxError(with_file, 'A feature file must contain ONLY ONE feature!') elif found == 0: raise LettuceSyntaxError(with_file, 'Features must have a name. e.g: "Feature: This is my name"') while lines: matched = re.search(r'%s:(.*)' % language.feature, lines[0], re.I) if matched: name = matched.groups()[0].strip() break lines.pop(0) feature = new_feature(name=name, remaining_lines=lines, with_file=with_file, original_string=string, language=language, tags=tags) return feature
def from_string(new_scenario, string, tags=None, with_file=None, original_string=None, language=None): """ Creates a new scenario from string""" tags = tags or [] tags = tags[:] # ignoring comments no_comments = strings.get_stripped_lines(string, ignore_lines_starting_with='#') # extract tags strings.consume_tags_lines(no_comments, tags) string = "\n".join(no_comments) if not language: language = Language() splitted = strings.split_wisely(string, u"(%s):" % language.examples, True) string = splitted[0] keys = [] outlines = [] if len(splitted) > 1: parts = [l for l in splitted[1:] if l not in language.examples] part = "".join(parts) keys, outlines = strings.parse_hashes(strings.get_stripped_lines(part)) lines = strings.get_stripped_lines(string) scenario_line = lines.pop(0) for repl in (language.scenario_outline, language.scenario): scenario_line = strings.remove_it(scenario_line, u"(%s): " % repl) scenario = new_scenario( name=scenario_line, remaining_lines=lines, keys=keys, outlines=outlines, with_file=with_file, original_string=original_string, language=language, tags=tags ) return scenario
def from_string(new_feature, string, with_file=None, language=None): """Creates a new feature from string""" lines = strings.get_stripped_lines(string, ignore_lines_starting_with='#') tags = [] if not language: language = Language() strings.consume_tags_lines(lines, tags) found = len(re.findall(r'%s:[ ]*\w+' % language.feature, "\n".join(lines), re.U)) if found > 1: raise LettuceSyntaxError( with_file, 'A feature file must contain ONLY ONE feature!' ) elif found == 0: return None raise LettuceSyntaxError( with_file, 'Features must have a name. e.g: "Feature: This is my name"' ) while lines: matched = re.search(r'%s:(.*)' % language.feature, lines[0], re.I) if matched: name = matched.groups()[0].strip() break lines.pop(0) feature = new_feature(name=name, remaining_lines=lines, with_file=with_file, original_string=string, language=language, tags=tags) return feature
def test_consume_tag_lines(): "strings.consume_tags_lines simple case" # No tags tags = [] my_string = 'first line\n' \ 'second Line\n' \ 'third LIne\n' \ 'fourth lINe' lines = string.split(my_string, "\n") strings.consume_tags_lines(lines, tags) assert_equals(tags, []) assert_equals(len(lines), 4) # One line of tags tags = [] my_string = '@tags @on\n' \ 'second Line\n' \ 'third LIne\n' \ 'fourth lINe' lines = string.split(my_string, "\n") strings.consume_tags_lines(lines, tags) assert_equals(tags, ["tags", "on"]) assert_equals(len(lines), 3) # first line gone assert_equals(lines[0], "second Line") # Two lines of tags tags = [] my_string = '@tags @on\n' \ '@second @line\n' \ 'third LIne\n' \ 'fourth lINe' lines = string.split(my_string, "\n") strings.consume_tags_lines(lines, tags) assert_equals(tags, ["tags", "on", "second", "line"]) assert_equals(len(lines), 2) # first two lines gone assert_equals(lines[0], "third LIne") # Only first line counts tags = [] my_string = 'first line\n' \ '@second @line\n' \ 'third LIne\n' \ 'fourth lINe' lines = string.split(my_string, "\n") strings.consume_tags_lines(lines, tags) assert_equals(tags, []) assert_equals(len(lines), 4) assert_equals(lines[0], "first line")