aboutsummaryrefslogtreecommitdiff
path: root/node_modules/reduce/test
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/reduce/test
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/reduce/test')
-rw-r--r--node_modules/reduce/test/simple.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/node_modules/reduce/test/simple.js b/node_modules/reduce/test/simple.js
new file mode 100644
index 00000000..2c056ab7
--- /dev/null
+++ b/node_modules/reduce/test/simple.js
@@ -0,0 +1,94 @@
+var test = require("tape")
+ , reduce = require("..")
+
+test("reduce calls each iterator", function (t) {
+ var item = createItem()
+ , timesCalled = 0
+ , accumulator = { key: '' }
+ , expectedKeys = ['a', 'b', 'c']
+ , expectedValues = ['a1', 'b1', 'c1']
+ , expectedAccumulatorKeys = ['', 'a1', 'a1b1', 'a1b1c1']
+ , calledArguments = []
+ , slice = Array.prototype.slice
+ , iterator = function (acc, value, key, list) {
+ var expectedKey = expectedKeys[timesCalled]
+ , expectedValue = expectedValues[timesCalled]
+ , expectedAccumulatorKey = expectedAccumulatorKeys[timesCalled]
+
+ calledArguments.push(slice.apply(arguments))
+
+ t.equal(value, expectedValue, 'value ' + value + ' does not match ' + expectedValue)
+ t.equal(key, expectedKey, 'key ' + key + ' does not match ' + expectedKey)
+ t.equal(list, item, 'list arg is not correct')
+ t.equal(acc.key, expectedAccumulatorKey, 'accumulator key ' + acc.key + ' does not match ' + expectedAccumulatorKey)
+
+ timesCalled += 1
+ acc.key += value
+ return acc
+ }
+
+ var result = reduce(item, iterator, accumulator)
+
+ t.equal(timesCalled, 3, "iterator was not called thrice")
+ t.deepEqual(result, { key: 'a1b1c1' }, 'result is incorrect');
+
+ t.deepEqual(calledArguments[0], [{
+ key: "a1b1c1"
+ }, "a1", "a", item], "iterator called with wrong arguments")
+ t.deepEqual(calledArguments[1], [{
+ key: "a1b1c1"
+ }, "b1", "b", item], "iterator called with wrong arguments")
+ t.deepEqual(calledArguments[2], [{
+ key: "a1b1c1"
+ }, "c1", "c", item], "iterator called with wrong arguments")
+ t.deepEqual(result, {
+ key: "a1b1c1"
+ })
+
+ t.end()
+})
+
+test("reduce calls iterator with correct this value", function (t) {
+ var item = createItem()
+ , thisValue = {}
+ , iterator = function () {
+ t.equal(this, thisValue, 'this value is incorrect');
+ }
+
+ reduce(item, iterator, thisValue, {})
+
+ t.end()
+})
+
+test("reduce reduces with first value if no initialValue", function (t) {
+ var list = [1, 2]
+ , iterator = function (sum, v) {
+ return sum + v
+ }
+
+ var result = reduce(list, iterator)
+
+ t.equal(result, 3, "result is incorrect")
+
+ t.end()
+})
+
+test("reduce throws a TypeError when an invalid iterator is provided", function (t) {
+ t.throws(function () { reduce([1, 2]); }, TypeError, 'requires a function')
+
+ t.end()
+})
+
+test("reduce has a length of 2, mimicking spec", function (t) {
+ t.equal(reduce.length, 2, 'reduce has a length of 2')
+
+ t.end()
+})
+
+function createItem() {
+ return {
+ a: "a1"
+ , b: "b1"
+ , c: "c1"
+ }
+}