forked from go-passwd/validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimilarity_test.go
45 lines (37 loc) · 1.39 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
42
43
44
45
package validator_test
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/go-passwd/validator"
)
func TestRatio(t *testing.T) {
r := validator.Ratio("abc", "abc")
assert.InEpsilon(t, 1.0, r, 1e-9) // Allow a small margin for floating-point precision
r = validator.Ratio("abc", "def")
assert.InEpsilon(t, 0.0, r, 1e-9) // Allow a small margin for floating-point precision
r = validator.Ratio("abcd", "bcde")
assert.InEpsilon(t, 0.75, r, 1e-9) // Allow a small margin for floating-point precision
}
func ExampleSimilarity() {
passwordValidator := validator.Similarity([]string{"username", "[email protected]"}, nil, nil)
fmt.Println(passwordValidator("username"))
fmt.Println(passwordValidator("example"))
similarity := 0.5
passwordValidator = validator.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 := validator.Similarity([]string{"username", "[email protected]"}, nil, err)
fmt.Println(passwordValidator("username"))
// Output:
// custom error message
}