github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/gauge/table_test.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package gauge
    19  
    20  import (
    21  	"testing"
    22  
    23  	. "gopkg.in/check.v1"
    24  )
    25  
    26  func Test(t *testing.T) { TestingT(t) }
    27  
    28  type MySuite struct{}
    29  
    30  var _ = Suite(&MySuite{})
    31  
    32  func (s *MySuite) TestIsInitalized(c *C) {
    33  	var table Table
    34  	c.Assert(table.IsInitialized(), Equals, false)
    35  	c.Assert(table.GetRowCount(), Equals, 0)
    36  
    37  	table.AddHeaders([]string{"one", "two", "three"})
    38  
    39  	c.Assert(table.IsInitialized(), Equals, true)
    40  }
    41  
    42  func (s *MySuite) TestShouldAddHeaders(c *C) {
    43  	var table Table
    44  
    45  	table.AddHeaders([]string{"one", "two", "three"})
    46  
    47  	c.Assert(len(table.Headers), Equals, 3)
    48  	c.Assert(table.headerIndexMap["one"], Equals, 0)
    49  	c.Assert(table.Headers[0], Equals, "one")
    50  	c.Assert(table.headerIndexMap["two"], Equals, 1)
    51  	c.Assert(table.Headers[1], Equals, "two")
    52  	c.Assert(table.headerIndexMap["three"], Equals, 2)
    53  	c.Assert(table.Headers[2], Equals, "three")
    54  }
    55  
    56  func (s *MySuite) TestShouldAddRowValues(c *C) {
    57  	var table Table
    58  
    59  	table.AddHeaders([]string{"one", "two", "three"})
    60  	table.AddRowValues([]string{"foo", "bar", "baz"})
    61  	table.AddRowValues([]string{"john", "jim"})
    62  
    63  	c.Assert(table.GetRowCount(), Equals, 2)
    64  	column1 := table.Get("one")
    65  	c.Assert(len(column1), Equals, 2)
    66  	c.Assert(column1[0].Value, Equals, "foo")
    67  	c.Assert(column1[0].CellType, Equals, Static)
    68  	c.Assert(column1[1].Value, Equals, "john")
    69  	c.Assert(column1[1].CellType, Equals, Static)
    70  
    71  	column2 := table.Get("two")
    72  	c.Assert(len(column2), Equals, 2)
    73  	c.Assert(column2[0].Value, Equals, "bar")
    74  	c.Assert(column2[0].CellType, Equals, Static)
    75  	c.Assert(column2[1].Value, Equals, "jim")
    76  	c.Assert(column2[1].CellType, Equals, Static)
    77  
    78  	column3 := table.Get("three")
    79  	c.Assert(len(column3), Equals, 2)
    80  	c.Assert(column3[0].Value, Equals, "baz")
    81  	c.Assert(column3[0].CellType, Equals, Static)
    82  	c.Assert(column3[1].Value, Equals, "")
    83  	c.Assert(column3[1].CellType, Equals, Static)
    84  }
    85  
    86  func (s *MySuite) TestShouldAddRows(c *C) {
    87  	var table Table
    88  
    89  	table.AddHeaders([]string{"one", "two", "three"})
    90  	table.addRows([]TableCell{TableCell{"foo", Static}, TableCell{"bar", Static}, TableCell{"baz", Static}})
    91  	table.addRows([]TableCell{TableCell{"john", Static}, TableCell{"jim", Static}})
    92  
    93  	c.Assert(table.GetRowCount(), Equals, 2)
    94  	column1 := table.Get("one")
    95  	c.Assert(len(column1), Equals, 2)
    96  	c.Assert(column1[0].Value, Equals, "foo")
    97  	c.Assert(column1[0].CellType, Equals, Static)
    98  	c.Assert(column1[1].Value, Equals, "john")
    99  	c.Assert(column1[1].CellType, Equals, Static)
   100  
   101  	column2 := table.Get("two")
   102  	c.Assert(len(column2), Equals, 2)
   103  	c.Assert(column2[0].Value, Equals, "bar")
   104  	c.Assert(column2[0].CellType, Equals, Static)
   105  	c.Assert(column2[1].Value, Equals, "jim")
   106  	c.Assert(column2[1].CellType, Equals, Static)
   107  
   108  	column3 := table.Get("three")
   109  	c.Assert(len(column3), Equals, 2)
   110  	c.Assert(column3[0].Value, Equals, "baz")
   111  	c.Assert(column3[0].CellType, Equals, Static)
   112  	c.Assert(column3[1].Value, Equals, "")
   113  	c.Assert(column3[1].CellType, Equals, Static)
   114  }
   115  
   116  func (s *MySuite) TestCoulmnNameExists(c *C) {
   117  	var table Table
   118  
   119  	table.AddHeaders([]string{"one", "two", "three"})
   120  	table.AddRowValues([]string{"foo", "bar", "baz"})
   121  	table.AddRowValues([]string{"john", "jim", "jack"})
   122  
   123  	c.Assert(table.headerExists("one"), Equals, true)
   124  	c.Assert(table.headerExists("two"), Equals, true)
   125  	c.Assert(table.headerExists("four"), Equals, false)
   126  }
   127  
   128  func (s *MySuite) TestGetInvalidColumn(c *C) {
   129  	var table Table
   130  
   131  	table.AddHeaders([]string{"one", "two", "three"})
   132  	table.AddRowValues([]string{"foo", "bar", "baz"})
   133  	table.AddRowValues([]string{"john", "jim", "jack"})
   134  
   135  	c.Assert(func() { table.Get("four") }, Panics, "Table column four not found")
   136  }
   137  
   138  func (s *MySuite) TestGetRows(c *C) {
   139  	var table Table
   140  
   141  	table.AddHeaders([]string{"one", "two", "three"})
   142  	table.AddRowValues([]string{"foo", "bar", "baz"})
   143  	table.AddRowValues([]string{"john", "jim", "jack"})
   144  
   145  	rows := table.Rows()
   146  	c.Assert(len(rows), Equals, 2)
   147  	firstRow := rows[0]
   148  	c.Assert(firstRow[0], Equals, "foo")
   149  	c.Assert(firstRow[1], Equals, "bar")
   150  	c.Assert(firstRow[2], Equals, "baz")
   151  
   152  	secondRow := rows[1]
   153  	c.Assert(secondRow[0], Equals, "john")
   154  	c.Assert(secondRow[1], Equals, "jim")
   155  	c.Assert(secondRow[2], Equals, "jack")
   156  }
   157  
   158  func (s *MySuite) TestValuesBasedOnHeaders(c *C) {
   159  	var table Table
   160  	table.AddHeaders([]string{"id", "name"})
   161  
   162  	firstRow := table.toHeaderSizeRow([]TableCell{TableCell{"123", Static}, TableCell{"foo", Static}})
   163  	secondRow := table.toHeaderSizeRow([]TableCell{TableCell{"jim", Static}, TableCell{"jack", Static}})
   164  	thirdRow := table.toHeaderSizeRow([]TableCell{TableCell{"789", Static}})
   165  
   166  	c.Assert(len(firstRow), Equals, 2)
   167  	c.Assert(firstRow[0].Value, Equals, "123")
   168  	c.Assert(firstRow[1].Value, Equals, "foo")
   169  
   170  	c.Assert(len(secondRow), Equals, 2)
   171  	c.Assert(secondRow[0].Value, Equals, "jim")
   172  	c.Assert(secondRow[1].Value, Equals, "jack")
   173  
   174  	c.Assert(len(thirdRow), Equals, 2)
   175  	c.Assert(thirdRow[0].Value, Equals, "789")
   176  	c.Assert(thirdRow[1].Value, Equals, "")
   177  }