1// Copyright The OpenTelemetry 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 15package internal 16 17import ( 18 "strings" 19 "testing" 20) 21 22func TestSanitize(t *testing.T) { 23 tests := []struct { 24 name string 25 input string 26 want string 27 }{ 28 { 29 name: "trunacate long string", 30 input: strings.Repeat("a", 101), 31 want: strings.Repeat("a", 100), 32 }, 33 { 34 name: "replace character", 35 input: "test/key-1", 36 want: "test_key_1", 37 }, 38 { 39 name: "add prefix if starting with digit", 40 input: "0123456789", 41 want: "key_0123456789", 42 }, 43 { 44 name: "add prefix if starting with _", 45 input: "_0123456789", 46 want: "key_0123456789", 47 }, 48 { 49 name: "starts with _ after sanitization", 50 input: "/0123456789", 51 want: "key_0123456789", 52 }, 53 { 54 name: "valid input", 55 input: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789", 56 want: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789", 57 }, 58 } 59 60 for _, tt := range tests { 61 t.Run(tt.name, func(t *testing.T) { 62 if got, want := Sanitize(tt.input), tt.want; got != want { 63 t.Errorf("sanitize() = %q; want %q", got, want) 64 } 65 }) 66 } 67} 68