Skip to content

Commit 8e47cbc

Browse files
committed
Basic test runner for py.test. Compiles Mochi files to Python bytecode.
1 parent d065412 commit 8e47cbc

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

mochi/utils/__init__.py

Whitespace-only changes.

mochi/utils/pycloader.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Import a Python object made by compiling a Mochi file.
2+
"""
3+
4+
import os
5+
6+
from mochi.core import pyc_compile_monkeypatch
7+
8+
9+
def get_function(name, file_path):
10+
"""Python function from Mochi.
11+
12+
Compiles a Mochi file to Python bytecode and returns the
13+
imported function.
14+
"""
15+
base_path = os.path.dirname(file_path)
16+
mochi_name = os.path.join(base_path, name + '.mochi')
17+
py_name = os.path.join(base_path, name + '_comp.pyc')
18+
pyc_compile_monkeypatch(mochi_name, py_name)
19+
return getattr(__import__(name), name)

tasks.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Using `invoke` to start tests and and other commandline tools.
2+
"""
3+
4+
from invoke import run, task
5+
6+
7+
@task
8+
def test():
9+
"""Run standard tests.
10+
11+
12+
Usage (commandline): inv test
13+
"""
14+
run('py.test --assert=reinterp', pty=True)

tests/factorial.mochi

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def factorial:
2+
n: factorial(n, 1)
3+
0, acc: acc
4+
n, acc: factorial(n - 1, acc * n)

tests/test_patternmatching.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
3+
from mochi.utils.pycloader import get_function
4+
5+
factorial = get_function('factorial', __file__)
6+
7+
def test_factorial_1():
8+
assert factorial(1) == 1
9+
10+
def test_factorial_2():
11+
assert factorial(2) == 2
12+
13+
14+

0 commit comments

Comments
 (0)