def test_content_minus_expansion(self): ''' > Test a specific bug where a tree with just a '-' would not get expanded correctly ''' p0 = Pyramid.from_text('-') p1 = Pyramid.from_text('long_variable_name') p2 = Pyramid.from_text('another_long_name') t = p0 + (p1, p2) self.assertIsInstance(t, Tree)
def test_output(self): ''' > Check output is what it is supposed to be ''' for c, p in zip(TEST_CONTENT, TEST_PYRAMIDS): with self.subTest(content=c): with capture_output() as output: print(Pyramid.from_text(c), end='') self.assertEqual(output.getvalue(), p)
def test_content_minus(self): ''' > Test a specific bug where a tree with just a '-' would not get expanded correctly ''' contents = ['-', ' ' * 10 + '-' + ' ' * 10] for c in contents: with self.subTest(contents=c): p = Pyramid.from_text(c, remove_spaces=False) self.assertEqual(p.content, '-')
def test_remove_spaces(self): ''' > Don't remove spaces from the input ''' p = Pyramid.from_text(' ! ', remove_spaces=False) target = ' ^ \n / \\ \n / ! \\ \n ----- ' with capture_output() as output: print(p, end='') self.assertEqual(output.getvalue(), target)
def test_invalid_type(self): ''' > Type error is raised when attempting to add wrong types ''' p = Pyramid.from_text('hello') for o in ('', 1, {}, [], ()): with self.subTest(other=o): with self.assertRaises(TypeError): p + o p.toTree() + o
def test_toTree(self): ''' Pyramid to Tree ''' for c in TEST_CONTENT: with self.subTest(content=c): p = Pyramid.from_text(c) t = p.toTree() self.assertIsInstance(t, Tree) self.assertEqual(hash(p), hash(t))
def test_cannot_expand_trees(self): ''' > Make sure only trees can get expanded ''' p3 = Pyramid.from_text('Quick brown fox jumped over a lazy dog' * 10) for c in product(TEST_CONTENT, repeat=2): p1, p2 = tuple(map(Pyramid.from_text, c)) with self.subTest(contents=c): t = p1 + (None, p2) # Make a tree with self.assertRaises(RuntimeError): r = t + (p3, p3) self.assertIsInstance(t, Tree) print('Oops:') print(t) print(r)
def build_tree(ast): ''' Build the call tree from the leaves to the root ''' if is_string(ast): return Pyramid.from_text(ast) elif ast is None: return None elif is_tuple(ast): if len(ast) != 3: raise RuntimeError( f'Invalid structure of the abstract syntax tree. ({ast})') if not is_string(ast[0]): raise RuntimeError( f'Invalid abstract syntax tree. The first element of each node must be a string, not a {type(ast[0])}' ) return build_tree(ast[0]) + (build_tree(ast[1]), build_tree(ast[2])) else: raise TypeError( f'Abstract syntax tree must be represented by a list (or just a string) not a {type(ast)}' )
def test_content(self): ''' > Pyramid returning its own content ''' for c in TEST_CONTENT: with self.subTest(content=c): p = Pyramid.from_text(c) self.assertEqual(p.content, c)
def test_min_width(self): ''' > Create a few pyramids, but now with minimum width ''' for c, w in product(TEST_CONTENT, range(10)): with self.subTest(content=c, min_width=w): p = Pyramid.from_text(c, min_width=w)
def test_creation(self): ''' > Create few pyramids ''' for c in TEST_CONTENT: with self.subTest(content=c): p = Pyramid.from_text(c)
def test_from_str(self): ''' > Make pyramids from string representation ''' for p in TEST_PYRAMIDS: with self.subTest(): self.assertEqual(str(Pyramid.from_str(p)), p)
def test_toPyramid(self): ''' Pyramid to Pyramid ''' for c in TEST_CONTENT: with self.subTest(content=c): p = Pyramid.from_text(c) self.assertEqual(p, p.toPyramid())