Skip to content

Commit

Permalink
wip tests (process and formatting)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbovy committed Jun 28, 2017
1 parent 0f46d00 commit 3ce2d21
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
22 changes: 22 additions & 0 deletions xsimlab/tests/test_formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest

from xsimlab.formatting import pretty_print, maybe_truncate, wrap_indent


class TestFormatting(unittest.TestCase):

def test_maybe_truncate(self):
self.assertEqual(maybe_truncate('test', 10), 'test')
self.assertEqual(maybe_truncate('longteststring', 10), 'longtes...')

def test_pretty_print(self):
self.assertEqual(pretty_print('test', 10), 'test' + ' ' * 6)

def test_wrap_indent(self):
text = "line1\nline2"

expected = '-line1\n line2'
self.assertEqual(wrap_indent(text, start='-'), expected)

expected = 'line1\n line2'
self.assertEqual(wrap_indent(text, length=1), expected)
32 changes: 30 additions & 2 deletions xsimlab/tests/test_process.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import unittest
from textwrap import dedent
from io import StringIO

from xsimlab.variable.base import (Variable, VariableList, VariableGroup,
diagnostic)
from xsimlab.process import Process


class MyProcess(Process):
var = Variable(())
var_list = VariableList([Variable(()), Variable(())])
var = Variable((), provided=True)
var_list = VariableList([Variable('x'), Variable(((), 'x'))])
var_group = VariableGroup('group')
no_var = 'this is not a variable object'

Expand Down Expand Up @@ -46,6 +48,16 @@ class TestProcess(unittest.TestCase):

def setUp(self):
self.my_process = MyProcess()
self.my_process_str = dedent("""\
Variables:
* var Variable ()
var_list VariableList
- Variable ('x')
- Variable (), ('x')
var_group VariableGroup 'group'
* diag DiagnosticVariable
Meta:
time_dependent: False""")

def test_constructor(self):
# test dict-like vs. attribute access
Expand Down Expand Up @@ -80,3 +92,19 @@ def test_name(self):
def test_run_step(self):
with self.assertRaisesRegex(NotImplementedError, "no method"):
self.my_process.run_step(1)

def test_info(self):
expected = self.my_process_str

for cls_or_obj in [MyProcess, self.my_process]:
buf = StringIO()
cls_or_obj.info(buf=buf)
actual = buf.getvalue()
self.assertEqual(actual, expected)

def test_repr(self):
expected = '\n'.join(
["<xsimlab.Process 'xsimlab.tests.test_process.MyProcess'>",
self.my_process_str]
)
self.assertEqual(repr(self.my_process), expected)

0 comments on commit 3ce2d21

Please sign in to comment.