github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/flexibletable/cells_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 "testing"
     7  
     8  func TestMultiCellMinWidth(t *testing.T) {
     9  	cell := MultiCell{
    10  		Sep: ",",
    11  		Items: []string{
    12  			"andy",
    13  			"bob",
    14  			"chris",
    15  			"david",
    16  			"evan",
    17  			"fred",
    18  			"gabriel",
    19  			"hooray",
    20  			"ikea",
    21  		},
    22  	}
    23  	if cell.minWidth() != 5 {
    24  		// "+10..."
    25  		t.Fatalf("wrong min width; expected 5, got %d\n", cell.minWidth())
    26  	}
    27  	cell.Items = append(cell.Items, "jack")
    28  	if cell.minWidth() != 6 {
    29  		// "+10..."
    30  		t.Fatalf("wrong min width; expected 6, got %d\n", cell.minWidth())
    31  	}
    32  }
    33  
    34  func TestMultiCellString(t *testing.T) {
    35  	cell := MultiCell{
    36  		Sep: ",",
    37  		Items: []string{
    38  			"andy",
    39  			"bob",
    40  			"chris",
    41  		},
    42  	}
    43  
    44  	str := cell.render(6)
    45  	if str != "+3..." {
    46  		t.Fatalf(`wrong string; expected "+3...", got "%s"`, str)
    47  	}
    48  
    49  	str = cell.render(10)
    50  	if str != "andy,+2..." {
    51  		t.Fatalf(`wrong string; expected "andy,+2...", got "%s"`, str)
    52  	}
    53  
    54  	str = cell.render(13)
    55  	if str != "andy,+2..." {
    56  		t.Fatalf(`wrong string; expected "andy,+2...", got "%s"`, str)
    57  	}
    58  
    59  	str = cell.render(14)
    60  	if str != "andy,bob,chris" {
    61  		t.Fatalf(`wrong string; expected "andy,bob,chris", got "%s"`, str)
    62  	}
    63  }
    64  
    65  func TestSingleCellWithFrame(t *testing.T) {
    66  	cell := Cell{
    67  		Content:   SingleCell{Item: "123456789"},
    68  		Frame:     [2]string{"[", "]"},
    69  		Alignment: Left,
    70  	}
    71  
    72  	str, err := cell.render(11)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	if str != "[123456789]" {
    77  		t.Fatalf("expected [123456789], got %s", str)
    78  	}
    79  
    80  	str, err = cell.render(10)
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	if str != "[12345...]" {
    85  		t.Fatalf("expected [12345...], got %s", str)
    86  	}
    87  }