1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg/stringid/stringid_test.go
Sebastiaan van Stijn 78eab14d0b
Minor refactor and beter coverage for pkg/stringid
This slightly simplifies TruncateID() by only
trimming the string if needed.

Also improved unit-tests for this package;

- Add a test for GenerateNonCryptoID()
- Add a test for shortening a sha-256 ID
- Make TestShortenId() more "unit", by using a fixed string, instead of calling GenerateRandomID()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2016-07-20 00:23:06 +02:00

72 lines
1.6 KiB
Go

package stringid
import (
"strings"
"testing"
)
func TestGenerateRandomID(t *testing.T) {
id := GenerateRandomID()
if len(id) != 64 {
t.Fatalf("Id returned is incorrect: %s", id)
}
}
func TestGenerateNonCryptoID(t *testing.T) {
id := GenerateNonCryptoID()
if len(id) != 64 {
t.Fatalf("Id returned is incorrect: %s", id)
}
}
func TestShortenId(t *testing.T) {
id := "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2"
truncID := TruncateID(id)
if truncID != "90435eec5c4e" {
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
}
}
func TestShortenSha256Id(t *testing.T) {
id := "sha256:4e38e38c8ce0b8d9041a9c4fefe786631d1416225e13b0bfe8cfa2321aec4bba"
truncID := TruncateID(id)
if truncID != "4e38e38c8ce0" {
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
}
}
func TestShortenIdEmpty(t *testing.T) {
id := ""
truncID := TruncateID(id)
if len(truncID) > len(id) {
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
}
}
func TestShortenIdInvalid(t *testing.T) {
id := "1234"
truncID := TruncateID(id)
if len(truncID) != len(id) {
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
}
}
func TestIsShortIDNonHex(t *testing.T) {
id := "some non-hex value"
if IsShortID(id) {
t.Fatalf("%s is not a short ID", id)
}
}
func TestIsShortIDNotCorrectSize(t *testing.T) {
id := strings.Repeat("a", shortLen+1)
if IsShortID(id) {
t.Fatalf("%s is not a short ID", id)
}
id = strings.Repeat("a", shortLen-1)
if IsShortID(id) {
t.Fatalf("%s is not a short ID", id)
}
}