Ejemplo n.º 1
0
def behavior(its):
    import specfor
    new_specfor = specfor.new_specfor()
    mock = new_specfor.mock
    plugins = new_specfor.mockings.plugins
    
    # new restriction
    class NeverRestriction(plugins.Restriction):
        name = "never"
        def prepare(self, responsibilities, *args, **kwargs):
            return True
        def called(self, responsibilities, returns, *args, **kwargs):
            return False # fail if called
        def completed(self, responsibilities):
            return
        def __repr__(self):
            return "[never]"
        pass
    plugins.register("never", lambda resp, val: NeverRestriction())
    
    # mock
    person_mock = mock.define("person")
    prop_def = person_mock.property("age")
    @prop_def.get.always
    def age(self):
        return 18
    @prop_def.set.define(never=True, like=dict(val=str))
    def age(self, val):
        return
    @prop_def.set.always
    def age(self, val):
        return
    
    
    # use
    person = person_mock("id-0")
    
    the[person.age].should.be[18]
    person.age = 20
    the[person.age].should.be[18]
    with the.raising[AssertionError]:
        person.age = "20"
        pass
    pass
Ejemplo n.º 2
0
def behavior(its):
    # refreshed module
    import specfor
    new_specfor = specfor.new_specfor()
    spec = new_specfor.spec
    the = new_specfor.the
    match = new_specfor.match
    # define
    
    class MatchNotInstance(match.MatchAction):
        def __call__(self, expected):
            message = "it[%s] should not be instance of %s" % (
                repr(self.expectation.value), repr(expected))
            assert not isinstance(self.expectation.value, expected), message
            pass
        pass
    match.register(
        "not_be_instance_of",
        property(lambda self: MatchNotInstance(self.expectation)))
    
    # use
    success_spec = spec.of("not instance")
    @success_spec.that("the[val].should.not_be_instance_of[exp_type]")
    def behavior(it):
        the[{}].should.not_be_instance_of[list]
        pass
    
    fail_spec = spec.of("not instance")
    @fail_spec.that("the[val].should.not_be_instance_of[exp_type]")
    def behavior(it):
        the[{}].should.not_be_instance_of[dict]
        pass
    
    the[spec.run(success_spec).wasSuccessful()].should.be_ok
    the[spec.run(fail_spec).wasSuccessful()].should.be_no
    pass