Example #1
0
def test_is_private():
    # XXX implicit test, but so are the rest :|
    class Foo(object):
        def foo(self):
            pass

        def _foo(self):
            pass

        def __foo(self):
            pass

        def trigger__foo(self):
            self.__foo()

        def __foo__(self):
            pass

    ds = DocStorage().from_dict({"Foo": Foo})
    dsa = DocStorageAccessor(ds)
    t = Tracer(ds)
    t.start_tracing()
    f = Foo()
    f.foo()
    f._foo()
    f.trigger__foo()
    f.__foo__()
    t.end_tracing()
    assert sorted(ds.descs["Foo"].getfields()) == ["__foo__", "foo", "trigger__foo"]
Example #2
0
def test_basic():
    descs = {"fun": fun}
    ds = DocStorage().from_dict(descs)
    t = Tracer(ds)
    t.start_tracing()
    fun(1, ("g", 3), 8)
    fun(2.0, ("a", 1.0), "a")
    t.end_tracing()
    desc = ds.descs["fun"]
    inputcells = desc.inputcells
    assert len(inputcells) == 3
    assert isinstance(inputcells[0], model.SomeUnion)
    assert isinstance(inputcells[1], model.SomeTuple)
    assert isinstance(inputcells[2], model.SomeUnion)
    assert isinstance(desc.retval, model.SomeString)
    cs = sorted(desc.call_sites.keys())
    assert len(cs) == 2
    f_name = cut_pyc(__file__)
    assert len(cs[0]) == 1
    assert len(cs[1]) == 1
    assert cs[1][0].filename == f_name
    # lines are counted from 0
    num = test_basic.func_code.co_firstlineno
    assert cs[1][0].lineno == num + 4 or cs[1][0].lineno == num + 5
    assert cs[0][0].filename == f_name
    assert cs[0][0].lineno == num + 5 or cs[0][0].lineno == num + 4
    if 0:
        pds = PermaDocStorage(DocStorageAccessor(ds))
        assert pds.get_function_names() == ["fun"]
        sig = pds.get_function_signature("fun")
        assert sig[0][0][0] == "a"
        assert isinstance(sig[0][0][1], model.SomeUnion)
        assert len(pds.get_function_callpoints("fun")) == 2
Example #3
0
 def test_build_callable_view(self):
     ds, dsa = get_dsa(self.fs_root, self.pkg_name)
     t = Tracer(ds)
     t.start_tracing()
     pkg = __import__(self.pkg_name)
     pkg.main.sub.func(10)
     pkg.main.sub.func(pkg.main.SomeClass(10))
     t.end_tracing()
     apb = ApiPageBuilder(self.base, self.linker, dsa, self.fs_root,
                          self.namespace_tree, self.project)
     snippet = apb.build_callable_view('main.sub.func')
     html = snippet.unicode()
     print html
     # XXX somewhat grokky tests because the order of the items may change
     assert 'arg1: AnyOf(' in html
     pos1 = html.find('arg1: AnyOf(')
     assert pos1 > -1
     pos2 = html.find('href="', pos1)
     assert pos2 > pos1
     pos3 = html.find('Class SomeClass', pos2)
     assert pos3 > pos2
     pos4 = html.find('Int>', pos1)
     assert pos4 > pos1
     pos5 = html.find('return value:', pos4)
     assert pos5 > pos4 and pos5 > pos3
     pos6 = html.find('<None>', pos5)
     assert pos6 > pos5
     sourcefile = self.fs_root.join('pkg/func.py')
     pos7 = html.find('source: %s' % (get_rel_sourcepath(apb.projpath,
                                                         sourcefile),),
                      pos6)
     assert pos7 > pos6
     _checkhtmlsnippet(html)
Example #4
0
 def test_som_fun(self):
     descs = {'fun_': fun_}
     ds = DocStorage().from_dict(descs)
     t = Tracer(ds)
     t.start_tracing()
     fun_()
     t.end_tracing()
     lg = DirectPaste()
     tempdir = temppath.ensure("some_fun", dir=True)
     r = RestGen(DocStorageAccessor(ds), lg, DirWriter(tempdir))
     r.write()
     self.check_rest(tempdir)
Example #5
0
def test_without_init():
    ds = DocStorage().from_dict({"A": A, "B": B})
    t = Tracer(ds)
    t.start_tracing()
    x = A()
    y = B()
    x.method(3)
    y.method(4)
    t.end_tracing()
    assert isinstance(ds.descs["A"].fields["method"].inputcells[1], model.SomeInt)
    assert isinstance(ds.descs["B"].fields["method"].inputcells[1], model.SomeInt)
    if 0:
        pds = PermaDocStorage(DocStorageAccessor(ds))
