Commit 68a43594 by Sven Marnach

Add unit tests for parsers.py.

parent d139a10c
__pycache__/
*.py[cod]
activetable_xblock.egg-info/**
.coverage
......@@ -5,6 +5,22 @@ This XBlock provides a tabular problem type, where students have to fill in some
table.
Running the tests
-----------------
Install the test prerequisites:
pip install -r test-requirements
Run pylint:
pylint activetable
Run the unit tests:
nosetests --with-coverage --cover-package=activetable tests/unit
The table definition
--------------------
......
......@@ -10,7 +10,15 @@ from __future__ import absolute_import, division, unicode_literals
import decimal
class StaticCell(object):
class Cell(object):
"""Abstract base class for all cells."""
def __eq__(self, other):
"""Test for equality based on type and attribute values."""
return type(self) is type(other) and vars(self) == vars(other)
class StaticCell(Cell):
"""A static cell with a fixed value in the table body."""
is_static = True
......@@ -19,7 +27,7 @@ class StaticCell(object):
self.value = value
class NumericCell(object):
class NumericCell(Cell):
"""A numeric response cell."""
is_static = False
......@@ -55,7 +63,7 @@ class NumericCell(object):
return abs(value - self.answer) <= self.abs_tolerance
class StringCell(object):
class StringCell(Cell):
"""A string response cell."""
is_static = False
......
......@@ -90,7 +90,10 @@ def parse_number_list(source):
This is used to parse the column_widths and row_heights lists entered by the user.
"""
try:
lst = ast.literal_eval(source)
except SyntaxError as exc:
raise ParseError(exc.msg)
if not isinstance(lst, list):
raise ParseError('not a list')
if not all(isinstance(x, numbers.Real) for x in lst):
......
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals
import ddt
import unittest
from activetable.cells import Cell, NumericCell, StaticCell, StringCell
from activetable.parsers import ParseError, parse_table, parse_number_list
@ddt.ddt
class ParserTest(unittest.TestCase):
def test_parse_table(self):
table_definition = """
[
['Event', 'Year'],
['French Revolution', Numeric(answer=1789)],
['Volcano exploded in 1883', String(answer='Krakatoa')],
[6.283, 123],
]
"""
thead, tbody = parse_table(table_definition)
expected = eval(table_definition.strip(), dict(Numeric=NumericCell, String=StringCell))
expected_body = []
for i, row in enumerate(expected[1:], 1):
cells = []
for j, cell in enumerate(row):
if not isinstance(cell, Cell):
cell = StaticCell(cell)
cell.index = j
cells.append(cell)
expected_body.append(dict(index=i, cells=cells))
self.assertEqual(thead, expected[0])
self.assertEqual(tbody, expected_body)
@ddt.data(
'syntax error',
'"wrong type"',
'["wrong type"]',
'[["wrong type in header", Numeric(answer=3)]],',
'[["header", "header"], "wrong type in body"]',
'[["header", "header"], ["illegal expression", 1 + 1]]',
'[["header", "header"], ["inconsistent", "row", "length"]]',
'[["header", "header"], ["wrong function name", Numerical(answer=3)]]',
'[["header", "header"], ["wrong argument class", Numeric(3)]]',
'[["header", "header"], ["wrong argument name", Numeric(giraffe=3)]]',
'[["header", "header"], ["wrong argument value", Numeric(giraffe="3")]]',
)
def test_parse_table_errors(self, table_definition):
with self.assertRaises(ParseError):
parse_table(table_definition)
def test_parse_number_list(self):
self.assertEquals(parse_number_list('[1, 2.3]'), [1, 2.3])
for string in [']', '123', '["123"]', '[1j]']:
with self.assertRaises(ParseError):
parse_number_list(string)
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