github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/exp/kvutil/key_factory_test.go (about)

     1  package kvutil
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func Test_Key(t *testing.T) {
     8  	var km KeyFactory
     9  	key := km.NewKey("abc:def:%d:%s")
    10  	want := "abc:def:1234567:x0BtEadepz6L"
    11  
    12  	got := key(1234567, "x0BtEadepz6L")
    13  	if got != want {
    14  		t.Errorf("failed Test_Key: got=%v want=%v", got, want)
    15  	}
    16  }
    17  
    18  func Test_Key_CornerCase(t *testing.T) {
    19  	cases := []struct {
    20  		key  string
    21  		args []any
    22  		want string
    23  	}{
    24  		{
    25  			key:  "%v:blah:%v",
    26  			args: []any{1234567, "x0BtEadepz6L"},
    27  			want: "1234567:blah:x0BtEadepz6L",
    28  		},
    29  		{
    30  			key:  "%v%v:blah:",
    31  			args: []any{1234567, "x0BtEadepz6L"},
    32  			want: "1234567x0BtEadepz6L:blah:",
    33  		},
    34  		{
    35  			key:  "%v:%v:blah",
    36  			args: []any{1234567, "x0BtEadepz6L"},
    37  			want: "1234567:x0BtEadepz6L:blah",
    38  		},
    39  		{
    40  			key:  "blah:%v%v",
    41  			args: []any{1234567, "x0BtEadepz6L"},
    42  			want: "blah:1234567x0BtEadepz6L",
    43  		},
    44  	}
    45  
    46  	var km KeyFactory
    47  	for _, c := range cases {
    48  		key := km.NewKey(c.key)
    49  		got := key(c.args...)
    50  
    51  		if got != c.want {
    52  			t.Errorf("failed Test_Key_CornerCase: key=%v got=%v want=%v", c.key, got, c.want)
    53  		}
    54  	}
    55  }
    56  
    57  func Test_Key_CurlyBrace(t *testing.T) {
    58  	cases := []struct {
    59  		key  string
    60  		args []any
    61  		want string
    62  	}{
    63  		{
    64  			key:  "{}:blah:{}",
    65  			args: []any{1234567, "x0BtEadepz6L"},
    66  			want: "1234567:blah:x0BtEadepz6L",
    67  		},
    68  		{
    69  			key:  "{}{}:blah:",
    70  			args: []any{uint32(1234567), "x0BtEadepz6L"},
    71  			want: "1234567x0BtEadepz6L:blah:",
    72  		},
    73  		{
    74  			key:  "{}:{}:blah",
    75  			args: []any{1234567, "x0BtEadepz6L"},
    76  			want: "1234567:x0BtEadepz6L:blah",
    77  		},
    78  		{
    79  			key:  "blah:{}{}",
    80  			args: []any{uint64(1234567), "x0BtEadepz6L"},
    81  			want: "blah:1234567x0BtEadepz6L",
    82  		},
    83  	}
    84  
    85  	var kf KeyFactory
    86  	for _, c := range cases {
    87  		key := kf.NewKey(c.key)
    88  		got := key(c.args...)
    89  
    90  		if got != c.want {
    91  			t.Errorf("failed Test_Key_CurlyBrace: key=%v got=%v want=%v", c.key, got, c.want)
    92  		}
    93  	}
    94  }
    95  
    96  func Test_Key_NamedArgs(t *testing.T) {
    97  	var km KeyFactory
    98  	key := km.NewKey("abc:{some_id}:{dummy}")
    99  	want := "abc:1234567:x0BtEadepz6L"
   100  	got := key(1234567, "x0BtEadepz6L")
   101  	if got != want {
   102  		t.Errorf("failed Test_Key_NamedArgs: got=%v, want=%v", got, want)
   103  	}
   104  }
   105  
   106  func Test_Key_WithArgNames(t *testing.T) {
   107  	var km KeyFactory
   108  
   109  	t.Run("sprintfKey", func(t *testing.T) {
   110  		key := km.NewKey("{{some_id_1}_foo_bar}:{some_id_2}:%v", "some_id_1", "some_id_2")
   111  		want := "{111_foo_bar}:222:333"
   112  		got := key(111, 222, 333)
   113  		if got != want {
   114  			t.Errorf("failed Test_Key_WithArgNames")
   115  		}
   116  	})
   117  
   118  	t.Run("builderKey", func(t *testing.T) {
   119  		key := km.NewKey("{{some_id_1}_foo_bar}:{some_id_2}", "some_id_1", "some_id_2")
   120  		want := "{111_foo_bar}:222"
   121  		got := key(111, 222)
   122  		if got != want {
   123  			t.Errorf("failed Test_Key_WithArgNames: got= %v, want= %v", got, want)
   124  		}
   125  	})
   126  }
   127  
   128  func Test_Key_UnmatchedArgCount(t *testing.T) {
   129  	var km KeyFactory
   130  
   131  	key1 := km.NewKey("abc:{some_id}:{arg2}:{dummy1}:{dummy2}")
   132  	got1 := key1(1234567, "x0BtEadepz6L")
   133  	want1 := "abc:1234567:x0BtEadepz6L:%!v(MISSING):%!v(MISSING)"
   134  	if got1 != want1 {
   135  		t.Errorf("failed Test_Key_UnmatchedArgCount: got1=%v, want1=%v", got1, want1)
   136  	}
   137  
   138  	key2 := km.NewKey("{{some_id_1}_foo_bar_count}:{arg2}:{dummy1}:{dummy2}",
   139  		"some_id_1", "arg2")
   140  	got2 := key2(1234567)
   141  	want2 := "{1234567_foo_bar_count}:%!v(MISSING):{dummy1}:{dummy2}"
   142  	if got2 != want2 {
   143  		t.Errorf("failed Test_Key_UnmatchedArgCount: got2=%v, want2=%v", got2, want2)
   144  	}
   145  }
   146  
   147  func Test_SetPrefix(t *testing.T) {
   148  	var km KeyFactory
   149  	km.SetPrefix("some_prefix:")
   150  
   151  	key := km.NewKey("abc:def:%d:%s")
   152  	want := "some_prefix:abc:def:1234567:x0BtEadepz6L"
   153  	got := key(1234567, "x0BtEadepz6L")
   154  	if got != want {
   155  		t.Errorf("failed Test_SetPrefix: got=%v want=%v", got, want)
   156  	}
   157  }
   158  
   159  var benchmarkData = []struct {
   160  	format   string
   161  	argNames []string
   162  	args     []any
   163  }{
   164  	{"abc:{some_id}:{dummy}", nil,
   165  		[]any{1234567, "x0BtEadepz6L"}},
   166  	{"{}:blah:{}", nil,
   167  		[]any{1234567, "x0BtEadepz6L"}},
   168  	{"{{some_id_1}_foo_bar_count}:{some_id_2}",
   169  		[]string{"some_id_1", "some_id_2"},
   170  		[]any{1234567, "x0BtEadepz6L"}},
   171  }
   172  
   173  var benchmarkSprintfKeys []Key
   174  
   175  func init() {
   176  	km := KeyFactory{prefix: "my_some_prefix"}
   177  	for i := 0; i < len(benchmarkData); i++ {
   178  		x := benchmarkData[i]
   179  		benchmarkSprintfKeys = append(benchmarkSprintfKeys, km.NewKey(x.format, x.argNames...))
   180  	}
   181  }
   182  
   183  func Benchmark_Key_Sprintf(b *testing.B) {
   184  	b.ReportAllocs()
   185  	for i := 0; i < b.N; i++ {
   186  		for j := 0; j < len(benchmarkData); j++ {
   187  			_ = benchmarkSprintfKeys[j](benchmarkData[j].args...)
   188  		}
   189  	}
   190  }
   191  
   192  func Benchmark_Key_Sprintf_Parallel(b *testing.B) {
   193  	b.ReportAllocs()
   194  	b.RunParallel(func(pb *testing.PB) {
   195  		for pb.Next() {
   196  			for j := 0; j < len(benchmarkData); j++ {
   197  				_ = benchmarkSprintfKeys[j](benchmarkData[j].args...)
   198  			}
   199  		}
   200  	})
   201  }