Skip to content

Commit

Permalink
Test and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
chuigda committed Oct 13, 2021
1 parent 9498112 commit b45cda6
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 144 deletions.
82 changes: 1 addition & 81 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,4 @@
Minimal JavaScript type assertions

## Basic usage
```javascript
// or you use import if use ESM
const { typeAssert } = require('./typeAssert.cjs')

// simple types
typeAssert(1, 'number')
typeAssert('2', 'string')

// object
typeAssert({ x: 1, y: 3 }, 'object')
typeAssert({ x: 1, y: 3 }, {})

// array
typeAssert([1, 2, 3], 'Array')
typeAssert([1, 2, 3], [])

// object fields
typeAssert({
x: 1,
y: '2'
}, {
x: 'number',
y: 'string'
})

// array elements
typeAssert([1, 2, 3], ['number'])

// "sum" types
const assertion = 'string'.sumWith('number')
typeAssert('abc', assertion)
typeAssert(123, assertion)

// nullable types
const assertion = { x: 'number', y: 'function' }.orNull()
typeAssert({
x: 144,
y: () => {}
}, assertion)
typeAssert(null, assertion)

// nullable shorthand for simple types
typeAssert(114, 'number?')
typeAssert(null, 'number?')

// nested situation
const assertion = {
a: 'number',
b: 'string',
c: [
{
x: 'function',
y: 'function'
}.sumWith({
x: [],
y: {}
}).orNull()
]
}

typeAssert({
a: 114,
b: '514',
c: [
null,
{
x: [1, 2, 3],
y: {
z: '4'
}
},
{
x: console.log,
y: Array.prototype.push
}
]
}, assertion)

// chained situation
typeAssert(5, 'number'.chainWith(x => x > 0 ? true : 'no negative numbers'))
```
See `test.cjs`
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "typeassert",
"version": "0.3.0",
"description": "A light weight type assertion \"framework\"?",
"type": "module",
"main": "",
"scripts": {
"test-esm": "node test.js",
"test-cjs": "node test.cjs"
},
"repository": {
"type": "git",
"url": "git+https://github.com/chuigda/typeAssert.git"
},
"keywords": [
"type",
"assertion",
"commonjs",
"esmodule"
],
"author": "Chuigda",
"license": "MIT",
"bugs": {
"url": "https://github.com/chuigda/typeAssert/issues"
},
"homepage": "https://github.com/chuigda/typeAssert#readme"
}
97 changes: 97 additions & 0 deletions test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const { typeAssert, enableChainAPI, preventErrTrace } = require('./typeAssert.cjs')

preventErrTrace(true)
enableChainAPI()

typeAssert(1, 'number')
typeAssert('2', 'string')

const expectFailure = testedCode => {
try {
testedCode()
console.error('expected failure not caught')
process.exit(-1)
} catch (e) {
console.info('expected failure caught:', e)
}
}

expectFailure(() => typeAssert(2, 'string'))

typeAssert({ x: 1, y: '3' }, 'object')
typeAssert({ x: 1, y: '3' }, {})
typeAssert({ x: 1, y: '3' }, { x: 'number', y: 'string' })

// By this time, arrays are also considered a kind of 'object'.
// this may change in further editions
typeAssert([], 'object')
typeAssert([], {})

expectFailure(() => typeAssert([], { x: 'number' }))
expectFailure(() => typeAssert({ x: 1, y: '2' }, { x: 'number', y: 'number' }))

typeAssert([1, 2, 3], 'Array')
typeAssert([1, 2, 3], [])
typeAssert([1, 2, 3], ['number'])
typeAssert(['1', '2', '3'], ['string'])

expectFailure(() => typeAssert({}, []))
expectFailure(() => typeAssert({}, 'Array'))
expectFailure(() => typeAssert([1, 2, 3], ['string']))

const sumAssertion = 'string'.sumWith('number')
typeAssert('abc', sumAssertion)
typeAssert(123, sumAssertion)

expectFailure(() => typeAssert(() => 114514, sumAssertion))

