File tree Expand file tree Collapse file tree 3 files changed +36
-2
lines changed
main/java/com/github/javafaker
test/java/com/github/javafaker Expand file tree Collapse file tree 3 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -27,7 +27,10 @@ public int randomDigitNotZero() {
27
27
* @see Number#numberBetween(long, long)
28
28
*/
29
29
public int numberBetween (int min , int max ) {
30
- return decimalBetween (min ,max ).setScale (0 , BigDecimal .ROUND_HALF_DOWN ).intValue ();
30
+ if (min == max ) return min ;
31
+
32
+ int value = decimalBetween (min ,max ).setScale (0 , BigDecimal .ROUND_HALF_DOWN ).intValue ();
33
+ return value == max ? value - 1 : value ;
31
34
}
32
35
33
36
/**
@@ -39,7 +42,10 @@ public int numberBetween(int min, int max) {
39
42
* @param max exclusive (unless min == max)
40
43
*/
41
44
public long numberBetween (long min , long max ) {
42
- return decimalBetween (min ,max ).setScale (0 , BigDecimal .ROUND_HALF_DOWN ).longValue ();
45
+ if (min == max ) return min ;
46
+
47
+ long value = decimalBetween (min , max ).setScale (0 , BigDecimal .ROUND_HALF_DOWN ).longValue ();
48
+ return value == max ? value - 1 : value ;
43
49
}
44
50
45
51
/**
Original file line number Diff line number Diff line change 1
1
package com .github .javafaker ;
2
2
3
+ import com .github .javafaker .repeating .Repeat ;
3
4
import org .junit .Test ;
4
5
5
6
import java .util .Locale ;
@@ -133,6 +134,12 @@ public void expression() {
133
134
assertThat (faker .expression ("#{color.name}" ), matchesRegularExpression ("[a-z\\ s]+" ));
134
135
}
135
136
137
+ @ Test
138
+ @ Repeat (times = 100 )
139
+ public void numberBetweenRepeated () {
140
+ assertThat (faker .expression ("#{number.number_between '1','10'}" ), matchesRegularExpression ("[1-9]" ));
141
+ }
142
+
136
143
@ Test
137
144
public void regexifyShouldGenerateSameValueForFakerWithSameSeed () {
138
145
long seed = 1L ;
Original file line number Diff line number Diff line change 1
1
package com .github .javafaker ;
2
2
3
+ import com .github .javafaker .repeating .Repeat ;
3
4
import com .google .common .base .Function ;
4
5
import com .google .common .collect .Lists ;
5
6
import com .google .common .collect .Sets ;
@@ -120,6 +121,26 @@ public void testNumberBetween() {
120
121
assertThat (v1 , is (lessThan (980000000L )));
121
122
}
122
123
124
+ @ Test
125
+ @ Repeat (times = 100 )
126
+ public void testLongNumberBetweenRepeated () {
127
+ long low = 1 ;
128
+ long high = 10 ;
129
+ long v = faker .number ().numberBetween (low , high );
130
+ assertThat (v , is (lessThan (high )));
131
+ assertThat (v , is (greaterThanOrEqualTo (low )));
132
+ }
133
+
134
+ @ Test
135
+ @ Repeat (times = 100 )
136
+ public void testIntNumberBetweenRepeated () {
137
+ int low = 1 ;
138
+ int high = 10 ;
139
+ int v = faker .number ().numberBetween (low , high );
140
+ assertThat (v , is (lessThan (high )));
141
+ assertThat (v , is (greaterThanOrEqualTo (low )));
142
+ }
143
+
123
144
@ Test
124
145
public void testNumberBetweenOneAndThree () {
125
146
Set <Integer > nums = Sets .newHashSet ();
You can’t perform that action at this time.
0 commit comments