diff --git a/README.md b/README.md index fc838b1..8496ed9 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ - MaskChineseIdCard64 中国身份证号脱敏,MaskChineseMobile简化版,保留前6位后4位 - MaskChineseIdCard11 中国身份证号脱敏,MaskChineseMobile简化版,保留前1位后1位 - MaskAnyString 任意字符串脱敏,可指定左侧保留几个字符、右侧保留几个字符 + - Masker 更强大的字符串脱敏工具💊,有多个选项可以用于实现您的脱敏需求,位于子包`kg_str`中 - [x] 本地缓存相关操作 - Set 设置缓存项,支持仅设置缓存,也支持同时给缓存添加一个过期时间 - Get 获得缓存内容 diff --git a/kg_str/str.go b/kg_str/str.go new file mode 100644 index 0000000..53c9a92 --- /dev/null +++ b/kg_str/str.go @@ -0,0 +1,111 @@ +package kg_str + +import ( + "fmt" + "unicode/utf8" +) + +const DefaultCharCount = 1 + +type MaskOption func(masker *Masker) + +// Masker 字符串脱敏工具 +type Masker struct { + maskCount int + left int + right int + maskChar rune +} + +// WithMaskCount 脱敏字符数量,默认是输入字符串总字符数量-左侧保留字符数-右侧保留字符数,即:默认脱敏之后的字符串字符数量和原字符串相同 +func WithMaskCount(maskCount int) MaskOption { + return func(masker *Masker) { + masker.maskCount = maskCount + } +} + +// WithLeft 脱敏字符左侧保留字符数量,未指定时使用默认值 DefaultCharCount +func WithLeft(left int) MaskOption { + return func(masker *Masker) { + masker.left = left + } +} + +// WithRight 脱敏字符右侧保留字符数量,未指定时使用默认值 DefaultCharCount +func WithRight(right int) MaskOption { + return func(masker *Masker) { + masker.right = right + } +} + +// WithMaskChar 脱敏字符,如果未指定,默认使用星号* +func WithMaskChar(maskChar rune) MaskOption { + return func(masker *Masker) { + masker.maskChar = maskChar + } +} + +// NewMasker 创建脱敏工具实例 +// +// 如果您既没有指定左侧保留字符数量,也没有指定右侧保留字符数量,那么默认左右各保留一个字符 +// 如果您没有指定脱敏字符,那么默认使用星号* +func NewMasker(options ...MaskOption) *Masker { + masker := &Masker{} + for _, option := range options { + option(masker) + } + // 判断并设置默认值 + if masker.maskChar == 0 { + masker.maskChar = '*' + } + if masker.left == 0 && masker.right == 0 { + masker.left = DefaultCharCount + masker.right = DefaultCharCount + } + return masker +} + +// Mask 脱敏任意字符串 +// 入参: s 要脱敏的字符串 +// +// 注意1:如果入参字符串太短,可能默认的左侧保留 DefaultCharCount、右侧保留 DefaultCharCount 无法满足您的需求,此时您在实例化时要指定相应选项 +// 注意2:如果入参字符串为空,也返回空,不会报错 +// 注意3:如果指定了脱敏字符串的数量,那么返回的字符串长度有可能和您提供的入参字符串的长度并不相同 +func (mk *Masker) Mask(s string) string { + var ( + left, middle, right string + ) + + size := utf8.RuneCountInString(s) + if size == 0 { + return "" + } + // 如果初始化的时候没有设置starCount,则默认用字符串长度减去左右保留的长度 + maskCount := mk.maskCount + if maskCount == 0 { + maskCount = size - mk.left - mk.right + } + + i := 0 + for _, n := range s { + if i < mk.left { + left += fmt.Sprintf("%c", n) + } else if i >= size-mk.right { + right += fmt.Sprintf("%c", n) + } else if utf8.RuneCountInString(middle) < maskCount { + middle += fmt.Sprintf("%c", mk.maskChar) + } + i++ + } + // 如果字符串太短,以至于无法脱敏,进行扩张 + middleSize := len(middle) + if maskCount > middleSize { + for i := 0; i < maskCount-middleSize; i++ { + middle += fmt.Sprintf("%c", mk.maskChar) + } + } + if middle == "" { + middle = fmt.Sprintf("%c", mk.maskChar) + } + return left + middle + right +} diff --git a/kg_str/str_test.go b/kg_str/str_test.go new file mode 100644 index 0000000..26ce67a --- /dev/null +++ b/kg_str/str_test.go @@ -0,0 +1,270 @@ +package kg_str + +import ( + "testing" + "unicode/utf8" +) + +func TestMaskShortStringWithLeft(t *testing.T) { + masker := NewMasker(WithLeft(1)) + name := "张三" + excepted := "张*" + actual := masker.Mask(name) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskShortStringWithRight(t *testing.T) { + masker := NewMasker(WithRight(1)) + name := "张三" + excepted := "*三" + actual := masker.Mask(name) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskShortStringOnlyLeft(t *testing.T) { + masker := NewMasker(WithLeft(1)) + name := "张三" + excepted := "张*" + actual := masker.Mask(name) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskShortStringOnlyRight(t *testing.T) { + name := "张三三" + excepted := "*三三" + masker := NewMasker(WithRight(utf8.RuneCountInString(name) - 1)) + actual := masker.Mask(name) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskShortStringOnlyRight1(t *testing.T) { + name := "张三三" + excepted := "**三" + masker := NewMasker(WithRight(1)) + actual := masker.Mask(name) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskShortStringWithRightExpended(t *testing.T) { + name := "张三" + excepted := "***三" + masker := NewMasker(WithRight(1), WithMaskCount(3)) + actual := masker.Mask(name) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskShortStringWithLeftExpended(t *testing.T) { + name := "张三" + excepted := "张***" + masker := NewMasker(WithLeft(1), WithMaskCount(3)) + actual := masker.Mask(name) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskDefault(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want string + }{ + {name: "最简人名", args: args{s: "张三"}, want: "张*三"}, + {name: "常规人名", args: args{s: "张一二"}, want: "张*二"}, + {name: "常规字符串", args: args{s: "一二三四五六七八九十"}, want: "一********十"}, + } + + masker := NewMasker() + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := masker.Mask(test.args.s); got != test.want { + t.Errorf("Mask() = %v, want %v", got, test.want) + } + }) + } +} + +func TestMaskDefaultLeftAndRight(t *testing.T) { + masker := NewMasker(WithMaskChar('?'), WithMaskCount(3)) + s := "一二三四五六七八九十" + excepted := "一???十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskDefaultLeftAndRightAndStar(t *testing.T) { + masker := NewMasker(WithMaskCount(3)) + s := "一二三四五六七八九十" + excepted := "一***十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskDefaultLeftAndRightAndMaskChar(t *testing.T) { + masker := NewMasker(WithMaskChar(',')) + s := "一二三四五六七八九十" + excepted := "一,,,,,,,,十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskDefaultMaskCharCount(t *testing.T) { + masker := NewMasker(WithMaskChar('〇')) + s := "一二三四五六七八九十" + excepted := "一〇〇〇〇〇〇〇〇十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithLeft(t *testing.T) { + masker := NewMasker(WithLeft(3)) + s := "一二三四五六七八九十" + excepted := "一二三*******" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithLeftAndMaskCount(t *testing.T) { + masker := NewMasker(WithLeft(3), WithMaskCount(3)) + s := "一二三四五六七八九十" + excepted := "一二三***" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithLeftAndMaskChar(t *testing.T) { + masker := NewMasker(WithLeft(3), WithMaskChar('❎')) + s := "一二三四五六七八九十" + excepted := "一二三❎❎❎❎❎❎❎" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithLeftAndMaskCharAndMaskCount(t *testing.T) { + masker := NewMasker(WithLeft(3), WithMaskCount(3), WithMaskChar('❎')) + s := "一二三四五六七八九十" + excepted := "一二三❎❎❎" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithRight(t *testing.T) { + masker := NewMasker(WithRight(3)) + s := "一二三四五六七八九十" + excepted := "*******八九十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithRightAndMaskCount(t *testing.T) { + masker := NewMasker(WithRight(3), WithMaskCount(3)) + s := "一二三四五六七八九十" + excepted := "***八九十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithRightAndMaskChar(t *testing.T) { + masker := NewMasker(WithRight(3), WithMaskChar('?')) + s := "一二三四五六七八九十" + excepted := "???????八九十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithRightAndMaskCharAndMaskCount(t *testing.T) { + masker := NewMasker(WithRight(3), WithMaskChar('?'), WithMaskCount(3)) + s := "一二三四五六七八九十" + excepted := "???八九十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithLeftAndRight(t *testing.T) { + masker := NewMasker(WithLeft(3), WithRight(3)) + s := "一二三四五六七八九十" + excepted := "一二三****八九十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithLeftAndRightAndMaskCount(t *testing.T) { + masker := NewMasker(WithLeft(3), WithRight(3), WithMaskCount(2)) + s := "一二三四五六七八九十" + excepted := "一二三**八九十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithLeftAndRightAndMaskChar(t *testing.T) { + masker := NewMasker(WithLeft(3), WithRight(3), WithMaskChar('.')) + s := "一二三四五六七八九十" + excepted := "一二三....八九十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithLeftAndRightAndMaskCharAndMaskCount(t *testing.T) { + masker := NewMasker(WithLeft(3), WithRight(2), WithMaskChar('❓'), WithMaskCount(3)) + s := "一二三四五六七八九十" + excepted := "一二三❓❓❓九十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskWithLeftAndRightAndMaskCharAndMaskCount2(t *testing.T) { + masker := NewMasker(WithLeft(3), WithRight(3), WithMaskChar('�'), WithMaskCount(2)) + s := "一二三四五六七八九十" + excepted := "一二三��八九十" + actual := masker.Mask(s) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} diff --git a/map_test.go b/map_test.go index c034b38..f70c45d 100644 --- a/map_test.go +++ b/map_test.go @@ -1,7 +1,6 @@ package kgo import ( - "reflect" "testing" ) @@ -42,11 +41,11 @@ func TestMapKeys(t *testing.T) { want []K } tests := []testCase[string, int]{ - {name: "string keys", args: args[string, int]{map[string]int{"a": 1, "b": 2, "c": 3}}, want: []string{"a", "b", "c"}}, + {name: "string keys", args: args[string, int]{map[string]int{"a": 1, "b": 2, "c": 3}}, want: []string{"b", "a", "c"}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := MapKeys(tt.args.m); !reflect.DeepEqual(got, tt.want) { + if got := MapKeys(tt.args.m); !ContainsAll(got, tt.want) { t.Errorf("MapKeys() = %v, want %v", got, tt.want) } }) diff --git a/num_test.go b/num_test.go index 655955a..003a4fc 100644 --- a/num_test.go +++ b/num_test.go @@ -14,7 +14,7 @@ func TestNumJoinStr(t *testing.T) { } for expected, src := range resources1 { if actual := NumJoinStr(src); actual != expected { - t.Fatalf("预期 %+v,实际值:%+v", expected, actual) + t.Errorf("预期 %+v,实际值:%+v", expected, actual) } } var n1, n2, n3 = 111, 222, 333 @@ -23,7 +23,7 @@ func TestNumJoinStr(t *testing.T) { } for expected, src := range resources11 { if actual := NumJoinStr(src); actual != expected { - t.Fatalf("预期 %+v,实际值:%+v", expected, actual) + t.Errorf("预期 %+v,实际值:%+v", expected, actual) } } resources2 := map[string][]float64{ @@ -32,7 +32,7 @@ func TestNumJoinStr(t *testing.T) { } for expected, src := range resources2 { if actual := NumJoinStr(src); actual != expected { - t.Fatalf("预期 %+v,实际值:%+v", expected, actual) + t.Errorf("预期 %+v,实际值:%+v", expected, actual) } } } @@ -48,7 +48,7 @@ func TestNums2Strings(t *testing.T) { actuals := Nums2Strings(expected) for j, actual := range actuals { if actual != strconv.FormatInt(int64(resources[i][j]), 10) { - t.Fatalf("预期 %+v,实际值:%+v", expected, actual) + t.Errorf("预期 %+v,实际值:%+v", expected, actual) } } } diff --git a/str.go b/str.go index 227834b..440ffa0 100644 --- a/str.go +++ b/str.go @@ -192,6 +192,8 @@ func MaskChineseIdCard(idCard string, left, right int) (masked string) { // right 右边保留几位 // // 示例 MaskAnyString("一二三四五", 1, 1) 得到 一***五 +// +// 示例 MaskAnyString("一二三四五六七八九十", 3, 1) 得到 一二三******十 func MaskAnyString(s string, left, right int) (masked string) { size := utf8.RuneCountInString(s) if size == 0 { diff --git a/str_test.go b/str_test.go index ae54868..ec7d314 100644 --- a/str_test.go +++ b/str_test.go @@ -25,7 +25,7 @@ func TestJoinElements(t *testing.T) { } for expected, src := range resources1 { if actual := JoinElements(src); actual != expected { - t.Fatalf("预期 %+v,实际值:%+v", expected, actual) + t.Errorf("预期 %+v,实际值:%+v", expected, actual) } } var n1, n2, n3 = 1, 2, 3 @@ -34,7 +34,7 @@ func TestJoinElements(t *testing.T) { } for expected, src := range resources11 { if actual := JoinElements(src); actual != expected { - t.Fatalf("预期 %+v,实际值:%+v", expected, actual) + t.Errorf("预期 %+v,实际值:%+v", expected, actual) } } @@ -43,7 +43,7 @@ func TestJoinElements(t *testing.T) { } for expected, src := range resources2 { if actual := JoinElements(src); actual != expected { - t.Fatalf("预期 %+v,实际值:%+v", expected, actual) + t.Errorf("预期 %+v,实际值:%+v", expected, actual) } } var s1, s2, s3 = "a", "b", "c" @@ -52,7 +52,7 @@ func TestJoinElements(t *testing.T) { } for expected, src := range resources22 { if actual := JoinElements(src); actual != expected { - t.Fatalf("预期 %+v,实际值:%+v", expected, actual) + t.Errorf("预期 %+v,实际值:%+v", expected, actual) } } @@ -61,7 +61,7 @@ func TestJoinElements(t *testing.T) { } for expected, src := range resources3 { if actual := JoinElements(src); actual != expected { - t.Fatalf("预期 %+v,实际值:%+v", expected, actual) + t.Errorf("预期 %+v,实际值:%+v", expected, actual) } } var b1, b2, b3 = true, false, true @@ -70,7 +70,7 @@ func TestJoinElements(t *testing.T) { } for expected, src := range resources33 { if actual := JoinElements(src); actual != expected { - t.Fatalf("预期 %+v,实际值:%+v", expected, actual) + t.Errorf("预期 %+v,实际值:%+v", expected, actual) } } } @@ -79,7 +79,7 @@ func TestB2S(t *testing.T) { b := []byte{'A', 'B', 'C'} s := B2S(b) if s != "ABC" { - t.Fatalf("预期 %+v,实际值:%+v", "ABC", s) + t.Errorf("预期 %+v,实际值:%+v", "ABC", s) } } @@ -87,7 +87,7 @@ func TestS2B(t *testing.T) { s := "!@#" b := S2B(s) if len(b) != 3 || s[0] != '!' || s[1] != '@' || s[2] != '#' { - t.Fatalf("预期 %+v,实际值:%+v", []byte{'!', '@', '#'}, b) + t.Errorf("预期 %+v,实际值:%+v", []byte{'!', '@', '#'}, b) } } @@ -96,7 +96,7 @@ func TestS2B2S(t *testing.T) { b := S2B(s) s2 := B2S(b) if s2 != s { - t.Fatalf("预期 %+v,实际值:%+v", s, s2) + t.Errorf("预期 %+v,实际值:%+v", s, s2) } } @@ -105,7 +105,7 @@ func TestMaskChineseName(t *testing.T) { excepted := "张*" actual := MaskChineseName(name) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -114,7 +114,7 @@ func TestMaskChineseName1(t *testing.T) { excepted := "张**" actual := MaskChineseName(name) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -123,7 +123,7 @@ func TestMaskChineseName2(t *testing.T) { excepted := "张***" actual := MaskChineseName(name) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -132,7 +132,7 @@ func TestMaskChineseName4(t *testing.T) { excepted := "h****" actual := MaskChineseName(name) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -141,7 +141,7 @@ func TestMaskChineseNameEx(t *testing.T) { excepted := "*三" actual := MaskChineseNameEx(name, 0, 1) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -150,7 +150,7 @@ func TestMaskChineseNameEx1(t *testing.T) { excepted := "*三一" actual := MaskChineseNameEx(name, 0, 2) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -159,7 +159,7 @@ func TestMaskChineseNameEx2(t *testing.T) { excepted := "*三三三" actual := MaskChineseNameEx(name, 0, 3) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -168,7 +168,7 @@ func TestMaskChineseNameEx4(t *testing.T) { excepted := "*ello" actual := MaskChineseNameEx(name, 0, 4) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -177,7 +177,7 @@ func TestMaskChineseNameEx5(t *testing.T) { excepted := "张*二" actual := MaskChineseNameEx(name, 1, 1) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -186,7 +186,7 @@ func TestMaskChineseNameEx6(t *testing.T) { excepted := "*一二" actual := MaskChineseNameEx(name, 0, utf8.RuneCountInString(name)-1) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -195,7 +195,7 @@ func TestMaskChineseMobile(t *testing.T) { excepted := "130****5678" actual := MaskChineseMobile(tel) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -204,7 +204,7 @@ func TestMaskChsCard34(t *testing.T) { excepted := "101***********1234" actual := MaskChineseIdCard34(idCard) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -213,7 +213,7 @@ func TestMaskChsCard64(t *testing.T) { excepted := "101101********1234" actual := MaskChineseIdCard64(idCard) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -222,7 +222,7 @@ func TestMaskChsCard11(t *testing.T) { excepted := "1****************4" actual := MaskChineseIdCard11(idCard) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -231,7 +231,7 @@ func TestMaskChineseIdCard(t *testing.T) { excepted := "101***********1234" actual := MaskChineseIdCard(idCard, 3, 4) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -240,7 +240,7 @@ func TestMaskChineseIdCard2(t *testing.T) { excepted := "101***********123X" actual := MaskChineseIdCard(idCard, 3, 4) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -249,7 +249,7 @@ func TestMaskChineseIdCard3(t *testing.T) { excepted := "101***********123x" actual := MaskChineseIdCard(idCard, 3, 4) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -258,7 +258,7 @@ func TestMaskAnyString(t *testing.T) { excepted := "一***五" actual := MaskAnyString(s, 1, 1) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -267,7 +267,7 @@ func TestMaskAnyString1(t *testing.T) { excepted := "一****" actual := MaskAnyString(s, 1, 0) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) } } @@ -276,6 +276,15 @@ func TestMaskAnyString2(t *testing.T) { excepted := "****五" actual := MaskAnyString(s, 0, 1) if actual != excepted { - t.Fatalf("预期 %v,实际值:%v", excepted, actual) + t.Errorf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskAnyString3(t *testing.T) { + s := "一二三四五六七八九十" + excepted := "一二三******十" + actual := MaskAnyString(s, 3, 1) + if actual != excepted { + t.Errorf("预期 %v,实际值:%v", excepted, actual) } }