-
Notifications
You must be signed in to change notification settings - Fork 3
/
number_test.go
74 lines (63 loc) · 2.03 KB
/
number_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
package validator
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNumber_ValidateValue_ValueNotNumber(t *testing.T) {
ctx := context.Background()
err := NewNumber(0, 0).ValidateValue(ctx, "abc")
assert.Error(t, err)
assert.Equal(t, "Value must be a number.", err.Error())
}
func TestNumber_ValidateValue_ValueNotNumberCustomError(t *testing.T) {
ctx := context.Background()
err := NewNumber(0, 0).
WithNotNumberMessage("test").
ValidateValue(ctx, "abc")
assert.Error(t, err)
assert.Equal(t, "test.", err.Error())
}
func TestNumber_ValidateValue_ValueLessThanMin(t *testing.T) {
ctx := context.Background()
err := NewNumber(1, 10).ValidateValue(ctx, 0)
assert.Error(t, err)
assert.Equal(t, "Value must be no less than {min}.", err.Error())
}
func TestNumber_ValidateValue_ValueLessThanMinCustomError(t *testing.T) {
ctx := context.Background()
err := NewNumber(1, 10).WithTooSmallMessage("test {min}").
ValidateValue(ctx, 0)
assert.Error(t, err)
assert.Equal(t, "test {min}.", err.Error())
}
func TestNumber_ValidateValue_ValueGreatThanMax(t *testing.T) {
ctx := context.Background()
err := NewNumber(1, 10).ValidateValue(ctx, 11)
assert.Error(t, err)
assert.Equal(t, "Value must be no greater than {max}.", err.Error())
}
func TestNumber_ValidateValue_ValueGreatThanMaxCustomError(t *testing.T) {
ctx := context.Background()
err := NewNumber(1, 10).
WithTooBigMessage("test {max}").
ValidateValue(ctx, 11)
assert.Error(t, err)
assert.Equal(t, "test {max}.", err.Error())
}
func TestNumber_ValidateValue_ErrTypeOfResultSet(t *testing.T) {
ctx := context.Background()
err := NewNumber(1, 10).ValidateValue(ctx, 11)
assert.Error(t, err)
assert.IsType(t, Result{}, err)
}
func TestNumber_ValidateValue_Successfully(t *testing.T) {
ctx := context.Background()
err := NewNumber(1, 10).ValidateValue(ctx, 2)
assert.NoError(t, err)
}
func TestNumber_ValidateValue_SuccessfullyZero(t *testing.T) {
ctx := context.Background()
err := NewNumber(0, 0).ValidateValue(ctx, 0)
assert.NoError(t, err)
}