Exemple #1
0
def parse_hashes(lines):
    lines = map(unicode, lines)
    keys = []
    hashes = []
    if lines:
        first_line = lines.pop(0)
        keys = strings.split_wisely(first_line, u"|", True)

        for line in lines:
            values = strings.split_wisely(line, u"|", True)
            hashes.append(dict(zip(keys, values)))

    return keys, hashes
Exemple #2
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):

        joined = u"\n".join(lines[1:])

        # replacing occurrencies of Scenario Outline, with just "Scenario"
        scenario_prefix = u'%s:' % self.language.first_of_scenario
        regex = re.compile(u"%s:\s" % self.language.scenario_separator, re.U | re.I)
        joined = regex.sub(scenario_prefix, joined)

        parts = strings.split_wisely(joined, scenario_prefix)

        description = u""

        if not re.search("^" + scenario_prefix, joined):
            description = parts[0]
            parts.pop(0)

        scenario_strings = [
            u"%s: %s" % (self.language.first_of_scenario, s) for s in parts if s.strip()
        ]
        kw = dict(
            original_string=original_string,
            with_file=with_file,
            language=self.language
        )

        scenarios = [Scenario.from_string(s, **kw) for s in scenario_strings]

        return scenarios, description
Exemple #3
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):
        joined = u"\n".join(lines[1:])

        self._check_scenario_syntax(lines, filename=with_file)
        # replacing occurrences of Scenario Outline, with just "Scenario"
        scenario_prefix = u"%s:" % self.language.first_of_scenario
        regex = re.compile(ur"%s:[\t\r\f\v]*" % self.language.scenario_separator, re.U | re.I | re.DOTALL)

        joined = regex.sub(scenario_prefix, joined)

        parts = strings.split_wisely(joined, scenario_prefix)

        description = u""
        background = None

        if not re.search("^" + scenario_prefix, joined):
            if not parts:
                raise LettuceSyntaxError(
                    with_file,
                    (
                        u"Features must have scenarios.\n"
                        "Please refer to the documentation available at http://lettuce.it for more information."
                    ),
                )

            description, background_lines = self._extract_desc_and_bg(parts[0])

            background = (
                background_lines
                and Background.from_string(
                    background_lines, self, with_file=with_file, original_string=original_string, language=self.language
                )
                or None
            )
            parts.pop(0)

        prefix = self.language.first_of_scenario

        upcoming_scenarios = [u"%s: %s" % (prefix, s) for s in parts if s.strip()]

        kw = dict(original_string=original_string, with_file=with_file, language=self.language)

        scenarios = []
        while upcoming_scenarios:
            current = self._strip_next_scenario_tags(upcoming_scenarios.pop(0))

            previous_scenario = None
            has_previous = len(scenarios) > 0

            if has_previous:
                previous_scenario = scenarios[-1]

            params = dict(previous_scenario=previous_scenario)

            params.update(kw)
            current_scenario = Scenario.from_string(current, **params)
            current_scenario.background = background
            scenarios.append(current_scenario)

        return background, scenarios, description
Exemple #4
0
    def from_string(new_scenario, string, with_file=None, original_string=None, language=None):
        """ Creates a new scenario from string"""

        if not language:
            language = Language()

        splitted = strings.split_wisely(string, u"(%s):" % language.examples, True)

        string = splitted[0]
        keys = []
        outlines = []
        if len(splitted) > 1:
            part = splitted[-1]
            keys, outlines = parse_hashes(strings.get_stripped_lines(part))

        lines = strings.get_stripped_lines(string)
        scenario_line = lines.pop(0)

        line = strings.remove_it(scenario_line, "(%s): " % language.scenario_outline)
        line = strings.remove_it(line, "(%s): " % language.scenario)

        scenario = new_scenario(
            name=line,
            remaining_lines=lines,
            keys=keys,
            outlines=outlines,
            with_file=with_file,
            original_string=original_string,
            language=language,
        )

        return scenario
Exemple #5
0
    def from_string(new_scenario, string, with_file=None, original_string=None, language=None):
        """ Creates a new scenario from string"""
        # ignoring comments
        string = "\n".join(strings.get_stripped_lines(string, ignore_lines_starting_with='#'))

        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
        )

        return scenario
