def testMakeTableExceptions(self): # Verify that contents is being type-checked and shape-checked. with self.assertRaises(ValueError): text_plugin.make_table([]) with self.assertRaises(ValueError): text_plugin.make_table('foo') with self.assertRaises(ValueError): invalid_shape = np.full((3, 3, 3), 'nope', dtype=np.dtype('S3')) text_plugin.make_table(invalid_shape) # Test headers exceptions in 2d array case. test_array = np.full((3, 3), 'foo', dtype=np.dtype('S3')) with self.assertRaises(ValueError): # Headers is wrong type. text_plugin.make_table(test_array, headers='foo') with self.assertRaises(ValueError): # Too many headers. text_plugin.make_table(test_array, headers=['foo', 'bar', 'zod', 'zoink']) with self.assertRaises(ValueError): # headers is 2d text_plugin.make_table(test_array, headers=test_array) # Also make sure the column counting logic works in the 1d array case. test_array = np.array(['foo', 'bar', 'zod']) with self.assertRaises(ValueError): # Too many headers. text_plugin.make_table(test_array, headers=test_array)
def testTableGeneration(self): array2d = np.array([['one', 'two'], ['three', 'four']]) expected_table = textwrap.dedent("""\ <table> <tbody> <tr> <td>one</td> <td>two</td> </tr> <tr> <td>three</td> <td>four</td> </tr> </tbody> </table>""") self.assertEqual(text_plugin.make_table(array2d), expected_table) expected_table_with_headers = textwrap.dedent("""\ <table> <thead> <tr> <th>c1</th> <th>c2</th> </tr> </thead> <tbody> <tr> <td>one</td> <td>two</td> </tr> <tr> <td>three</td> <td>four</td> </tr> </tbody> </table>""") actual_with_headers = text_plugin.make_table(array2d, headers=['c1', 'c2']) self.assertEqual(actual_with_headers, expected_table_with_headers) array_1d = np.array(['one', 'two', 'three', 'four', 'five']) expected_1d = textwrap.dedent("""\ <table> <tbody> <tr> <td>one</td> </tr> <tr> <td>two</td> </tr> <tr> <td>three</td> </tr> <tr> <td>four</td> </tr> <tr> <td>five</td> </tr> </tbody> </table>""") self.assertEqual(text_plugin.make_table(array_1d), expected_1d) expected_1d_with_headers = textwrap.dedent("""\ <table> <thead> <tr> <th>X</th> </tr> </thead> <tbody> <tr> <td>one</td> </tr> <tr> <td>two</td> </tr> <tr> <td>three</td> </tr> <tr> <td>four</td> </tr> <tr> <td>five</td> </tr> </tbody> </table>""") actual_1d_with_headers = text_plugin.make_table(array_1d, headers=['X']) self.assertEqual(actual_1d_with_headers, expected_1d_with_headers)
def testTableGeneration(self): array2d = np.array([["one", "two"], ["three", "four"]]) expected_table = textwrap.dedent("""\ <table> <tbody> <tr> <td>one</td> <td>two</td> </tr> <tr> <td>three</td> <td>four</td> </tr> </tbody> </table> """.rstrip()) self.assertEqual(text_plugin.make_table(array2d), expected_table) expected_table_with_headers = textwrap.dedent("""\ <table> <thead> <tr> <th>c1</th> <th>c2</th> </tr> </thead> <tbody> <tr> <td>one</td> <td>two</td> </tr> <tr> <td>three</td> <td>four</td> </tr> </tbody> </table> """.rstrip()) actual_with_headers = text_plugin.make_table(array2d, headers=["c1", "c2"]) self.assertEqual(actual_with_headers, expected_table_with_headers) array_1d = np.array(["one", "two", "three", "four", "five"]) expected_1d = textwrap.dedent("""\ <table> <tbody> <tr> <td>one</td> </tr> <tr> <td>two</td> </tr> <tr> <td>three</td> </tr> <tr> <td>four</td> </tr> <tr> <td>five</td> </tr> </tbody> </table> """.rstrip()) self.assertEqual(text_plugin.make_table(array_1d), expected_1d) expected_1d_with_headers = textwrap.dedent("""\ <table> <thead> <tr> <th>X</th> </tr> </thead> <tbody> <tr> <td>one</td> </tr> <tr> <td>two</td> </tr> <tr> <td>three</td> </tr> <tr> <td>four</td> </tr> <tr> <td>five</td> </tr> </tbody> </table> """.rstrip()) actual_1d_with_headers = text_plugin.make_table(array_1d, headers=["X"]) self.assertEqual(actual_1d_with_headers, expected_1d_with_headers)