def desc_should_use_comparator(self): ''' describe_constraint should delegate to comparator.description ''' comparator = MockSpec() spec = Spec(Constraint(comparator)) comparator_description = comparator.description() comparator_description.will_return('subtitled') spec.describe_constraint() spec.should_collaborate_with(comparator_description, and_result='should be subtitled')
def should_trap_incorrect_args(self): ''' Specified foo(2) & bar(), and foo(1) called: UnmetSpecification''' mock_spec = MockSpec() collaborations = (mock_spec.foo(2), mock_spec.bar()) descriptions = [collaboration.description() for collaboration in collaborations] spec = Spec(CollaborateWith(*collaborations)) spec.describe_constraint().should_be(','.join(descriptions)) spec.verify(lambda: mock_spec.foo(1)).should_raise(UnmetSpecification)
def should_use_nothing_comparator(self): ''' Constraint should use Nothing comparator by default''' spec = Spec(Constraint()) spec.describe_constraint().should_be('should be nothing') spec.verify_value(1).should_be(False) spec.verify_value(2).should_be(False) spec.verify_value(None).should_be(False) spec.verify_value(['majestic', 'moose']).should_be(False) spec.verify_value({'gumby': 'brain surgeon'}).should_be(False)
def should_trap_incorrect_args(self): ''' Specified foo(2) & bar(), and foo(1) called: UnmetSpecification''' mock_spec = MockSpec() collaborations = (mock_spec.foo(2), mock_spec.bar()) descriptions = [ collaboration.description() for collaboration in collaborations ] spec = Spec(CollaborateWith(*collaborations)) spec.describe_constraint().should_be(','.join(descriptions)) spec.verify(lambda: mock_spec.foo(1)).should_raise(UnmetSpecification)
def not_behaviour(): ''' Not should raise exception iff underlying check succeeds ''' spec = Spec(Not(Constraint(EqualsEquals(2)))) spec.verify(number_one).should_not_raise(UnmetSpecification) spec = Spec(Not(Constraint(EqualsEquals(1)))) msg = 'should not be == 1' spec.describe_constraint().should_be(msg) spec.verify(number_one).should_raise(UnmetSpecification(msg)) spec = Spec(Not(Not(Constraint(EqualsEquals(2))))) msg = 'should be == 2' spec.describe_constraint().should_be(msg) spec.verify(number_one).should_raise(UnmetSpecification(msg))
def should_have_meaningful_msg(self): ''' Raise should produce meaningful UnmetSpecification messages''' spec = Spec(Raise(IndexError)) msg = "should raise IndexError" spec.describe_constraint().should_be(msg) spec.verify(dont_raise_index_error) spec.should_raise(UnmetSpecification(msg)) spec = Spec(Raise(IndexError('with some message'))) msg = "should raise IndexError('with some message',)" spec.describe_constraint().should_be(msg) unmet_msg = msg + ", not IndexError('with message',)" unmet_specification = UnmetSpecification(unmet_msg) spec.verify(raise_index_error).should_raise(unmet_specification)
def should_trap_incorrect_call(self): ''' Specified foo() but bar() called: UnmetSpecification ''' mock_spec = MockSpec() spec = Spec(CollaborateWith(mock_spec.foo())) spec.describe_constraint().should_be(mock_spec.foo().description()) spec.verify(lambda: mock_spec.bar()).should_raise(UnmetSpecification)