const nullableAssertion = { x: 'number', y: 'function' }.orNull()
typeAssert({ x: 114, y: () => 514 }, nullableAssertion)
typeAssert(null, nullableAssertion)

// `undefined` is not considered a kind of `null` till now.
expectFailure(() => typeAssert(undefined, nullableAssertion))

// cannot nest nullable modification
expectFailure(() => 'number?'.orNull())
expectFailure(() => { return { x: 'string' }.orNull().orNull()} )

const compoundAssertion = {
a: 'number',
b: 'string',
c: [
{
x: 'function',
y: 'function'
}.sumWith({
x: [],
y: {}
}).orNull()
]
}

typeAssert({
a: 114,
b: '514',
c: [
null,
{
x: [1, 2, 3],
y: {
z: '4'
}
},
{
x: console.log,
y: Array.prototype.push
}
]
}, compoundAssertion)

typeAssert(5, 'number'.chainWith(x => x > 0 ? true : 'no negative numbers'))
expectFailure(() => typeAssert(-1, 'number'.chainWith(x => x > 0 ? true : 'no negative numbers')))

typeAssert(5, 'number'.assertValue(5))
expectFailure(() => typeAssert(5, 'number'.assertValue(114514)))

console.info('mission accomplished')
97 changes: 97 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { typeAssert, enableChainAPI, preventErrTrace } from './typeAssert.js'

preventErrTrace(true)
enableChainAPI()

typeAssert(1, 'number')
typeAssert('2', 'string')

const expectFailure = testedCode => {
try {
testedCode()
console.error('expected failure not caught')
process.exit(-1)
} catch (e) {
console.info('expected failure caught:', e)
}
}

expectFailure(() => typeAssert(2, 'string'))

typeAssert({ x: 1, y: '3' }, 'object')
typeAssert({ x: 1, y: '3' }, {})
typeAssert({ x: 1, y: '3' }, { x: 'number', y: 'string' })

// By this time, arrays are also considered a kind of 'object'.
// this may change in further editions
typeAssert([], 'object')
typeAssert([], {})

expectFailure(() => typeAssert([], { x: 'number' }))
expectFailure(() => typeAssert({ x: 1, y: '2' }, { x: 'number', y: 'number' }))

typeAssert([1, 2, 3], 'Array')
typeAssert([1, 2, 3], [])
typeAssert([1, 2, 3], ['number'])
typeAssert(['1', '2', '3'], ['string'])

expectFailure(() => typeAssert({}, []))
expectFailure(() => typeAssert({}, 'Array'))
expectFailure(() => typeAssert([1, 2, 3], ['string']))

const sumAssertion = 'string'.sumWith('number')
typeAssert('abc', sumAssertion)
typeAssert(123, sumAssertion)

expectFailure(() => typeAssert(() => 114514, sumAssertion))

const nullableAssertion = { x: 'number', y: 'function' }.orNull()
typeAssert({ x: 114, y: () => 514 }, nullableAssertion)
typeAssert(null, nullableAssertion)

// `undefined` is not considered a kind of `null` till now.
expectFailure(() => typeAssert(undefined, nullableAssertion))

// cannot nest nullable modification
expectFailure(() => 'number?'.orNull())
expectFailure(() => { return { x: 'string' }.orNull().orNull()} )

const compoundAssertion = {
a: 'number',
b: 'string',
c: [
{
x: 'function',
y: 'function'
}.sumWith({
x: [],
y: {}
}).orNull()
]
}

typeAssert({
a: 114,
b: '514',
c: [
null,
{
x: [1, 2, 3],
y: {
z: '4'
}
},
{
x: console.log,
y: Array.prototype.push
}
]
}, compoundAssertion)

typeAssert(5, 'number'.chainWith(x => x > 0 ? true : 'no negative numbers'))
expectFailure(() => typeAssert(-1, 'number'.chainWith(x => x > 0 ? true : 'no negative numbers')))

typeAssert(5, 'number'.assertValue(5))
expectFailure(() => typeAssert(5, 'number'.assertValue(114514)))

console.info('mission accomplished')
Loading

0 comments on commit b45cda6

Please sign in to comment.