github.com/getgauge/gauge@v1.6.9/gauge/table_test.go (about)

     1  /*----------------------------------------------------------------
     2   *  Copyright (c) ThoughtWorks, Inc.
     3   *  Licensed under the Apache License, Version 2.0
     4   *  See LICENSE in the project root for license information.
     5   *----------------------------------------------------------------*/
     6  
     7  package gauge
     8  
     9  import (
    10  	"testing"
    11  
    12  	. "gopkg.in/check.v1"
    13  )
    14  
    15  func Test(t *testing.T) { TestingT(t) }
    16  
    17  type MySuite struct{}
    18  
    19  var _ = Suite(&MySuite{})
    20  
    21  func (s *MySuite) TestIsInitalized(c *C) {
    22  	var table Table
    23  	c.Assert(table.IsInitialized(), Equals, false)
    24  	c.Assert(table.GetRowCount(), Equals, 0)
    25  
    26  	table.AddHeaders([]string{"one", "two", "three"})
    27  
    28  	c.Assert(table.IsInitialized(), Equals, true)
    29  }
    30  
    31  func (s *MySuite) TestShouldAddHeaders(c *C) {
    32  	var table Table
    33  
    34  	table.AddHeaders([]string{"one", "two", "three"})
    35  
    36  	c.Assert(len(table.Headers), Equals, 3)
    37  	c.Assert(table.headerIndexMap["one"], Equals, 0)
    38  	c.Assert(table.Headers[0], Equals, "one")
    39  	c.Assert(table.headerIndexMap["two"], Equals, 1)
    40  	c.Assert(table.Headers[1], Equals, "two")
    41  	c.Assert(table.headerIndexMap["three"], Equals, 2)
    42  	c.Assert(table.Headers[2], Equals, "three")
    43  }
    44  
    45  func (s *MySuite) TestShouldAddRowValues(c *C) {
    46  	var table Table
    47  
    48  	table.AddHeaders([]string{"one", "two", "three"})
    49  	table.AddRowValues(table.CreateTableCells([]string{"foo", "bar", "baz"}))
    50  	table.AddRowValues(table.CreateTableCells([]string{"john", "jim"}))
    51  
    52  	c.Assert(table.GetRowCount(), Equals, 2)
    53  	column1, _ := table.Get("one")
    54  	c.Assert(len(column1), Equals, 2)
    55  	c.Assert(column1[0].Value, Equals, "foo")
    56  	c.Assert(column1[0].CellType, Equals, Static)
    57  	c.Assert(column1[1].Value, Equals, "john")
    58  	c.Assert(column1[1].CellType, Equals, Static)
    59  
    60  	column2, _ := table.Get("two")
    61  	c.Assert(len(column2), Equals, 2)
    62  	c.Assert(column2[0].Value, Equals, "bar")
    63  	c.Assert(column2[0].CellType, Equals, Static)
    64  	c.Assert(column2[1].Value, Equals, "jim")
    65  	c.Assert(column2[1].CellType, Equals, Static)
    66  
    67  	column3, _ := table.Get("three")
    68  	c.Assert(len(column3), Equals, 2)
    69  	c.Assert(column3[0].Value, Equals, "baz")
    70  	c.Assert(column3[0].CellType, Equals, Static)
    71  	c.Assert(column3[1].Value, Equals, "")
    72  	c.Assert(column3[1].CellType, Equals, Static)
    73  }
    74  
    75  func (s *MySuite) TestShouldAddRows(c *C) {
    76  	var table Table
    77  
    78  	table.AddHeaders([]string{"one", "two", "three"})
    79  	table.addRows([]TableCell{TableCell{"foo", Static}, TableCell{"bar", Static}, TableCell{"baz", Static}})
    80  	table.addRows([]TableCell{TableCell{"john", Static}, TableCell{"jim", Static}})
    81  
    82  	c.Assert(table.GetRowCount(), Equals, 2)
    83  	column1, _ := table.Get("one")
    84  	c.Assert(len(column1), Equals, 2)
    85  	c.Assert(column1[0].Value, Equals, "foo")
    86  	c.Assert(column1[0].CellType, Equals, Static)
    87  	c.Assert(column1[1].Value, Equals, "john")
    88  	c.Assert(column1[1].CellType, Equals, Static)
    89  
    90  	column2, _ := table.Get("two")
    91  	c.Assert(len(column2), Equals, 2)
    92  	c.Assert(column2[0].Value, Equals, "bar")
    93  	c.Assert(column2[0].CellType, Equals, Static)
    94  	c.Assert(column2[1].Value, Equals, "jim")
    95  	c.Assert(column2[1].CellType, Equals, Static)
    96  
    97  	column3, _ := table.Get("three")
    98  	c.Assert(len(column3), Equals, 2)
    99  	c.Assert(column3[0].Value, Equals, "baz")
   100  	c.Assert(column3[0].CellType, Equals, Static)
   101  	c.Assert(column3[1].Value, Equals, "")
   102  	c.Assert(column3[1].CellType, Equals, Static)
   103  }
   104  
   105  func (s *MySuite) TestCoulmnNameExists(c *C) {
   106  	var table Table
   107  
   108  	table.AddHeaders([]string{"one", "two", "three"})
   109  	table.AddRowValues(table.CreateTableCells([]string{"foo", "bar", "baz"}))
   110  	table.AddRowValues(table.CreateTableCells([]string{"john", "jim", "jack"}))
   111  
   112  	c.Assert(table.headerExists("one"), Equals, true)
   113  	c.Assert(table.headerExists("two"), Equals, true)
   114  	c.Assert(table.headerExists("four"), Equals, false)
   115  }
   116  
   117  func (s *MySuite) TestGetRows(c *C) {
   118  	var table Table
   119  
   120  	table.AddHeaders([]string{"one", "two", "three"})
   121  	table.AddRowValues(table.CreateTableCells([]string{"foo", "bar", "baz"}))
   122  	table.AddRowValues(table.CreateTableCells([]string{"john", "jim", "jack"}))
   123  
   124  	rows := table.Rows()
   125  	c.Assert(len(rows), Equals, 2)
   126  	firstRow := rows[0]
   127  	c.Assert(firstRow[0], Equals, "foo")
   128  	c.Assert(firstRow[1], Equals, "bar")
   129  	c.Assert(firstRow[2], Equals, "baz")
   130  
   131  	secondRow := rows[1]
   132  	c.Assert(secondRow[0], Equals, "john")
   133  	c.Assert(secondRow[1], Equals, "jim")
   134  	c.Assert(secondRow[2], Equals, "jack")
   135  }
   136  
   137  func (s *MySuite) TestValuesBasedOnHeaders(c *C) {
   138  	var table Table
   139  	table.AddHeaders([]string{"id", "name"})
   140  
   141  	firstRow := table.toHeaderSizeRow([]TableCell{TableCell{"123", Static}, TableCell{"foo", Static}})
   142  	secondRow := table.toHeaderSizeRow([]TableCell{TableCell{"jim", Static}, TableCell{"jack", Static}})
   143  	thirdRow := table.toHeaderSizeRow([]TableCell{TableCell{"789", Static}})
   144  
   145  	c.Assert(len(firstRow), Equals, 2)
   146  	c.Assert(firstRow[0].Value, Equals, "123")
   147  	c.Assert(firstRow[1].Value, Equals, "foo")
   148  
   149  	c.Assert(len(secondRow), Equals, 2)
   150  	c.Assert(secondRow[0].Value, Equals, "jim")
   151  	c.Assert(secondRow[1].Value, Equals, "jack")
   152  
   153  	c.Assert(len(thirdRow), Equals, 2)
   154  	c.Assert(thirdRow[0].Value, Equals, "789")
   155  	c.Assert(thirdRow[1].Value, Equals, "")
   156  }
   157  
   158  func (s *MySuite) TestCreateTableCells(c *C) {
   159  	var table Table
   160  	table.AddHeaders([]string{"id", "name"})
   161  	table.AddRowValues(table.CreateTableCells([]string{"cell 1", "cell 2   "}))
   162  
   163  	c.Assert(table.Columns[0][0].Value, Equals, "cell 1")
   164  	c.Assert(table.Columns[1][0].Value, Equals, "cell 2   ")
   165  }