~ruby/pythoscope/better-exception-handling

« back to all changes in this revision

Viewing changes to pythoscope/generator.py

  • Committer: Michal Kwiatkowski
  • Date: 2008-10-11 17:48:37 UTC
  • Revision ID: constant.beta@gmail.com-20081011174837-q2wc8eyw9x73f5gi
Generator generates proper tests for Python generators with incomplete inputs or outputs (part of the 'Handle Python generators' blueprint).

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    elif isinstance(object, Type):
22
22
        return object.type
23
23
    else:
24
 
        raise ValueNeeded()
25
 
 
26
 
# :: ObjectWrapper -> string
 
24
        raise ValueNeeded("Can't unwrap the type of %s" % object)
 
25
 
 
26
# :: [string] -> string
 
27
def list_of(strings):
 
28
    return "[%s]" % ', '.join(strings)
 
29
 
 
30
# :: ObjectWrapper | [ObjectWrapper] -> string
27
31
def type_as_string(object):
28
32
    """Return a most common representation of the wrapped object type.
29
33
 
33
37
    'dict'
34
38
    >>> type_as_string(Type(lambda: None))
35
39
    'types.FunctionType'
 
40
    >>> type_as_string([Type(()), Type({})])
 
41
    '[tuple, dict]'
36
42
    """
 
43
    if isinstance(object, list):
 
44
        return list_of(map(type_as_string, object))
 
45
 
37
46
    mapping = {
38
47
        list: 'list',
39
48
        dict: 'dict',
124
133
    elif isinstance(object, Repr):
125
134
        return CallString("<TODO: %s>" % object.repr, uncomplete=True)
126
135
    elif isinstance(object, list):
127
 
        return "[%s]" % ', '.join(map(constructor_as_string, object))
 
136
        return list_of(map(constructor_as_string, object))
128
137
    else:
129
138
        raise TypeError("constructor_as_string expected ObjectWrapper or LiveObject object at input, not %s" % object)
130
139
 
287
296
def type_of(string):
288
297
    return "type(%s)" % string
289
298
 
 
299
def map_types(string):
 
300
    return "map(type, %s)" % string
 
301
 
290
302
def should_ignore_method(method):
291
303
    return method.name.startswith('_') and method.name != "__init__"
292
304
 
546
558
            else:
547
559
                # If we can't test for real values, let's at least test for the right type.
548
560
                output_type = type_as_string(call.output)
 
561
                if isinstance(call, GeneratorObject):
 
562
                    input_type = map_types(input)
 
563
                else:
 
564
                    input_type = type_of(input)
549
565
                self.ensure_import('types')
550
 
                return (assertion_type, output_type, type_of(input))
 
566
                return (assertion_type, output_type, input_type)
551
567
 
552
568
class UnittestTestGenerator(TestGenerator):
553
569
    main_snippet = parse_fragment("if __name__ == '__main__':\n    unittest.main()\n")