#!/usr/bin/env python3
import sys
import re

def _generate(infile):
    path = infile.replace('.py', '').split('/')

    methods = []
    stubs = []

    for line in open(infile, 'r'):
        line = line.strip()
        m = re.match(r'^(async )?def\s([^_\(][^\(]+)\(([^\)]*)\):$', line)
        if m:
            isasync = bool(m[1])
            method = m[2]
            args = m[3]

            methods.append(method)
            stub = "def test_%s():\n    assert %s%s(%s) == expected" % (
                method, 'await ' if isasync else '', method, args)
            stubs.append(stub)

    outfile = open('tests/test_' + '_'.join(path[1:]) + '.py', 'w')
    outfile.write("#pylint: disable=missing-docstring\n")
    outfile.write("from %s import (\n" % '.'.join(path))
    for method in methods:
        outfile.write("    %s,\n" % method)
    outfile.write(")\n")
    outfile.write("\n" + "\n\n".join(stubs))
    outfile.close()

if __name__ == '__main__':
    _generate(sys.argv[1])
