@cycle/time
Advanced tools
Comparing version 0.5.0 to 0.6.0
"use strict"; | ||
var xstream_1 = require("xstream"); | ||
var deepEqual = require("deep-equal"); | ||
var variableDiff = require("variable-diff"); | ||
function checkEqual(completeStore, assert, interval) { | ||
var equal = deepEqual(completeStore['actual'], completeStore['expected']); | ||
var failReasons = []; | ||
@@ -23,2 +23,8 @@ if (completeStore['actual'].length !== completeStore['expected'].length) { | ||
} | ||
if (actual.type === 'complete') { | ||
var rightTime = diagramFrame(actual.time, interval) === diagramFrame(expected.time, interval); | ||
if (!rightTime) { | ||
failReasons.push("Expected stream to complete at " + expected.time + " but completed at " + actual.time); | ||
} | ||
} | ||
if (actual.type === 'next') { | ||
@@ -31,3 +37,3 @@ var rightTime = diagramFrame(actual.time, interval) === diagramFrame(expected.time, interval); | ||
if (!rightTime || !rightValue) { | ||
failReasons.push("Expected value " + JSON.stringify(expected.value) + " at time " + expected.time + " but got " + JSON.stringify(actual.value) + " at " + actual.time); | ||
failReasons.push("Expected value at time " + expected.time + " but got different value at " + actual.time + "\n\nDiff (actual => expected):\n" + variableDiff(actual.value, expected.value).text); | ||
} | ||
@@ -55,3 +61,3 @@ } | ||
assert.state = 'failed'; | ||
assert.error = new Error(strip("\n Expected\n\n " + diagramString(completeStore['expected'], interval) + "\n\n Got\n\n " + diagramString(completeStore['actual'], interval) + "\n\n Failed because\n\n " + failReasons[0] + "\n\n " + displayUnexpectedErrors(assert.unexpectedErrors) + "\n ")); | ||
assert.error = new Error(strip("\nExpected\n\n" + diagramString(completeStore['expected'], interval) + "\n\nGot\n\n" + diagramString(completeStore['actual'], interval) + "\n\nFailed because:\n\n" + failReasons.map(function (reason) { return " * " + reason; }).join('\n') + "\n\n" + displayUnexpectedErrors(assert.unexpectedErrors) + "\n ")); | ||
} | ||
@@ -99,2 +105,31 @@ } | ||
} | ||
function chunkBy(values, f) { | ||
function chunkItGood(_a, value) { | ||
var items = _a.items, previousValue = _a.previousValue; | ||
var v = f(value); | ||
if (v !== previousValue) { | ||
return { | ||
items: items.concat([[value]]), | ||
previousValue: v | ||
}; | ||
} | ||
var lastItem = items[items.length - 1]; | ||
return { | ||
items: items.slice(0, -1).concat([lastItem.concat(value)]), | ||
previousValue: previousValue | ||
}; | ||
} | ||
return values.reduce(chunkItGood, { items: [], previousValue: undefined }).items; | ||
} | ||
function characterString(entry) { | ||
if (entry.type === 'next') { | ||
return stringifyIfObject(entry.value); | ||
} | ||
if (entry.type == 'complete') { | ||
return '|'; | ||
} | ||
if (entry.type === 'error') { | ||
return '#'; | ||
} | ||
} | ||
function diagramString(entries, interval) { | ||
@@ -107,22 +142,12 @@ if (entries.length === 0) { | ||
var diagram = fill(new Array(characterCount), '-'); | ||
entries.forEach(function (entry) { | ||
var characterIndex = Math.max(0, Math.floor(entry.time / interval)); | ||
if (entry.type === 'next') { | ||
diagram[characterIndex] = stringifyIfObject(entry.value); | ||
var chunks = chunkBy(entries, function (entry) { return Math.max(0, Math.floor(entry.time / interval)); }); | ||
chunks.forEach(function (chunk) { | ||
var characterIndex = Math.max(0, Math.floor(chunk[0].time / interval)); | ||
if (chunk.length === 1) { | ||
diagram[characterIndex] = characterString(chunk[0]); | ||
} | ||
if (entry.type == 'complete') { | ||
diagram[characterIndex] = '|'; | ||
else { | ||
diagram[characterIndex] = "(" + chunk.map(characterString).join('') + ")"; | ||
} | ||
if (entry.type === 'error') { | ||
diagram[characterIndex] = '#'; | ||
} | ||
}); | ||
if (entries.length > 1) { | ||
var completeEntry = entries[entries.length - 1]; | ||
var lastEntry = entries[entries.length - 2]; | ||
if (completeEntry.type === 'complete' && lastEntry.time === completeEntry.time) { | ||
diagram[diagram.length - 1] = lastEntry.value; | ||
diagram.push('|'); | ||
} | ||
} | ||
return diagram.join(''); | ||
@@ -129,0 +154,0 @@ } |
import xs from 'xstream'; | ||
declare function makeDiagram(schedule: any, currentTime: any, interval: any): (diagramString: string, values?: {}) => xs<any>; | ||
declare function makeDiagram(schedule: any, currentTime: any, interval: any, setMaxTime: any): (diagramString: string, values?: {}) => xs<any>; | ||
export { makeDiagram }; |
@@ -10,3 +10,3 @@ "use strict"; | ||
}; | ||
function makeDiagram(schedule, currentTime, interval) { | ||
function makeDiagram(schedule, currentTime, interval, setMaxTime) { | ||
return function diagram(diagramString, values) { | ||
@@ -17,2 +17,4 @@ if (values === void 0) { values = {}; } | ||
var valueFor = function (character) { return values[character] || parseIntIfDecimal(character); }; | ||
setMaxTime(diagramString.length * interval); | ||
var multipleValueFrame = false; | ||
characters.forEach(function (character, index) { | ||
@@ -23,2 +25,13 @@ if (character === '-') { | ||
var timeToSchedule = index * interval; | ||
if (character === '(') { | ||
multipleValueFrame = timeToSchedule; | ||
return; | ||
} | ||
if (character === ')') { | ||
multipleValueFrame = false; | ||
return; | ||
} | ||
if (multipleValueFrame !== false) { | ||
timeToSchedule = multipleValueFrame; | ||
} | ||
if (character === '|') { | ||
@@ -25,0 +38,0 @@ schedule.completion(stream, timeToSchedule); |
@@ -37,2 +37,3 @@ "use strict"; | ||
var time = 0; | ||
var maxTime = null; | ||
var asserts = []; | ||
@@ -50,4 +51,7 @@ var done; | ||
} | ||
function setMaxTime(newTime) { | ||
maxTime = Math.max(newTime, maxTime); | ||
} | ||
var timeSource = { | ||
diagram: diagram_1.makeDiagram(scheduler.add, currentTime, interval), | ||
diagram: diagram_1.makeDiagram(scheduler.add, currentTime, interval, setMaxTime), | ||
record: record_1.makeRecord(scheduler.add, currentTime, interval), | ||
@@ -65,2 +69,5 @@ assertEqual: assert_equal_1.makeAssertEqual(function () { return timeSource; }, scheduler.add, currentTime, interval, addAssert), | ||
done = doneCallback; | ||
if (!timeToRunTo) { | ||
timeToRunTo = maxTime; | ||
} | ||
run_virtually_1.runVirtually(scheduler, function () { return finish(asserts, done); }, currentTime, setTime, timeToRunTo); | ||
@@ -67,0 +74,0 @@ }, |
@@ -18,3 +18,2 @@ "use strict"; | ||
} | ||
return 0; | ||
} | ||
@@ -21,0 +20,0 @@ return 1; |
@@ -16,2 +16,3 @@ "use strict"; | ||
schedule.next(listener, currentTime(), lastValue); | ||
emittedLastValue = true; | ||
} | ||
@@ -18,0 +19,0 @@ } |
{ | ||
"name": "@cycle/time", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "A time driver designed to enable awesome testing and dev tooling", | ||
@@ -45,2 +45,3 @@ "main": "dist/index.js", | ||
"sorted-immutable-list": "^1.1.0", | ||
"variable-diff": "^1.1.0", | ||
"xstream": "*" | ||
@@ -47,0 +48,0 @@ }, |
@@ -227,3 +227,3 @@ # @cycle/time | ||
The diagram syntax is inspired by xstream's [fromDiagram](Vhttps://github.com/staltz/xstream/blob/master/EXTRA_DOCS.md#-fromdiagramdiagram-options) and RxJS's [marble diagrams](Vhttps://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md). | ||
The diagram syntax is inspired by xstream's [fromDiagram](https://github.com/staltz/xstream/blob/master/EXTRA_DOCS.md#-fromdiagramdiagram-options) and RxJS's [marble diagrams](Vhttps://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md). | ||
@@ -235,2 +235,3 @@ * `-` the passage of time without any events, by default 20 virtual milliseconds | ||
* `#` an error | ||
* `(ab)` "a" and "b" simultaneously | ||
@@ -237,0 +238,0 @@ We make input streams, run our app, and then make assertions about what comes out the other side. |
51882
913
438
10
+ Addedvariable-diff@^1.1.0
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedvariable-diff@1.1.0(transitive)