Exemple #6
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):

        joined = u"\n".join(lines[1:])

        # replacing occurrencies of Scenario Outline, with just "Scenario"
        scenario_prefix = u'%s:' % self.language.first_of_scenario
        regex = re.compile(u"%s:\s" % self.language.scenario_separator,
                           re.U | re.I)
        joined = regex.sub(scenario_prefix, joined)

        parts = strings.split_wisely(joined, scenario_prefix)

        description = u""

        if not re.search("^" + scenario_prefix, joined):
            description = parts[0]
            parts.pop(0)

        scenario_strings = [
            u"%s: %s" % (self.language.first_of_scenario, s) for s in parts
            if s.strip()
        ]
        kw = dict(original_string=original_string,
                  with_file=with_file,
                  language=self.language)

        scenarios = [Scenario.from_string(s, **kw) for s in scenario_strings]

        return scenarios, description
Exemple #7
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):
        joined = u"\n".join(lines[1:])

        self._check_scenario_syntax(lines, filename=with_file)
        # replacing occurrences of Scenario Outline, with just "Scenario"
        scenario_prefix = u'%s:' % self.language.first_of_scenario
        regex = re.compile(
            ur"%s:[\t\r\f\v]*" % self.language.scenario_separator,
            re.U | re.I | re.DOTALL)

        joined = regex.sub(scenario_prefix, joined)

        parts = strings.split_wisely(joined, scenario_prefix)

        description = u""
        background = None

        if not re.search("^" + scenario_prefix, joined):
            description, background_lines = self._extract_desc_and_bg(parts[0])

            background = background_lines and Background.from_string(
                background_lines,
                self,
                with_file=with_file,
                original_string=original_string,
                language=self.language,
            ) or None
            parts.pop(0)

        prefix = self.language.first_of_scenario

        upcoming_scenarios = [
            u"%s: %s" % (prefix, s) for s in parts if s.strip()
        ]

        kw = dict(
            original_string=original_string,
            with_file=with_file,
            language=self.language,
        )

        scenarios = []
        while upcoming_scenarios:
            current = self._strip_next_scenario_tags(upcoming_scenarios.pop(0))

            previous_scenario = None
            has_previous = len(scenarios) > 0

            if has_previous:
                previous_scenario = scenarios[-1]

            params = dict(previous_scenario=previous_scenario, )

            params.update(kw)
            current_scenario = Scenario.from_string(current, **params)
            scenarios.append(current_scenario)

        return background, scenarios, description
Exemple #8
0
def test_split_wisely_splits_ignoring_case():
    "strings.split_wisely splits ignoring case"
    my_string = 'first line\n' \
        'second Line\n' \
        'third LIne\n' \
        'fourth lINe\n'

    assert_equals(strings.split_wisely(my_string, 'line', strip=False),
                  ['first ', 'second ', 'third ', 'fourth '])
Exemple #9
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):
        joined = u"\n".join(lines[1:])

        # replacing occurrences of Scenario Outline, with just "Scenario"
        scenario_prefix = u'%s:' % self.language.first_of_scenario
        regex = re.compile(
            u"%s:\s" % self.language.scenario_separator, re.U | re.I | re.DOTALL)

        joined = regex.sub(scenario_prefix, joined)

        parts = strings.split_wisely(joined, scenario_prefix)

        description = u""
        background = None

        if not re.search("^" + scenario_prefix, joined):
            description, background_lines = self._extract_desc_and_bg(parts[0])

            background = background_lines and Background.from_string(
                background_lines,
                self,
                with_file=with_file,
                original_string=original_string,
                language=self.language,
            ) or None
            parts.pop(0)

        prefix = self.language.first_of_scenario
        upcoming_scenarios = [
            u"%s: %s" % (prefix, s) for s in parts if s.strip()]

        kw = dict(
            original_string=original_string,
            with_file=with_file,
            language=self.language,
        )

        scenarios = []
        while upcoming_scenarios:
            current = self._strip_next_scenario_tags(upcoming_scenarios.pop(0))

            previous_scenario = None
            has_previous = len(scenarios) > 0

            if has_previous:
                previous_scenario = scenarios[-1]

            params = dict(
                previous_scenario=previous_scenario,
            )

            params.update(kw)
            current_scenario = Scenario.from_string(current, **params)
            scenarios.append(current_scenario)

        return background, scenarios, description
Exemple #10
0
def test_split_wisely_splits_ignoring_case_and_stripping():
    "strings.split_wisely splits ignoring case and stripping"
    my_string = """

             first line

       second Line

           third LIne
       fourth lINe

    """
    assert_equals(strings.split_wisely(my_string, "line", strip=True), ["first", "second", "third", "fourth"])
