github.com/blend/go-sdk@v1.20220411.3/profanity/filter_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package profanity 9 10 import ( 11 "strings" 12 "testing" 13 14 "github.com/blend/go-sdk/assert" 15 ) 16 17 func Test_Filter_IsZero(t *testing.T) { 18 its := assert.New(t) 19 20 its.True(Filter{}.IsZero()) 21 its.False(Filter{ 22 Include: []string{"foo", "bar"}, // any of these 23 }.IsZero()) 24 its.False(Filter{ 25 Exclude: []string{"foo", "bar"}, // any of these 26 }.IsZero()) 27 its.False(Filter{ 28 Include: []string{"foo", "bar"}, // any of these 29 Exclude: []string{"foo", "bar"}, // any of these 30 }.IsZero()) 31 } 32 33 func Test_Filter_Match_Include(t *testing.T) { 34 its := assert.New(t) 35 36 f := Filter{ 37 Include: []string{"foo", "bar"}, // any of these 38 } 39 40 var includeMatch, excludeMatch string 41 includeMatch, excludeMatch = f.Match("foo bar buzz", strings.Contains) 42 its.Equal("foo", includeMatch) 43 its.Equal("", excludeMatch) 44 } 45 46 func Test_Filter_Match_Include_Exclude(t *testing.T) { 47 its := assert.New(t) 48 49 f := Filter{ 50 Include: []string{"foo", "bar"}, // any of these 51 Exclude: []string{"buzz", "wuzz"}, // but not these 52 } 53 54 var includeMatch, excludeMatch string 55 includeMatch, excludeMatch = f.Match("foo bar buzz", strings.Contains) 56 its.Equal("foo", includeMatch) 57 its.Equal("buzz", excludeMatch) 58 } 59 60 func Test_Filter_Match_EmptyInput(t *testing.T) { 61 its := assert.New(t) 62 63 f := Filter{ 64 Include: []string{"foo", "bar"}, // any of these 65 Exclude: []string{"buzz", "wuzz"}, // but not these 66 } 67 68 var includeMatch, excludeMatch string 69 includeMatch, excludeMatch = f.Match("", strings.Contains) 70 its.Equal("", includeMatch) 71 its.Equal("", excludeMatch) 72 } 73 74 func Test_Filter_AllowMatch_IncludeExclude(t *testing.T) { 75 its := assert.New(t) 76 77 f := Filter{ 78 Include: []string{"foo", "bar"}, // any of these 79 Exclude: []string{"buzz", "wuzz"}, // but not these 80 } 81 82 its.True(f.AllowMatch("test", "")) 83 its.False(f.AllowMatch("test", "not-test")) 84 its.False(f.AllowMatch("", "not-test")) 85 } 86 87 func Test_Filter_AllowMatch_Include(t *testing.T) { 88 its := assert.New(t) 89 90 f := Filter{ 91 Include: []string{"foo", "bar"}, // any of these 92 } 93 94 its.True(f.AllowMatch("test", "")) 95 its.True(f.AllowMatch("test", "not-test")) 96 its.False(f.AllowMatch("", "not-test")) 97 } 98 99 func Test_Filter_AllowMatch_Exclude(t *testing.T) { 100 its := assert.New(t) 101 102 f := Filter{ 103 Exclude: []string{"foo", "bar"}, // any of these 104 } 105 106 its.True(f.AllowMatch("test", "")) 107 its.False(f.AllowMatch("test", "not-test")) 108 its.False(f.AllowMatch("", "not-test")) 109 }