def verify_should_invoke_callable(self): ''' verify should invoke callable and compare result ''' a_list = [] with_callable = lambda: a_list.append(True) spec = Spec(Constraint()) spec.verify(with_callable).should_raise(UnmetSpecification) spec.then(a_list.__len__).should_be(1)
def should_check_message(self): ''' Raise should verify exception type & message vs specified ''' spec = Spec(Raise(IndexError('with message'))) spec.verify(raise_index_error).should_not_raise(UnmetSpecification) spec = Spec(Raise(IndexError('with different message'))) spec.verify(raise_index_error).should_raise(UnmetSpecification)
def verify_should_invoke_callable(self): ''' verify should invoke callable and compare result ''' a_list = [] with_callable = lambda: a_list.append(True) spec = Spec(Constraint()) spec.verify(with_callable).should_raise(UnmetSpecification) spec.then(a_list.__len__).should_be(1)
def should_check_message(self): ''' Raise should verify exception type & message vs specified ''' spec = Spec(Raise(IndexError('with message'))) spec.verify(raise_index_error).should_not_raise(UnmetSpecification) spec = Spec(Raise(IndexError('with different message'))) spec.verify(raise_index_error).should_raise(UnmetSpecification)
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_trap_incorrect_return(self): ''' Specified and_result="bar" but was "baz": UnmetSpecification. Note: and_result refers to the value returned from the callable invoked in verify(), not the return value from the mock. See the Hungarian gentleman in the examples for a clearer picture... ''' mock_spec = MockSpec() spec = Spec(CollaborateWith(mock_spec.foo().will_return('baz'), and_result='bar')) spec.verify(lambda: mock_spec.foo()).should_raise(UnmetSpecification)
def should_trap_incorrect_return(self): ''' Specified and_result="bar" but was "baz": UnmetSpecification. Note: and_result refers to the value returned from the callable invoked in verify(), not the return value from the mock. See the Hungarian gentleman in the examples for a clearer picture... ''' mock_spec = MockSpec() spec = Spec( CollaborateWith(mock_spec.foo().will_return('baz'), and_result='bar')) spec.verify(lambda: mock_spec.foo()).should_raise(UnmetSpecification)
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_check_unverified_collaborations(self): ''' check for unverified collaborations after start_collaborating ''' spec = Spec(MockSpec()) spec.when(spec.foo(), spec.start_collaborating()) spec.then(spec.verify()) msg = 'should be collaborating with unnamed_mock.foo()' spec.should_raise(UnmetSpecification(msg)) spec = Spec(MockSpec()) spec.when(spec.foo(), spec.start_collaborating(), spec.foo()) spec.then(spec.verify()) spec.should_not_raise(UnmetSpecification)
def should_check_unverified_collaborations(self): ''' check for unverified collaborations after start_collaborating ''' spec = Spec(MockSpec()) spec.when(spec.foo(), spec.start_collaborating()) spec.then(spec.verify()) msg = 'should be collaborating with unnamed_mock.foo()' spec.should_raise(UnmetSpecification(msg)) spec = Spec(MockSpec()) spec.when(spec.foo(), spec.start_collaborating(), spec.foo()) spec.then(spec.verify()) spec.should_not_raise(UnmetSpecification)
def should_return_all_results(self): ''' verify() should return the result of all attempted / successful verifications ''' spec = Spec(AllVerifiable, given=silent_listener) spec.verify().should_be({'total': 0, 'verified': 0, 'unverified': 0}) spec = Spec(AllVerifiable, given=silent_listener) spec.when(spec.include(number_one)) spec.then(spec.verify()) spec.should_be({'total': 1, 'verified': 1, 'unverified': 0}) spec.when(spec.include(raise_index_error)) spec.then(spec.verify()) spec.should_be({'total': 2, 'verified': 1, 'unverified': 1})
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 all_verif_failfast_behaviour(): ''' verify(fail_fast=True) should stop after the first unmet specification or unexpected exception''' spec = Spec(AllVerifiable, given=silent_listener) spec.when(spec.include(raise_index_error), spec.include(dont_raise_index_error)) spec.then(spec.verify(fail_fast=True)) spec.should_be({'total':2, 'verified':0, 'unverified':2, 'fail_fast':True}) spec = Spec(AllVerifiable, given=silent_listener) spec.when(spec.include(unmet_specification), spec.include(dont_raise_index_error)) spec.then(spec.verify(fail_fast=True)) spec.should_be({'total':2, 'verified':0, 'unverified':2, 'fail_fast':True})
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_return_all_results(self): ''' verify() should return the result of all attempted / successful verifications ''' spec = Spec(AllVerifiable, given=silent_listener) spec.verify().should_be({'total':0, 'verified':0, 'unverified':0}) spec = Spec(AllVerifiable, given=silent_listener) spec.when(spec.include(number_one)) spec.then(spec.verify()) spec.should_be({'total':1, 'verified':1, 'unverified':0}) spec.when(spec.include(raise_index_error)) spec.then(spec.verify()) spec.should_be({'total':2, 'verified':1, 'unverified':1})
def should_verify_each_item(self): ''' verify() should execute each included item ''' a_list = [] lambda_list_append1 = lambda: a_list.append(0) lambda_list_append2 = lambda: a_list.extend((1, 2)) spec = Spec(AllVerifiable, given=silent_listener) spec.when(spec.include(lambda_list_append1), spec.include(lambda_list_append2), spec.verify()) spec.then(a_list.__len__).should_be(3)
def should_verify_each_item(self): ''' verify() should execute each included item ''' a_list = [] lambda_list_append1 = lambda: a_list.append(0) lambda_list_append2 = lambda: a_list.extend((1, 2)) spec = Spec(AllVerifiable, given=silent_listener) spec.when(spec.include(lambda_list_append1), spec.include(lambda_list_append2), spec.verify()) spec.then(a_list.__len__).should_be(3)
def grouped_methods_should_verify(self): ''' grouping() methods should allow them to be executed & verified ''' all_verifiable = silent_listener() def add_related_verifiables(): grouping(RelatedVerifiables, all_verifiable) verifiable(RelatedVerifiables.verifiable1, all_verifiable) verifiable(RelatedVerifiables.verifiable2, all_verifiable) all_verifiable.add_related_verifiables = add_related_verifiables spec = Spec(all_verifiable) spec.when(spec.add_related_verifiables()) spec.then(spec.total()).should_be(2) spec.then(spec.verify()) spec.should_be({'total':2, 'verified':2, 'unverified':0})
def all_verif_failfast_behaviour(): ''' verify(fail_fast=True) should stop after the first unmet specification or unexpected exception''' spec = Spec(AllVerifiable, given=silent_listener) spec.when(spec.include(raise_index_error), spec.include(dont_raise_index_error)) spec.then(spec.verify(fail_fast=True)) spec.should_be({ 'total': 2, 'verified': 0, 'unverified': 2, 'fail_fast': True }) spec = Spec(AllVerifiable, given=silent_listener) spec.when(spec.include(unmet_specification), spec.include(dont_raise_index_error)) spec.then(spec.verify(fail_fast=True)) spec.should_be({ 'total': 2, 'verified': 0, 'unverified': 2, 'fail_fast': True })
def grouped_methods_should_verify(self): ''' grouping() methods should allow them to be executed & verified ''' all_verifiable = silent_listener() def add_related_verifiables(): grouping(RelatedVerifiables, all_verifiable) verifiable(RelatedVerifiables.verifiable1, all_verifiable) verifiable(RelatedVerifiables.verifiable2, all_verifiable) all_verifiable.add_related_verifiables = add_related_verifiables spec = Spec(all_verifiable) spec.when(spec.add_related_verifiables()) spec.then(spec.total()).should_be(2) spec.then(spec.verify()) spec.should_be({'total': 2, 'verified': 2, 'unverified': 0})
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 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 notification_behaviour(): ''' listener should receive notifications AllVerifiable.verify() ''' listener = MockSpec() all_verifiable_with_mock_listener = AllVerifiable(listener) results = {'total': 3, 'verified': 1, 'unverified': 2} spec = Spec(all_verifiable_with_mock_listener) spec.when(spec.include(string_abc), spec.include(raise_index_error), spec.include(unmet_specification)) spec.then(spec.verify()) spec.should_collaborate_with( listener.all_verifiable_starting(all_verifiable_with_mock_listener), listener.verification_started(string_abc), listener.specification_met(string_abc), listener.verification_started(raise_index_error), listener.unexpected_exception(raise_index_error, Type(IndexError)), listener.verification_started(unmet_specification), listener.specification_unmet(unmet_specification, Type(UnmetSpecification)), listener.all_verifiable_ending(all_verifiable_with_mock_listener, results), and_result=results)
def notification_behaviour(): ''' listener should receive notifications AllVerifiable.verify() ''' listener = MockSpec() all_verifiable_with_mock_listener = AllVerifiable(listener) results = {'total': 3, 'verified': 1, 'unverified': 2} spec = Spec(all_verifiable_with_mock_listener) spec.when(spec.include(string_abc), spec.include(raise_index_error), spec.include(unmet_specification)) spec.then(spec.verify()) spec.should_collaborate_with( listener.all_verifiable_starting(all_verifiable_with_mock_listener), listener.verification_started(string_abc), listener.specification_met(string_abc), listener.verification_started(raise_index_error), listener.unexpected_exception(raise_index_error, Type(IndexError)), listener.verification_started(unmet_specification), listener.specification_unmet(unmet_specification, Type(UnmetSpecification)), listener.all_verifiable_ending(all_verifiable_with_mock_listener, results), and_result = results)
def should_check_type(self): ''' Raise should check that exception is raised ''' spec = Spec(Raise(IndexError)) spec.verify(raise_index_error).should_not_raise(UnmetSpecification) spec.verify(dont_raise_index_error).should_raise(UnmetSpecification)
def verify_should_use_comparator(self): ''' verify should delegate to comparator.compares_to ''' comparator = MockSpec() spec = Spec(Constraint(comparator)) comparator_compares_to = comparator.compares_to(1).will_return(True) spec.verify(lambda: 1).should_collaborate_with(comparator_compares_to)
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)
def correct_call_should_be_ok(self): ''' Specified foo() and foo() called: met specification ''' mock_spec = MockSpec() spec = Spec(CollaborateWith(mock_spec.foo())) spec.verify(lambda: mock_spec.foo()) spec.should_not_raise(UnmetSpecification)
def verify_should_use_comparator(self): ''' verify should delegate to comparator.compares_to ''' comparator = MockSpec() spec = Spec(Constraint(comparator)) comparator_compares_to = comparator.compares_to(1).will_return(True) spec.verify(lambda: 1).should_collaborate_with(comparator_compares_to)
def correct_call_should_be_ok(self): ''' Specified foo() and foo() called: met specification ''' mock_spec = MockSpec() spec = Spec(CollaborateWith(mock_spec.foo())) spec.verify(lambda: mock_spec.foo()) spec.should_not_raise(UnmetSpecification)
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)
def should_check_type(self): ''' Raise should check that exception is raised ''' spec = Spec(Raise(IndexError)) spec.verify(raise_index_error).should_not_raise(UnmetSpecification) spec.verify(dont_raise_index_error).should_raise(UnmetSpecification)