Exemple #11
0
def test_split_wisely_splits_ignoring_case_and_stripping():
    "strings.split_wisely splits ignoring case and stripping"
    my_string = '''

             first line

       second Line

           third LIne
       fourth lINe

    '''
    assert_equals(strings.split_wisely(my_string, 'line', strip=True),
                  ['first', 'second', 'third', 'fourth'])
Exemple #12
0
def test_split_wisely_splits_ignoring_case():
    "strings.split_wisely splits ignoring case"
    my_string = 'first line\n' \
        'second Line\n' \
        'third LIne\n' \
        'fourth lINe\n'

    assert_equals(
        strings.split_wisely(my_string, 'line', strip=False),
        [
            'first ',
            'second ',
            'third ',
            'fourth '
        ]
    )
Exemple #13
0
    def _extract_desc_and_bg(self, joined):
        if not re.search(self.language.background, joined):
            return joined, None

        parts = strings.split_wisely(joined, "(%s):\s*" % self.language.background)

        description = parts.pop(0)

        if not re.search(self.language.background, description):
            if parts:
                parts = parts[1:]
        else:
            description = ""

        background_string = "".join(parts).splitlines()
        return description, background_string
Exemple #14
0
    def _extract_desc_and_bg(self, joined):
        if not re.search(self.language.background, joined):
            return joined, None

        parts = strings.split_wisely(joined,
                                     "(%s):\s*" % self.language.background)

        description = parts.pop(0)

        if not re.search(self.language.background, description):
            if parts:
                parts = parts[1:]
        else:
            description = ""

        background_string = "".join(parts).splitlines()
        return description, background_string
Exemple #15
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):
        # replacing occurrences of Scenario Outline, with just "Scenario"
        joined = u"\n".join(lines[1:])
        scenario_prefix = u'%s:' % self.language.first_of_scenario
        regex = re.compile(
            u"%s:\s" % self.language.scenario_separator, re.U | re.I | re.DOTALL)

        joined = regex.sub(scenario_prefix, joined)

        parts = strings.split_wisely(joined, scenario_prefix)

        description = u""

        if not re.search("^" + scenario_prefix, joined):
            description = parts[0]
            parts.pop(0)

        prefix = self.language.first_of_scenario
        upcoming_scenarios = [
            u"%s: %s" % (prefix, s) for s in parts if s.strip()]

        kw = dict(
            original_string=original_string,
            with_file=with_file,
            language=self.language,
        )

        scenarios = []
        while upcoming_scenarios:
            current = self._strip_next_scenario_tags(upcoming_scenarios.pop(0))

            previous_scenario = None
            has_previous = len(scenarios) > 0

            if has_previous:
                previous_scenario = scenarios[-1]

            params = dict(
                previous_scenario=previous_scenario,
            )

            params.update(kw)
            current_scenario = Scenario.from_string(current, **params)
            scenarios.append(current_scenario)

        return scenarios, description
Exemple #16
0
    def from_string(new_scenario,
                    string,
                    with_file=None,
                    original_string=None,
                    language=None,
                    previous_scenario=None):
        """ Creates a new scenario from string"""
        # ignoring comments
        string = "\n".join(
            strings.get_stripped_lines(string, ignore_lines_starting_with='#'))

        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).strip()

        for repl in (language.scenario_outline, language.scenario):
            scenario_line = strings.remove_it(scenario_line,
                                              u"(%s): " % repl).strip()

        scenario = new_scenario(
            name=scenario_line,
            remaining_lines=lines,
            keys=keys,
            outlines=outlines,
            with_file=with_file,
            original_string=original_string,
            language=language,
            previous_scenario=previous_scenario,
        )

        return scenario
Exemple #17
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):
        joined = u"\n".join(lines[1:])

        # replacing occurrences of Scenario Outline, with just "Scenario"
        scenario_prefix = u'%s:' % self.language.first_of_scenario
        regex = re.compile(u"%s:\s" % self.language.scenario_separator, re.U | re.I)
        joined = regex.sub(scenario_prefix, joined)

        parts = strings.split_wisely(joined, scenario_prefix)

        description = u""

        tags_array = []
        first_tags = []
        if not re.search("^" + scenario_prefix, joined):
            description = parts[0]
            first_tags, description_array = strings.steal_tags_from_lines(description.split("\n"))
            description = u"\n".join(description_array)
            parts.pop(0)
        tags_array.append(first_tags)
        
        scenario_strings = [
            u"%s: %s" % (self.language.first_of_scenario, s) for s in parts if s.strip()
        ]
        tmp = []
        for s in scenario_strings:
            split_lines = s.split("\n")
            tags, minus_tags = strings.steal_tags_from_lines(split_lines)
            tmp.append(u"\n".join(minus_tags))
            tags_array.append(tags)
        scenario_strings = tmp

        kw = dict(
            original_string=original_string,
            with_file=with_file,
            language=self.language
        )

        scenarios = []
        for s in scenario_strings:
            scenario_tags = tags_array[0] + self.tags
            tags_array.pop(0)
            scenarios.append(Scenario.from_string(s, scenario_tags, **kw))
        return scenarios, description
