utils_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package mezmoexporter
  4. import (
  5. "math/rand"
  6. "testing"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestTruncateString(t *testing.T) {
  10. t.Run("Test empty string", func(t *testing.T) {
  11. s := truncateString("", 10)
  12. require.Len(t, s, 0)
  13. })
  14. // Test string is less than the maximum length
  15. t.Run("Test shorter string", func(t *testing.T) {
  16. s := truncateString("short", 10)
  17. require.Len(t, s, 5)
  18. require.Equal(t, s, "short")
  19. })
  20. // Test string is equal to the maximum length
  21. t.Run("Test equal string", func(t *testing.T) {
  22. s := truncateString("short", 5)
  23. require.Len(t, s, 5)
  24. require.Equal(t, s, "short")
  25. })
  26. // Test string is longer than the maximum length
  27. t.Run("Test longer string", func(t *testing.T) {
  28. s := truncateString("longstring", 4)
  29. require.Len(t, s, 4)
  30. require.Equal(t, s, "long")
  31. })
  32. }
  33. func TestRandString(t *testing.T) {
  34. t.Run("Test fixed length string", func(t *testing.T) {
  35. var s = randString(16 * 1024)
  36. require.Len(t, s, 16*1024)
  37. })
  38. }
  39. const letters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  40. // randString Returns a random string of the specified length.
  41. func randString(n int) string {
  42. b := make([]byte, n)
  43. for i := range b {
  44. b[i] = letters[rand.Intn(len(letters))]
  45. }
  46. return string(b)
  47. }