Added Node, Line and Graph unit tests.

parent c123d2c4
from testrunner import main
import sys
main(sys.argv[1:])
TESTS=$(wildcard tests/test_*.py)
COVERAGE_OUTPUT_DIR := coverage
OMIT := --omit /usr/share/pyshared/*
ifeq ($(findstring python-coverage,$(wildcard /usr/bin/*)), python-coverage)
COVERAGE=/usr/bin/python-coverage
else
COVERAGE=/usr/bin/coverage
endif
.PHONY: test coverage $(TESTS)
test: $(TESTS) build
coverage: ${COVERAGE} build
mkdir ${COVERAGE_OUTPUT_DIR} 2>/dev/null || true
${COVERAGE} erase
for t in ${TESTS}; do \
echo $$t; \
${COVERAGE} ${OMIT} -x test.py $$t; \
${COVERAGE} ${OMIT} -c; \
done
${COVERAGE} html ${OMIT} --dir ${COVERAGE_OUTPUT_DIR}
${COVERAGE}:
@echo "Install package 'python-coverage' to generate a coverage report."
@echo "On Debian/Ubuntu use: sudo apt-get install python-coverage"; false
$(TESTS): build; @python -m testrunner $@
# vim: set fileencoding=utf-8 :
import unittest
from node import Node, Leaf
from graph import generate_graph
class TestGraph(unittest.TestCase):
def setUp(self):
self.l0, self.l1, self.multi = Leaf(0), Leaf(1), Leaf('test')
def tearDown(self):
pass
def test_simple_unary(self):
uminus = Node('-', self.l1)
g = generate_graph(uminus, Node)
expect = self.strip("""
-
1
""")
assert g == expect
def test_simple_binary(self):
plus = Node('+', self.l0, self.l1)
g = generate_graph(plus, Node)
expect = self.strip("""
+
╭┴╮
0 1
""")
assert g == expect
def test_multichar_unary(self):
uminus = Node('-', self.multi)
g = generate_graph(uminus, Node)
expect = self.strip("""
-
test
""")
print g
print expect
assert g == expect
def test_multichar_binary(self):
plus = Node('+', self.multi, self.l1)
g = generate_graph(plus, Node)
expect = self.strip("""
+
╭───┴╮
test 1
""")
assert g == expect
def test_function(self):
exp = Leaf('x')
inf = Leaf('oo')
minus_inf = Node('-', inf)
integral = Node('int', exp, minus_inf, inf)
g = generate_graph(integral, Node)
expect = self.strip("""
int
╭─┼──╮
x - oo
oo
""")
assert g == expect
def strip(self, str):
return str.replace('\n ', '\n')[1:-1]
import unittest
from node import Node, Leaf
from line import generate_line
class TestLine(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_simple(self):
l0, l1 = Leaf(1), Leaf(2)
plus = Node('+', l0, l1)
assert generate_line(plus, Node) == '1 + 2'
def test_parentheses(self):
l0, l1 = Leaf(1), Leaf(2)
plus = Node('+', l0, l1)
times = Node('*', plus, plus)
assert generate_line(times, Node) == '(1 + 2) * (1 + 2)'
def test_function(self):
exp = Leaf('x')
inf = Leaf('oo')
minus_inf = Node('-', inf)
integral = Node('int', exp, minus_inf, inf)
assert generate_line(integral, Node) == 'int(x, -oo, oo)'
def test_mod(self):
l0, l1 = Leaf(1), Leaf(2)
mod = Node('mod', l1, l0)
assert generate_line(mod, Node) == '2 mod 1'
import unittest
from node import Node, Leaf
class TestNode(unittest.TestCase):
def setUp(self):
self.l0 = Leaf('leaf 1')
self.l1 = Leaf('leaf 2')
self.node = Node('node', self.l0, self.l1)
def tearDown(self):
pass
def test_getitem(self):
assert self.node[0] == self.l0
def test_setitem(self):
self.node[0] = Leaf('leaf 3')
assert self.node[0] != self.l0
def test_iter(self):
s = ''
for leaf in self.node:
s += leaf.title()
assert s == 'leaf 1leaf 2'
def test_len(self):
assert len(self.node) == 2
def test_title(self):
assert self.node.title() == 'node'
assert self.l0.title() == 'leaf 1'
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment