Ejemplo n.º 1
0
    def test_adapter_fn(self):
        @Opt.adapter
        def fn(x):
            return x

        eq(Opt.absent, fn(None))
        eq(Opt(5), fn(5))
Ejemplo n.º 2
0
    def test_person_example(self):
        john = Person(
            first_name='John',
            last_name='Doe',
            middle_name='Michael',
            social_security=1234,
        )

        assert john.middle_name.is_present
        assert not john.middle_name.is_absent
        eq('Michael', john.middle_name.opt)

        assert john.social_security.is_present
        assert not john.social_security.is_absent
        eq(1234, john.social_security.opt)

        alice = Person(
            first_name='Alice',
            last_name='Smith',
        )

        assert not alice.middle_name.is_present
        assert alice.middle_name.is_absent

        assert not alice.social_security.is_present
        assert alice.social_security.is_absent
  def test_person_example( self ):
    john = Person(
      first_name = 'John',
      last_name = 'Doe',
      middle_name = 'Michael',
      social_security = 1234,
    )
  
    assert john.middle_name.is_present
    assert not john.middle_name.is_absent
    eq( 'Michael', john.middle_name.opt )
    
    assert john.social_security.is_present
    assert not john.social_security.is_absent
    eq( 1234, john.social_security.opt )
    
    alice = Person(
      first_name = 'Alice',
      last_name = 'Smith',
    )
  
    assert not alice.middle_name.is_present
    assert alice.middle_name.is_absent

    assert not alice.social_security.is_present
    assert alice.social_security.is_absent
  def test_default_value( self ):
    class C( ValueMixin ):
      def __init__( self, a, b = 2 ):
        pass

    o = C( 1 )

    eq( o, C( 1, 2 ) )
    eq( o.b, 2 )
Ejemplo n.º 5
0
    def test_default_value(self):
        class C(ValueMixin):
            def __init__(self, a, b=2):
                pass

        o = C(1)

        eq(o, C(1, 2))
        eq(o.b, 2)
Ejemplo n.º 6
0
    def test_absent(self):

        assert not Opt.absent.is_present
        assert Opt.absent.is_absent

        assert Opt.absent is not None

        raises(Opt.OptWasAbsent, lambda: Opt.absent.opt)

        eq(6, Opt.absent.opt_default(6))
Ejemplo n.º 7
0
    def test_adapter_method(self):
        class Person(object):
            @Opt.adapter
            def method(s, x):
                return x

        person = Person()

        eq(Opt.absent, person.method(None))
        eq(Opt(5), person.method(5))
Ejemplo n.º 8
0
    def test_helper_not_cached_on_entities(self):

        entity = EntityWithCounter(HashCounter())

        # helper not cached
        assert entity.object_helper is not entity.object_helper

        eq(0, entity.counter.hash_count)

        # nested object shouldn't be hashed
        hash(entity)
        eq(0, entity.counter.hash_count)
  def test_helper_not_cached_on_entities( self ):

    entity = EntityWithCounter( HashCounter() )

    # helper not cached
    assert entity.object_helper is not entity.object_helper

    eq( 0, entity.counter.hash_count )

    # nested object shouldn't be hashed
    hash( entity )
    eq( 0, entity.counter.hash_count )
Ejemplo n.º 10
0
    def test_unnamed_lambdas(sef):
        '''
    make sure lambda with the same __name__ are cached separately
    '''
        class C(object):
            x = once(lambda s: 'x')
            y = once(lambda s: 'y')

        obj = C()

        eq(obj.x, 'x')
        eq(obj.y, 'y')

        eq(obj.x, 'x')
        eq(obj.y, 'y')
Ejemplo n.º 11
0
  def test_unnamed_lambdas( sef ):
    '''
    make sure lambda with the same __name__ are cached separately
    '''

    class C( object ):
      x = once( lambda s: 'x' )
      y = once( lambda s: 'y' )

    obj = C()

    eq( obj.x, 'x' )
    eq( obj.y, 'y' )

    eq( obj.x, 'x' )
    eq( obj.y, 'y' )
  def test_helper_is_cached_on_values( self ):

    value = ValueWithCounter( HashCounter() )

    # helper is cached
    assert value.object_helper is value.object_helper

    eq( 0, value.counter.hash_count )

    hash( value )
    eq( 1, value.counter.hash_count )

    # count stays at 1 forever
    hash( value )
    eq( 1, value.counter.hash_count )
    hash( value )
    eq( 1, value.counter.hash_count )
