<commit_before># -*- coding: utf-8 -*-
import ast
import base64
import re
from textwrap import dedent

from astunparse import unparse as astunparse

from astdispatch import astdispatch


def parse(dirty_code):
    return ast.parse(dedent(dirty_code))


def to_transform(src_code, dest_code, func, *args, **kwargs):
    """Used to assert node transformation by a source and a destination code.

    ::

       assert to_transform('x = 10 ** 2', 'x = 100', inliner)

    """
    node = parse(src_code)
    func(node)
    return astunparse(node) == astunparse(parse(dest_code))


def test_transform():
    def mangle_id(id_):
        return re.sub(r'[+/=]', '', base64.b64encode(id_))[:6].lower()
    @astdispatch(transform=True)
    def mangle(node):
        return node
    @mangle.register(ast.Name)
    def mangle_name(name):
        return ast.Name(id=mangle_id(name.id), ctx=name.ctx)
    @mangle.register(ast.FunctionDef)
    def mangle_func_def(func_def):
        return ast.FunctionDef(name=mangle_id(func_def.name),
                               args=func_def.args, body=func_def.body,
                               decorator_list=func_def.decorator_list)
    assert to_transform('''
        a = 123
        b = 456
        c = a + b
        def pow(x, y):
            return x ** y
        d = pow(a, b)
    ''', '''
        yq = 123
        yg = 456
        yw = (yq + yg)
        def cg93(ea, eq):
            return (ea ** eq)
        za = cg93(yq, yg)
    ''', mangle)


def test_args():
    @astdispatch<commit_msg>pass tests in Python 3<commit_after># -*- coding: utf-8 -*-
import ast
import base64
import re
from textwrap import dedent

from astunparse import unparse as astunparse

from astdispatch import astdispatch


def parse(dirty_code):
    return ast.parse(dedent(dirty_code))


def to_transform(src_code, dest_code, func, *args, **kwargs):
    """Used to assert node transformation by a source and a destination code.

    ::

       assert to_transform('x = 10 ** 2', 'x = 100', inliner)

    """
    node = parse(src_code)
    func(node)
    return astunparse(node) == astunparse(parse(dest_code))


def test_transform():
    def mangle_id(id_):
        b64_id = base64.b64encode(id_.encode()).decode()
        return re.sub(r'[+/=]', '', b64_id)[:6].lower()
    @astdispatch(transform=True)
    def mangle(node):
        return node
    @mangle.register(ast.Name)
    def mangle_name(name):
        return ast.Name(id=mangle_id(name.id), ctx=name.ctx)
    @mangle.register(ast.FunctionDef)
    def mangle_func_def(func_def):
        return ast.FunctionDef(name=mangle_id(func_def.name),
                               args=func_def.args, body=func_def.body,
                               decorator_list=func_def.decorator_list)
    @mangle.register(ast.arg)
    def mangle_arg(arg):
        return ast.arg(arg=mangle_id(arg.arg), annotation=arg.annotation)
    assert to_transform('''
        a = 123
        b = 456
        c = a + b
        def pow(x, y):
            return x ** y
        d = pow(a, b)
    ''', '''
        yq = 123
        yg = 456
        yw = (yq + yg)
        def cg93(ea, eq):
            return (ea ** eq)
        za = cg93(yq, yg)
    ''', mangle)


def test_args():
    @astdispatch
{'fraction_non_alphanumeric': 0.12746913580246913, 'fraction_numerical': 0.020061728395061727, 'mean_word_length': 2.3655244029075804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4747430', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1083, 'n_words': 255}