-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimilarity_test.go
41 lines (36 loc) · 1.1 KB
/
similarity_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
package validator
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRatio(t *testing.T) {
r := ratio("abc", "abc")
assert.Equal(t, 1.0, r)
r = ratio("abc", "def")
assert.Equal(t, 0.0, r)
r = ratio("abcd", "bcde")
assert.Equal(t, 0.75, r)
}
func ExampleSimilarity() {
passwordValidator := Similarity([]string{"username", "[email protected]"}, nil, nil)
fmt.Println(passwordValidator("username"))
fmt.Println(passwordValidator("example"))
similarity := 0.5
passwordValidator = Similarity([]string{"username", "[email protected]"}, &similarity, nil)
fmt.Println(passwordValidator("username"))
fmt.Println(passwordValidator("examplecom"))
// Output:
// The password is too similar to the username
// <nil>
// The password is too similar to the username
// The password is too similar to the [email protected]
}
func ExampleSimilarity_customError() {
err := errors.New("custom error message")
passwordValidator := Similarity([]string{"username", "[email protected]"}, nil, err)
fmt.Println(passwordValidator("username"))
// Output:
// custom error message
}