Skip to content

Commit

Permalink
feat: number parsers
Browse files Browse the repository at this point in the history
- Added `hex`, `octal`, `binary`, and `whole` parsers.
- Renamed `int` to `integer`.
- Added and fixed tests.
  • Loading branch information
norskeld committed Jul 29, 2022
1 parent 638ed56 commit c22af49
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 183 deletions.
11 changes: 7 additions & 4 deletions src/__tests__/@helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ export const expectedCombinators = [
'chainl',
'choice',
'error',
'sepBy',
'many',
'many1',
'map',
'mapTo',
'optional',
'sepBy',
'sepBy',
'sepBy1',
'sequence',
'takeLeft',
Expand All @@ -87,21 +87,24 @@ export const expectedCombinators = [

export const expectedParsers = [
'any',
'binary',
'defer',
'eof',
'eol',
'float',
'int',
'uint',
'hexadecimal',
'integer',
'letter',
'letters',
'noneOf',
'nothing',
'octal',
'oneOf',
'regexp',
'rest',
'run',
'string',
'ustring',
'whitespace'
'whitespace',
'whole'
] as const
43 changes: 0 additions & 43 deletions src/__tests__/parsers/float.spec.ts

This file was deleted.

66 changes: 0 additions & 66 deletions src/__tests__/parsers/integer.spec.ts

This file was deleted.

74 changes: 74 additions & 0 deletions src/__tests__/parsers/numbers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { hexadecimal, binary, octal, whole, integer, float } from '../../parsers/numbers'
import { describe, testFailure, testSuccess } from '../@helpers'

describe('hexadecimal', (it) => {
it('should succeed if given a hexadecimal number', () => {
const tcases = ['0x1F', '0X1F', '0x1f', '0X1f']
tcases.forEach((tcase) => testSuccess(tcase, tcase, hexadecimal()))
})

it('should fail if given a non-hexadecimal number', () => {
const tcases = ['', 'zero', '0', '-42', '0o42', '0b10', '0x', '0xXB']
tcases.forEach((tcase) => testFailure(tcase, hexadecimal()))
})
})

describe('binary', (it) => {
it('should succeed if given a binary number', () => {
const tcases = ['0b10', '0B10']
tcases.forEach((tcase) => testSuccess(tcase, tcase, binary()))
})

it('should fail if given a non-binary number', () => {
const tcases = ['', 'zero', '0', '-42', '0o42', '0b', '0xXB', '0bff']
tcases.forEach((tcase) => testFailure(tcase, binary()))
})
})

describe('octal', (it) => {
it('should succeed if given an octal number', () => {
const tcases = ['0o42', '0O42']
tcases.forEach((tcase) => testSuccess(tcase, tcase, octal()))
})

it('should fail if given a non-octal number', () => {
const tcases = ['', 'zero', '0', '-42', '0x42', '0b11', '0o', '0off']
tcases.forEach((tcase) => testFailure(tcase, octal()))
})
})

describe('whole', (it) => {
it('should succeed if given a whole number', () => {
const tcases = ['0', '1', '42', '1000']
tcases.forEach((tcase) => testSuccess(tcase, tcase, whole()))
})

it('should fail if given a non-whole number', () => {
const tcases = ['', 'zero', '-0', '-42']
tcases.forEach((tcase) => testFailure(tcase, whole()))
})
})

describe('integer', (it) => {
it('should succeed if given an integer number', () => {
const tcases = ['0', '1', '-1', '42', '-42']
tcases.forEach((tcase) => testSuccess(tcase, tcase, integer()))
})

it('should fail if given a non-integer number', () => {
const tcases = ['', 'zero']
tcases.forEach((tcase) => testFailure(tcase, integer()))
})
})

describe('float', (it) => {
it('should succeed if given an float number', () => {
const tcases = ['0.25', '4.20', '-42.0']
tcases.forEach((tcase) => testSuccess(tcase, tcase, float()))
})

it('should fail if given a non-float number', () => {
const tcases = ['', 'zero', '0xFF', '0b10', '0o42']
tcases.forEach((tcase) => testFailure(tcase, float()))
})
})
3 changes: 1 addition & 2 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ export * from './parsers/any'
export * from './parsers/defer'
export * from './parsers/eof'
export * from './parsers/eol'
export * from './parsers/float'
export * from './parsers/integer'
export * from './parsers/letter'
export * from './parsers/noneOf'
export * from './parsers/nothing'
export * from './parsers/numbers'
export * from './parsers/oneOf'
export * from './parsers/regexp'
export * from './parsers/rest'
Expand Down
32 changes: 0 additions & 32 deletions src/parsers/float.ts

This file was deleted.

36 changes: 0 additions & 36 deletions src/parsers/integer.ts

This file was deleted.

Loading

0 comments on commit c22af49

Please sign in to comment.