-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgocensorword_test.go
executable file
·109 lines (88 loc) · 2.44 KB
/
gocensorword_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package gocensorword_test
import (
"fmt"
"testing"
gocensorword "github.com/pcpratheesh/go-censorword"
"github.com/stretchr/testify/require"
)
func TestBadWord(t *testing.T) {
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
)
word := "bitch"
resultString, err := detector.CensorWord(word)
if err != nil {
panic(err)
}
require.Equal(t, resultString, "*****")
}
func TestWithCustomList(t *testing.T) {
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
gocensorword.WithCustomCensorList([]string{
"ass", "bitch",
}),
)
word := "bad ass"
resultString, err := detector.CensorWord(word)
if err != nil {
panic(err)
}
require.Equal(t, resultString, "bad ***")
}
func TestBadWordFirstLetterKept(t *testing.T) {
var detector = gocensorword.NewDetector(
gocensorword.WithKeepPrefixChar(),
)
word := "bitch"
detector.KeepPrefixChar = true
resultString, err := detector.CensorWord(word)
if err != nil {
panic(err)
}
require.Equal(t, resultString, "b****")
}
func TestBadWordFirstAndLastLetterKept(t *testing.T) {
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
gocensorword.WithKeepPrefixChar(),
gocensorword.WithKeepSuffixChar(),
)
word := "bitch"
resultString, err := detector.CensorWord(word)
if err != nil {
panic(err)
}
require.Equal(t, resultString, "b***h")
}
func TestBadWordEmptyList(t *testing.T) {
var detector = gocensorword.NewDetector(
gocensorword.WithCustomCensorList(nil),
)
word := "bitch"
_, err := detector.CensorWord(word)
require.NotNil(t, err)
}
func TestBadFullLength(t *testing.T) {
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
gocensorword.WithKeepPrefixChar(),
gocensorword.WithKeepSuffixChar(),
)
word := "fuck post content asshole suck sucker"
resultString, _ := detector.CensorWord(word)
require.Equal(t, resultString, "f**k post content a*****e s**k s****r")
}
func TestBadWithCustomReplacePattern(t *testing.T) {
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
gocensorword.WithKeepPrefixChar(),
gocensorword.WithKeepSuffixChar(),
gocensorword.WithReplaceCheckPattern(`\b%s\b`),
)
detector.ReplaceCheckPattern = `\b%s\b`
word := "pass ass fucker sucker"
resultString, _ := detector.CensorWord(word)
fmt.Println("resulr----", resultString)
require.Equal(t, resultString, "pass a*s f****r s****r")
}