Example #6
0
def test_while_call():
    ds = DocStorage().from_dict({"other_fun": other_fun})
    t = Tracer(ds)
    t.start_tracing()
    for x in xrange(8):
        other_fun()
    t.end_tracing()
    desc = ds.descs["other_fun"]
    assert len(desc.call_sites.keys()) == 1
    # assert isinstance(desc.call_sites.values()[0][0], py.code.Frame)
    if 0:
        pds = PermaDocStorage(DocStorageAccessor(ds))
        assert len(pds.get_function_callpoints("other_fun")) == 1
Example #7
0
 def get_filled_docstorage(self):
     descs = {'SomeClass': SomeClass,
              'SomeSubClass': SomeSubClass,
              'fun':fun}
     ds = DocStorage().from_dict(descs)
     t = Tracer(ds)
     t.start_tracing()
     s1 = SomeClass("a")
     fun(1, 2, s1)
     s2 = SomeSubClass("b")
     s2.method(1,2,3)
     fun(1, 3, s2)
     t.end_tracing()
     return DocStorageAccessor(ds)
Example #8
0
def test_local_changes_nochange():
    class testclass(object):
        def __init__(self):
            self.foo = 0
        def bar(self, x):
            self.foo = x
    ds = DocStorage().from_dict({'testclass': testclass})
    t = Tracer(ds)
    t.start_tracing()
    c = testclass()
    t.end_tracing()
    desc = ds.descs['testclass']
    methdesc = desc.fields['bar']
    assert methdesc.get_local_changes() == {}
    return ds
Example #9
0
def test_subclass():
    descs = {"ANotherClass": ANotherClass}
    ds = DocStorage().from_dict(descs)
    t = Tracer(ds)
    t.start_tracing()
    s = ANotherClass("blah blah")
    s.another_exposed_method(1)
    t.end_tracing()
    desc = ds.descs["ANotherClass"]
    assert len(desc.fields) == 4
    inputcells = desc.fields["__init__"].inputcells
    assert len(inputcells) == 2
    inputcells = desc.fields["another_exposed_method"].inputcells
    assert len(inputcells) == 2
    bases = desc.bases
    assert len(bases) == 2
    return ds
Example #10
0
def test_multiple_classes_with_same_init():
    class A:
        def __init__(self, x):
            self.x = x

    class B(A):
        pass

    ds = DocStorage().from_dict({"A": A, "B": B})
    t = Tracer(ds)
    t.start_tracing()
    c = A(3)
    d = B(4)
    t.end_tracing()
    assert len(ds.descs["A"].fields["__init__"].call_sites) == 1
    assert len(ds.descs["B"].fields["__init__"].call_sites) == 1
    return ds
Example #11
0
def test_local_changes():
    class testclass(object):
        def __init__(self):
            self.foo = 0
        def bar(self, x):
            self.foo = x
    ds = DocStorage().from_dict({'testclass': testclass})
    t = Tracer(ds)
    t.start_tracing()
    c = testclass()
    c.bar(1)
    t.end_tracing()
    desc = ds.descs['testclass']
    methdesc = desc.fields['bar']
    #assert methdesc.old_dict != methdesc.new_dict
    assert methdesc.get_local_changes() == {'foo': set(['changed'])}
    return ds
Example #12
0
    def test_sourceview_fun(self):
        def f():
            pass

        descs = {'f':f}
        ds = DocStorage().from_dict(descs)
        t = Tracer(ds)
        t.start_tracing()
        f()
        t.end_tracing()
        tempdir = temppath.ensure("sourceview_fun", dir=True)
        lg = SourceView('http://localhost:8000')
        r = RestGen(DocStorageAccessor(ds), lg, DirWriter(tempdir))
        r.write()
        self.check_rest(tempdir)
        assert tempdir.join('function_f.txt').open().read().find(
            '.. _`/py/apigen/rest/testing/test\_rest.py\:f`: http://localhost:8000/py/apigen/rest/testing/test_rest.py#f') != -1
Example #13
0
 def get_filled_docstorage_modules(self):
     import somemodule
     import someothermodule
     descs = {
         'somemodule.SomeClass': somemodule.SomeClass,
         'someothermodule.SomeSubClass': someothermodule.SomeSubClass,
         'someothermodule.fun': someothermodule.fun,
     }
     ds = DocStorage().from_dict(descs)
     t = Tracer(ds)
     t.start_tracing()
     s1 = somemodule.SomeClass("a")
     someothermodule.fun(1, 2, s1)
     s2 = someothermodule.SomeSubClass("b")
     s2.method(1, 2, 3)
     someothermodule.fun(1, 3, s2)
     t.end_tracing()
     return DocStorageAccessor(ds)
Example #14
0
 def test_sourceview(self):
     class A:
         def method(self):
             pass
     
     descs = {'A':A}
     ds = DocStorage().from_dict(descs)
     t = Tracer(ds)
     t.start_tracing()
     A().method()
     t.end_tracing()
     lg = SourceView('http://localhost:8000')
     tempdir = temppath.ensure("sourceview", dir=True)
     r = RestGen(DocStorageAccessor(ds), lg, DirWriter(tempdir))
     r.write()
     self.check_rest(tempdir)
     assert tempdir.join('traceback_A.method.0.txt').open().read().find(
        '.. _`/py/apigen/rest/testing/test\_rest.py\:A.method`: http://localhost:8000/py/apigen/rest/testing/test_rest.py#A.method') != -1
Example #15
0
 def test_nonexist_origin(self):
     class A:
         def method(self):
             pass
     
     class B(A):
         pass
     
     descs = {'B':B}
     ds = DocStorage().from_dict(descs)
     t = Tracer(ds)
     t.start_tracing()
     B().method()
     t.end_tracing()
     lg = DirectPaste()
     tempdir = temppath.ensure("nonexit_origin", dir=True)
     r = RestGen(DocStorageAccessor(ds), lg, DirWriter(tempdir))
     r.write()
     self.check_rest(tempdir)
Example #16
0
 def test_exc_raising(self):
     def x():
         try:
             1/0
         except:
             pass
     
     descs = {'x':x}
     ds = DocStorage().from_dict(descs)
     t = Tracer(ds)
     t.start_tracing()
     x()
     t.end_tracing()
     lg = DirectPaste()
     tempdir = temppath.ensure("exc_raising", dir=True)
     r = RestGen(DocStorageAccessor(ds), lg, DirWriter(tempdir))
     r.write()
     source = tempdir.join('function_x.txt').open().read()
     assert source.find('ZeroDivisionError') < source.find('call sites\:')
Example #17
0
def test_multiple_methods():
    class A(object):
        def meth(self):
            pass

    class B(A):
        pass

    class C(A):
        pass

    ds = DocStorage().from_dict({"C": C, "B": B})
    dsa = DocStorageAccessor(ds)
    t = Tracer(ds)
    t.start_tracing()
    B().meth()
    C().meth()
    t.end_tracing()
    assert len(ds.descs["B"].fields["meth"].call_sites) == 1
    assert len(ds.descs["C"].fields["meth"].call_sites) == 1
    return ds
Example #18
0
def test_get_initpkg_star_items():
    pkg, ds = setup_pkg_docstorage()
    sit = get_star_import_tree(pkg.other, "pkg.other")
    assert sorted(sit.keys()) == ["pkg.other.baz", "pkg.other.foo"]
    t = Tracer(ds)
    t.start_tracing()
    pkg.main.sub.func("a1")
    pkg.main.SomeClass(3).get_somevar()
    pkg.main.SomeSubClass(4).get_somevar()
    t.end_tracing()
    assert isinstance(ds.descs["main.sub.func"].inputcells[0], model.SomeString)
    desc = ds.descs["main.SomeClass"]
    assert ds.descs["main.SomeClass.get_somevar"] is desc.fields["get_somevar"]
    cell = desc.fields["get_somevar"].inputcells[0]
    assert isinstance(cell, model.SomeInstance)
    assert cell.classdef.cls is desc.pyobj
    desc = ds.descs["main.SomeSubClass"]
    assert ds.descs["main.SomeSubClass.get_somevar"] is desc.fields["get_somevar"]
    cell = desc.fields["get_somevar"].inputcells[0]
    assert isinstance(cell, model.SomeInstance)
    assert cell.classdef.cls is desc.pyobj
Example #19
0
 def test_function_source(self):
     def blah():
         a = 3
         b = 4
         return a + b
     
     descs = {'blah': blah}
     ds = DocStorage().from_dict(descs)
     t = Tracer(ds)
     t.start_tracing()
     blah()
     t.end_tracing()
     lg = DirectPaste()
     tempdir = temppath.ensure("function_source", dir=True)
     r = RestGen(DocStorageAccessor(ds), lg, DirWriter(tempdir))
     r.write()
     assert tempdir.join("function_blah.txt").read().find("a = 3") != -1
     self.check_rest(tempdir)
     ps = DocStorageAccessor(ds)
     r = RestGen(ps, lg, DirWriter(tempdir))
     r.write()
     assert tempdir.join("function_blah.txt").read().find("a = 3") != -1
