<commit_before>from pydoctor.test.test_astbuilder import fromText
from pydoctor.test.test_packages import processPackage
from pydoctor import server
from nevow import context, testutil, appserver, flat, inevow
from twisted.internet.defer import maybeDeferred

def deferredResult(*args):
    hack = []
    maybeDeferred(*args).addCallback(lambda x:hack.append(x))
    return hack[0]

def getTextOfPage(root, page, args=None):
    """This perpetrates several awful hacks."""
    r = testutil.FakeRequest(args=args)
    r.postpath = [page]
    ctx = context.RequestContext(tag=r)
    pageContext = deferredResult(appserver.NevowSite(root).getPageContextForRequestContext, ctx)
    while 1:
        html = deferredResult(pageContext.tag.renderHTTP, pageContext)
        if isinstance(html, str):
            return r.v + html
        res = inevow.IResource(html)
        pageContext = context.PageContext(tag=res, parent=pageContext)

def test_simple():
    m = fromText('''
    """This is a docstring!"""
    class C:
        pass
    ''', modname="mod")
    root = server.PyDoctorResource(m.system)
    assert 'This is a docstring!' in getTextOfPage(root, 'mod.html')

def test_edit_renders_ok():
    system = processPackage('localimporttest')
    root = server.EditingPyDoctorResource(system)
    args = {'ob':['localimporttest.mod1.C']}
    result = getTextOfPage(root, 'edit', args=args)
    # very weak, but it's an assert that things didn't explode
    assert 'textarea' in result

    args = {'ob':['does.not.exist']}
    result = getTextOfPage(root, 'edit', args=args)
    assert 'An error occurred' in result
<commit_msg>porting patches from bzr... ------------------------------------------------------------ revno: 337 committer: <NAME> <<EMAIL>> branch nick: pydoctor.dev timestamp: Sat 2007-04-21 11:50:05 +0200 message:   test editing the docstring<commit_after>from pydoctor.test.test_astbuilder import fromText
from pydoctor.test.test_packages import processPackage
from pydoctor import server
from nevow import context, testutil, appserver, flat, inevow
from twisted.internet.defer import maybeDeferred

def deferredResult(*args):
    hack = []
    maybeDeferred(*args).addCallback(lambda x:hack.append(x))
    return hack[0]

def getTextOfPage(root, page, args=None, return_request=False):
    """This perpetrates several awful hacks."""
    if args is not None:
        args_ = {}
        for k, v in args.iteritems():
            args_[k] = [v]
        args = args_
    r = testutil.FakeRequest(args=args)
    r.postpath = [page]
    ctx = context.RequestContext(tag=r)
    pageContext = deferredResult(appserver.NevowSite(root).getPageContextForRequestContext, ctx)
    while 1:
        html = deferredResult(pageContext.tag.renderHTTP, pageContext)
        if isinstance(html, str):
            if return_request:
                return r.v + html, r
            else:
                return r.v + html
        res = inevow.IResource(html)
        pageContext = context.PageContext(tag=res, parent=pageContext)

def test_simple():
    m = fromText('''
    """This is a docstring!"""
    class C:
        pass
    ''', modname="mod")
    root = server.PyDoctorResource(m.system)
    assert 'This is a docstring!' in getTextOfPage(root, 'mod.html')

def test_edit_renders_ok():
    system = processPackage('localimporttest')
    root = server.EditingPyDoctorResource(system)
    args = {'ob':'localimporttest.mod1.C'}
    result = getTextOfPage(root, 'edit', args=args)
    # very weak, but it's an assert that things didn't explode
    assert 'textarea' in result

    args = {'ob':'does.not.exist'}
    result = getTextOfPage(root, 'edit', args=args)
    assert 'An error occurred' in result

def test_edit():
    system = processPackage('localimporttest')
    root = server.EditingPyDoctorResource(system)

    ob = system.allobjects['localimporttest.mod1.C']
    docstring = root.currentDocstringForObject(ob)
    assert docstring == ob.docstring

    args = {'ob':ob.fullName(), 'docstring':'"This *is* a docstring"',
            'action':'Submit'}
    result, r = getTextOfPage(root, 'edit', args=args, return_request=True)
    assert not result
    assert 'localimporttest.mod1.C' in r.redirected_to
    docstring = root.currentDocstringForObject(ob)
    assert docstring == eval(args['docstring'])
    assert ob.docstring != docstring

{'fraction_non_alphanumeric': 0.1282110091743119, 'fraction_numerical': 0.006651376146788991, 'mean_word_length': 3.4454638124362895, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7031153', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1301, 'n_words': 335}