6 * Unit tests for table formatting.
10 class tablesUnitTest extends UnitUnishTestCase {
12 // Bootstrap to ensure the auto-loaded is running so that Console_Table is found.
14 $this->original_columns = drush_get_context('DRUSH_COLUMNS');
16 // Some table data we reuse between tests.
17 $this->numbers = array(
18 array('1', '12', '123'),
19 array('1234', '12345', '123456'),
20 array('1234567', '12345678', '123456789'),
23 array('Drush is a command line shell', 'scripting interface', 'for Drupal'),
24 array('A veritable', 'Swiss Army knife', 'designed to make life easier for us'),
29 drush_set_context('DRUSH_COLUMNS', $this->original_columns);
33 * Tests drush_format_table() at various table widths with automatic column
36 * @see drush_format_table().
38 public function testFormatAutoWidths() {
39 // print "\n'" . str_replace("\n", "' . PHP_EOL . '", $output) . "'\n";
40 drush_set_context('DRUSH_COLUMNS', 16);
41 $output = drush_format_table($this->numbers);
42 $expected = ' 1 12 123 ' . PHP_EOL . ' 123 123 1234 ' . PHP_EOL . ' 4 45 56 ' . PHP_EOL . ' 123 123 1234 ' . PHP_EOL . ' 456 456 5678 ' . PHP_EOL . ' 7 78 9 ' . PHP_EOL;
43 $this->assertEquals($expected, $output);
45 drush_set_context('DRUSH_COLUMNS', 22);
46 $output = drush_format_table($this->numbers);
47 $expected = ' 1 12 123 ' . PHP_EOL . ' 1234 12345 123456 ' . PHP_EOL . ' 12345 12345 123456 ' . PHP_EOL . ' 67 678 789 ' . PHP_EOL;
48 $this->assertEquals($expected, $output);
50 drush_set_context('DRUSH_COLUMNS', 24);
51 $output = drush_format_table($this->numbers);
52 $expected = ' 1 12 123 ' . PHP_EOL . ' 1234 12345 123456 ' . PHP_EOL . ' 123456 123456 123456 ' . PHP_EOL . ' 7 78 789 ' . PHP_EOL;
53 $this->assertEquals($expected, $output);
55 drush_set_context('DRUSH_COLUMNS', 80);
56 $output = drush_format_table($this->numbers);
57 $expected = ' 1 12 123 ' . PHP_EOL . ' 1234 12345 123456 ' . PHP_EOL . ' 1234567 12345678 123456789 ' . PHP_EOL;
58 $this->assertEquals($expected, $output);
62 * Tests drush_format_table() at various table widths.
64 * @see drush_format_table().
66 public function testFormatWidths() {
67 // print "\n'" . str_replace("\n", "' . PHP_EOL . '", $output) . "'\n";
68 drush_set_context('DRUSH_COLUMNS', 22);
69 $output = drush_format_table($this->numbers, FALSE, array(2));
70 $expected = ' 1 12 123 ' . PHP_EOL . ' 12 12345 123456 ' . PHP_EOL . ' 34 ' . PHP_EOL . ' 12 1234567 1234567 ' . PHP_EOL . ' 34 8 89 ' . PHP_EOL . ' 56 ' . PHP_EOL . ' 7 ' . PHP_EOL;
71 $this->assertEquals($expected, $output);
73 $output = drush_format_table($this->numbers, FALSE, array(10));
74 $expected = ' 1 12 123 ' . PHP_EOL . ' 1234 123 123 ' . PHP_EOL . ' 45 456 ' . PHP_EOL . ' 1234567 123 123 ' . PHP_EOL . ' 456 456 ' . PHP_EOL . ' 78 789 ' . PHP_EOL;
75 $this->assertEquals($expected, $output);
77 $output = drush_format_table($this->numbers, FALSE, array(2, 2));
78 $expected = ' 1 12 123 ' . PHP_EOL . ' 12 12 123456 ' . PHP_EOL . ' 34 34 ' . PHP_EOL . ' 5 ' . PHP_EOL . ' 12 12 123456789 ' . PHP_EOL . ' 34 34 ' . PHP_EOL . ' 56 56 ' . PHP_EOL . ' 7 78 ' . PHP_EOL;
79 $this->assertEquals($expected, $output);
81 $output = drush_format_table($this->numbers, FALSE, array(4, 4, 4));
82 $expected = ' 1 12 123 ' . PHP_EOL . ' 1234 1234 1234 ' . PHP_EOL . ' 5 56 ' . PHP_EOL . ' 1234 1234 1234 ' . PHP_EOL . ' 567 5678 5678 ' . PHP_EOL . ' 9 ' . PHP_EOL;
83 $this->assertEquals($expected, $output);
87 * Tests drush_format_table() with a header.
89 * @see drush_format_table().
91 public function testFormatTableHeader() {
92 drush_set_context('DRUSH_COLUMNS', 16);
93 $rows = $this->numbers;
94 array_unshift($rows, array('A', 'B', 'C'));
95 $output = drush_format_table($rows, TRUE);
96 $expected = ' A B C ' . PHP_EOL . ' 1 12 123 ' . PHP_EOL . ' 123 123 1234 ' . PHP_EOL . ' 4 45 56 ' . PHP_EOL . ' 123 123 1234 ' . PHP_EOL . ' 456 456 5678 ' . PHP_EOL . ' 7 78 9 ' . PHP_EOL;
97 $this->assertEquals($expected, $output);
101 * Tests drush_format_table() with word wrapping.
103 * @see drush_format_table().
105 public function testFormatTableWordWrap() {
106 drush_set_context('DRUSH_COLUMNS', 60);
107 $output = drush_format_table($this->words);
108 $expected = ' Drush is a command scripting for Drupal ' . PHP_EOL . ' line shell interface ' . PHP_EOL . ' A veritable Swiss Army knife designed to make ' . PHP_EOL . ' life easier for us ' . PHP_EOL;
109 $this->assertEquals($expected, $output);