Explorar o código

Added Node, Line and Graph unit tests.

Sander Mathijs van Veen %!s(int64=14) %!d(string=hai) anos
pai
achega
526d37362d
Modificáronse 6 ficheiros con 178 adicións e 0 borrados
  1. 3 0
      test.py
  2. 0 0
      tests/__init__.py
  3. 29 0
      tests/rules.mk
  4. 74 0
      tests/test_graph.py
  5. 36 0
      tests/test_line.py
  6. 36 0
      tests/test_node.py

+ 3 - 0
test.py

@@ -0,0 +1,3 @@
+from testrunner import main
+import sys
+main(sys.argv[1:])

+ 0 - 0
tests/__init__.py


+ 29 - 0
tests/rules.mk

@@ -0,0 +1,29 @@
+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 $@

+ 74 - 0
tests/test_graph.py

@@ -0,0 +1,74 @@
+# 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]

+ 36 - 0
tests/test_line.py

@@ -0,0 +1,36 @@
+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'

+ 36 - 0
tests/test_node.py

@@ -0,0 +1,36 @@
+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'