@@ -11,6 +11,8 @@ describe('number', () => {
1111 seededTests ( faker , 'number' , ( t ) => {
1212 t . describeEach (
1313 'int' ,
14+ 'binary' ,
15+ 'octal' ,
1416 'hex'
1517 ) ( ( t ) => {
1618 t . it ( 'noArgs' )
@@ -240,6 +242,72 @@ describe('number', () => {
240242 } ) ;
241243 } ) ;
242244
245+ describe ( 'binary' , ( ) => {
246+ it ( 'generates single binary character when no additional argument was provided' , ( ) => {
247+ const binary = faker . number . binary ( ) ;
248+ expect ( binary ) . toBeTypeOf ( 'string' ) ;
249+ expect ( binary ) . toHaveLength ( 1 ) ;
250+ expect ( binary ) . toMatch ( / ^ [ 0 1 ] $ / ) ;
251+ } ) ;
252+
253+ it ( 'generates a random binary string with a custom max value' , ( ) => {
254+ const binary = faker . number . binary ( 5 ) ;
255+ const binaryNum = parseInt ( binary , 2 ) ;
256+ expect ( binaryNum ) . toBeLessThanOrEqual ( 5 ) ;
257+ expect ( binary ) . toMatch ( / ^ [ 0 1 ] + $ / ) ;
258+ } ) ;
259+
260+ it ( 'generates a random binary in a specific range' , ( ) => {
261+ const binary = faker . number . binary ( { min : 15 , max : 255 } ) ;
262+
263+ const binaryNum = parseInt ( binary , 2 ) ;
264+ expect ( binaryNum ) . toBeLessThanOrEqual ( 255 ) ;
265+ expect ( binaryNum ) . greaterThanOrEqual ( 15 ) ;
266+ } ) ;
267+
268+ it ( 'should throw when min > max' , ( ) => {
269+ const min = 10 ;
270+ const max = 9 ;
271+
272+ expect ( ( ) => {
273+ faker . number . binary ( { min, max } ) ;
274+ } ) . toThrowError ( `Max ${ max } should be greater than min ${ min } .` ) ;
275+ } ) ;
276+ } ) ;
277+
278+ describe ( 'octal' , ( ) => {
279+ it ( 'generates single octal character when no additional argument was provided' , ( ) => {
280+ const octal = faker . number . octal ( ) ;
281+ expect ( octal ) . toBeTypeOf ( 'string' ) ;
282+ expect ( octal ) . toHaveLength ( 1 ) ;
283+ expect ( octal ) . toMatch ( / ^ [ 0 - 7 ] $ / ) ;
284+ } ) ;
285+
286+ it ( 'generates a random octal string with a custom max value' , ( ) => {
287+ const octal = faker . number . octal ( 5 ) ;
288+ const octalNum = parseInt ( octal , 8 ) ;
289+ expect ( octalNum ) . toBeLessThanOrEqual ( 5 ) ;
290+ expect ( octal ) . toMatch ( / ^ [ 0 - 7 ] + $ / ) ;
291+ } ) ;
292+
293+ it ( 'generates a random octal in a specific range' , ( ) => {
294+ const octal = faker . number . octal ( { min : 15 , max : 255 } ) ;
295+
296+ const octalNum = parseInt ( octal , 8 ) ;
297+ expect ( octalNum ) . toBeLessThanOrEqual ( 255 ) ;
298+ expect ( octalNum ) . greaterThanOrEqual ( 15 ) ;
299+ } ) ;
300+
301+ it ( 'should throw when min > max' , ( ) => {
302+ const min = 10 ;
303+ const max = 9 ;
304+
305+ expect ( ( ) => {
306+ faker . number . octal ( { min, max } ) ;
307+ } ) . toThrowError ( `Max ${ max } should be greater than min ${ min } .` ) ;
308+ } ) ;
309+ } ) ;
310+
243311 describe ( 'hex' , ( ) => {
244312 it ( 'generates single hex character when no additional argument was provided' , ( ) => {
245313 const hex = faker . number . hex ( ) ;
0 commit comments