github.com/jfrog/frogbot/v2@v2.21.0/utils/outputwriter/markdowntable_test.go (about)

     1  package outputwriter
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestMarkdownTableContent(t *testing.T) {
    10  	testCases := []struct {
    11  		name           string
    12  		columns        []string
    13  		rows           [][]string
    14  		expectedOutput [][]CellData
    15  	}{
    16  		{
    17  			name:           "Empty",
    18  			columns:        []string{},
    19  			rows:           [][]string{},
    20  			expectedOutput: [][]CellData{},
    21  		},
    22  		{
    23  			name:           "No rows",
    24  			columns:        []string{"col1"},
    25  			rows:           [][]string{},
    26  			expectedOutput: [][]CellData{},
    27  		},
    28  		{
    29  			name:    "Same number of columns",
    30  			columns: []string{"col1", "col2", "col3"},
    31  			rows: [][]string{
    32  				{"row1col1", "row1col2", "row1col3"},
    33  				{"row2col1", "row2col2", "row2col3"},
    34  				{"row3col1", "row3col2", "row3col3"},
    35  			},
    36  			expectedOutput: [][]CellData{
    37  				{{"row1col1"}, {"row1col2"}, {"row1col3"}},
    38  				{{"row2col1"}, {"row2col2"}, {"row2col3"}},
    39  				{{"row3col1"}, {"row3col2"}, {"row3col3"}},
    40  			},
    41  		},
    42  		{
    43  			name:    "Different number of columns",
    44  			columns: []string{"col1", "col2", "col3"},
    45  			rows: [][]string{
    46  				{"row1col1", "row1col2", ""},
    47  				{"row2col1", "row2col2"},
    48  				{"row3col1", "", "row3col3", "row3col4"},
    49  				{"row4col1"},
    50  			},
    51  			expectedOutput: [][]CellData{
    52  				{{"row1col1"}, {"row1col2"}, {""}},
    53  				{{"row2col1"}, {"row2col2"}, {""}},
    54  				{{"row3col1"}, {""}, {"row3col3"}},
    55  				{{"row4col1"}, {""}, {""}},
    56  			},
    57  		},
    58  	}
    59  	for _, tc := range testCases {
    60  		t.Run(tc.name, func(t *testing.T) {
    61  			table := NewMarkdownTable(tc.columns...)
    62  			for _, row := range tc.rows {
    63  				table.AddRow(row...)
    64  			}
    65  			assert.Len(t, table.rows, len(tc.expectedOutput))
    66  			for i, row := range table.rows {
    67  				assertRow(t, row, tc.expectedOutput[i], len(tc.columns))
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func assertRow(t *testing.T, actual []CellData, expected []CellData, expectedNumberColumns int) {
    74  	assert.Len(t, actual, expectedNumberColumns)
    75  	for i, cell := range actual {
    76  		assert.Len(t, cell, len(expected[i]))
    77  		for j, value := range cell {
    78  			assert.Equal(t, expected[i][j], value)
    79  		}
    80  	}
    81  }
    82  
    83  func TestMarkdownTableBuild(t *testing.T) {
    84  	testCases := []struct {
    85  		name           string
    86  		expectedOutput string
    87  		columns        []string
    88  		rows           [][]string
    89  	}{
    90  		{
    91  			name:           "Empty",
    92  			columns:        []string{},
    93  			rows:           [][]string{},
    94  			expectedOutput: "",
    95  		},
    96  		{
    97  			name:           "No rows",
    98  			columns:        []string{"col1"},
    99  			rows:           [][]string{},
   100  			expectedOutput: "| col1                |\n" + tableRowFirstColumnSeparator,
   101  		},
   102  		{
   103  			name:    "Same number of columns",
   104  			columns: []string{"col1", "col2"},
   105  			rows: [][]string{
   106  				{"row1col1", "row1col2"},
   107  				{"row2col1", "row2col2"},
   108  				{"row3col1", "row3col2"},
   109  			},
   110  			expectedOutput: "| col1                | col2                  |\n" + tableRowFirstColumnSeparator + tableRowColumnSeparator + `
   111  | row1col1 | row1col2 |
   112  | row2col1 | row2col2 |
   113  | row3col1 | row3col2 |`,
   114  		},
   115  		{
   116  			name:    "Different number of columns",
   117  			columns: []string{"col1", "col2", "col3"},
   118  			rows: [][]string{
   119  				{"row1col1", "row1col2", ""},
   120  				{"row2col1", "row2col2"},
   121  				{},
   122  				{"row3col1", "", "row3col3", "row3col4"},
   123  				{"row4col1"},
   124  				{"row5col1", "row5col2", "row5col3"},
   125  			},
   126  			expectedOutput: "| col1                | col2                  | col3                  |\n" + tableRowFirstColumnSeparator + tableRowColumnSeparator + tableRowColumnSeparator + `
   127  | row1col1 | row1col2 | - |
   128  | row2col1 | row2col2 | - |
   129  | row3col1 | - | row3col3 |
   130  | row4col1 | - | - |
   131  | row5col1 | row5col2 | row5col3 |`,
   132  		},
   133  	}
   134  	for _, tc := range testCases {
   135  		t.Run(tc.name, func(t *testing.T) {
   136  			table := NewMarkdownTable(tc.columns...)
   137  			for _, row := range tc.rows {
   138  				table.AddRow(row...)
   139  			}
   140  
   141  			assert.Equal(t, tc.expectedOutput, table.Build())
   142  		})
   143  	}
   144  }
   145  
   146  func TestMultipleValuesInColumnRow(t *testing.T) {
   147  	testCases := []struct {
   148  		name           string
   149  		expectedOutput string
   150  		columns        []MarkdownColumn
   151  		rows           [][]CellData
   152  	}{
   153  		{
   154  			name:    "Empty on multi value column",
   155  			columns: []MarkdownColumn{{Name: "col1", ColumnType: MultiRowColumn}, {Name: "col2"}, {Name: "col3"}},
   156  			rows: [][]CellData{
   157  				{{""}, {"row1col2"}, {"row1col3"}},
   158  			},
   159  			expectedOutput: "| col1                | col2                  | col3                  |\n" + tableRowFirstColumnSeparator + tableRowColumnSeparator + tableRowColumnSeparator + `
   160  | - | row1col2 | row1col3 |`,
   161  		},
   162  		{
   163  			name:    "One value on multi value column",
   164  			columns: []MarkdownColumn{{Name: "col1"}, {Name: "col2"}, {Name: "col3", ColumnType: MultiRowColumn}},
   165  			rows: [][]CellData{
   166  				{{"row1col1"}, {"row1col2"}, {"row1col3"}},
   167  				{{"row2col1"}, {"row2col2"}, {"row2col3"}},
   168  			},
   169  			expectedOutput: "| col1                | col2                  | col3                  |\n" + tableRowFirstColumnSeparator + tableRowColumnSeparator + tableRowColumnSeparator + `
   170  | row1col1 | row1col2 | row1col3 |
   171  | row2col1 | row2col2 | row2col3 |`,
   172  		},
   173  		{
   174  			name:    "Multiple values on separator delimited column",
   175  			columns: []MarkdownColumn{{Name: "col1"}, {Name: "col2"}, {Name: "col3"}},
   176  			rows: [][]CellData{
   177  				{{"row1col1"}, {""}, {"row1col3"}},
   178  				{{"row2col1"}, {"row2col2"}, {"row2col3val1", "row2col3val2"}},
   179  				{{"row3col1"}, {"row3col2val1", "row3col2val2", "row3col2val3"}, {"row3col3"}},
   180  			},
   181  			expectedOutput: "| col1                | col2                  | col3                  |\n" + tableRowFirstColumnSeparator + tableRowColumnSeparator + tableRowColumnSeparator + `
   182  | row1col1 | - | row1col3 |
   183  | row2col1 | row2col2 | row2col3val1, row2col3val2 |
   184  | row3col1 | row3col2val1, row3col2val2, row3col2val3 | row3col3 |`,
   185  		},
   186  		{
   187  			name:    "Multiple values on multi row column",
   188  			columns: []MarkdownColumn{{Name: "col1"}, {Name: "col2", ColumnType: MultiRowColumn}, {Name: "col3"}},
   189  			rows: [][]CellData{
   190  				{{"row1col1"}, {""}, {"row1col3"}},
   191  				{{"row2col1"}, {"row2col2"}, {"row2col3val1", "row2col3val2"}},
   192  				{{"row3col1"}, {"row3col2val1", "row3col2val2", "row3col2val3"}, {"row3col3"}},
   193  			},
   194  			expectedOutput: "| col1                | col2                  | col3                  |\n" + tableRowFirstColumnSeparator + tableRowColumnSeparator + tableRowColumnSeparator + `
   195  | row1col1 | - | row1col3 |
   196  | row2col1 | row2col2 | row2col3val1, row2col3val2 |
   197  | row3col1 | row3col2val1 | row3col3 |
   198  |   | row3col2val2 |   |
   199  |   | row3col2val3 |   |`,
   200  		},
   201  	}
   202  	for _, tc := range testCases {
   203  		t.Run(tc.name, func(t *testing.T) {
   204  			columns := []string{}
   205  			for _, column := range tc.columns {
   206  				columns = append(columns, column.Name)
   207  			}
   208  			table := NewMarkdownTable(columns...)
   209  			for _, column := range tc.columns {
   210  				if column.ColumnType == MultiRowColumn {
   211  					table.GetColumnInfo(column.Name).ColumnType = MultiRowColumn
   212  				}
   213  			}
   214  			for _, row := range tc.rows {
   215  				table.AddRowWithCellData(row...)
   216  			}
   217  			assert.Equal(t, tc.expectedOutput, table.Build())
   218  		})
   219  	}
   220  }