github.com/haraldrudell/parl@v0.4.176/pstrings/fit_test.go (about)

     1  /*
     2  © 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pstrings
     7  
     8  import (
     9  	"testing"
    10  )
    11  
    12  func TestFit(t *testing.T) {
    13  	t.Logf("a: %d", len([]rune("…")))
    14  	type args struct {
    15  		s     string
    16  		width int
    17  		pad   bool
    18  	}
    19  	tests := []struct {
    20  		name   string
    21  		args   args
    22  		wantS2 string
    23  	}{
    24  		{"nil", args{"", 0, false}, ""},
    25  		{"width 0", args{"ab", 0, false}, "ab"},
    26  		{"length == width", args{"ab", 2, false}, "ab"},
    27  		{"pad", args{"a", 2, true}, "a "},
    28  		{"cut", args{"abcdef", 4, true}, "ab…f"},
    29  		{"failure1", args{
    30  			"/System/Library/CoreServices/Applications/Screen Sharing.app/Contents/MacOS/Screen Sharing",
    31  			59, true},
    32  			"/System/Library/CoreServices/A…ontents/MacOS/Screen Sharing",
    33  		},
    34  	}
    35  	for _, tt := range tests {
    36  		t.Run(tt.name, func(t *testing.T) {
    37  			if gotS2 := Fit(tt.args.s, tt.args.width, tt.args.pad); gotS2 != tt.wantS2 {
    38  				t.Errorf("Fit() = %d %q, want %q", len(gotS2), gotS2, tt.wantS2)
    39  			}
    40  		})
    41  	}
    42  }