Exemple #18
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):
        joined = u"\n".join(lines[1:])

        scenario_prefix = u'%s:' % self.language.first_of_scenario
        background_prefix = u'%s:' % self.language.background

        # replacing occurrencies of Scenario Outline, with just "Scenario"
        regex = re.compile(u"%s:\s" % self.language.scenario_separator,
                           re.U | re.I)
        joined = regex.sub(scenario_prefix, joined)

        # Split on Background: and Scenario:
        combo_prefix = u'%s:|%s:' % (self.language.first_of_scenario,
                                     self.language.background)
        parts = strings.split_wisely(joined, combo_prefix)

        description = u""
        background = None
        kw = dict(original_string=original_string,
                  with_file=with_file,
                  language=self.language)

        description_prefix = u'^%s:|^%s:' % (self.language.first_of_scenario,
                                             self.language.background)

        if not re.search(description_prefix, joined):
            description = parts[0]
            parts.pop(0)

        if re.search(background_prefix, joined):
            bg = "%s:\n%s" % (self.language.background, parts.pop(0).strip())
            background = Background.from_string(bg, **kw)

        scenario_strings = [
            u"%s: %s" % (self.language.first_of_scenario, s) for s in parts
            if s.strip()
        ]

        scenarios = [Scenario.from_string(s, **kw) for s in scenario_strings]

        return scenarios, background, description
Exemple #19
0
def test_split_wisely_splits_ignoring_case_and_stripping():
    "strings.split_wisely splits ignoring case and stripping"
    my_string = '''

             first line

       second Line

           third LIne
       fourth lINe

    '''
    assert_equals(
        strings.split_wisely(my_string, 'line', strip=True),
        [
            'first',
            'second',
            'third',
            'fourth'
        ]
    )
Exemple #20
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):
        # replacing occurrences of Scenario Outline, with just "Scenario"
        joined = u"\n".join(lines[1:])
        scenario_prefix = u'%s:' % self.language.first_of_scenario

        background_prefix = u'%s:' % self.language.background
        background_exists = 0
        parts = strings.split_wisely(joined, background_prefix)

        description = u""
        if len(parts) == 1 and re.search(u"^" + background_prefix, joined):
            joined = parts[0]
            background_exists = 1
        if len(parts) == 2:
            description = parts[0]
            if re.search(u"^" + scenario_prefix, description, re.U | re.M | re.DOTALL):
                raise LettuceSyntaxError(with_file,
                    "Background section should be before any scenario")
            joined = parts[1]
            background_exists = 1
        if len(parts) == 3:
            raise LettuceSyntaxError(with_file,
                "There should be only one background section")

        regex = re.compile(
            u"%s:\s" % self.language.scenario_separator, re.U | re.I | re.DOTALL)

        joined = regex.sub(scenario_prefix, joined)

        parts = strings.split_wisely(joined, scenario_prefix)

        background_strings = u""
        if background_exists == 1:
            background_strings = parts[0]
            parts.pop(0)
        else:
            description = u""

            if not re.search("^" + scenario_prefix, joined):
                description = parts[0]
                parts.pop(0)

        prefix = self.language.first_of_scenario
        upcoming_scenarios = [
            u"%s: %s" % (prefix, s) for s in parts if s.strip()]

        kw = dict(
            original_string=original_string,
            with_file=with_file,
            language=self.language,
        )

        scenarios = []
        while upcoming_scenarios:
            current = self._strip_next_scenario_tags(upcoming_scenarios.pop(0))

            previous_scenario = None
            has_previous = len(scenarios) > 0

            if has_previous:
                previous_scenario = scenarios[-1]

            params = dict(
                previous_scenario=previous_scenario,
            )

            params.update(kw)
            current_scenario = Scenario.from_string(current, **params)
            scenarios.append(current_scenario)

        if background_exists==1:
            background = Background(background_strings)
        else:
            background = None

        return scenarios, description, background
