...

Source file src/go.opencensus.io/tag/validate_test.go

Documentation: go.opencensus.io/tag

     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  
    15  package tag
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  )
    21  
    22  func TestCheckKeyName(t *testing.T) {
    23  	tests := []struct {
    24  		name   string
    25  		key    string
    26  		wantOK bool
    27  	}{
    28  		{
    29  			name:   "valid",
    30  			key:    "k1",
    31  			wantOK: true,
    32  		},
    33  		{
    34  			name:   "invalid i",
    35  			key:    "k\x19",
    36  			wantOK: false,
    37  		},
    38  		{
    39  			name:   "invalid ii",
    40  			key:    "k\x7f",
    41  			wantOK: false,
    42  		},
    43  		{
    44  			name:   "empty",
    45  			key:    "",
    46  			wantOK: false,
    47  		},
    48  		{
    49  			name:   "whitespace",
    50  			key:    " k ",
    51  			wantOK: true,
    52  		},
    53  		{
    54  			name:   "long",
    55  			key:    strings.Repeat("a", 256),
    56  			wantOK: false,
    57  		},
    58  	}
    59  	for _, tt := range tests {
    60  		ok := checkKeyName(tt.key)
    61  		if ok != tt.wantOK {
    62  			t.Errorf("%v: got %v; want %v", tt.name, ok, tt.wantOK)
    63  		}
    64  	}
    65  }
    66  
    67  func TestCheckValue(t *testing.T) {
    68  	tests := []struct {
    69  		name   string
    70  		value  string
    71  		wantOK bool
    72  	}{
    73  		{
    74  			name:   "valid",
    75  			value:  "v1",
    76  			wantOK: true,
    77  		},
    78  		{
    79  			name:   "invalid i",
    80  			value:  "k\x19",
    81  			wantOK: false,
    82  		},
    83  		{
    84  			name:   "invalid ii",
    85  			value:  "k\x7f",
    86  			wantOK: false,
    87  		},
    88  		{
    89  			name:   "empty",
    90  			value:  "",
    91  			wantOK: true,
    92  		},
    93  		{
    94  			name:   "whitespace",
    95  			value:  " v ",
    96  			wantOK: true,
    97  		},
    98  		{
    99  			name:   "long",
   100  			value:  strings.Repeat("a", 256),
   101  			wantOK: false,
   102  		},
   103  	}
   104  	for _, tt := range tests {
   105  		ok := checkValue(tt.value)
   106  		if ok != tt.wantOK {
   107  			t.Errorf("%v: got %v; want %v", tt.name, ok, tt.wantOK)
   108  		}
   109  	}
   110  }
   111  

View as plain text