...

Source file src/github.com/jedib0t/go-pretty/v6/table/config_test.go

Documentation: github.com/jedib0t/go-pretty/v6/table

     1  package table
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/jedib0t/go-pretty/v6/text"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestColumnConfig_getWidthMaxEnforcer(t *testing.T) {
    11  	t.Run("no width enforcer", func(t *testing.T) {
    12  		cc := ColumnConfig{}
    13  
    14  		widthEnforcer := cc.getWidthMaxEnforcer()
    15  		assert.Equal(t, "1234567890", widthEnforcer("1234567890", 0))
    16  		assert.Equal(t, "1234567890", widthEnforcer("1234567890", 1))
    17  		assert.Equal(t, "1234567890", widthEnforcer("1234567890", 5))
    18  		assert.Equal(t, "1234567890", widthEnforcer("1234567890", 10))
    19  		assert.Equal(t, "1234567890", widthEnforcer("1234567890", 100))
    20  		assert.Equal(t, "1234567890", widthEnforcer("1234567890", 1000))
    21  	})
    22  
    23  	t.Run("default width enforcer", func(t *testing.T) {
    24  		cc := ColumnConfig{
    25  			WidthMax: 10,
    26  		}
    27  
    28  		widthEnforcer := cc.getWidthMaxEnforcer()
    29  		assert.Equal(t, "", widthEnforcer("1234567890", 0))
    30  		assert.Equal(t, "1\n2\n3\n4\n5\n6\n7\n8\n9\n0", widthEnforcer("1234567890", 1))
    31  		assert.Equal(t, "12345\n67890", widthEnforcer("1234567890", 5))
    32  		assert.Equal(t, "1234567890", widthEnforcer("1234567890", 10))
    33  		assert.Equal(t, "1234567890", widthEnforcer("1234567890", 100))
    34  		assert.Equal(t, "1234567890", widthEnforcer("1234567890", 1000))
    35  	})
    36  
    37  	t.Run("custom width enforcer (1)", func(t *testing.T) {
    38  		cc := ColumnConfig{
    39  			WidthMax:         10,
    40  			WidthMaxEnforcer: text.Trim,
    41  		}
    42  
    43  		widthEnforcer := cc.getWidthMaxEnforcer()
    44  		assert.Equal(t, text.Trim("1234567890", 0), widthEnforcer("1234567890", 0))
    45  		assert.Equal(t, text.Trim("1234567890", 1), widthEnforcer("1234567890", 1))
    46  		assert.Equal(t, text.Trim("1234567890", 5), widthEnforcer("1234567890", 5))
    47  		assert.Equal(t, text.Trim("1234567890", 10), widthEnforcer("1234567890", 10))
    48  		assert.Equal(t, text.Trim("1234567890", 100), widthEnforcer("1234567890", 100))
    49  		assert.Equal(t, text.Trim("1234567890", 1000), widthEnforcer("1234567890", 1000))
    50  	})
    51  
    52  	t.Run("custom width enforcer (2)", func(t *testing.T) {
    53  		cc := ColumnConfig{
    54  			WidthMax: 10,
    55  			WidthMaxEnforcer: func(col string, maxLen int) string {
    56  				return "foo"
    57  			},
    58  		}
    59  
    60  		widthEnforcer := cc.getWidthMaxEnforcer()
    61  		assert.Equal(t, "foo", widthEnforcer("1234567890", 0))
    62  		assert.Equal(t, "foo", widthEnforcer("1234567890", 1))
    63  		assert.Equal(t, "foo", widthEnforcer("1234567890", 5))
    64  		assert.Equal(t, "foo", widthEnforcer("1234567890", 10))
    65  		assert.Equal(t, "foo", widthEnforcer("1234567890", 100))
    66  		assert.Equal(t, "foo", widthEnforcer("1234567890", 1000))
    67  	})
    68  }
    69  

View as plain text