-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81dbe6d
commit 47dbb61
Showing
5 changed files
with
105 additions
and
4 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,30 @@ | ||
const jmv = require('../plugins/json.match'); | ||
|
||
const specs = {}; | ||
|
||
function memorize_spec(name, props) { | ||
if (!specs[name]) { | ||
specs[name] = []; | ||
} | ||
specs[name].push({ props }); | ||
} | ||
|
||
function is_spec_memoized(name, new_props) { | ||
const items = specs[name]; | ||
if (!items) { | ||
return false; | ||
} | ||
for (const item of items) { | ||
const rules = jmv.getMatchingRules(item.props, '$'); | ||
const errors = jmv.validate(item.props, new_props, rules, '$', true); | ||
if (!errors) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
module.exports = { | ||
memorize_spec, | ||
is_spec_memoized | ||
} |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const { spec, handler } = require('../../src'); | ||
const expect = require('chai').expect; | ||
|
||
describe('Memo', () => { | ||
|
||
before(() => { | ||
handler.addSpecHandler('default get memo', ({ spec }) => { | ||
spec.useInteraction('default get'); | ||
spec.get('http://localhost:9393/default/get'); | ||
spec.expectStatus(200); | ||
}); | ||
handler.addSpecHandler('default get memo duplicate', ({ spec }) => { | ||
spec.useInteraction('default get'); | ||
spec.get('http://localhost:9393/default/get'); | ||
spec.expectStatus(200); | ||
}); | ||
}); | ||
|
||
it('should memoize with boolean', async () => { | ||
let response = await spec('default get memo', null, { memo: true }).returns('method'); | ||
expect(response).equals('GET') | ||
response = await spec('default get memo', null, { memo: true }).returns('method'); | ||
expect(response).to.be.undefined; | ||
response = await spec('default get memo', null, { memo: false }).returns('method'); | ||
expect(response).equals('GET') | ||
}); | ||
|
||
it('should memoize with string', async () => { | ||
let response = await spec('default get memo', null, { memo: 'free' }).returns('method'); | ||
expect(response).equals('GET'); | ||
response = await spec('default get memo', null, { memo: 'paid' }).returns('method'); | ||
expect(response).equals('GET'); | ||
response = await spec('default get memo', null, { memo: 'free' }).returns('method'); | ||
expect(response).to.be.undefined; | ||
response = await spec('default get memo', null, { memo: 'paid' }).returns('method'); | ||
expect(response).to.be.undefined; | ||
}); | ||
|
||
it('should memoize with object with multiple specs', async () => { | ||
let response = await spec('default get memo', null, { memo: { plan: 'free' } }).returns('method'); | ||
expect(response).equals('GET'); | ||
response = await spec('default get memo', null, { memo: { plan: 'paid' } }).returns('method'); | ||
expect(response).equals('GET'); | ||
response = await spec('default get memo', null, { memo: { plan: 'free' } }).returns('method'); | ||
expect(response).to.be.undefined; | ||
response = await spec('default get memo', null, { memo: { plan: 'paid' } }).returns('method'); | ||
expect(response).to.be.undefined; | ||
response = await spec('default get memo duplicate', null, { memo: { plan: 'free' } }).returns('method'); | ||
expect(response).equals('GET'); | ||
response = await spec('default get memo duplicate', null, { memo: { plan: 'paid' } }).returns('method'); | ||
expect(response).equals('GET'); | ||
response = await spec('default get memo duplicate', null, { memo: { plan: 'free' } }).returns('method'); | ||
expect(response).to.be.undefined; | ||
response = await spec('default get memo duplicate', null, { memo: { plan: 'paid' } }).returns('method'); | ||
expect(response).to.be.undefined; | ||
}); | ||
|
||
}); |