From 8e47cbc42244917b4050b76774e57a6a20a02ee1 Mon Sep 17 00:00:00 2001 From: Mike Mueller Date: Wed, 15 Apr 2015 17:13:19 +0200 Subject: [PATCH] Basic test runner for py.test. Compiles Mochi files to Python bytecode. --- mochi/utils/__init__.py | 0 mochi/utils/pycloader.py | 19 +++++++++++++++++++ tasks.py | 14 ++++++++++++++ tests/factorial.mochi | 4 ++++ tests/test_patternmatching.py | 14 ++++++++++++++ 5 files changed, 51 insertions(+) create mode 100644 mochi/utils/__init__.py create mode 100644 mochi/utils/pycloader.py create mode 100644 tasks.py create mode 100644 tests/factorial.mochi create mode 100644 tests/test_patternmatching.py diff --git a/mochi/utils/__init__.py b/mochi/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mochi/utils/pycloader.py b/mochi/utils/pycloader.py new file mode 100644 index 0000000..f8dd77a --- /dev/null +++ b/mochi/utils/pycloader.py @@ -0,0 +1,19 @@ +"""Import a Python object made by compiling a Mochi file. +""" + +import os + +from mochi.core import pyc_compile_monkeypatch + + +def get_function(name, file_path): + """Python function from Mochi. + + Compiles a Mochi file to Python bytecode and returns the + imported function. + """ + base_path = os.path.dirname(file_path) + mochi_name = os.path.join(base_path, name + '.mochi') + py_name = os.path.join(base_path, name + '_comp.pyc') + pyc_compile_monkeypatch(mochi_name, py_name) + return getattr(__import__(name), name) diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..432ac4d --- /dev/null +++ b/tasks.py @@ -0,0 +1,14 @@ +"""Using `invoke` to start tests and and other commandline tools. +""" + +from invoke import run, task + + +@task +def test(): + """Run standard tests. + + + Usage (commandline): inv test + """ + run('py.test --assert=reinterp', pty=True) diff --git a/tests/factorial.mochi b/tests/factorial.mochi new file mode 100644 index 0000000..f3b16da --- /dev/null +++ b/tests/factorial.mochi @@ -0,0 +1,4 @@ +def factorial: + n: factorial(n, 1) + 0, acc: acc + n, acc: factorial(n - 1, acc * n) diff --git a/tests/test_patternmatching.py b/tests/test_patternmatching.py new file mode 100644 index 0000000..22f9570 --- /dev/null +++ b/tests/test_patternmatching.py @@ -0,0 +1,14 @@ +import os + +from mochi.utils.pycloader import get_function + +factorial = get_function('factorial', __file__) + +def test_factorial_1(): + assert factorial(1) == 1 + +def test_factorial_2(): + assert factorial(2) == 2 + + +