Example #20
0
def test_exception_raise():
    def x():
        1/0
    
    def y():
        try:
            x()
        except ZeroDivisionError:
            pass
    
    def z():
        y()
    
    ds = DocStorage().from_dict({'x':x, 'y':y, 'z':z})
    t = Tracer(ds)
    t.start_tracing()
    z()
    t.end_tracing()
    assert ds.descs['x'].exceptions.keys() == [ZeroDivisionError]
    assert ds.descs['y'].exceptions.keys() == [ZeroDivisionError]
    assert ds.descs['z'].exceptions.keys() == []
    return ds
Example #21
0
def test_exception_raise():
    def x():
        1 / 0

    def y():
        try:
            x()
        except ZeroDivisionError:
            pass

    def z():
        y()

    ds = DocStorage().from_dict({"x": x, "y": y, "z": z})
    t = Tracer(ds)
    t.start_tracing()
    z()
    t.end_tracing()
    assert ds.descs["x"].exceptions.keys() == [ZeroDivisionError]
    assert ds.descs["y"].exceptions.keys() == [ZeroDivisionError]
    assert ds.descs["z"].exceptions.keys() == []
    return ds
Example #22
0
def test_class():
    descs = {"AClass": AClass}
    ds = DocStorage().from_dict(descs)
    t = Tracer(ds)
    t.start_tracing()
    s = AClass()
    s.exposed_method(1, 2.0, [1, 2, 3])
    t.end_tracing()
    desc = ds.descs["AClass"]
    inputcells = desc.fields["__init__"].inputcells
    assert len(inputcells) == 2
    assert isinstance(inputcells[0], model.SomeInstance)
    # assert inputcells[0].classdef.classdesc.pyobj is SomeClass
    # XXX: should work
    assert isinstance(inputcells[1], model.SomeString)
    f_name = __file__
    if f_name.endswith(".pyc"):
        f_name = f_name[:-1]
    cs = sorted(desc.fields["__init__"].call_sites.keys())
    assert len(cs) == 1
    assert len(cs[0]) == 1
    assert cs[0][0].filename == f_name
    assert cs[0][0].lineno == test_class.func_code.co_firstlineno + 4
    # method check
    assert sorted(desc.getfields()) == ["__init__", "exposed_method"]
    inputcells = desc.fields["exposed_method"].inputcells
    assert len(inputcells) == 4
    assert isinstance(inputcells[0], model.SomeInstance)
    # assert inputcells[0].classdef.classdesc.pyobj is SomeClass
    # XXX should work
    assert isinstance(inputcells[1], model.SomeInt)
    assert isinstance(inputcells[2], model.SomeFloat)
    assert isinstance(inputcells[3], model.SomeList)
    assert isinstance(desc.fields["exposed_method"].retval, model.SomeString)
    if 0:
        pds = PermaDocStorage(DocStorageAccessor(ds))
        assert pds.get_class_names() == ["AClass"]
        assert len(pds.get_function_signature("AClass.exposed_method")[0]) == 4
Example #23
0
    def test_class_typedefs(self):
        class A(object):
            def __init__(self, x):
                pass

            def a(self):
                pass
        
        class B(A):
            def __init__(self, y):
                pass
        
        def xxx(x):
            return x
        
        descs = {'A': A, 'B': B, 'xxx':xxx}
        ds = DocStorage().from_dict(descs)
        t = Tracer(ds)
        t.start_tracing()
        xxx(A(3))
        xxx(B("f"))
        t.end_tracing()
        lg = DirectPaste()
        tempdir = temppath.ensure("classargs", dir=True)
        r = RestGen(DocStorageAccessor(ds), lg, DirWriter(tempdir))
        r.write()
        source = tempdir.join("function_xxx.txt").read()
        call_point = source.find("call sites\:")
        assert call_point != -1
        print source
        assert -1 < source.find("x \:\: <Instance of AnyOf( `Class B`_ , "
                                "`Class A`_ )>") < call_point
        source = tempdir.join('method_B.a.txt').read()
        py.test.skip('XXX needs to be fixed, clueless atm though')
        assert source.find('**origin** \: `A`_') > -1
        self.check_rest(tempdir)
Example #24
0
 def test_function_arguments(self):
     def blah(a, b, c):
         return "axx"
     
     class C:
         pass
     
     descs = {'blah':blah}
     ds = DocStorage().from_dict(descs)
     t = Tracer(ds)
     t.start_tracing()
     blah(3, "x", C())
     t.end_tracing()
     lg = DirectPaste()
     tempdir = temppath.ensure("function_args", dir=True)
     r = RestGen(DocStorageAccessor(ds), lg, DirWriter(tempdir))
     r.write()
     source = tempdir.join("function_blah.txt").read()
     call_point = source.find("call sites\:")
     assert call_point != -1
     assert source.find("a \:\: <Int>") < call_point
     assert source.find("b \:\: <String>") < call_point
     assert source.find("c \:\: <Instance of Class C>") < call_point
     self.check_rest(tempdir)