Ejemplo n.º 13
0
    def test_helper_is_cached_on_values(self):

        value = ValueWithCounter(HashCounter())

        # helper is cached
        assert value.object_helper is value.object_helper

        eq(0, value.counter.hash_count)

        hash(value)
        eq(1, value.counter.hash_count)

        # count stays at 1 forever
        hash(value)
        eq(1, value.counter.hash_count)
        hash(value)
        eq(1, value.counter.hash_count)
Ejemplo n.º 14
0
    def test_adapt(self):
        eq(
            Opt.absent,
            Opt.adapt(Opt.absent),
        )

        eq(
            Opt(5),
            Opt.adapt(Opt(5)),
        )

        eq(
            Opt.absent,
            Opt.adapt(None),
        )

        eq(
            Opt(5),
            Opt.adapt(5),
        )
Ejemplo n.º 15
0
    def test_present(self):

        five = Opt(5)

        assert five.is_present
        assert not five.is_absent

        eq(5, five.opt)

        eq(5, five.opt_default(6))

        eq(five, Opt(5))

        ne(five, Opt.absent)
Ejemplo n.º 16
0
    def test_once_example(self):
        '''
    make sure properties decorated with @once are only computed once
    '''
        class Person(object):
            def __init__(s, age):
                s.compute_count = 0
                s.age = age

            @once
            def double_age(s):
                s.compute_count += 1
                return s.age * 2

        person = Person(age=5)

        eq(5, person.age)
        eq(0, person.compute_count)

        eq(10, person.double_age)
        eq(1, person.compute_count)

        eq(10, person.double_age)
        eq(1, person.compute_count)
      def __init__( self, a ):

        evaluated[0] = True

        eq( self.a, 1 )
Ejemplo n.º 18
0
    def test_value_mixin_inheritance(self):

        parent = Parent(1)
        eq(1, parent.a)
        eq(2, parent.double_a)
        eq(9, parent.c)

        parent = Parent(1, c=64325)
        eq(1, parent.a)
        eq(2, parent.double_a)
        eq(64325, parent.c)

        raises(
            TypeError,
            lambda: Parent(1, 2, 3),
        )

        child = Child(1, 10)
        eq(1, child.a)
        eq(2, child.double_a)
        eq(10, child.b)
        eq(30, child.triple_b)
        eq(99, child.c)

        child = Child(1, 10, c=4562)
        eq(1, child.a)
        eq(2, child.double_a)
        eq(10, child.b)
        eq(30, child.triple_b)
        eq(4562, child.c)

        # internal details
        eq(('a', 'c'), parent.field_names)
        eq(('a', 'b', 'c'), child.field_names)
Ejemplo n.º 19
0
  def test_once_example( self ):
    '''
    make sure properties decorated with @once are only computed once
    '''
    
    class Person( object ):
      def __init__( s, age ):
        s.compute_count = 0
        s.age = age

      @once
      def double_age( s ):
        s.compute_count += 1
        return s.age * 2

    person = Person( age = 5 )

    eq( 5, person.age )
    eq( 0, person.compute_count )

    eq( 10, person.double_age )
    eq( 1, person.compute_count )

    eq( 10, person.double_age )
    eq( 1, person.compute_count )
  def test_value_mixin_inheritance( self ):

    parent = Parent( 1 )
    eq( 1, parent.a )
    eq( 2, parent.double_a )
    eq( 9, parent.c )

    parent = Parent( 1, c = 64325 )
    eq( 1, parent.a )
    eq( 2, parent.double_a )
    eq( 64325, parent.c )

    raises( 
      TypeError,
      lambda: Parent( 1, 2, 3 ),
    )

    child = Child( 1, 10 )
    eq( 1, child.a )
    eq( 2, child.double_a )
    eq( 10, child.b )
    eq( 30, child.triple_b )
    eq( 99, child.c )

    child = Child( 1, 10, c = 4562 )
    eq( 1, child.a )
    eq( 2, child.double_a )
    eq( 10, child.b )
    eq( 30, child.triple_b )
    eq( 4562, child.c )

    # internal details
    eq( ( 'a', 'c' ), parent.field_names )
    eq( ( 'a', 'b', 'c' ), child.field_names )
Ejemplo n.º 21
0
            def __init__(self, a):

                evaluated[0] = True

                eq(self.a, 1)