-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathint.go
148 lines (120 loc) · 4.04 KB
/
int.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package assert
import (
"strconv"
"strings"
)
type Int struct {
logFacade *logFacade
actual int
}
func (a *Int) IsZero() *Int {
return a.isTrue(a.actual == 0,
"Expected zero, but was <%d>.", a.actual)
}
func (a *Int) IsNonZero() *Int {
return a.isTrue(a.actual != 0,
"Expected nonzero, but was zero.")
}
func (a *Int) IsEqualTo(expected int) *Int {
return a.isTrue(a.actual == expected,
"Expected integer equal to <%d>, but was <%d>.", expected, a.actual)
}
func (a *Int) IsNotEqualTo(unexpected int) *Int {
return a.isTrue(a.actual != unexpected,
"Expected integer not equal to <%d>, but was equal.", unexpected)
}
func (a *Int) IsPositive() *Int {
return a.isTrue(a.actual > 0,
"Expected positive integer, but was <%d>.", a.actual)
}
func (a *Int) IsNonPositive() *Int {
return a.isTrue(a.actual <= 0,
"Expected nonpositive integer, but was <%d>.", a.actual)
}
func (a *Int) IsNegative() *Int {
return a.isTrue(a.actual < 0,
"Expected negative integer, but was <%d>.", a.actual)
}
func (a *Int) IsNonNegative() *Int {
return a.isTrue(a.actual >= 0,
"Expected nonnegative integer, but was <%d>.", a.actual)
}
func (a *Int) IsGreaterThan(less int) *Int {
return a.isTrue(a.actual > less,
"Expected integer greater than <%d>, but was <%d>.", less, a.actual)
}
func (a *Int) IsGreaterOrEqualTo(lessOrEqual int) *Int {
return a.isTrue(a.actual >= lessOrEqual,
"Expected integer greater or equal to <%d>, but was <%d>.", lessOrEqual, a.actual)
}
func (a *Int) IsLessThan(greater int) *Int {
return a.isTrue(a.actual < greater,
"Expected integer lesser than <%d>, but was <%d>.", greater, a.actual)
}
func (a *Int) IsLessOrEqualTo(greaterOrEqual int) *Int {
return a.isTrue(a.actual <= greaterOrEqual,
"Expected integer lesser or equal to <%d>, but was <%d>.", greaterOrEqual, a.actual)
}
func (a *Int) IsBetween(lowerBound, upperBound int) *Int {
return a.isTrue(lowerBound <= a.actual && a.actual <= upperBound,
"Expected integer to be between <%d, %d>, but was <%d>.", lowerBound, upperBound, a.actual)
}
func (a *Int) IsNotBetween(lowerBound, upperBound int) *Int {
return a.isTrue(a.actual < lowerBound || a.actual > upperBound,
"Expected integer not to be between <%d, %d>, but was <%d>.", lowerBound, upperBound, a.actual)
}
func (a *Int) IsIn(elements ...int) *Int {
return a.isTrue(a.isIn(elements...),
"Expected integer to be in (%s), but was <%d>.", strings.Join(strSlice(elements...), ", "), a.actual)
}
func (a *Int) IsNotIn(elements ...int) *Int {
return a.isTrue(!a.isIn(elements...),
"Expected integer not to be in (%s), but was <%d>.", strings.Join(strSlice(elements...), ", "), a.actual)
}
func (a *Int) IsEven() *Int {
return a.isTrue(a.actual%2 == 0,
"Expected integer to be even, but was <%d>.", a.actual)
}
func (a *Int) IsOdd() *Int {
return a.isTrue(a.actual%2 != 0,
"Expected integer to be odd, but was <%d>.", a.actual)
}
func (a *Int) IsDivisibleBy(divisor int) *Int {
return a.isTrue(a.actual%divisor == 0,
"Expected integer to be divisible by <%d>, but was <%d>.", divisor, a.actual)
}
func (a *Int) IsNotDivisibleBy(divisor int) *Int {
return a.isTrue(a.actual%divisor != 0,
"Expected integer not to be divisible by <%d>, but was <%d>.", divisor, a.actual)
}
func (a *Int) IsPrime() *Int {
return a.isTrue(isPrime(a.actual),
"Expected integer to be prime, but was <%d>.", a.actual)
}
func (a *Int) IsNotPrime() *Int {
return a.isTrue(!isPrime(a.actual),
"Expected integer not to be prime, but was <%d>.", a.actual)
}
func (a *Int) IsAnswerToTheUltimateQuestionOfLife() *Int {
return a.isTrue(a.actual == 42,
"Expected answer to the ultimate question of life, but was <%d>.", a.actual)
}
func (a *Int) isTrue(condition bool, format string, args ...interface{}) *Int {
logIfFalse(a.logFacade, condition, format, args...)
return a
}
func (a *Int) isIn(elements ...int) bool {
for _, e := range elements {
if a.actual == e {
return true
}
}
return false
}
func strSlice(values ...int) []string {
ret := make([]string, len(values))
for i, v := range values {
ret[i] = strconv.Itoa(v)
}
return ret
}