github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/utils/progressbar/progressbar_test.go (about)

     1  package progressbar
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestBuildProgressDescription(t *testing.T) {
     8  	// Set an arbitrary terminal width
     9  	terminalWidth = 100
    10  	tests := getTestCases()
    11  	for _, test := range tests {
    12  		t.Run(test.name, func(t *testing.T) {
    13  			desc := buildProgressDescription(test.prefix, test.path, test.extraCharsLen)
    14  
    15  			// Validate result
    16  			if desc != test.expectedDesc {
    17  				t.Errorf("Expected value of: \"%s\", got: \"%s\".", test.expectedDesc, desc)
    18  			}
    19  		})
    20  	}
    21  }
    22  
    23  func getTestCases() []testCase {
    24  	prefix := "downloading"
    25  	path := "/a/path/to/a/file"
    26  	separator := " | "
    27  
    28  	fullDesc := separator + prefix + separator + path + separator
    29  	emptyPathDesc := separator + prefix + separator + "..." + separator
    30  	shortenedDesc := separator + prefix + separator + "...ggggg/path/to/a/file" + separator
    31  
    32  	widthMinusProgress := terminalWidth - progressBarWidth*2
    33  	return []testCase{
    34  		{"commonUseCase", prefix, path, 17, fullDesc},
    35  		{"zeroExtraChars", prefix, path, 0, fullDesc},
    36  		{"minDescLength", prefix, path, widthMinusProgress - len(emptyPathDesc), emptyPathDesc},
    37  		{"longPath", prefix, "/a/longggggggggggggggggggggg/path/to/a/file", 17, shortenedDesc},
    38  		{"longPrefix", "longgggggggggggggggggggggg prefix", path, 17, ""},
    39  		{"manyExtraChars", prefix, path, widthMinusProgress - len(emptyPathDesc) + 1, ""},
    40  	}
    41  }
    42  
    43  type testCase struct {
    44  	name          string
    45  	prefix        string
    46  	path          string
    47  	extraCharsLen int
    48  	expectedDesc  string
    49  }