def test_deal_with_pesky_carriage_returns( self): # because Morse Code will live forever! source = "Given a string with spacies and \r\n another string " steps = Parser().parse_feature(source) step = steps[0] assert isinstance(step, Given) assert step.predicate == "a string with spacies and\nanother string"
def test_fail_informatively_on_bad_scenario_regex(self): filename = os.path.join(pwd, 'features/scenario_matching.feature') self._matching_pattern = '\\' with self.assertRaises(SyntaxError): self._ast = Parser().parse_file(filename, scenario=self._matching_pattern)
def test_two_dimensional_table_reconstruction(self): language = self._get_language() p = Parser(language=language).parse_features( self.assemble_short_scene_table()) step = p.steps[0].steps[0].steps[0] self.assertEqual(step.keyword + ': ' + step.predicate, step.reconstruction().strip())
def test_step_multiline_predicate(self): feature = '%s multiline predicate' % self.when_keyword language = self._get_language() steps = Parser(language=language).parse_feature(feature) matcher = self._get_default_machers() visitor = TestVisitor(self, matcher, NullFormatter()) visitor.visit(steps[0])
def test_given_a_string_with_given_in_it(self): source = "Given a string with Given in it \nAnd another string" steps = Parser().parse_feature(source) # ^ note the spacies step = steps[0] assert isinstance(step, Given) assert (step.predicate == "a string with Given in it" ) # <-- note spacies are gone
def run(filename, suite, as_str=None, scenario=r".*", verbose=False, show_all_missing=True, **kwargs): # NOQA """Parse file and run tests on given suite. :param str filename: file name :param unittest.TestCase suite: TestCase instance :param string as_str: None to use file or a string containing the feature to parse :param string scenario: a regex pattern to match the scenario to run :param boolean verbose: be verbose :param boolean show_all_missing: show all missing steps """ formatter = kwargs.get("formatter", None) if verbose and not formatter: if has_color_support(): formatter = ColorTextFormatter() else: formatter = PlainTextFormatter() kwargs["formatter"] = formatter kwargs["show_all_missing"] = show_all_missing if as_str is None: source = File(filename) else: source = Text(as_str, filename) feature = Parser().parse_features(source) return execute_script(feature, suite, scenario=scenario, **kwargs)
def test_language_directive(self): input = '# language: pl\n%s: prevent wild animals from eating us' % self.feature_keyword steps = Parser().parse_feature(input) step = steps[0] assert step.__class__ == Feature self.assertEqual(step.keyword, self.feature_keyword) self.assertEqual(step.predicate, 'prevent wild animals from eating us')
def run(filename, suite, as_str=None, scenario=r'.*', verbose=False, show_all_missing=True, **kwargs): # NOQA """Parse file and run tests on given suite. :param str filename: file name :param unittest.TestCase suite: TestCase instance :param string as_str: None to use file or a string containing the feature to parse :param string scenario: a regex pattern to match the scenario to run :param boolean verbose: be verbose :param boolean show_all_missing: show all missing steps """ formatter = kwargs.get('formatter', None) if verbose and not formatter: if has_color_support(): formatter = ColorTextFormatter() else: formatter = PlainTextFormatter() kwargs['formatter'] = formatter kwargs['show_all_missing'] = show_all_missing parser = Parser() ast = parser.parse_file(filename, scenario=scenario) if as_str is None \ else parser.parse_as_str(filename, as_str, scenario=scenario) return ast.evaluate(suite, **kwargs)
def test_record_filename(self): filename = features_dir / "morelia.feature" feature = Parser().parse_features(File(filename)) assert feature.__class__ == Feature assert str(filename) == feature.filename step = feature.steps[3].steps[1] assert str(filename) == step.get_filename()
def test___scenario(self): input = ' %s: with spacies' % self.scenario_keyword language = self._get_language() steps = Parser(language=language).parse_feature(input) step = steps[0] assert step.__class__ == Scenario self.assertEqual(step.keyword, self.scenario_keyword) self.assertEqual(step.predicate, 'with spacies')
def test_feature(self): language = self._get_language() input = '%s: prevent wild animals from eating us' % self.feature_keyword steps = Parser(language=language).parse_feature(input) step = steps[0] assert step.__class__ == Feature self.assertEqual(step.keyword, self.feature_keyword) self.assertEqual(step.predicate, 'prevent wild animals from eating us')
def test_given_a_string_with_a_line_breaker_followed_by_a_keyword(self): source = "Given a string \\\n And another string" steps = Parser().parse_feature(source) assert 1 == len(steps) step = steps[0] assert isinstance(step, Given) assert step.source == "Given a string \\\n And another string" assert step.predicate == "a string \\\n And another string"
def test_two_dimensional_table(self): global elements, factions elements = [] factions = [] language = self._get_language() Parser(language=language).parse_features( self.assemble_short_scene_table()).evaluate(self) self.assertEqual([['Pangolin', 'Glyptodon'], ['Pangea', 'Laurasia']], [factions, elements])
def test_record_filename(self): language = self._get_language() filename = pwd + '/features/morelia%s.feature' % (language or '') thang = Parser(language=language).parse_file(filename) feature = thang.steps[0] assert feature.__class__ == Feature assert feature.filename == filename step = feature.steps[3].steps[1] assert filename == step.get_filename()
def test_should_only_run_matching_scenarios(self): filename = os.path.join(pwd, 'features/scenario_matching.feature') self._matching_pattern = r'Scenario Matches [12]' self._ast = Parser().parse_file(filename, scenario=self._matching_pattern) scenario_matcher_re = re.compile(self._matching_pattern) for included_scenario in self._ast.steps[0].steps: self.assertIsNotNone( scenario_matcher_re.match(included_scenario.predicate))
def test_given_a_broken_string_with_excess_spacies(self): input = '%s a string with spacies and \n another string ' % self.given_keyword language = self._get_language() steps = Parser(language=language).parse_feature(input) step = steps[0] assert step.__class__ == Given self.assertEqual(step.keyword, self.given_keyword) self.assertEqual(step.predicate, 'a string with spacies and\nanother string')
def test_feature_with_scenario(self): source = """Feature: Civi-lie-zation Scenario: starz upon tharz bucks""" steps = Parser().parse_feature(source) step = steps[0] assert isinstance(step, Feature) assert step.predicate == "Civi-lie-zation" step = steps[1] assert isinstance(step, Scenario) assert step.predicate == "starz upon tharz bucks"
def test_deal_with_pesky_carriage_returns( self): # because Morse Code will live forever! input = '%s a string with spacies and \r\n another string ' % self.given_keyword language = self._get_language() steps = Parser(language=language).parse_feature(input) step = steps[0] assert step.__class__ == Given self.assertEqual(step.keyword, self.given_keyword) self.assertEqual(step.predicate, 'a string with spacies and\nanother string')
def test_scenario_with_steps_is_parsed(self): scenario = self.__sample_scenario() steps = Parser().parse_feature(scenario) scenario, given_step, and_step, when_step, then_step = steps assert scenario.predicate == "See all vendors" assert (given_step.predicate == "I am logged in as a user in the administrator role") assert and_step.predicate == "There are 3 vendors" assert when_step.predicate == "I go to the manage vendors page" assert then_step.predicate == "I should see the first 3 vendor names"
def step_Morelia_evaluates_the_file(self): self.diagnostic = None try: p = Parser() self.file_contents.replace( "\\#", "#") # note - this is how to unescape characters - DIY prefix = "Feature: Sample\nScenario: Sample\n" feature = p.parse_features(prefix + self.file_contents) execute_script(feature, self) except (MissingStepError, AssertionError) as e: self.diagnostic = str(e)
def test_another_two_dimensional_table(self): global crunks, zones crunks = [] zones = [] scene = self.assemble_scene_table_source( 'Step my milkshake brings all the boys to the yard\n') language = self._get_language() Parser(language=language).parse_features(scene).evaluate(self) self.assertEqual(['work', 'mall', 'jail', 'work', 'mall', 'jail'], crunks) self.assertEqual( ['beach', 'beach', 'beach', 'hotel', 'hotel', 'hotel'], zones)
def test_given_a_string_with_a_line_breaker_followed_by_a_keyword(self): input = '%(given)s a string \\\n And another string' % { 'given': self.given_keyword, 'and': self.and_keyword, } language = self._get_language() steps = Parser(language=language).parse_feature(input) assert 1 == len(steps) step = steps[0] assert step.__class__ == Given self.assertEqual(step.keyword, self.given_keyword) self.assertEqual(step.predicate, 'a string \\\n And another string')
def step_a_file_contains_statements_produce_diagnostics_( self, statements, diagnostics): r"a file contains (.+), it produces (.+)" try: statements = statements.replace("\\n", "\n") statements = statements.replace("\\", "") feature = Parser().parse_features(statements) execute_script(feature, self) raise Exception("we expect syntax errors here") # pragma: nocover except (SyntaxError, AssertionError) as e: e = e.args[0] self.assert_regex_contains(re.escape(diagnostics), e)
def step_Morelia_evaluates_the_file(self): self.diagnostic = None try: language = self._get_language() p = Parser(language=language) self.file_contents.replace( '\\#', '#') # note - this is how to unescape characters - DIY prefix = u'{}: Sample\n{}: Sample\n'.format( self.feature_keyword, self.scenario_keyword) p.parse_features(prefix + self.file_contents).evaluate(self) except (MissingStepError, AssertionError) as e: self.diagnostic = str(e)
def test_given_a_string_with_given_in_it(self): input = '%(given)s a string with Given in it \n%(and)s another string' % { 'given': self.given_keyword, 'and': self.and_keyword, } language = self._get_language() steps = Parser(language=language).parse_feature( input) # ^ note the spacies step = steps[0] assert step.__class__ == Given self.assertEqual(step.keyword, self.given_keyword) self.assertEqual( step.predicate, 'a string with Given in it') # <-- note spacies are gone
def step_a_file_contains_statements_produce_diagnostics_( self, statements, diagnostics): r'a file contains (.+), it produces (.+)' try: statements = statements.replace('\\n', '\n') statements = statements.replace('\\', '') language = self._get_language() p = Parser(language=language).parse_features(statements) p.evaluate(self) raise Exception('we expect syntax errors here') # pragma: nocover except (SyntaxError, AssertionError) as e: e = e.args[0] self.assert_regex_contains(re.escape(diagnostics), e)
def test_format_faults_like_python_errors(self): language = self._get_language() filename = pwd + '/features/morelia%s.feature' % (language or '') thang = Parser(language=language).parse_file(filename) step = thang.steps[0].steps[3].steps[1] assert filename == step.get_filename() omen = 'The Alpine glaciers move' diagnostic = step.format_fault(omen) parent_reconstruction = step.parent.reconstruction().strip('\n') reconstruction = step.reconstruction() expect = '\n File "%s", line %s, in %s\n %s\n%s' % \ (step.get_filename(), step.line_number, parent_reconstruction, reconstruction, omen) assert expect == diagnostic
def test_feature_with_scenario(self): input = '''%(feature)s: Civi-lie-zation %(scenario)s: starz upon tharz bucks''' % { 'feature': self.feature_keyword, 'scenario': self.scenario_keyword, } language = self._get_language() steps = Parser(language=language).parse_feature(input) step = steps[0] assert step.__class__ == Feature self.assertEqual(step.keyword, self.feature_keyword) self.assertEqual(step.predicate, 'Civi-lie-zation') step = steps[1] assert step.__class__ == Scenario self.assertEqual(step.keyword, self.scenario_keyword) self.assertEqual(step.predicate, 'starz upon tharz bucks')
def test_parse_scenario(self): scenario = self.pet_scenario() language = self._get_language() steps = Parser(language=language).parse_feature(scenario) step_0, step_1, step_2, step_3, step_4 = steps self.assertEqual(step_0.keyword, self.scenario_keyword) self.assertEqual(step_0.predicate, 'See all vendors') self.assertEqual(step_1.keyword, self.given_keyword) self.assertEqual(step_1.predicate, 'I am logged in as a user in the administrator role') self.assertEqual(step_2.keyword, self.and_keyword) self.assertEqual(step_2.predicate, 'There are 3 vendors') self.assertEqual(step_3.keyword, self.when_keyword) self.assertEqual(step_3.predicate, 'I go to the manage vendors page') self.assertEqual(step_4.keyword, self.then_keyword) self.assertEqual(step_4.predicate, 'I should see the first 3 vendor names')
def test_file_without_feature_defined(self): input = 'i be a newbie feature' language = self._get_language() p = Parser(language=language) try: p.parse_feature(input) assert False # should fail! # pragma: nocover except SyntaxError as e: e = e.args[0] try: feature_name = TRANSLATIONS[language].get( 'feature', self.feature_keyword) except KeyError: feature_name = self.feature_keyword else: feature_name = feature_name.replace('|', ' or ') self.assert_regex_contains( r'feature files must start with a %s' % feature_name, e)