stringid_test.go 1.6 KB

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