github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/strings_test.go (about)

     1  // Copyright 2016 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package util
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestGetSingleRune(t *testing.T) {
    20  	tests := []struct {
    21  		s        string
    22  		expected rune
    23  		err      bool
    24  	}{
    25  		{"a", 'a', false},
    26  		{"", 0, false},
    27  		{"🐛"[:1], 0, true},
    28  		{"aa", 'a', true},
    29  	}
    30  	for _, tc := range tests {
    31  		t.Run(tc.s, func(t *testing.T) {
    32  			got, err := GetSingleRune(tc.s)
    33  			if (err != nil) != tc.err {
    34  				t.Fatalf("got unexpected err: %v", err)
    35  			}
    36  			if tc.expected != got {
    37  				t.Fatalf("expected %v, got %v", tc.expected, got)
    38  			}
    39  		})
    40  	}
    41  }
    42  
    43  func TestToLowerSingleByte(t *testing.T) {
    44  	testCases := []struct {
    45  		from     byte
    46  		expected byte
    47  	}{
    48  		{'a', 'a'},
    49  		{'A', 'a'},
    50  		{'c', 'c'},
    51  		{'C', 'c'},
    52  		{'Z', 'z'},
    53  		{'1', '1'},
    54  		{'\n', '\n'},
    55  	}
    56  
    57  	for _, tc := range testCases {
    58  		t.Run(string(tc.from), func(t *testing.T) {
    59  			ret := ToLowerSingleByte(tc.from)
    60  			require.Equal(t, tc.expected, ret)
    61  		})
    62  	}
    63  }