github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/flexibletable/table_test.go (about)

     1  // Copyright 2016 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package flexibletable
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func genTableForTest(t *testing.T) *Table {
    14  	table := &Table{}
    15  	if err := table.Insert(Row{
    16  		Cell{Frame: [2]string{"[", "]"}, Alignment: Right, Content: SingleCell{"0"}},
    17  		Cell{Alignment: Left, Content: MultiCell{Items: []string{"alice", "bob", "charlie", "david"}, Sep: ","}},
    18  		Cell{Frame: [2]string{"[", "]"}, Alignment: Right, Content: SingleCell{"alice 4h"}},
    19  		Cell{Alignment: Left, Content: SingleCell{"hello!"}},
    20  	}); err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	if err := table.Insert(Row{
    24  		Cell{Frame: [2]string{"[", "]"}, Alignment: Right, Content: SingleCell{"1"}},
    25  		Cell{Alignment: Left, Content: MultiCell{Items: []string{"alice", "bob", "charlie", "david"}, Sep: ","}},
    26  		Cell{Frame: [2]string{"[", "]"}, Alignment: Right, Content: SingleCell{"bob 2h"}},
    27  		Cell{Alignment: Left, Content: SingleCell{"hello! wejoi fwoi jwe oiew oiwfowfw"}},
    28  	}); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	if err := table.Insert(Row{
    32  		Cell{Frame: [2]string{"[", "]"}, Alignment: Right, Content: SingleCell{"10"}},
    33  		Cell{Alignment: Left, Content: MultiCell{Items: []string{"alice", "bob", "charlie", "david"}, Sep: ","}},
    34  		Cell{Frame: [2]string{"[", "]"}, Alignment: Right, Content: SingleCell{"charlie 4h"}},
    35  		Cell{Alignment: Left, Content: SingleCell{"hello! this is super long hahahaha blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah"}},
    36  	}); err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	if err := table.Insert(Row{
    40  		Cell{Frame: [2]string{"[", "]"}, Alignment: Right, Content: SingleCell{"11"}},
    41  		Cell{Alignment: Left, Content: MultiCell{Items: []string{"alice", "bob", "charlie", "david"}, Sep: ","}},
    42  		Cell{Frame: [2]string{"[", "]"}, Alignment: Right, Content: SingleCell{"charliecharliecharlie 2h"}},
    43  		Cell{Alignment: Left, Content: SingleCell{"hello! hello!"}},
    44  	}); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	return table
    48  }
    49  
    50  func readable(in string) string {
    51  	return strings.ReplaceAll(strings.ReplaceAll(in, "\n", "⏎\n"), " ", "␣")
    52  }
    53  
    54  func TestTable(t *testing.T) {
    55  	table := genTableForTest(t)
    56  	expected := `
    57   [0] alice,+3...   [alice 4h] hello!                                            
    58   [1] alice,+3...     [bob 2h] hello! wejoi fwoi jwe oiew oiwfowfw               
    59  [10] alice,+3... [charlie 4h] hello! this is super long hahahaha blah blah bl...
    60  [11] alice,+3... [charlie...] hello! hello!                                     
    61  `
    62  	out := &bytes.Buffer{}
    63  	fmt.Fprintln(out)
    64  	err := table.Render(out, " ", 80, []ColumnConstraint{10, 12, 12, Expandable})
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	if out.String() != expected {
    69  		t.Fatalf("wrong rendering result.\nGot:\n%s\nExpected:\n%s",
    70  			readable(out.String()), readable(expected))
    71  	}
    72  }
    73  
    74  func TestTableWrap(t *testing.T) {
    75  	table := genTableForTest(t)
    76  	expected := `
    77   [0] alice,+3...   [alice 4h] hello!                                            
    78   [1] alice,+3...     [bob 2h] hello! wejoi fwoi jwe oiew oiwfowfw               
    79  [10] alice,+3... [charlie 4h] hello! this is super long hahahaha blah blah blah 
    80                                blah blah blah blah blah blah blah blah blah blah 
    81                                blah blah blah blah blah blah blah                
    82  [11] alice,+3... [charlie...] hello! hello!                                     
    83  `
    84  	out := &bytes.Buffer{}
    85  	fmt.Fprintln(out)
    86  	err := table.Render(out, " ", 80, []ColumnConstraint{10, 12, 12, ExpandableWrappable})
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	if out.String() != expected {
    91  		t.Fatalf("wrong rendering result.\nGot:\n%s\nExpected:\n%s",
    92  			readable(out.String()),
    93  			readable(expected))
    94  	}
    95  }
    96  
    97  func TestTableMultiline(t *testing.T) {
    98  	table := genTableForTest(t)
    99  	table.rows[1][3].Content = SingleCell{"first line\nsecond line\nblahblahblahblahblahblah supre long line hahaha aaa line line foo bar foo bar"}
   100  	expected := `
   101   [0] alice,+3...   [alice 4h] hello!                                            
   102   [1] alice,+3...     [bob 2h] first line                                        
   103                                second line                                       
   104                                blahblahblahblahblahblah supre long line hahaha aa
   105                                a line line foo bar foo bar                       
   106  [10] alice,+3... [charlie 4h] hello! this is super long hahahaha blah blah blah 
   107                                blah blah blah blah blah blah blah blah blah blah 
   108                                blah blah blah blah blah blah blah                
   109  [11] alice,+3... [charlie...] hello! hello!                                     
   110  `
   111  	out := &bytes.Buffer{}
   112  	fmt.Fprintln(out)
   113  	err := table.Render(out, " ", 80, []ColumnConstraint{10, 12, 12, ExpandableWrappable})
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	if out.String() != expected {
   118  		t.Fatalf("wrong rendering result.\nGot:\n%s\nExpected:\n%s",
   119  			readable(out.String()),
   120  			readable(expected))
   121  	}
   122  }