Exemple #21
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):
        # replacing occurrences of Scenario Outline, with just "Scenario"
        joined = u""
        for line in lines[1:]:
            if line.find('@') != 0:
                joined += u"\n" + line

        background_prefix = u'%s:' % self.language.background
        background_exists = 0
        regex = re.compile(
            u"%s:" % self.language.background, re.U | re.I)
        parts = strings.split_wisely(joined, background_prefix)

        description = u""
        if len(parts) == 1 and re.search("^\s" + background_prefix, joined):
            joined = parts[0]
            background_exists = 1

        if len(parts) == 2:
            description = parts[0]
            joined = parts[1]
            background_exists = 1

        scenario_prefix = u'%s:' % self.language.first_of_scenario
        regex = re.compile(
            u"%s:\s" % self.language.scenario_separator, re.U | re.I)
        joined = regex.sub(scenario_prefix, joined)

        parts = strings.split_wisely(joined, scenario_prefix)

        if background_exists == 1:
            background_strings = parts[0]
            parts.pop(0)
        else:
            description = u""

            if not re.search("^\s" + scenario_prefix, joined):
                description = parts[0]
                parts.pop(0)

        scenario_strings = [u"%s: %s" % (self.language.first_of_scenario, s) \
                            for s in parts if s.strip()]

        if background_exists == 1:
            k = 0
            for s in scenario_strings:
                scenario_strings[k] += background_strings
                pos = s.find("\n")
                scenario_strings[k] = s[0:pos+1] + background_strings + "\n" + s[pos+1:len(s)]
                k += 1

        foundTagline = False
        tagline = u''
        taglinesByScenarios = []
        for row in lines:
            if row.find(scenario_prefix) == 0:
                if foundTagline:
                    if self.tags: tagline += u' ' + self.tags
                    taglinesByScenarios.append(tagline)
                    foundTagline = False
                else:
                    tagline = None
                    if self.tags: tagline = self.tags
                    taglinesByScenarios.append(tagline)

            if row.find('@') == 0:
                foundTagline = True
                tagline = row

        kw = dict(
            original_string=original_string,
            with_file=with_file,
            language=self.language,
        )

        k = 0
        scenarios = []
        for s in scenario_strings:
            kw['tags_string'] = None
            if k < len(taglinesByScenarios): kw['tags_string'] =  taglinesByScenarios[k]
            scenarios.append(Scenario.from_string(s, **kw))
            k += 1

        return scenarios, description
Exemple #22
0
def test_split_wisely_splits_ignoring_case():
    "strings.split_wisely splits ignoring case"
    my_string = "first line\n" "second Line\n" "third LIne\n" "fourth lINe\n"

    assert_equals(strings.split_wisely(my_string, "line", strip=False), ["first ", "second ", "third ", "fourth "])
Exemple #23
0
    def _parse_remaining_lines(self, lines, original_string, with_file=None):
        joined = u"\n".join(lines[1:])

        self._check_scenario_syntax(lines, filename=with_file)
        # replacing occurrences of Scenario Outline, with just "Scenario"
        scenario_prefix = u'%s:' % self.language.first_of_scenario
        regex = re.compile(
            ur"%s:[\t\r\f\v]*" % self.language.scenario_separator,
            re.U | re.I | re.DOTALL)

        joined = regex.sub(scenario_prefix, joined)

        parts = strings.split_wisely(joined, scenario_prefix)

        description = u""
        background = None
        tags_scenario = []

        if not re.search("^" + scenario_prefix, joined):
            if not parts:
                raise LettuceSyntaxError(with_file, (
                    u"Features must have scenarios.\n"
                    "Please refer to the documentation available at http://lettuce.it for more information."
                ))
            tags_scenario, description_and_background = self._extract_tags(
                parts[0])
            description, background_lines = self._extract_desc_and_bg(
                description_and_background)

            background = background_lines and Background.from_string(
                background_lines,
                self,
                with_file=with_file,
                original_string=original_string,
                language=self.language,
            ) or None
            parts.pop(0)

        prefix = self.language.first_of_scenario

        upcoming_scenarios = [
            u"%s: %s" % (prefix, s) for s in parts if s.strip()
        ]

        kw = dict(
            original_string=original_string,
            with_file=with_file,
            language=self.language,
        )

        scenarios = []
        while upcoming_scenarios:
            tags_next_scenario, current = self._extract_tags(
                upcoming_scenarios[0])
            current = self._strip_next_scenario_tags(upcoming_scenarios.pop(0))

            params = dict(tags=tags_scenario, )

            params.update(kw)
            current_scenario = Scenario.from_string(current, **params)
            current_scenario.background = background
            scenarios.append(current_scenario)
            tags_scenario = tags_next_scenario

        return background, scenarios, description