Skip to content

Commit

Permalink
Add tests and documentation for bigint codec
Browse files Browse the repository at this point in the history
  • Loading branch information
mixedCase authored and gcanti committed Feb 10, 2020
1 parent e7b23a1 commit 8b0b214
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
36 changes: 36 additions & 0 deletions docs/modules/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Added in v1.0.0
- [~~AnyC~~ (interface)](#anyc-interface)
- [AnyProps (interface)](#anyprops-interface)
- [ArrayC (interface)](#arrayc-interface)
- [BigIntC (interface)](#bigintc-interface)
- [BooleanC (interface)](#booleanc-interface)
- [Brand (interface)](#brand-interface)
- [BrandC (interface)](#brandc-interface)
Expand Down Expand Up @@ -88,6 +89,7 @@ Added in v1.0.0
- [AnyDictionaryType (class)](#anydictionarytype-class)
- [~~AnyType~~ (class)](#anytype-class)
- [ArrayType (class)](#arraytype-class)
- [BigIntType (class)](#biginttype-class)
- [BooleanType (class)](#booleantype-class)
- [DictionaryType (class)](#dictionarytype-class)
- [ExactType (class)](#exacttype-class)
Expand Down Expand Up @@ -123,6 +125,7 @@ Added in v1.0.0
- [UnknownRecord](#unknownrecord)
- [appendContext](#appendcontext)
- [array](#array)
- [bigint](#bigint)
- [boolean](#boolean)
- [brand](#brand)
- [exact](#exact)
Expand Down Expand Up @@ -212,6 +215,16 @@ export interface ArrayC<C extends Mixed> extends ArrayType<C, Array<TypeOf<C>>,

Added in v1.5.3

# BigIntC (interface)

**Signature**

```ts
export interface BigIntC extends BigIntType {}
```

Added in v2.1.0

# BooleanC (interface)

**Signature**
Expand Down Expand Up @@ -1093,6 +1106,19 @@ export class ArrayType<C, A, O, I> {

Added in v1.0.0

# BigIntType (class)

**Signature**

```ts
export class BigIntType {
constructor() { ... }
...
}
```

Added in v2.1.0

# BooleanType (class)

**Signature**
Expand Down Expand Up @@ -1642,6 +1668,16 @@ export const array = <C extends Mixed>(codec: C, name: string = `Array<${codec.n
Added in v1.0.0
# bigint
**Signature**
```ts
export const bigint: BigIntC = ...
```
Added in v2.1.0
# boolean
**Signature**
Expand Down
23 changes: 23 additions & 0 deletions test/default-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,29 @@ describe('boolean', () => {
})
})

describe('bigint', () => {
const T = t.bigint
it('should decode bigint values', () => {
assertSuccess(T.decode(BigInt(0)))
assertSuccess(T.decode(BigInt(15)))
const decodedBigNumber = T.decode(BigInt(Number.MAX_SAFE_INTEGER) + BigInt(4))
assertSuccess(decodedBigNumber)
if (decodedBigNumber._tag === 'Right') {
assert.equal(decodedBigNumber.right.toString(), '9007199254740995')
}
})

it('should not decode non-bigint values', () => {
assertFailure(T, true, ['Invalid value true supplied to : bigint'])
assertFailure(T, 'test', ['Invalid value "test" supplied to : bigint'])
assertFailure(T, 123, ['Invalid value 123 supplied to : bigint'])
assertFailure(T, {}, ['Invalid value {} supplied to : bigint'])
assertFailure(T, [], ['Invalid value [] supplied to : bigint'])
assertFailure(T, null, ['Invalid value null supplied to : bigint'])
assertFailure(T, undefined, ['Invalid value undefined supplied to : bigint'])
})
})

describe('Integer', () => {
it('should validate integers', () => {
// tslint:disable-next-line: deprecation
Expand Down
1 change: 1 addition & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function assertSuccess<T>(result: t.Validation<T>, expected?: T): void {
if (expected !== undefined) {
assert.deepStrictEqual(a, expected)
}
return true
}
)
)
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"stripInternal": true,
"lib": ["es2015"]
"lib": ["es2015", "ESNext.BigInt"]
},
"include": ["./src/**/*"]
}

0 comments on commit 8b0b214

Please sign in to comment.