-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimilarity_test.go
More file actions
51 lines (42 loc) · 1.49 KB
/
similarity_test.go
File metadata and controls
51 lines (42 loc) · 1.49 KB
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
package validator_test
import (
"errors"
"fmt"
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/go-passwd/validator"
)
func TestRatio_bothEmpty(t *testing.T) {
r := validator.Ratio("", "")
assert.False(t, math.IsNaN(r))
}
func TestRatio(t *testing.T) {
r := validator.Ratio("abc", "abc")
assert.InDelta(t, 1.0, r, 1e-9) // Allow a small margin for floating-point precision
r = validator.Ratio("abc", "def")
assert.InDelta(t, 0.0, r, 1e-9) // Allow a small margin for floating-point precision
r = validator.Ratio("abcd", "bcde")
assert.InDelta(t, 0.75, r, 1e-9) // Allow a small margin for floating-point precision
}
func ExampleSimilarity() {
passwordValidator := validator.Similarity([]string{"username", "username@example.com"}, nil, nil)
fmt.Println(passwordValidator("username"))
fmt.Println(passwordValidator("example"))
similarity := 0.5
passwordValidator = validator.Similarity([]string{"username", "username@example.com"}, &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 username@example.com
}
func ExampleSimilarity_customError() {
err := errors.New("custom error message")
passwordValidator := validator.Similarity([]string{"username", "username@example.com"}, nil, err)
fmt.Println(passwordValidator("username"))
// Output:
// custom error message
}