github.com/cnotch/ipchub@v1.1.0/utils/scan/pair_test.go (about)

     1  // Copyright (c) 2019,CAOHONGJU All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package scan
     6  
     7  import (
     8  	"testing"
     9  	"unicode"
    10  )
    11  
    12  func TestPair_Scan(t *testing.T) {
    13  	tests := []struct {
    14  		name      string
    15  		args      string
    16  		wantKey   string
    17  		wantValue string
    18  		wantOk    bool
    19  	}{
    20  		{
    21  			"不带引号",
    22  			"a=chj",
    23  			"a",
    24  			"chj",
    25  			true,
    26  		},
    27  		{
    28  			"带引号",
    29  			"a=\"chj\"",
    30  			"a",
    31  			"chj",
    32  			true,
    33  		},
    34  		{
    35  			"带空个",
    36  			" \ta=  \"chj\"\t",
    37  			"a",
    38  			"chj",
    39  			true,
    40  		},
    41  	}
    42  	for _, tt := range tests {
    43  		t.Run(tt.name, func(t *testing.T) {
    44  
    45  			gotKey, gotValue, gotOk := EqualPair.Scan(tt.args)
    46  			if gotKey != tt.wantKey {
    47  				t.Errorf("Pair.Scan() gotKey = %v, want %v", gotKey, tt.wantKey)
    48  			}
    49  			if gotValue != tt.wantValue {
    50  				t.Errorf("Pair.Scan() gotValue = %v, want %v", gotValue, tt.wantValue)
    51  			}
    52  			if gotOk != tt.wantOk {
    53  				t.Errorf("Pair.Scan() gotOk = %v, want %v", gotOk, tt.wantOk)
    54  			}
    55  		})
    56  	}
    57  }
    58  
    59  func TestPair_ScanMultiRune(t *testing.T) {
    60  	chinesePair := NewPair('是', unicode.IsSpace)
    61  	tests := []struct {
    62  		name      string
    63  		args      string
    64  		wantKey   string
    65  		wantValue string
    66  		wantOk    bool
    67  	}{
    68  		{
    69  			"不带空格",
    70  			"a是chj",
    71  			"a",
    72  			"chj",
    73  			true,
    74  		},
    75  		{
    76  			"不带空格",
    77  			"a是 chj\t",
    78  			"a",
    79  			"chj",
    80  			true,
    81  		},
    82  	}
    83  	for _, tt := range tests {
    84  		t.Run(tt.name, func(t *testing.T) {
    85  
    86  			gotKey, gotValue, gotOk := chinesePair.Scan(tt.args)
    87  			if gotKey != tt.wantKey {
    88  				t.Errorf("Pair.Scan() gotKey = %v, want %v", gotKey, tt.wantKey)
    89  			}
    90  			if gotValue != tt.wantValue {
    91  				t.Errorf("Pair.Scan() gotValue = %v, want %v", gotValue, tt.wantValue)
    92  			}
    93  			if gotOk != tt.wantOk {
    94  				t.Errorf("Pair.Scan() gotOk = %v, want %v", gotOk, tt.wantOk)
    95  			}
    96  		})
    97  	}
    98  }
    99  
   100  func Benchmark_Pair_Scan(b *testing.B) {
   101  	s := `realm="Another Streaming Media"`
   102  	b.ResetTimer()
   103  	b.RunParallel(func(pb *testing.PB) {
   104  		for pb.Next() {
   105  			key, value, ok := EqualPair.Scan(s)
   106  			_ = key
   107  			_ = value
   108  			_ = ok
   109  		}
   110  	})
   111  }