Source file src/net/http/internal/ascii/print_test.go

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ascii
     6  
     7  import "testing"
     8  
     9  func TestEqualFold(t *testing.T) {
    10  	var tests = []struct {
    11  		name string
    12  		a, b string
    13  		want bool
    14  	}{
    15  		{
    16  			name: "empty",
    17  			want: true,
    18  		},
    19  		{
    20  			name: "simple match",
    21  			a:    "CHUNKED",
    22  			b:    "chunked",
    23  			want: true,
    24  		},
    25  		{
    26  			name: "same string",
    27  			a:    "chunked",
    28  			b:    "chunked",
    29  			want: true,
    30  		},
    31  		{
    32  			name: "Unicode Kelvin symbol",
    33  			a:    "chunKed", // This "K" is 'KELVIN SIGN' (\u212A)
    34  			b:    "chunked",
    35  			want: false,
    36  		},
    37  	}
    38  	for _, tt := range tests {
    39  		t.Run(tt.name, func(t *testing.T) {
    40  			if got := EqualFold(tt.a, tt.b); got != tt.want {
    41  				t.Errorf("AsciiEqualFold(%q,%q): got %v want %v", tt.a, tt.b, got, tt.want)
    42  			}
    43  		})
    44  	}
    45  }
    46  
    47  func TestIsPrint(t *testing.T) {
    48  	var tests = []struct {
    49  		name string
    50  		in   string
    51  		want bool
    52  	}{
    53  		{
    54  			name: "empty",
    55  			want: true,
    56  		},
    57  		{
    58  			name: "ASCII low",
    59  			in:   "This is a space: ' '",
    60  			want: true,
    61  		},
    62  		{
    63  			name: "ASCII high",
    64  			in:   "This is a tilde: '~'",
    65  			want: true,
    66  		},
    67  		{
    68  			name: "ASCII low non-print",
    69  			in:   "This is a unit separator: \x1F",
    70  			want: false,
    71  		},
    72  		{
    73  			name: "Ascii high non-print",
    74  			in:   "This is a Delete: \x7F",
    75  			want: false,
    76  		},
    77  		{
    78  			name: "Unicode letter",
    79  			in:   "Today it's 280K outside: it's freezing!", // This "K" is 'KELVIN SIGN' (\u212A)
    80  			want: false,
    81  		},
    82  		{
    83  			name: "Unicode emoji",
    84  			in:   "Gophers like 🧀",
    85  			want: false,
    86  		},
    87  	}
    88  	for _, tt := range tests {
    89  		t.Run(tt.name, func(t *testing.T) {
    90  			if got := IsPrint(tt.in); got != tt.want {
    91  				t.Errorf("IsASCIIPrint(%q): got %v want %v", tt.in, got, tt.want)
    92  			}
    93  		})
    94  	}
    95  }
    96  

View as plain text