@@ -878,13 +878,51 @@ func TestTable_Render_Reset(t *testing.T) {
878878}
879879
880880func TestTable_Render_RowPainter (t * testing.T ) {
881- tw := NewWriter ()
882- tw .AppendHeader (testHeader )
883- tw .AppendRows (testRows )
884- tw .AppendRow (testRowMultiLine )
885- tw .AppendFooter (testFooter )
886- tw .SetIndexColumn (1 )
887- tw .SetRowPainter (func (row Row ) text.Colors {
881+ runTestWithRowPainter := func (t * testing.T , rowPainter interface {}) {
882+ tw := NewWriter ()
883+ tw .AppendHeader (testHeader )
884+ tw .AppendRows (testRows )
885+ tw .AppendRow (testRowMultiLine )
886+ tw .AppendFooter (testFooter )
887+ tw .SetIndexColumn (1 )
888+ tw .SetRowPainter (rowPainter )
889+ tw .SetStyle (StyleLight )
890+ tw .SortBy ([]SortBy {{Name : "Salary" , Mode : AscNumeric }})
891+
892+ expectedOutLines := []string {
893+ "┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐" ,
894+ "│ # │ FIRST NAME │ LAST NAME │ SALARY │ │" ,
895+ "├─────┼────────────┼───────────┼────────┼─────────────────────────────┤" ,
896+ "│ 0 │\x1b [41;30m Winter \x1b [0m│\x1b [41;30m Is \x1b [0m│\x1b [41;30m 0 \x1b [0m│\x1b [41;30m Coming. \x1b [0m│" ,
897+ "│ │\x1b [41;30m \x1b [0m│\x1b [41;30m \x1b [0m│\x1b [41;30m \x1b [0m│\x1b [41;30m The North Remembers! \x1b [0m│" ,
898+ "│ │\x1b [41;30m \x1b [0m│\x1b [41;30m \x1b [0m│\x1b [41;30m \x1b [0m│\x1b [41;30m This is known. \x1b [0m│" ,
899+ "│ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │" ,
900+ "│ 1 │ Arya │ Stark │ 3000 │ │" ,
901+ "│ 300 │\x1b [43;30m Tyrion \x1b [0m│\x1b [43;30m Lannister \x1b [0m│\x1b [43;30m 5000 \x1b [0m│\x1b [43;30m \x1b [0m│" ,
902+ "├─────┼────────────┼───────────┼────────┼─────────────────────────────┤" ,
903+ "│ │ │ TOTAL │ 10000 │ │" ,
904+ "└─────┴────────────┴───────────┴────────┴─────────────────────────────┘" ,
905+ }
906+ expectedOut := strings .Join (expectedOutLines , "\n " )
907+ assert .Equal (t , expectedOut , tw .Render ())
908+
909+ tw .SetStyle (StyleColoredBright )
910+ tw .Style ().Color .RowAlternate = tw .Style ().Color .Row
911+ expectedOutLines = []string {
912+ "\x1b [106;30m # \x1b [0m\x1b [106;30m FIRST NAME \x1b [0m\x1b [106;30m LAST NAME \x1b [0m\x1b [106;30m SALARY \x1b [0m\x1b [106;30m \x1b [0m" ,
913+ "\x1b [106;30m 0 \x1b [0m\x1b [41;30m Winter \x1b [0m\x1b [41;30m Is \x1b [0m\x1b [41;30m 0 \x1b [0m\x1b [41;30m Coming. \x1b [0m" ,
914+ "\x1b [106;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m The North Remembers! \x1b [0m" ,
915+ "\x1b [106;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m This is known. \x1b [0m" ,
916+ "\x1b [106;30m 20 \x1b [0m\x1b [107;30m Jon \x1b [0m\x1b [107;30m Snow \x1b [0m\x1b [107;30m 2000 \x1b [0m\x1b [107;30m You know nothing, Jon Snow! \x1b [0m" ,
917+ "\x1b [106;30m 1 \x1b [0m\x1b [107;30m Arya \x1b [0m\x1b [107;30m Stark \x1b [0m\x1b [107;30m 3000 \x1b [0m\x1b [107;30m \x1b [0m" ,
918+ "\x1b [106;30m 300 \x1b [0m\x1b [43;30m Tyrion \x1b [0m\x1b [43;30m Lannister \x1b [0m\x1b [43;30m 5000 \x1b [0m\x1b [43;30m \x1b [0m" ,
919+ "\x1b [46;30m \x1b [0m\x1b [46;30m \x1b [0m\x1b [46;30m TOTAL \x1b [0m\x1b [46;30m 10000 \x1b [0m\x1b [46;30m \x1b [0m" ,
920+ }
921+ expectedOut = strings .Join (expectedOutLines , "\n " )
922+ assert .Equal (t , expectedOut , tw .Render ())
923+ }
924+
925+ rowPainter := func (row Row ) text.Colors {
888926 if salary , ok := row [3 ].(int ); ok {
889927 if salary > 3000 {
890928 return text.Colors {text .BgYellow , text .FgBlack }
@@ -893,41 +931,25 @@ func TestTable_Render_RowPainter(t *testing.T) {
893931 }
894932 }
895933 return nil
934+ }
935+ t .Run ("RowPainter 1" , func (t * testing.T ) {
936+ runTestWithRowPainter (t , rowPainter )
937+ })
938+ t .Run ("RowPainter 2" , func (t * testing.T ) {
939+ runTestWithRowPainter (t , RowPainter (rowPainter ))
896940 })
897- tw .SetStyle (StyleLight )
898- tw .SortBy ([]SortBy {{Name : "Salary" , Mode : AscNumeric }})
899941
900- expectedOutLines := []string {
901- "┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐" ,
902- "│ # │ FIRST NAME │ LAST NAME │ SALARY │ │" ,
903- "├─────┼────────────┼───────────┼────────┼─────────────────────────────┤" ,
904- "│ 0 │\x1b [41;30m Winter \x1b [0m│\x1b [41;30m Is \x1b [0m│\x1b [41;30m 0 \x1b [0m│\x1b [41;30m Coming. \x1b [0m│" ,
905- "│ │\x1b [41;30m \x1b [0m│\x1b [41;30m \x1b [0m│\x1b [41;30m \x1b [0m│\x1b [41;30m The North Remembers! \x1b [0m│" ,
906- "│ │\x1b [41;30m \x1b [0m│\x1b [41;30m \x1b [0m│\x1b [41;30m \x1b [0m│\x1b [41;30m This is known. \x1b [0m│" ,
907- "│ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │" ,
908- "│ 1 │ Arya │ Stark │ 3000 │ │" ,
909- "│ 300 │\x1b [43;30m Tyrion \x1b [0m│\x1b [43;30m Lannister \x1b [0m│\x1b [43;30m 5000 \x1b [0m│\x1b [43;30m \x1b [0m│" ,
910- "├─────┼────────────┼───────────┼────────┼─────────────────────────────┤" ,
911- "│ │ │ TOTAL │ 10000 │ │" ,
912- "└─────┴────────────┴───────────┴────────┴─────────────────────────────┘" ,
942+ rowPainterWithAttributes := func (row Row , attr RowAttributes ) text.Colors {
943+ assert .NotZero (t , attr .Number )
944+ assert .NotZero (t , attr .NumberSorted )
945+ return rowPainter (row )
913946 }
914- expectedOut := strings .Join (expectedOutLines , "\n " )
915- assert .Equal (t , expectedOut , tw .Render ())
916-
917- tw .SetStyle (StyleColoredBright )
918- tw .Style ().Color .RowAlternate = tw .Style ().Color .Row
919- expectedOutLines = []string {
920- "\x1b [106;30m # \x1b [0m\x1b [106;30m FIRST NAME \x1b [0m\x1b [106;30m LAST NAME \x1b [0m\x1b [106;30m SALARY \x1b [0m\x1b [106;30m \x1b [0m" ,
921- "\x1b [106;30m 0 \x1b [0m\x1b [41;30m Winter \x1b [0m\x1b [41;30m Is \x1b [0m\x1b [41;30m 0 \x1b [0m\x1b [41;30m Coming. \x1b [0m" ,
922- "\x1b [106;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m The North Remembers! \x1b [0m" ,
923- "\x1b [106;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m \x1b [0m\x1b [41;30m This is known. \x1b [0m" ,
924- "\x1b [106;30m 20 \x1b [0m\x1b [107;30m Jon \x1b [0m\x1b [107;30m Snow \x1b [0m\x1b [107;30m 2000 \x1b [0m\x1b [107;30m You know nothing, Jon Snow! \x1b [0m" ,
925- "\x1b [106;30m 1 \x1b [0m\x1b [107;30m Arya \x1b [0m\x1b [107;30m Stark \x1b [0m\x1b [107;30m 3000 \x1b [0m\x1b [107;30m \x1b [0m" ,
926- "\x1b [106;30m 300 \x1b [0m\x1b [43;30m Tyrion \x1b [0m\x1b [43;30m Lannister \x1b [0m\x1b [43;30m 5000 \x1b [0m\x1b [43;30m \x1b [0m" ,
927- "\x1b [46;30m \x1b [0m\x1b [46;30m \x1b [0m\x1b [46;30m TOTAL \x1b [0m\x1b [46;30m 10000 \x1b [0m\x1b [46;30m \x1b [0m" ,
928- }
929- expectedOut = strings .Join (expectedOutLines , "\n " )
930- assert .Equal (t , expectedOut , tw .Render ())
947+ t .Run ("RowPainterWithAttributes 1" , func (t * testing.T ) {
948+ runTestWithRowPainter (t , rowPainterWithAttributes )
949+ })
950+ t .Run ("RowPainterWithAttributes 2" , func (t * testing.T ) {
951+ runTestWithRowPainter (t , RowPainterWithAttributes (rowPainterWithAttributes ))
952+ })
931953}
932954
933955func TestTable_Render_Sorted (t * testing.T ) {
0 commit comments