-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Mark Wubben <[email protected]>
- Loading branch information
1 parent
9f7852f
commit 1638c09
Showing
9 changed files
with
214 additions
and
5 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
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,45 @@ | ||
'use strict' | ||
class CustomError extends Error { | ||
constructor (message, code) { | ||
super(message) | ||
this.code = code | ||
this.name = 'CustomError' | ||
} | ||
} | ||
|
||
exports.CustomError = CustomError | ||
|
||
exports.factory = function ({ DescribedMixin, DeserializedMixin, ObjectValue }) { | ||
const tag = Symbol.for('customError') | ||
|
||
class DescribedErrorValue extends DescribedMixin(ObjectValue) { | ||
createPropertyRecursor () { | ||
let i = 0 | ||
return { | ||
size: 1, | ||
next: () => { | ||
if (i === 1) { | ||
return null | ||
} | ||
i++ | ||
return this.describeProperty('code', this.describeAny(this.value.code)) | ||
}, | ||
} | ||
} | ||
} | ||
|
||
Object.defineProperty(DescribedErrorValue.prototype, 'tag', { value: tag }) | ||
|
||
const DeserializedErrorValue = DeserializedMixin(ObjectValue) | ||
Object.defineProperty(DeserializedErrorValue.prototype, 'tag', { value: tag }) | ||
|
||
return { | ||
describe (props) { | ||
return new DescribedErrorValue(props) | ||
}, | ||
deserialize (state, recursor) { | ||
return new DeserializedErrorValue(state, recursor) | ||
}, | ||
tag, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const test = require('ava') | ||
const proxyquire = require('proxyquire') | ||
|
||
const pluginRegistry = proxyquire('../lib/pluginRegistry', { '../package.json': { version: '1.0.0' } }) | ||
|
||
test('registration should fail when plugin name invalid', t => { | ||
t.throws(() => pluginRegistry.add({ name: { for: 'complex' } }), { name: 'PluginTypeError' }) | ||
}) | ||
|
||
test('registration should fail when api version unsupported', t => { | ||
t.throws(() => pluginRegistry.add({ name: 'complex', apiVersion: 2 }), { name: 'UnsupportedApiError' }) | ||
}) | ||
|
||
test('registration should fail when installed concordance version below minimal version required by plugin', t => { | ||
t.throws(() => pluginRegistry.add({ name: 'complex', apiVersion: 1, minimalConcordanceVersion: '2.0.0' }), | ||
{ name: 'UnsupportedError' }) | ||
}) | ||
|
||
test('registration should fail when descriptor id used twice', t => { | ||
const plugin = { | ||
name: 'complex', | ||
apiVersion: 1, | ||
register: concordance => { | ||
concordance.addDescriptor(1, 'complexCustomValue') | ||
concordance.addDescriptor(1, 'complexCustomValue') | ||
}, | ||
} | ||
t.throws(() => pluginRegistry.add(plugin), { name: 'DuplicateDescriptorIdError' }) | ||
}) | ||
|
||
test('registration should fail when descriptor tag used twice', t => { | ||
const plugin = { | ||
name: 'complex', | ||
apiVersion: 1, | ||
register: concordance => { | ||
concordance.addDescriptor(1, 'complexCustomValue') | ||
concordance.addDescriptor(2, 'complexCustomValue') | ||
}, | ||
} | ||
t.throws(() => pluginRegistry.add(plugin), { name: 'DuplicateDescriptorTagError' }) | ||
}) | ||
|
||
test('registration should be successful', t => { | ||
t.plan(2) | ||
const tryDescribeValue = () => { } // eslint-disable-line unicorn/consistent-function-scoping | ||
const plugin = { | ||
name: 'complex', | ||
apiVersion: 1, | ||
serializerVersion: 1, | ||
register: concordance => { | ||
t.pass() | ||
concordance.addDescriptor(1, 'complexCustomObject', 'objectDeserializer') | ||
concordance.addDescriptor(2, 'complexCustomArray', 'arrayDeserializer') | ||
return tryDescribeValue | ||
}, | ||
} | ||
|
||
t.snapshot(pluginRegistry.add(plugin)) | ||
}) |
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
Binary file not shown.
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,24 @@ | ||
# Snapshot report for `test/pluginRegistry.js` | ||
|
||
The actual snapshot is saved in `pluginRegistry.js.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## registration should be successful | ||
|
||
> Snapshot 1 | ||
{ | ||
id2deserialize: Map { | ||
1 => 'objectDeserializer', | ||
2 => 'arrayDeserializer', | ||
}, | ||
name: 'complex', | ||
serializerVersion: 1, | ||
tag2id: Map { | ||
'complexCustomObject' => 1, | ||
'complexCustomArray' => 2, | ||
}, | ||
theme: {}, | ||
tryDescribeValue: Function tryDescribeValue {}, | ||
} |
Binary file not shown.