sanitize_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2017, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package internal
  15. import (
  16. "strings"
  17. "testing"
  18. )
  19. func TestSanitize(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. input string
  23. want string
  24. }{
  25. {
  26. name: "trunacate long string",
  27. input: strings.Repeat("a", 101),
  28. want: strings.Repeat("a", 100),
  29. },
  30. {
  31. name: "replace character",
  32. input: "test/key-1",
  33. want: "test_key_1",
  34. },
  35. {
  36. name: "add prefix if starting with digit",
  37. input: "0123456789",
  38. want: "key_0123456789",
  39. },
  40. {
  41. name: "add prefix if starting with _",
  42. input: "_0123456789",
  43. want: "key_0123456789",
  44. },
  45. {
  46. name: "starts with _ after sanitization",
  47. input: "/0123456789",
  48. want: "key_0123456789",
  49. },
  50. {
  51. name: "valid input",
  52. input: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789",
  53. want: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789",
  54. },
  55. }
  56. for _, tt := range tests {
  57. t.Run(tt.name, func(t *testing.T) {
  58. if got, want := Sanitize(tt.input), tt.want; got != want {
  59. t.Errorf("sanitize() = %q; want %q", got, want)
  60. }
  61. })
  62. }
  63. }