...

Source file src/code.rocketnine.space/tslocum/cview/table_test.go

Documentation: code.rocketnine.space/tslocum/cview

     1  package cview
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  var tableTestCases = generateTableTestCases()
     9  
    10  type tableTestCase struct {
    11  	rows         int
    12  	columns      int
    13  	fixedRows    int
    14  	fixedColumns int
    15  }
    16  
    17  func (c *tableTestCase) String() string {
    18  	return fmt.Sprintf("Rows=%d/Cols=%d/FixedRows=%d/FixedCols=%d", c.rows, c.columns, c.fixedRows, c.fixedColumns)
    19  }
    20  
    21  func TestTable(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	for _, c := range tableTestCases {
    25  		c := c // Capture
    26  
    27  		t.Run(c.String(), func(t *testing.T) {
    28  			t.Parallel()
    29  
    30  			table := tc(c)
    31  
    32  			app, err := newTestApp(table)
    33  			if err != nil {
    34  				t.Errorf("failed to initialize Application: %s", err)
    35  			}
    36  
    37  			for row := 0; row < c.rows; row++ {
    38  				for column := 0; column < c.columns; column++ {
    39  					contents := table.GetCell(row, column).GetText()
    40  					expected := fmt.Sprintf("%d,%d", column, row)
    41  					if contents != expected {
    42  						t.Errorf("failed to either get or set TableCell text: expected %s, got %s", expected, contents)
    43  					}
    44  				}
    45  			}
    46  
    47  			table.Draw(app.screen)
    48  
    49  			table.Clear()
    50  		})
    51  	}
    52  }
    53  
    54  func BenchmarkTableDraw(b *testing.B) {
    55  	for _, c := range tableTestCases {
    56  		c := c // Capture
    57  
    58  		b.Run(c.String(), func(b *testing.B) {
    59  			table := tc(c)
    60  
    61  			app, err := newTestApp(table)
    62  			if err != nil {
    63  				b.Errorf("failed to initialize Application: %s", err)
    64  			}
    65  
    66  			table.Draw(app.screen)
    67  
    68  			b.ReportAllocs()
    69  			b.ResetTimer()
    70  
    71  			for i := 0; i < b.N; i++ {
    72  				table.Draw(app.screen)
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func generateTableTestCases() []*tableTestCase {
    79  	var cases []*tableTestCase
    80  	for i := 1; i < 3; i++ {
    81  		rows := i * 5
    82  		for i := 1; i < 3; i++ {
    83  			columns := i * 7
    84  			for fixedRows := 0; fixedRows < 3; fixedRows++ {
    85  				for fixedColumns := 0; fixedColumns < 3; fixedColumns++ {
    86  					cases = append(cases, &tableTestCase{rows, columns, fixedRows, fixedColumns})
    87  				}
    88  			}
    89  		}
    90  	}
    91  	return cases
    92  }
    93  
    94  func tc(c *tableTestCase) *Table {
    95  	table := NewTable()
    96  
    97  	for row := 0; row < c.rows; row++ {
    98  		for column := 0; column < c.columns; column++ {
    99  			table.SetCellSimple(row, column, fmt.Sprintf("%d,%d", column, row))
   100  		}
   101  	}
   102  
   103  	table.SetFixed(c.fixedRows, c.fixedColumns)
   104  
   105  	return table
   106  }
   107  

View as plain text