@@ -61,6 +61,82 @@ describe('number', () => {
6161 expect ( actual ) . lessThanOrEqual ( Number . MAX_SAFE_INTEGER ) ;
6262 } ) ;
6363
64+ it ( 'should return an even integer' , ( ) => {
65+ const actual = faker . number . int ( { multipleOf : 2 } ) ;
66+
67+ expect ( actual ) . toBeTypeOf ( 'number' ) ;
68+ expect ( actual ) . toSatisfy ( Number . isInteger ) ;
69+ expect ( actual ) . toSatisfy ( ( x : number ) => x % 2 === 0 ) ;
70+ expect ( actual ) . toBeGreaterThanOrEqual ( 0 ) ;
71+ expect ( actual ) . toBeLessThanOrEqual ( Number . MAX_SAFE_INTEGER ) ;
72+ } ) ;
73+
74+ it ( 'provides numbers with a given multipleOf of 10 with exclusive ends' , ( ) => {
75+ const results = [
76+ ...new Set (
77+ Array . from ( { length : 100 } , ( ) =>
78+ faker . number . int ( {
79+ min : 12 ,
80+ max : 37 ,
81+ multipleOf : 10 ,
82+ } )
83+ )
84+ ) ,
85+ ] . sort ( ) ;
86+ expect ( results ) . toEqual ( [ 20 , 30 ] ) ;
87+ } ) ;
88+
89+ it ( 'provides numbers with a given multipleOf of 10 with inclusive ends' , ( ) => {
90+ const results = [
91+ ...new Set (
92+ Array . from ( { length : 100 } , ( ) =>
93+ faker . number . int ( {
94+ min : 10 ,
95+ max : 50 ,
96+ multipleOf : 10 ,
97+ } )
98+ )
99+ ) ,
100+ ] . sort ( ) ;
101+ expect ( results ) . toEqual ( [ 10 , 20 , 30 , 40 , 50 ] ) ;
102+ } ) ;
103+
104+ it ( 'throws for float multipleOf' , ( ) => {
105+ const input = {
106+ min : 0 ,
107+ max : 10 ,
108+ multipleOf : 0.1 ,
109+ } ;
110+
111+ expect ( ( ) => faker . number . int ( input ) ) . toThrow (
112+ new FakerError ( 'multipleOf should be an integer.' )
113+ ) ;
114+ } ) ;
115+
116+ it ( 'throws for negative multipleOf' , ( ) => {
117+ const input = {
118+ min : - 10 ,
119+ max : 10 ,
120+ multipleOf : - 1 ,
121+ } ;
122+
123+ expect ( ( ) => faker . number . int ( input ) ) . toThrow (
124+ new FakerError ( 'multipleOf should be greater than 0.' )
125+ ) ;
126+ } ) ;
127+
128+ it ( 'throws for impossible multipleOf' , ( ) => {
129+ const input = {
130+ min : 11 ,
131+ max : 19 ,
132+ multipleOf : 10 ,
133+ } ;
134+
135+ expect ( ( ) => faker . number . int ( input ) ) . toThrow (
136+ new FakerError ( 'No suitable integer value between 11 and 19 found.' )
137+ ) ;
138+ } ) ;
139+
64140 it ( 'should return a random number given a maximum value as Number' , ( ) => {
65141 const actual = faker . number . int ( 10 ) ;
66142
@@ -167,7 +243,7 @@ describe('number', () => {
167243 expect ( ( ) => {
168244 faker . number . int ( { min : 2.1 , max : 2.9 } ) ;
169245 } ) . toThrow (
170- new FakerError ( `No integer value between 2.1 and 2.9 found.` )
246+ new FakerError ( `No suitable integer value between 2.1 and 2.9 found.` )
171247 ) ;
172248 } ) ;
173249 } ) ;
@@ -368,7 +444,7 @@ describe('number', () => {
368444 expect ( ( ) => {
369445 faker . number . binary ( { min : 2.1 , max : 2.9 } ) ;
370446 } ) . toThrow (
371- new FakerError ( `No integer value between 2.1 and 2.9 found.` )
447+ new FakerError ( `No suitable integer value between 2.1 and 2.9 found.` )
372448 ) ;
373449 } ) ;
374450 } ) ;
@@ -419,7 +495,7 @@ describe('number', () => {
419495 expect ( ( ) => {
420496 faker . number . octal ( { min : 2.1 , max : 2.9 } ) ;
421497 } ) . toThrow (
422- new FakerError ( `No integer value between 2.1 and 2.9 found.` )
498+ new FakerError ( `No suitable integer value between 2.1 and 2.9 found.` )
423499 ) ;
424500 } ) ;
425501 } ) ;
@@ -467,7 +543,7 @@ describe('number', () => {
467543 expect ( ( ) => {
468544 faker . number . hex ( { min : 2.1 , max : 2.9 } ) ;
469545 } ) . toThrow (
470- new FakerError ( `No integer value between 2.1 and 2.9 found.` )
546+ new FakerError ( `No suitable integer value between 2.1 and 2.9 found.` )
471547 ) ;
472548 } ) ;
473549 } ) ;
0 commit comments