This repository was archived by the owner on Aug 31, 2023. It is now read-only.
generated from esdmr/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add instanceof * Add equality functions * Add inequality operators
- Loading branch information
Showing
12 changed files
with
434 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@esdmr/assert": minor | ||
--- | ||
|
||
Add equality operators: `isEqual`, `isNotEqual`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@esdmr/assert": minor | ||
--- | ||
|
||
Add inequality operators: `isGreater`, `isGreaterOrEqual`, `isLess`, `isLessOrEqual`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@esdmr/assert": minor | ||
--- | ||
|
||
New function `isInstanceOf`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as assert from '@esdmr/assert'; | ||
|
||
export function parseConfig (json: unknown) { | ||
try { | ||
assert.isObject(json); | ||
assert.isNotNull(json); | ||
assert.isBoolean(json.private, 'property "private"'); | ||
assert.isEqual(json.private, false, 'property "private"'); | ||
assert.isNumber(json.major, 'property "major"'); | ||
assert.isGreater(json.major, 0, 'property "major"'); | ||
assert.isNumber(json.minor, 'property "minor"'); | ||
assert.isGreaterOrEqual(json.minor, 0, 'property "minor"'); | ||
} catch (error) { | ||
throw assert.wrap(error, 'Failed to parse config'); | ||
} | ||
|
||
return { | ||
major: json.major, | ||
minor: json.minor, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,93 @@ | ||
import { test } from 'tap'; | ||
import { testFormat } from './test-util/format.js'; | ||
import { assert, wrap } from '#src/assert.js'; | ||
import { testDetail, testFormat } from '#test/test-util/format.js'; | ||
import * as assert from '#src/assert.js'; | ||
import { AssertionError, WrappedError } from '#src/errors.js'; | ||
import { DEFAULT_MESSAGE } from '#src/messages.js'; | ||
import * as messages from '#src/messages.js'; | ||
import { format } from '#src/utils.js'; | ||
|
||
await test('assert', async (t) => { | ||
t.doesNotThrow( | ||
() => { | ||
assert(true, 'This should not throw'); | ||
assert.assert(true, 'This should not throw'); | ||
}, | ||
'expected to not throw if condition is true', | ||
); | ||
|
||
t.throws( | ||
() => { | ||
assert(false); | ||
assert.assert(false); | ||
}, | ||
new AssertionError(DEFAULT_MESSAGE), | ||
new AssertionError(messages.DEFAULT_MESSAGE), | ||
'expected to throw the default message if condition is false', | ||
); | ||
|
||
await testFormat(t, (...args) => { | ||
assert(false, ...args); | ||
assert.assert(false, ...args); | ||
}, (message: string) => new AssertionError(message)); | ||
}); | ||
|
||
await test('wrap', async (t) => { | ||
t.strictSame(wrap('value'), wrap('value', DEFAULT_MESSAGE), | ||
t.strictSame(assert.wrap('value'), assert.wrap('value', messages.DEFAULT_MESSAGE), | ||
'expected to use the default message if not given one'); | ||
|
||
await testFormat(t, (...args) => { | ||
throw wrap('value', ...args); | ||
throw assert.wrap('value', ...args); | ||
}, (message: string) => new WrappedError(message, 'value')); | ||
}); | ||
|
||
await test('isEqual', async (t) => { | ||
t.doesNotThrow( | ||
() => { | ||
assert.isEqual(0, 0); | ||
}, | ||
'expected not to throw for equal values', | ||
); | ||
|
||
t.throws( | ||
() => { | ||
assert.isEqual(true, false); | ||
}, | ||
new AssertionError(format(messages.NOT_EQUAL, false)), | ||
'expected to throw for not equal values', | ||
); | ||
|
||
t.throws( | ||
() => { | ||
assert.isEqual<number | boolean>(0, false); | ||
}, | ||
new AssertionError(format(messages.NOT_EQUAL, false)), | ||
'expected to throw for not equal types with same value', | ||
); | ||
|
||
await testDetail(t, (...args) => { | ||
assert.isEqual(true, false, ...args); | ||
}, (message: string) => new AssertionError(message), format(messages.NOT_EQUAL, false)); | ||
}); | ||
|
||
await test('isNotEqual', async (t) => { | ||
t.throws( | ||
() => { | ||
assert.isNotEqual(0, 0); | ||
}, | ||
new AssertionError(format(messages.IS_EQUAL, 0)), | ||
'expected to throw for equal values', | ||
); | ||
|
||
t.doesNotThrow( | ||
() => { | ||
assert.isNotEqual(true, false); | ||
}, | ||
'expected to not throw for not equal values', | ||
); | ||
|
||
t.doesNotThrow( | ||
() => { | ||
assert.isNotEqual<number | boolean>(0, false); | ||
}, | ||
'expected to not throw for not equal types with same value', | ||
); | ||
|
||
await testDetail(t, (...args) => { | ||
assert.isNotEqual(false, false, ...args); | ||
}, (message: string) => new AssertionError(message), format(messages.IS_EQUAL, false)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.