Comparing version 0.3.0 to 0.4.0
'use strict'; | ||
const Action = require('./action'); | ||
const Cardinality = require('./cardinality'); | ||
const matcher = require('./matcher'); | ||
const _ = require('lodash'); | ||
function forceCardinality(min, max) { | ||
if(this.cardinalityForced) { | ||
throw new Error('Cardinality already set for the expectation'); | ||
} else { | ||
this.cardinalityForced = true; | ||
this.cardinality.set(min, max); | ||
} | ||
} | ||
function tryUpdateCardinality(count) { | ||
if(!this.cardinalityForced) { | ||
if(this.actions.length === 0) { | ||
if(this.count >= 0) { | ||
this.cardinality.set(count, count); | ||
} else { | ||
this.cardinality.set(1, -1); | ||
} | ||
} else if (count >= 0) { | ||
this.cardinality.bump(count); | ||
} else { | ||
this.cardinality.bump(1); | ||
this.cardinality.unbound(); | ||
} | ||
} | ||
} | ||
/** @class Expectation | ||
@@ -17,3 +44,2 @@ * | ||
class Expectation { | ||
/** | ||
@@ -26,2 +52,4 @@ * | ||
this.actions = []; | ||
this.cardinality = new Cardinality(1); | ||
this.cardinalityForced = false; | ||
} | ||
@@ -56,13 +84,30 @@ | ||
validate() { | ||
if(this.actions.length === 0) { | ||
return true; | ||
} | ||
return (_.findIndex(this.actions, a => !a.validate()) < 0); | ||
return this.cardinality.validate(); | ||
// if(this.actions.length === 0) { | ||
// return this.cardinality.validate(); | ||
// } | ||
// return (_.findIndex(this.actions, a => !a.validate()) < 0); | ||
} | ||
times(count) { | ||
return forceCardinality.call(this, count, count); | ||
} | ||
atLeast(count) { | ||
return forceCardinality.call(this, count, -1); | ||
} | ||
atMost(count) { | ||
return forceCardinality.call(this, 1, count); | ||
} | ||
between(min, max) { | ||
return forceCardinality.call(this, min, max); | ||
} | ||
/** | ||
* @param {Array} args | ||
* @returns {*} | ||
* Returns true if no action was specified for expectation. If there is | ||
* an action to be executed returns result of that execution. | ||
* If no action was specified for the expectation, will return no value. | ||
* If there is an action to be executed returns result of that execution. | ||
* @throws {Error} | ||
@@ -72,4 +117,7 @@ * Throws error if none of specified actions are valid for execution | ||
execute(args) { | ||
if(!this.cardinality.use()) { | ||
throw new Error('Expectation oversaturated'); | ||
} | ||
if(this.actions.length === 0) { | ||
return true; | ||
return; | ||
} | ||
@@ -94,16 +142,2 @@ const idx = _.findIndex(this.actions, a => a.ready()); | ||
/** Adds a new action from provided arguments and adds it to | ||
* expectation actions list. | ||
* | ||
* @param {*} action | ||
* Parameter passed for Action constructor | ||
* @returns {Action} | ||
* Returns newly created Action object for chaining. | ||
*/ | ||
will(action) { | ||
const act = new Action(this, action); | ||
this.actions.push(act); | ||
return act; | ||
} | ||
/** Adds a new action from provided arguments and adds it to | ||
* expectation action list. Newly created Action has expected | ||
@@ -118,2 +152,3 @@ * execution count set to 1. | ||
willOnce(action) { | ||
tryUpdateCardinality.call(this, 1); | ||
this.actions.push(new Action(this, action, 1)); | ||
@@ -133,2 +168,3 @@ return this; | ||
willTwice(action) { | ||
tryUpdateCardinality.call(this, 2); | ||
this.actions.push(new Action(this, action, 2)); | ||
@@ -148,2 +184,3 @@ return this; | ||
willRepeatedly(action) { | ||
tryUpdateCardinality.call(this, -1); | ||
this.actions.push(new Action(this, action, -1)); | ||
@@ -150,0 +187,0 @@ return this; |
{ | ||
"name": "jsmock", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Mocking framework for javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -75,3 +75,2 @@ [![npm Package](https://img.shields.io/npm/v/jsmock.svg?style=flat-square)](https://www.npmjs.org/package/jsmock) | ||
.willOnce((a,b) => a * b) // First call will return multiplication of arguments | ||
.will(6).times(3) // Next 3 calls will return value 6 | ||
.willRepeteadly((a,b) => b) // All following calls will return second argument | ||
@@ -78,0 +77,0 @@ ``` |
@@ -5,7 +5,4 @@ 'use strict'; | ||
const Action = require('../lib/action'); | ||
const create = require('./helper').create; | ||
function create(T) { | ||
return new (Function.prototype.bind.apply(T, arguments)); | ||
} | ||
describe('Action', () => { | ||
@@ -12,0 +9,0 @@ |
@@ -69,19 +69,27 @@ 'use strict'; | ||
describe('validate', () => { | ||
it('Should return true if no actions defined', () => { | ||
it('Should return false by default', () => { | ||
let exp = new Expectation(); | ||
expect(exp.validate()).to.be.true; | ||
expect(exp.validate()).to.be.false; | ||
}); | ||
it('Shoud return false if at least one action is not valid', () => { | ||
it('Should return false if forced cardinallity is not fullfilled', () => { | ||
let exp = new Expectation(); | ||
exp.times(3); | ||
expect(exp.validate()).to.be.false; | ||
exp.execute(); | ||
exp.execute(); | ||
expect(exp.validate()).to.be.false; | ||
exp.execute(); | ||
expect(exp.validate()).to.be.true; | ||
}); | ||
exp.actions.push({validate: () => true}); | ||
exp.actions.push({validate: () => true}); | ||
exp.actions.push({validate: () => true}); | ||
it('Should return false if automatic cardinality is not fullfilled', () => { | ||
let exp = new Expectation(); | ||
exp.willOnce(1).willTwice(2); | ||
expect(exp.validate()).to.be.false; | ||
expect(exp.execute()).to.be.equal(1); | ||
expect(exp.execute()).to.be.equal(2); | ||
expect(exp.validate()).to.be.false; | ||
expect(exp.execute()).to.be.equal(2); | ||
expect(exp.validate()).to.be.true; | ||
exp.actions.push({validate: () => true}); | ||
exp.actions.push({validate: () => false}); | ||
exp.actions.push({validate: () => true}); | ||
expect(exp.validate()).to.be.false; | ||
}); | ||
@@ -91,7 +99,8 @@ }); | ||
describe('execute', () => { | ||
it('Should return true directly if no actions provided', () => { | ||
it('Should return no value if no actions provided', () => { | ||
let exp = new Expectation(); | ||
expect(exp.execute([1,2,3])).to.be.true; | ||
expect(exp.execute([])).to.be.true; | ||
expect(exp.execute()).to.be.true; | ||
exp.atLeast(1); | ||
expect(exp.execute([1,2,3])).to.be.undefined; | ||
expect(exp.execute([])).to.be.undefined; | ||
expect(exp.execute()).to.be.undefined; | ||
}); | ||
@@ -101,2 +110,3 @@ | ||
let exp = new Expectation(); | ||
exp.atLeast(1); | ||
exp.actions.push({ | ||
@@ -132,2 +142,3 @@ ready: () => false, | ||
let exp = new Expectation(); | ||
exp.atLeast(1); | ||
exp.actions.push({ | ||
@@ -175,3 +186,3 @@ ready: () => true, | ||
describe('will', () => { | ||
describe.skip('will', () => { | ||
it('Should add new action to the list', () => { | ||
@@ -236,2 +247,26 @@ let exp = new Expectation(); | ||
describe('willTwice', () => { | ||
it('Should create action with counter set to 2', () => { | ||
let exp = new Expectation(); | ||
exp.willTwice(() => true); | ||
expect(exp.actions).to.have.length(1); | ||
expect(exp.actions[0].counter).to.be.equal(2); | ||
}); | ||
it('Should return instance of current expectation', () => { | ||
let exp1 = new Expectation(); | ||
let exp2 = new Expectation(); | ||
expect(exp1.willTwice(() => true)).to.be.equal(exp1); | ||
expect(exp2.willTwice(() => false)).to.be.equal(exp2); | ||
}) | ||
it('Should create action with provided function', () => { | ||
let exp = new Expectation(); | ||
exp.willTwice(() => 4531); | ||
exp.willTwice(() => 'Hello World'); | ||
expect(exp.actions[0].execute()).to.be.equal(4531); | ||
expect(exp.actions[1].execute()).to.be.equal('Hello World'); | ||
}); | ||
}); | ||
describe('willRepeatedly', () => { | ||
@@ -238,0 +273,0 @@ it('Should create action with counter set to negative value', () => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39802
18
1057
81