-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
87 lines (76 loc) · 1.44 KB
/
utils.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
package validators
import (
"errors"
"fmt"
"strings"
"testing"
)
var (
err error
abortValidation bool
jsonValue string
errCustomMessage = "error"
errCustomMessage2 = "error2"
errCustom = errors.New(errCustomMessage)
errCustom2 = errors.New(errCustomMessage2)
)
func setError(
message string,
) string {
return fmt.Sprintf(
message,
getArgs()...,
)
}
func getArgs() []any {
return []any{
func() any {
if err == nil {
return nil
}
return fmt.Sprintf("\"%s\"", err.Error())
}(),
abortValidation,
}
}
const (
validateTestDefault = "ValidateTestDefault"
validateTestCustom = "ValidateTestCustom"
)
func initValidateTest(
t *testing.T,
validateTestType string,
validateName string,
structValue any,
) {
err = Validate(structValue, jsonValue)
isValidateTestCustom := validateTestType == validateTestCustom
errorMessage := "\"[error message]\""
if isValidateTestCustom {
errorMessage = "\"error\""
}
if err == nil || isValidateTestCustom && !strings.Contains(err.Error(), errCustomMessage) {
t.Errorf(
fmt.Sprintf(
"%s - received: nil; expected: %s",
validateName,
errorMessage,
),
)
}
}
func convertToFloat64(value any) float64 {
switch value := value.(type) {
case int:
return float64(value)
case int32:
return float64(value)
case int64:
return float64(value)
case float32:
return float64(value)
case float64:
return value
}
return 0.0
}