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_execute_fn(self): ''' verify_fn() should execute the fn ''' a_list = [] lambda_list_append = lambda: a_list.append(len(a_list)) spec = Spec(AllVerifiable, given=silent_listener) spec.when(spec.verify_fn(verifiable_fn=string_abc)) spec.then(a_list.__len__).should_be(0) spec.when(spec.verify_fn(verifiable_fn=lambda_list_append)) spec.then(a_list.__len__).should_be(1)
def should_add_to_collation(self): ''' verifiable should add fn to ALL_VERIFIABLE and return it ''' all_verifiable = silent_listener() spec = Spec(verifiable) spec.verifiable(number_one, all_verifiable).should_be(number_one) num_verifiable_before = all_verifiable.total() spec.when(spec.verifiable(string_abc, all_verifiable)) spec.then(all_verifiable.total).should_be(num_verifiable_before + 1)
def should_increment(self): ''' total() should increment as verifiable_fn is included ''' spec = Spec(AllVerifiable, given=silent_listener) spec.total().should_be(0) spec.when(spec.include(raise_index_error)) spec.then(spec.total()).should_be(1) spec.when(spec.include(dont_raise_index_error)) spec.then(spec.total()).should_be(2)
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 supply_different_values_each_time(self): ''' supplies(a,b,...) should return a,b,... on successive calls ''' spec = Spec(MockResult(MockCall(MockSpec(), ''))) spec.when(spec.times(3)) spec.then(spec.supplies('x', 'y', 'z')).should_not_raise(ValueError) spec.then(spec.specified_times()).should_be(3) spec.then(spec.next()).should_be('x') spec.then(spec.next()).should_be('y') spec.then(spec.next()).should_be('z') spec.then(spec.next()).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 external_then_behaviour(): ''' Spec for then()... actions that call outside the spec itself. Note that the action on the spec is invoked in client code with parens(): spec.then( * spec.__len__() * ).should_be(1) but the action outside the spec is NOT: spec.then( * 'they called him brian'.__len__ * ).should_be(21) ''' spec = Spec([]) spec.when(spec.append('brian')) spec.then(spec.__len__()).should_be(1) spec.then('they called him brian'.__len__).should_be(21)
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_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 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 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 supply_single_value_twice(self): ''' combining supplies(a_value) & times(2) (in any order) should return a_value, twice ''' spec = Spec(MockResult(MockCall(None, ''))) spec.when(spec.times(2), spec.supplies('f')) spec.then(spec.specified_times()).should_be(2) spec.then(spec.next()).should_be('f') spec.then(spec.next()).should_be('f') spec = Spec(MockResult(MockCall(None, ''))) spec.when(spec.supplies('f'), spec.times(2)) spec.then(spec.specified_times()).should_be(2) spec.then(spec.next()).should_be('f') spec.then(spec.next()).should_be('f')
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 defaults(self): ''' By default should return None just once ''' spec = Spec(MockResult(MockCall(MockSpec(), ''))) spec.specified_times().should_be(1) spec.times_remaining().should_be(1) spec.then(spec.next()).should_be(None) spec.then(spec.times_remaining()).should_be(0) spec.then(spec.next()).should_raise(UnmetSpecification)
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 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 supply_single_value(self): ''' supplies(a_value) should return that value, once ''' spec = Spec(MockResult(MockCall(None, ''))) spec.when(spec.supplies('f')) spec.then(spec.specified_times()).should_be(1) spec.then(spec.next()).should_be('f')
def return_twice(self): ''' times(2) should return default (None) value just twice ''' spec = Spec(MockResult(MockCall(MockSpec(), ''))) spec.when(spec.times(2)) spec.then(spec.specified_times()).should_be(2) spec.then(spec.times_remaining()).should_be(2) spec.then(spec.times_remaining()).should_be(2) spec.then(spec.next()).should_be(None) spec.then(spec.times_remaining()).should_be(1) spec.then(spec.next()).should_be(None) spec.then(spec.times_remaining()).should_be(0) spec.then(spec.next()).should_raise(UnmetSpecification)
def check_number_values_against_times(self): ''' supplies(a,b,...) iff times(len(a,b,...)) ''' spec = Spec(MockResult(MockCall(None, ''))) spec.when(spec.times(2)) spec.then(spec.supplies('a', 'b')).should_not_raise(ValueError) spec.then(spec.supplies('x', 'y', 'z')).should_raise(ValueError)
def should_verify_specified_collaborations(self): ''' after start_collaborating, collaborations should be verified ''' spec = Spec(MockSpec(name='a')) spec.when(spec.start_collaborating()) spec.then(spec.foo()) msg = 'should not be collaborating with a.foo()' spec.should_raise(UnmetSpecification(msg)) spec = Spec(MockSpec(name='b')) spec.when(spec.foo(), spec.start_collaborating()) spec.then(spec.bar()) msg = 'should be collaborating with b.foo(), not b.bar()' spec.should_raise(UnmetSpecification(msg)) spec = Spec(MockSpec(name='c')) spec.when(spec.foo(), spec.bar(), spec.start_collaborating()) spec.then(spec.foo()).should_not_raise(UnmetSpecification) msg = 'should be collaborating with c.bar(), not c.baz()' spec.then(spec.baz()).should_raise(UnmetSpecification(msg)) mock = MockSpec(name='d') mock.foo().times(2).will_return('camelot') spec = Spec(mock) spec.when(spec.start_collaborating()) spec.then(spec.foo()).should_not_raise(UnmetSpecification) spec.then(spec.foo()).should_not_raise(UnmetSpecification) msg = 'should not be collaborating with d.foo()' spec.then(spec.foo()).should_raise(UnmetSpecification(msg))
def raise_exception(self): ''' raises(exception) should raise exceptions in the same fashion as will_suppply_values should return values ''' spec = Spec(MockResult(MockCall(MockSpec(), ''))) exception = ValueError('the number of the counting shall be three') spec.when(spec.raises(exception)) spec.then(spec.next()).should_raise(exception) spec.then(spec.times_remaining()).should_be(0) spec.then(spec.next()).should_raise(UnmetSpecification) spec = Spec(MockResult(MockCall(MockSpec(), ''))) exception = ValueError('the number of the counting shall be three') spec.when(spec.times(2), spec.raises(exception)) spec.then(spec.next()).should_raise(exception) spec.then(spec.next()).should_raise(exception) spec.then(spec.next()).should_raise(UnmetSpecification) spec = Spec(MockResult(MockCall(MockSpec(), ''))) exceptions = (ValueError('the number of the counting shall be three'), ValueError('Four shalt thou not count')) spec.when(spec.times(2), spec.raises(*exceptions)) spec.then(spec.next()).should_raise(exceptions[0]) spec.then(spec.next()).should_raise(exceptions[1]) spec.then(spec.next()).should_raise(UnmetSpecification)
def should_ignore_duplicates(self): ''' total() should ignore duplicate include()s ''' spec = Spec(AllVerifiable, given=silent_listener) spec.when(spec.include(raise_index_error), spec.include(raise_index_error)) spec.then(spec.total()).should_be(1)