def find_specs_in_module(self, feature_registration_transformer): with setup: spec_definition = """from nimoy.specification import Specification class JimbobSpec(Specification): pass class JonesSpec(Specification): pass class Bobson: pass """ node = ast.parse(spec_definition, mode='exec') found_metadata = [] with when: SpecTransformer(Location('some_spec.py'), found_metadata).visit(node) with then: len(found_metadata) == 2 found_metadata[0].name == 'JimbobSpec' found_metadata[1].name == 'JonesSpec' feature_registration_transformer.expect_called_once() 2 * feature_registration_transformer.return_value.visit()
def only_the_specified_feature_was_added(self, feature_block_transformer, feature_block_rule_enforcer): with setup: module_definition = """ class JSpec: def test_jim(self): pass def test_bob(self): pass def _jim(self): pass """ node = ast.parse(module_definition, mode='exec') metadata = SpecMetadata('jim') with when: FeatureRegistrationTransformer(Location('some_spec.py::test_bob'), metadata).visit(node) with then: len(metadata.features) == 1 metadata.features[0] == 'test_bob' feature_block_transformer.assert_called_once_with( metadata, 'test_bob') 1 * feature_block_transformer.return_value.visit( node.body[0].body[1]) feature_block_rule_enforcer.assert_called_once_with( metadata, 'test_bob', node.body[0].body[1]) 1 * feature_block_rule_enforcer.return_value.enforce_tail_end_rules( )
def where_methods_are_extracted_from_features(self): with given: spec_definition = """from nimoy.specification import Specification class JimbobSpec(Specification): def my_feature(self): with setup: a = value_of_a with expect: a == 5 with where: value_of_a = [5] """ node = ast.parse(spec_definition, mode='exec') with when: found_metadata = [] SpecTransformer(Location('some_spec.py'), found_metadata).visit(node) with then: len( node.body[1].body ) == 2 # The where method should have been extracted to class level node.body[1].body[1].name == 'my_feature_where'
def apply_chain(self, spec_transformer_mock): with given: node = {} with when: spec_metadata = ast_chain.apply(Location('some_spec.py'), node) with then: isinstance(spec_metadata, list) == True 1 * spec_transformer_mock.return_value.visit(node)
def spec_path_and_spec_name_and_feature_name(self): with when: location = Location('some_spec.py::SpecName::feature_name') with then: location.spec_path == 'some_spec.py' location.spec_name == 'SpecName' location.feature_name == 'feature_name'
def spec_path_and_feature_name(self): with when: location = Location('some_spec.py::feature_name') with then: location.spec_path == 'some_spec.py' hasattr(location, 'spec_name') == False location.feature_name == 'feature_name'
def spec_path(self): with when: location = Location('some_spec.py') with then: location.spec_path == 'some_spec.py' hasattr(location, 'spec_name') == False hasattr(location, 'feature_name') == False
def read(self): with given: location = Location('/path/to/spec.py') reader_mock = mock.Mock() reader_mock.read.return_value = 'class Jimbob:\n pass' with when: spec_contents = SpecReader(reader_mock).read([location]) with then: (location, contents) = next(spec_contents) location.spec_path == '/path/to/spec.py' contents == 'class Jimbob:\n pass' reader_mock.read.assert_called_once()
def a_feature_with_where_block_was_added(self, feature_block_transformer, feature_block_rule_enforcer): with setup: module_definition = """ class JSpec: def test_jim(self): pass """ node = ast.parse(module_definition, mode='exec') metadata = SpecMetadata('jim') def visit(feature_node): metadata.add_feature_variable('test_jim', 'var_a') metadata.add_where_function('test_jim', {'name': 'test_jim_where'}) where_function_mock = mock.Mock() where_function_mock.name = 'test_jim_where' feature_node.body.append(where_function_mock) feature_block_transformer.return_value.visit.side_effect = visit with when: FeatureRegistrationTransformer(Location('some_spec.py'), metadata).visit(node) with then: len(metadata.features) == 1 metadata.features[0] == 'test_jim' feature_block_transformer.assert_called_once_with( metadata, 'test_jim') feature_block_transformer.return_value.visit.assert_called_once_with( node.body[0].body[0]) feature_block_rule_enforcer.assert_called_once_with( metadata, 'test_jim', node.body[0].body[0]) feature_block_rule_enforcer.return_value.enforce_tail_end_rules.assert_called_once( ) len(node.body[0].body[0].body) == 1 len(node.body[0].body[0].args.args) == 2 node.body[0].body[0].args.args[1].arg == 'var_a'
def load(self): with given: ast_chain = mock.Mock() metadata = SpecMetadata('Jimbob') ast_chain.apply.return_value = [metadata] with when: returned_spec_metadata = SpecLoader(ast_chain).load([ (Location('/path/to/spec.py'), 'class Jimbob:\n pass') ]) with then: spec_metadata = next(returned_spec_metadata) spec_metadata.name == 'Jimbob' spec_metadata.owning_module != None ast_chain.apply.assert_called_once()
def run_spec_contents(spec_contents): str_io = StringIO() execution_framework = UnitTestExecutionFramework(stream=str_io) return SpecRunner._run_on_contents(execution_framework, [(Location('/fake/path.py'), spec_contents)])