Ejemplo n.º 1
0
def test_can_override_handlers_for_supertypes():
    s = SpecificationMapper()
    s.define_specification_for_instances(tuple, lambda s, d: sum(d))
    s.define_specification_for_instances(Litter, lambda s, d: len(d))

    assert s.specification_for((1, 2)) == 3
    assert s.specification_for(Litter(2, 2)) == 2
Ejemplo n.º 2
0
def test_is_not_confused_by_equal_things_of_different_types():
    mapper = SpecificationMapper()
    mapper.define_specification_for_instances(Confused1, lambda s, d: 1)
    mapper.define_specification_for_instances(Confused2, lambda s, d: 2)
    mapper.define_specification_for_instances(
        tuple, lambda s, d: sum(s.specification_for(x) for x in d))
    assert mapper.specification_for((Confused1(), )) == 1
    assert mapper.specification_for((Confused2(), )) == 2
Ejemplo n.º 3
0
def test_defining_new_handlers_resets_cache():
    s = SpecificationMapper()
    s.define_specification_for_instances(str, lambda *_: Foo())
    x = s.specification_for('foo')
    s.define_specification_for_instances(str, lambda *_: Fooc())
    y = s.specification_for('foo')
    assert y is not x
    assert isinstance(y, Fooc)
Ejemplo n.º 4
0
def test_can_call_previous_in_overridden_specifications():
    s = SpecificationMapper()
    s.define_specification_for_instances(str, lambda _, s: len(s))
    s.define_specification_for_instances(
        str, lambda _, s: 5 if len(s) > 5 else next_in_chain())
    assert s.specification_for('foo') == 3
    assert s.specification_for(
        'I am the very model of a modern major general') == 5
Ejemplo n.º 5
0
def test_correctly_reports_specifications():
    mapper = SpecificationMapper()
    mapper.define_specification_for_instances(
        list, lambda s, d: s.specification_for(d[0]) + 1 if d else 0)
    mapper.define_specification_for(int, const(1))
    assert mapper.has_specification_for(int)
    assert mapper.has_specification_for([int])
    assert mapper.has_specification_for([])
    assert not mapper.has_specification_for(str)
    assert not mapper.has_specification_for([str])
Ejemplo n.º 6
0
def test_cache_correctly_handles_inheritance():
    s = SpecificationMapper()
    s.define_specification_for_instances(
        list, lambda s, d: [s.specification_for(d[0])])
    t = s.new_child_mapper()
    t.define_specification_for_instances(str, lambda *_: Foo())

    x = t.specification_for('foo')
    y = t.specification_for(['foo'])[0]
    assert x is y
Ejemplo n.º 7
0
def test_can_handle_large_numbers_of_instance_mappers():
    def f(s, x):
        return str(x)

    s = SpecificationMapper()
    s.define_specification_for_instances(tuple, f)
    s.define_specification_for_instances(Litter, f)
    s.define_specification_for_instances(list, f)
    s.define_specification_for_instances(set, f)
    s.define_specification_for_instances(str, f)
    s.define_specification_for_instances(int, f)

    assert s.specification_for((1, 1)) == '(1, 1)'
Ejemplo n.º 8
0
def test_chooses_most_specific_subclass(classes, r):
    classes = list(classes)
    random.shuffle(classes)
    classes = tuple(classes)
    mapper = SpecificationMapper()
    for i in hrange(len(classes)):
        mapper.define_specification_for_instances(
            classes[i],
            const(i),
        )
    for c in all_test_classes:
        if issubclass(c, classes):
            x = c()
            correct_choice = find_most_specific(x, classes)
            i = mapper.specification_for(x)
            assert i == classes.index(correct_choice)
Ejemplo n.º 9
0
def test_specifications_will_match_on_subclasses():
    s = SpecificationMapper()

    def trivial(x):
        return lambda s, d: x

    s.define_specification_for_instances(Parent, trivial(1))
    assert s.specification_for(Parent()) == 1
    assert s.specification_for(Child()) == 1
    assert s.specification_for(Grandchild()) == 1
    assert s.specification_for(GreatGrandchild()) == 1

    s.define_specification_for_instances(Grandchild, trivial(2))
    assert s.specification_for(Parent()) == 1
    assert s.specification_for(Child()) == 1
    assert s.specification_for(Grandchild()) == 2
    assert s.specification_for(GreatGrandchild()) == 2

    s.define_specification_for_instances(Child, trivial(3))
    assert s.specification_for(Parent()) == 1
    assert s.specification_for(Child()) == 3
    assert s.specification_for(Grandchild()) == 2
    assert s.specification_for(GreatGrandchild()) == 2

    s.define_specification_for_instances(GreatGrandchild, trivial(4))
    assert s.specification_for(Parent()) == 1
    assert s.specification_for(Child()) == 3
    assert s.specification_for(Grandchild()) == 2
    assert s.specification_for(GreatGrandchild()) == 4

    choices = [Parent, Child, Grandchild, GreatGrandchild]
    values = {Parent: 1, Child: 3, Grandchild: 2, GreatGrandchild: 4}

    for i in hrange(5, 200):
        c = random.choice(choices)
        assert s.specification_for(c()) == values[c]
        s.define_specification_for_instances(c, trivial(i))
        assert s.specification_for(c()) == i
        values[c] = i
Ejemplo n.º 10
0
def test_can_define_instance_specifications_for_lists():
    sm = SpecificationMapper()
    sm.define_specification_for_instances(list, lambda _, l: len(l))
    assert sm.specification_for([1, 2]) == 2
Ejemplo n.º 11
0
def test_can_define_instance_specifications():
    sm = SpecificationMapper()
    sm.define_specification_for_instances(str, lambda _, i: i + 'bar')
    assert sm.specification_for('foo') == 'foobar'
Ejemplo n.º 12
0
def test_can_handle_subtypes_of_instances():
    s = SpecificationMapper()
    s.define_specification_for_instances(tuple, lambda s, d: sum(d))

    assert s.specification_for((1, 2)) == 3
    assert s.specification_for(Litter(2, 2)) == 4
Ejemplo n.º 13
0
def test_multiple_calls_return_same_value():
    s = SpecificationMapper()
    s.define_specification_for_instances(str, lambda *_: Foo())

    assert s.specification_for('foo') is s.specification_for('foo')
    assert s.specification_for('foo') is not s.specification_for('bar')
Ejemplo n.º 14
0
def test_can_override_instance_specifications():
    s = SpecificationMapper()
    s.define_specification_for_instances(str, const(1))
    s.define_specification_for_instances(str, const(2))
    assert s.specification_for('foo') == 2