PK!çPPspecify/__init__.pyfrom .object_behavior import ObjectBehavior from .mocking_decorator import mock PK! }}specify/__main__.pyimport argparse from specify.runner import run_specs from specify.spec_finder import get_spec parser = argparse.ArgumentParser() parser.add_argument('paths', help='The files where the specs are located', nargs='+') paths = parser.parse_args().paths specs = [get_spec(path) for path in paths] specs = [class_obj for class_obj in specs if class_obj is not None] run_specs(specs) PK!ނAspecify/builtin_matchers.pyfrom .exceptions import MatcherError, MatcherNotFoundError def be(value, expected): if value is not expected: raise MatcherError(f"Expected {value} to be {expected}") def be_like(value, expected): if value != expected: raise MatcherError(f"Expected {value} to be like {expected}") def not_be(value, not_expected): if value is not_expected: raise MatcherError(f"Expected {value} not to be {not_expected}") def not_be_like(value, not_expected): if value == not_expected: raise MatcherError(f"Expected {value} not to be like {not_expected}") def be_an_instance_of(value, cls): if not isinstance(value, cls): raise MatcherError(f"Expected {value} to be an instance of {cls.__qualname__}, got {type(value)}") def have_length(value, length): if len(value) != length: raise MatcherError(f"Expected value length to be {length} but length is {len(value)}") def get_matcher(type): items = { 'be': be, 'not_be': not_be, 'return': be, 'not_return': not_be, 'be_like': be_like, 'not_be_like': not_be_like, 'return_like': be_like, 'not_return_like': not_be_like, 'be_an_instance_of': be_an_instance_of, 'return_an_instance_of': be_an_instance_of, 'have_length': have_length } if type not in items: raise MatcherNotFoundError(f'No builtin matcher exists with type "{type}"') return items[type] PK!ﺍuspecify/exceptions.pyclass MatcherError(AssertionError): pass class CustomMatcherError(AssertionError): pass class MatcherNotFoundError(NotImplementedError): pass PK!%-JJspecify/mocking_decorator.pyfrom prophepy import prophesize def mock(cls): def func_wrapper(func): mocked_object = prophesize(cls) def arg_wrapper(*args): self = args[0] self._mocked_objects.append(mocked_object) return func(*args, mocked_object) return arg_wrapper return func_wrapper PK!>% % specify/object_behavior.pyfrom prophepy import Mock from .subject import Subject from .utils import reveal_if_needed, map_to_dict class ObjectBehavior: ''' This is the class used as parent for every spec. ''' def __init__(self): self._mocked_objects = [] def _describe(self, cls): ''' Needed first to tell specify which class to spec ''' obj = cls.__new__(cls) self.__obj = Subject(obj, self) def _be_constructed_with(self, *args, **kwargs): ''' Launch the init method on the specced class with the provided arguments ''' revealed_args = map(reveal_if_needed, args) revealed_kwargs = map_to_dict(reveal_if_needed, kwargs) self.__obj._get_value().__init__(*revealed_args, **revealed_kwargs) def _get_wrapped_object(self): ''' Access to the subject (Subject class) ''' return self.__obj def __getattr__(self, attr_name): ''' The magic happens here, when it comes to _should_ methods. If the method is a _should_ one, it will try to find a matcher (a builtin or a custom one). Otherwise, it really calls the specced object method, and it returns a Subject instance. ''' matcher_prefix = '_should_' if attr_name.startswith(matcher_prefix): matchers_keys = self.matchers.keys() key = attr_name[len(prefix):] if key in matchers_keys: custom_matcher = self.matchers()[key] def custom_matcher_wrapper(*args): return self.__obj.match_with_custom_matcher( key, custom_matcher, *args ) return custom_matcher_wrapper def subject_wrapper(*args, **kwargs): return getattr(self.__obj, attr_name)(*args, **kwargs) return subject_wrapper def _matchers(self): ''' By default, empty dict of custom matchers ''' return {} def _let(self): ''' By default, nothing happens before a test, but can be overriden. ''' pass def _let_go(self): ''' By default, nothing happens after a test, but can be overriden. ''' pass PK!o/specify/result.pyfrom typing import List class ResultLine: ''' A result is made of several result lines ''' def __init__(self, spec_class, test_name, exception=None): ''' Take the spec_class (object_behavior), the test name (it_…), and the exception if any ''' self.spec_class = spec_class self.test_name = test_name self.exception = exception class Result: ''' The final Result object that will contain the whole results ''' def __init__(self, result_lines: List[ResultLine]): self.result_lines = result_lines def _prettify_test_name(self, test_name): ''' "it_works_correctly" becomes "it works correctly" ''' return test_name.replace('_', ' ') def _print_line(self, num: int, line: ResultLine): ''' Take an index and a result line, and transform it to a valid TAP line ("ok 1 - Test name…") ''' if line.exception is None: prefix = 'ok' else: prefix = 'not ok' desc = f"{line.spec_class.__qualname__}: {self._prettify_test_name(line.test_name)}" text = f"{prefix} {num} - {desc}" if line.exception is not None: text = text + "\n ---" text = text + "\n " + line.exception text = text + "\n ..." return text def export(self): ''' Export the final output for a valid TAP result ''' text = "TAP version 13\n" last_elem = len(self.result_lines) text = text + f"1..{last_elem}\n" for index, line in enumerate(self.result_lines): text = text + "\n" + self._print_line(index+1, line) return text PK!CU specify/runner.pyfrom typing import List from specify.object_behavior import ObjectBehavior from specify.result import Result, ResultLine def run_spec(spec_class: ObjectBehavior) -> List[ResultLine]: result_lines = [] spec = spec_class() tests = [func for func in dir(spec) if func.startswith('it_') and callable(getattr(spec, func))] for test in tests: spec._let() exception = None try: getattr(spec, test)() for mock in spec._mocked_objects: mock.check_prophecies() except Exception as e: exception = str(e) result_lines.append(ResultLine(spec_class, test, exception)) spec._let_go() return result_lines def run_specs(specs: List[ObjectBehavior]): result_lines = [] # type: List[ResultLine] for spec in specs: result_lines = result_lines + run_spec(spec) result = Result(result_lines) print(result.export()) PK!kXXspecify/spec_finder.pyfrom importlib import import_module import re def get_spec(path): if not path.endswith('_spec.py'): return None with open(path, 'r') as file: content = file.read() result = re.search(r"class\s+(?P\w+)\(ObjectBehavior\):", content) if result is None: return None class_name = result.group('class_name') path = path.rstrip('.py') path = path.replace('/', '.') path = path.replace('\\', '.') module = import_module(path) class_obj = getattr(module, class_name) return class_obj PK!Q0 specify/subject.pyfrom prophepy import Mock from .builtin_matchers import get_matcher from .exceptions import CustomMatcherError from .utils import map_for_dict, reveal_if_needed class Subject: ''' This class represents the specced object. ''' def __init__(self, value, object_behavior): ''' It is instanciated with the real object, and the spec ''' self.__value = value self.__object_behavior = object_behavior def _get_value(self): ''' Get the real specced object ''' return self.__value def match_with_custom_matcher(self, matcher_name, matcher, *args): ''' Launch a test against a custom matcher and raise a CustomMatcherError if it fails ''' if not matcher(self.__value, *args): raise CustomMatcherError(f'Custom matcher "{matcher_name}" failed.') return self.__value def __getattr__(self, attr_name): ''' If the method is a _should_ one, it will try to find a matcher (builtin or custom one). If not, it will executes the action on the internal specced object and return a new Subject instance. ''' if attr_name.startswith('_should_'): matcher_type = attr_name[len('_should_'):] # custom matcher if matcher_type in self.__object_behavior._matchers().keys(): matcher = self.__object_behavior._matchers()[matcher_type] def custom_matcher_wrapper(*args): return Subject( self.match_with_custom_matcher(matcher_type, matcher, *args), self.__object_behavior ) return custom_matcher_wrapper # builtin matcher matcher = get_matcher(matcher_type) def checker_wrapper(expected_value): matcher(self.__value, expected_value) return Subject( self.__value, self.__object_behavior ) return checker_wrapper def action_wrapper(*args, **kwargs): args = map(reveal_if_needed, args) kwargs = map_for_dict(reveal_if_needed, kwargs) return Subject( getattr(self.__value, attr_name)(*args, **kwargs), self.__object_behavior ) return action_wrapper PK!{Yspecify/utils.pyfrom prophepy import Mock def map_for_dict(func, dictionary): return {k: func(v) for k, v in dictionary.items()} def reveal_if_needed(value): if isinstance(value, Mock): return value._reveal() return value PK!2۾..specify-0.0.8.dist-info/LICENSEMIT License Copyright (c) 2019 Yann Rabiller Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HڽTUspecify-0.0.8.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!HB' P specify-0.0.8.dist-info/METADATAX[s۶~6"]vU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yPK!kXXspecify/spec_finder.pyPK!Q0 H!specify/subject.pyPK!{Y*specify/utils.pyPK!2۾..,specify-0.0.8.dist-info/LICENSEPK!HڽTUz0specify-0.0.8.dist-info/WHEELPK!HB' P  1specify-0.0.8.dist-info/METADATAPK!Hgkw8specify-0.0.8.dist-info/RECORDPK;