def can_override_comparators(self): ''' should be able to specify any comparator for arg verification ''' mock_spec = MockSpec(comparators={Exception:Nothing}) mock_call = mock_spec.your_mother(TypeError('hamster')) mock_call_result = mock_call.result_of('your_mother') spec = Spec(mock_call_result) spec.__call__(TypeError('hamster')).should_raise(UnmetSpecification)
def can_override_comparators(self): ''' should be able to specify any comparator for arg verification ''' mock_spec = MockSpec(comparators={Exception: Nothing}) mock_call = mock_spec.your_mother(TypeError('hamster')) mock_call_result = mock_call.result_of('your_mother') spec = Spec(mock_call_result) spec.__call__(TypeError('hamster')).should_raise(UnmetSpecification)
def spec_getattr_behaviour(): ''' getattr from spec should return wrapper for unknown attributes ''' spec = Spec(lambda: getattr(Spec('grail'), 'should')) spec.__call__().should_not_be(Type(WrapFunction)) # Spec.should() exists spec = Spec(lambda: getattr(Spec('life'), 'death')) spec.__call__().should_be(Type(WrapFunction)) # Spec.death() not exists
def should_mimic_specification(self): ''' result_of should be callable and return specified value or raise specified exception ''' mock_call = MockSpec().foo() mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_be(None) mock_call = MockSpec().foo().will_return(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_be(1) mock_call = MockSpec().foo().will_return((2, 3)) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_be((2, 3)) mock_call = MockSpec().foo().will_raise(StopIteration) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_raise(StopIteration) value_error = ValueError("that's no ordinary rabbit") mock_spec = MockSpec() mock_call = mock_spec.foo().will_raise(value_error) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_raise(value_error) # check that after exception raised the collaboration is 'over' Spec(mock_spec).verify().should_not_raise(UnmetSpecification)
def should_mimic_specification(self): ''' result_of should be callable and return specified value or raise specified exception ''' mock_call = MockSpec().foo() mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_be(None) mock_call = MockSpec().foo().will_return(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_be(1) mock_call = MockSpec().foo().will_return((2, 3)) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_be((2, 3)) mock_call = MockSpec().foo().will_raise(StopIteration) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_raise(StopIteration) value_error = ValueError("that's no ordinary rabbit") mock_spec = MockSpec() mock_call = mock_spec.foo().will_raise(value_error) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_raise(value_error) # check that after exception raised the collaboration is 'over' Spec(mock_spec).verify().should_not_raise(UnmetSpecification)
def spec_getattr_behaviour(): ''' getattr from spec should return wrapper for unknown attributes ''' spec = Spec(lambda: getattr(Spec('grail'), 'should')) spec.__call__().should_not_be(Type(WrapFunction)) # Spec.should() exists spec = Spec(lambda: getattr(Spec('life'), 'death')) spec.__call__().should_be(Type(WrapFunction)) # Spec.death() not exists
def override_comparators_dont_replace_all(self): ''' comparator overrides only affect comparator used for that type ''' mock_spec = MockSpec(comparators={float: EqualsEquals}) mock_call = mock_spec.your_mother(TypeError('hamster')) mock_call_result = mock_call.result_of('your_mother') spec = Spec(mock_call_result) spec.__call__(TypeError('hamster')) spec.should_not_raise(UnmetSpecification)
def should_delegate_eq(self): ''' base Comparator should delegate __eq__ to compare_to. ''' #TODO: nicer way of forcing spec to use underlying __eq__ base_comparator_equals = Comparator(1).__eq__ spec = Spec(base_comparator_equals) spec.__call__(1).should_be(False) spec.__call__(2).should_be(False) spec.__call__(int).should_be(False)
def override_comparators_dont_replace_all(self): ''' comparator overrides only affect comparator used for that type ''' mock_spec = MockSpec(comparators={float:EqualsEquals}) mock_call = mock_spec.your_mother(TypeError('hamster')) mock_call_result = mock_call.result_of('your_mother') spec = Spec(mock_call_result) spec.__call__(TypeError('hamster')) spec.should_not_raise(UnmetSpecification)
def should_delegate_eq(self): ''' base Comparator should delegate __eq__ to compare_to. ''' #TODO: nicer way of forcing spec to use underlying __eq__ base_comparator_equals = Comparator(1).__eq__ spec = Spec(base_comparator_equals) spec.__call__(1).should_be(False) spec.__call__(2).should_be(False) spec.__call__(int).should_be(False)
def given_typechecking_behaviour(): ''' Spec for check that given=... is correct type ''' def spec_for_dict_given_empty_list(): ''' callable method to defer instance creation until within Spec ''' return Spec(type({}), given=lambda: []) spec = Spec(spec_for_dict_given_empty_list) type_error = TypeError("[] is not instance of <class 'dict'>") spec.__call__().should_raise(type_error)
def given_typechecking_behaviour(): ''' Spec for check that given=... is correct type ''' def spec_for_dict_given_empty_list(): ''' callable method to defer instance creation until within Spec ''' return Spec(type({}), given=lambda: []) spec = Spec(spec_for_dict_given_empty_list) type_error = TypeError("[] is not instance of <class 'dict'>") spec.__call__().should_raise(type_error)
def verify_exceptions_with_comparator(self): ''' ExceptionValue should be used to verify Exception args ''' mock_call = MockSpec().your_mother(TypeError('hamster')) mock_call_result = mock_call.result_of('your_mother') spec = Spec(mock_call_result) spec.__call__(TypeError('hamster')) spec.should_not_raise(UnmetSpecification) mock_call = MockSpec().your_father(smelt_of=TypeError('elderberries')) mock_call_result = mock_call.result_of('your_father') spec = Spec(mock_call_result) spec.__call__(smelt_of=TypeError('elderberries')) spec.should_not_raise(UnmetSpecification)
def verify_exceptions_with_comparator(self): ''' ExceptionValue should be used to verify Exception args ''' mock_call = MockSpec().your_mother(TypeError('hamster')) mock_call_result = mock_call.result_of('your_mother') spec = Spec(mock_call_result) spec.__call__(TypeError('hamster')) spec.should_not_raise(UnmetSpecification) mock_call = MockSpec().your_father(smelt_of=TypeError('elderberries')) mock_call_result = mock_call.result_of('your_father') spec = Spec(mock_call_result) spec.__call__(smelt_of=TypeError('elderberries')) spec.should_not_raise(UnmetSpecification)
def result_invoke_underlying(self): '''result() should invoke the wrapped function and return its result''' spec = Spec(WrapFunction(None, 'a', 'startswith')) spec.when(spec.__call__('a')).then(spec.result()).should_be(True) spec.when(spec.__call__('b')).then(spec.result()).should_be(False)
def result_invoke_underlying(self): """result() should invoke the wrapped function and return its result""" spec = Spec(WrapFunction(None, "a", "startswith")) spec.when(spec.__call__("a")).then(spec.result()).should_be(True) spec.when(spec.__call__("b")).then(spec.result()).should_be(False)
def should_verify_args_specification(self): ''' result_of should verify the args specification and supply a meaningful message if specification is unmet ''' mock_call = MockSpec(name='a').foo() mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with a.foo(), not a.foo(1)' spec.__call__(1).should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='b').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = "should be collaborating with b.foo(1), not b.foo('1')" spec.__call__('1').should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='c').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with c.foo(1), not c.foo()' spec.__call__().should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='d').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with d.foo(1), not d.foo(2)' spec.__call__(2).should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='e').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__(1).should_not_raise(UnmetSpecification) mock_call = MockSpec(name='f').foo(1).will_return(2) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__(1).should_be(2) mock_call = MockSpec(name='g').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) msg = "should be collaborating with g.bar(keyword='named argument'), "\ + "not g.bar(keyword='wrong argument')" spec.__call__(keyword='wrong argument').should_raise( UnmetSpecification(msg)) mock_call = MockSpec(name='h').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) msg = "should be collaborating with h.bar(keyword='named argument'), "\ + "not h.bar(bad_keyword='named argument')" spec.__call__(bad_keyword='named argument').should_raise( UnmetSpecification(msg)) mock_call = MockSpec(name='i').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) spec.__call__(keyword='named argument').should_not_raise( UnmetSpecification) mock_call = MockSpec(name='j').bar( keyword='named argument').will_return('monty') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) spec.__call__(keyword='named argument').should_be('monty')
def should_wrap_modulefunctions(self): """Wrapping module functions and instance methods should be possible""" spec = Spec(WrapFunction(None, number_one, "number_one")) spec.when(spec.__call__()).then(spec.result()).should_be(1)
def result_of_successive_times(): ''' result_of should "iterate" over will_return value(s) and provide a meaningful error message if the specification is unmet''' mock_call = MockSpec(name='x').foo().times(2).will_return(3, 4) spec = Spec(mock_call.result_of('foo')) spec.__call__().should_be(3) spec = Spec(mock_call.result_of('foo')) spec.__call__().should_be(4) spec = Spec(mock_call.result_of('foo')) msg = 'should be collaborating with x.foo() only 2 successive times' spec.__call__().should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='y').bar().times(3).will_return(5) spec = Spec(mock_call.result_of('bar')) spec.__call__().should_be(5) spec = Spec(mock_call.result_of('bar')) spec.__call__().should_be(5) spec = Spec(mock_call.result_of('bar')) spec.__call__().should_be(5) spec = Spec(mock_call.result_of('bar')) msg = 'should be collaborating with y.bar() only 3 successive times' spec.__call__().should_raise(UnmetSpecification(msg))
def mock_call_call_returns_self(): '''making a mock call should return the mock call''' mock_call = MockSpec().foo spec = Spec(mock_call) spec.__call__().should_be(mock_call)
def verify_floats_with_comparator(self): ''' FloatValue should be used to verify float args ''' mock_call = MockSpec().kernigget(3.14) mock_call_result = mock_call.result_of('kernigget') spec = Spec(mock_call_result) spec.__call__(3.141).should_not_raise(UnmetSpecification)
def result_of_successive_times(): ''' result_of should "iterate" over will_return value(s) and provide a meaningful error message if the specification is unmet''' mock_call = MockSpec(name='x').foo().times(2).will_return(3, 4) spec = Spec(mock_call.result_of('foo')) spec.__call__().should_be(3) spec = Spec(mock_call.result_of('foo')) spec.__call__().should_be(4) spec = Spec(mock_call.result_of('foo')) msg = 'should be collaborating with x.foo() only 2 successive times' spec.__call__().should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='y').bar().times(3).will_return(5) spec = Spec(mock_call.result_of('bar')) spec.__call__().should_be(5) spec = Spec(mock_call.result_of('bar')) spec.__call__().should_be(5) spec = Spec(mock_call.result_of('bar')) spec.__call__().should_be(5) spec = Spec(mock_call.result_of('bar')) msg = 'should be collaborating with y.bar() only 3 successive times' spec.__call__().should_raise(UnmetSpecification(msg))
def mock_call_call_returns_self(): '''making a mock call should return the mock call''' mock_call = MockSpec().foo spec = Spec(mock_call) spec.__call__().should_be(mock_call)
def should_wrap_modulefunctions(self): '''Wrapping module functions and instance methods should be possible''' spec = Spec(WrapFunction(None, number_one, 'number_one')) spec.when(spec.__call__()).then(spec.result()).should_be(1)
def verify_floats_with_comparator(self): ''' FloatValue should be used to verify float args ''' mock_call = MockSpec().kernigget(3.14) mock_call_result = mock_call.result_of('kernigget') spec = Spec(mock_call_result) spec.__call__(3.141).should_not_raise(UnmetSpecification)
def should_verify_args_specification(self): ''' result_of should verify the args specification and supply a meaningful message if specification is unmet ''' mock_call = MockSpec(name='a').foo() mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with a.foo(), not a.foo(1)' spec.__call__(1).should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='b').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = "should be collaborating with b.foo(1), not b.foo('1')" spec.__call__('1').should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='c').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with c.foo(1), not c.foo()' spec.__call__().should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='d').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with d.foo(1), not d.foo(2)' spec.__call__(2).should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='e').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__(1).should_not_raise(UnmetSpecification) mock_call = MockSpec(name='f').foo(1).will_return(2) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__(1).should_be(2) mock_call = MockSpec(name='g').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) msg = "should be collaborating with g.bar(keyword='named argument'), "\ + "not g.bar(keyword='wrong argument')" spec.__call__(keyword='wrong argument').should_raise( UnmetSpecification(msg)) mock_call = MockSpec(name='h').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) msg = "should be collaborating with h.bar(keyword='named argument'), "\ + "not h.bar(bad_keyword='named argument')" spec.__call__(bad_keyword='named argument').should_raise( UnmetSpecification(msg)) mock_call = MockSpec(name='i').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) spec.__call__( keyword='named argument').should_not_raise(UnmetSpecification) mock_call = MockSpec(name='j').bar( keyword='named argument').will_return('monty') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) spec.__call__(keyword='named argument').should_be('monty')