-
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.
- Loading branch information
0 parents
commit 34cea52
Showing
9 changed files
with
375 additions
and
0 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,21 @@ | ||
{ | ||
"extends": "eslint-config-simplifield", | ||
"env": { | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"globals": { | ||
"require": false, | ||
"describe": false, | ||
"beforeEach": false, | ||
"afterEach": false, | ||
"before": false, | ||
"after": false, | ||
"it": false, | ||
"sinon": false, | ||
"module": false | ||
}, | ||
"rules": { | ||
"strict": [2, "global"] | ||
} | ||
} |
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 @@ | ||
*~ | ||
*.swp | ||
node_modules/ | ||
.git | ||
.coveralls.yml |
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,6 @@ | ||
*~ | ||
*.swp | ||
node_modules/ | ||
.git | ||
.coveralls.yml | ||
|
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,11 @@ | ||
language: node_js | ||
node_js: | ||
- 0.10 | ||
- 0.12 | ||
|
||
before_script: | ||
- npm install --dev | ||
|
||
script: | ||
- npm test | ||
|
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 @@ | ||
The MIT License | ||
|
||
Copyright (c) 2015 SimpliField, <http://www.simplifield.com/> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,96 @@ | ||
# sf-time-mock | ||
> Mock utility for a function returning a timestamp (like Date.now()). | ||
[![NPM version](https://badge.fury.io/js/sf-time-mock.svg)](https://npmjs.org/package/sf-time-mock) [![Build status](https://secure.travis-ci.org/SimpliField/sf-time-mock.svg)](https://travis-ci.org/SimpliField/sf-time-mock) [![Dependency Status](https://david-dm.org/SimpliField/sf-time-mock.svg)](https://david-dm.org/SimpliField/sf-time-mock) [![devDependency Status](https://david-dm.org/SimpliField/sf-time-mock/dev-status.svg)](https://david-dm.org/SimpliField/sf-time-mock#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/SimpliField/sf-time-mock/badge.svg?branch=master)](https://coveralls.io/r/SimpliField/sf-time-mock?branch=master) [![Code Climate](https://codeclimate.com/github/SimpliField/sf-time-mock.svg)](https://codeclimate.com/github/SimpliField/sf-time-mock) | ||
|
||
## Usage | ||
|
||
```js | ||
var initTimeMock = require('sf-time-mock'); | ||
|
||
// Init the time mock | ||
var timeStub = initTimeMock(); | ||
|
||
// Set the current time | ||
timeStub.setTime(1267833600000); | ||
|
||
// Use it everywhere | ||
new Date(timeStub()).toISOString(); | ||
// '2010-03-06T00:00:00.000Z' | ||
|
||
``` | ||
|
||
<a name="initTimeMock"></a> | ||
## initTimeMock() ⇒ <code>function</code> | ||
Spawn a new time stub | ||
|
||
**Kind**: global function | ||
**Returns**: <code>function</code> - A time stub | ||
|
||
* [initTimeMock()](#initTimeMock) ⇒ <code>function</code> | ||
* [~timeStub()](#initTimeMock..timeStub) ⇒ <code>Number</code> | ||
* [.setTime(value, n)](#initTimeMock..timeStub.setTime) ⇒ <code>void</code> | ||
* [.reset()](#initTimeMock..timeStub.reset) ⇒ <code>void</code> | ||
* [.ended()](#initTimeMock..timeStub.ended) ⇒ <code>Boolean</code> | ||
* [.setTimes(theTimes)](#initTimeMock..timeStub.setTimes) ⇒ <code>void</code> | ||
* [.appendTimes(theTimes)](#initTimeMock..timeStub.appendTimes) ⇒ <code>void</code> | ||
|
||
<a name="initTimeMock..timeStub"></a> | ||
### initTimeMock~timeStub() ⇒ <code>Number</code> | ||
Return the next timestamp | ||
|
||
**Kind**: inner method of <code>[initTimeMock](#initTimeMock)</code> | ||
**Returns**: <code>Number</code> - The next mocked timestamp | ||
**Throws**: | ||
|
||
- <code>YError</code> E_TIME_ENDED exception when no timestamp available | ||
|
||
|
||
* [~timeStub()](#initTimeMock..timeStub) ⇒ <code>Number</code> | ||
* [.setTime(value, n)](#initTimeMock..timeStub.setTime) ⇒ <code>void</code> | ||
* [.reset()](#initTimeMock..timeStub.reset) ⇒ <code>void</code> | ||
* [.ended()](#initTimeMock..timeStub.ended) ⇒ <code>Boolean</code> | ||
* [.setTimes(theTimes)](#initTimeMock..timeStub.setTimes) ⇒ <code>void</code> | ||
* [.appendTimes(theTimes)](#initTimeMock..timeStub.appendTimes) ⇒ <code>void</code> | ||
|
||
<a name="initTimeMock..timeStub.setTime"></a> | ||
#### timeStub.setTime(value, n) ⇒ <code>void</code> | ||
Set a timestamp for the n next calls | ||
|
||
**Kind**: static method of <code>[timeStub](#initTimeMock..timeStub)</code> | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| value | <code>Number</code> | The timestamp value | | ||
| n | <code>Number</code> | The number of call it should be used. Default to Infinity. | | ||
|
||
<a name="initTimeMock..timeStub.reset"></a> | ||
#### timeStub.reset() ⇒ <code>void</code> | ||
Reset the time stub | ||
|
||
**Kind**: static method of <code>[timeStub](#initTimeMock..timeStub)</code> | ||
<a name="initTimeMock..timeStub.ended"></a> | ||
#### timeStub.ended() ⇒ <code>Boolean</code> | ||
Say if there is no more timestamp available | ||
|
||
**Kind**: static method of <code>[timeStub](#initTimeMock..timeStub)</code> | ||
**Returns**: <code>Boolean</code> - A boolean indicating if it ended or not | ||
<a name="initTimeMock..timeStub.setTimes"></a> | ||
#### timeStub.setTimes(theTimes) ⇒ <code>void</code> | ||
Set a bunch of timestamp to the queue | ||
|
||
**Kind**: static method of <code>[timeStub](#initTimeMock..timeStub)</code> | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| theTimes | <code>Array</code> | An array of timestamps or timestamp objects (format: {value, n}). | | ||
|
||
<a name="initTimeMock..timeStub.appendTimes"></a> | ||
#### timeStub.appendTimes(theTimes) ⇒ <code>void</code> | ||
Add a bunch of timestamps to the queue | ||
|
||
**Kind**: static method of <code>[timeStub](#initTimeMock..timeStub)</code> | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| theTimes | <code>Array</code> | An array of timestamps or timestamp objects (format: {value, n}). | |
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,44 @@ | ||
{ | ||
"name": "sf-time-mock", | ||
"version": "0.1.0", | ||
"description": "Build strong and efficient REST web services.", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "mocha src/*.mocha.js", | ||
"lint": "eslint src/*.js", | ||
"prepublish": "npm test && npm lint", | ||
"coveralls": "istanbul cover _mocha --report lcovonly -- src/*.mocha.js -R spec -t 5000 && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", | ||
"cover": "istanbul cover --report html _mocha -- src/*.mocha.js -R spec -t 5000", | ||
"cli": "env NPM_RUN_CLI=1" | ||
}, | ||
"keywords": [ | ||
"REST", | ||
"HTTP", | ||
"server", | ||
"web", | ||
"services" | ||
], | ||
"author": "Nicolas Froidure", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/SimpliField/sf-time-mock.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/SimpliField/sf-time-mock/issues" | ||
}, | ||
"homepage": "https://github.com/SimpliField/sf-time-mock", | ||
"devDependencies": { | ||
"coveralls": "^2.11.2", | ||
"eslint": "^1.3.1", | ||
"eslint-config-simplifield": "^1.0.3", | ||
"istanbul": "^0.3.16", | ||
"jsdoc-to-markdown": "^1.1.1", | ||
"mocha": "^2.2.5", | ||
"mocha-lcov-reporter": "^0.0.2" | ||
}, | ||
"dependencies": { | ||
"debug": "^2.2.0", | ||
"yerror": "^1.0.1" | ||
} | ||
} |
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,86 @@ | ||
'use strict'; | ||
|
||
var YError = require('yerror'); | ||
|
||
/** | ||
* Spawn a new time stub | ||
* @return {Function} A time stub | ||
*/ | ||
function initTimeMock() { | ||
var times = []; | ||
|
||
/** | ||
* Return the next timestamp | ||
* @return {Number} The next mocked timestamp | ||
* @throws {YError} E_TIME_ENDED exception when no timestamp available | ||
*/ | ||
function timeStub() { | ||
if(times[0] && 1 > times[0].n) { | ||
times.shift(); | ||
} | ||
if(!times.length) { | ||
throw new YError('E_TIME_ENDED'); | ||
} | ||
times[0].n--; | ||
return times[0].value; | ||
} | ||
|
||
/** | ||
* Set a timestamp for the n next calls | ||
* @param {Number} value The timestamp value | ||
* @param {Number} n The number of call it should be used. Default to Infinity. | ||
* @return {void} | ||
*/ | ||
timeStub.setTime = function timeStubSetTimes(value, n) { | ||
times = [{ | ||
n: 'undefined' !== typeof n ? n : Infinity, | ||
value: value, | ||
}]; | ||
}; | ||
|
||
/** | ||
* Reset the time stub | ||
* @return {void} | ||
*/ | ||
timeStub.reset = function timeStubReset() { | ||
times = []; | ||
}; | ||
|
||
/** | ||
* Say if there is no more timestamp available | ||
* @return {Boolean} A boolean indicating if it ended or not | ||
*/ | ||
timeStub.ended = function timeStubEnded() { | ||
return !times.length; | ||
}; | ||
|
||
/** | ||
* Set a bunch of timestamp to the queue | ||
* @param {Array} theTimes An array of timestamps or timestamp objects (format: {value, n}). | ||
* @return {void} | ||
*/ | ||
timeStub.setTimes = function timeStubSetTimes(theTimes) { | ||
times = theTimes.map(function timesMapper(theTime) { | ||
if(!(theTime.n)) { | ||
return { | ||
value: theTime, | ||
n: 1, | ||
}; | ||
} | ||
return theTime; | ||
}); | ||
}; | ||
|
||
/** | ||
* Add a bunch of timestamps to the queue | ||
* @param {Array} theTimes An array of timestamps or timestamp objects (format: {value, n}). | ||
* @return {void} | ||
*/ | ||
timeStub.appendTimes = function timeStubAppendTimes(theTimes) { | ||
timeStub.setTimes(times.concat(theTimes)); | ||
}; | ||
|
||
return timeStub; | ||
} | ||
|
||
module.exports = initTimeMock; |
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,85 @@ | ||
'use strict'; | ||
|
||
var initTimeMock = require('./'); | ||
var assert = require('assert'); | ||
|
||
describe('sf-time-mock', function() { | ||
var timeStub; | ||
|
||
beforeEach(function() { | ||
timeStub = initTimeMock(); | ||
}); | ||
|
||
it('should throw an error when calling the stub and no timestamp where given', function() { | ||
assert.throws(function() { | ||
timeStub(); | ||
}); | ||
}); | ||
|
||
it('should work when using setTime()', function() { | ||
timeStub.setTime(1267833600000, 1); | ||
assert.equal(timeStub(), 1267833600000); | ||
assert.throws(function() { | ||
timeStub(); | ||
}); | ||
assert(timeStub.ended()); | ||
timeStub.setTime(1267833600001); | ||
assert.equal(timeStub(), 1267833600001); | ||
assert.equal(timeStub(), 1267833600001); | ||
assert.equal(timeStub(), 1267833600001); | ||
assert(!timeStub.ended()); | ||
}); | ||
|
||
it('should work when using reset()', function() { | ||
timeStub.setTime(1267833600000); | ||
assert.equal(timeStub(), 1267833600000); | ||
assert.equal(timeStub(), 1267833600000); | ||
timeStub.reset(1267833600000); | ||
assert.throws(function() { | ||
timeStub(); | ||
}); | ||
assert(timeStub.ended()); | ||
}); | ||
|
||
it('should work when using setTimes() with timestamps', function() { | ||
timeStub.setTimes([1267833600000, 1267833600001, 1267833600002]); | ||
assert.equal(timeStub(), 1267833600000); | ||
assert.equal(timeStub(), 1267833600001); | ||
assert.equal(timeStub(), 1267833600002); | ||
assert.throws(function() { | ||
timeStub(); | ||
}); | ||
assert(timeStub.ended()); | ||
}); | ||
|
||
it('should work when using setTimes() with timestamp objects', function() { | ||
timeStub.setTimes([{ | ||
value: 1267833600000, | ||
n: 2, | ||
}, { | ||
value: 1267833600001, | ||
n: 1, | ||
}]); | ||
assert.equal(timeStub(), 1267833600000); | ||
assert.equal(timeStub(), 1267833600000); | ||
assert.equal(timeStub(), 1267833600001); | ||
assert.throws(function() { | ||
timeStub(); | ||
}); | ||
assert(timeStub.ended()); | ||
}); | ||
|
||
it('should work when using appendTimes() with timestamps', function() { | ||
timeStub.appendTimes([1267833600000, 1267833600001]); | ||
timeStub.appendTimes([1267833600002, 1267833600003]); | ||
assert.equal(timeStub(), 1267833600000); | ||
assert.equal(timeStub(), 1267833600001); | ||
assert.equal(timeStub(), 1267833600002); | ||
assert.equal(timeStub(), 1267833600003); | ||
assert.throws(function() { | ||
timeStub(); | ||
}); | ||
assert(timeStub.ended()); | ||
}); | ||
|
||
}); |