github.com/jandre/docker@v1.7.0/pkg/stringid/stringid_test.go (about)

     1  package stringid
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestGenerateRandomID(t *testing.T) {
     9  	id := GenerateRandomID()
    10  
    11  	if len(id) != 64 {
    12  		t.Fatalf("Id returned is incorrect: %s", id)
    13  	}
    14  }
    15  
    16  func TestShortenId(t *testing.T) {
    17  	id := GenerateRandomID()
    18  	truncID := TruncateID(id)
    19  	if len(truncID) != 12 {
    20  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    21  	}
    22  }
    23  
    24  func TestShortenIdEmpty(t *testing.T) {
    25  	id := ""
    26  	truncID := TruncateID(id)
    27  	if len(truncID) > len(id) {
    28  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    29  	}
    30  }
    31  
    32  func TestShortenIdInvalid(t *testing.T) {
    33  	id := "1234"
    34  	truncID := TruncateID(id)
    35  	if len(truncID) != len(id) {
    36  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    37  	}
    38  }
    39  
    40  func TestIsShortIDNonHex(t *testing.T) {
    41  	id := "some non-hex value"
    42  	if IsShortID(id) {
    43  		t.Fatalf("%s is not a short ID", id)
    44  	}
    45  }
    46  
    47  func TestIsShortIDNotCorrectSize(t *testing.T) {
    48  	id := strings.Repeat("a", shortLen+1)
    49  	if IsShortID(id) {
    50  		t.Fatalf("%s is not a short ID", id)
    51  	}
    52  	id = strings.Repeat("a", shortLen-1)
    53  	if IsShortID(id) {
    54  		t.Fatalf("%s is not a short ID", id)
    55  	}
    56  }