1 var test = require("tape")
2 , forEach = require("..")
4 test("forEach calls each iterator", function (t) {
7 forEach({ a: 1, b: 2 }, function (value, key) {
19 test("forEach calls iterator with correct this value", function (t) {
24 forEach([0], function () {
25 t.equal(this, thisValue)
29 test('second argument: iterator', function (t) {
31 t.throws(function () { forEach(arr) }, TypeError, 'undefined is not a function')
32 t.throws(function () { forEach(arr, null) }, TypeError, 'null is not a function')
33 t.throws(function () { forEach(arr, '') }, TypeError, 'string is not a function')
34 t.throws(function () { forEach(arr, /a/) }, TypeError, 'regex is not a function')
35 t.throws(function () { forEach(arr, true) }, TypeError, 'true is not a function')
36 t.throws(function () { forEach(arr, false) }, TypeError, 'false is not a function')
37 t.throws(function () { forEach(arr, NaN) }, TypeError, 'NaN is not a function')
38 t.throws(function () { forEach(arr, 42) }, TypeError, '42 is not a function')
39 t.doesNotThrow(function () { forEach(arr, function () {}) }, 'function is a function')
40 t.doesNotThrow(function () { forEach(arr, setTimeout) }, 'setTimeout is a function')
41 if (typeof window !== 'undefined') {
42 t.doesNotThrow(function () { forEach(arr, window.alert) }, 'alert is a function')
47 test('array', function (t) {
50 t.test('iterates over every item', function (st) {
52 forEach(arr, function () { index += 1 })
53 st.equal(index, arr.length, 'iterates ' + arr.length + ' times')
57 t.test('first iterator argument', function (st) {
60 forEach(arr, function (item) {
61 st.equal(arr[index], item, 'item ' + index + ' is passed as first argument')
67 t.test('second iterator argument', function (st) {
70 forEach(arr, function (item, index) {
71 st.equal(counter, index, 'index ' + index + ' is passed as second argument')
77 t.test('third iterator argument', function (st) {
79 forEach(arr, function (item, index, array) {
80 st.deepEqual(arr, array, 'array is passed as third argument')
85 t.test('context argument', function (st) {
87 forEach([], function () {
88 st.equal(this, context, '"this" is the passed context')
96 test('object', function (t) {
102 var keys = ['a', 'b', 'c']
104 var F = function () {
109 var fKeys = ['a', 'b']
111 t.test('iterates over every object literal key', function (st) {
113 forEach(obj, function () { counter += 1 })
114 st.equal(counter, keys.length, 'iterated ' + counter + ' times')
118 t.test('iterates only over own keys', function (st) {
120 forEach(new F(), function () { counter += 1 })
121 st.equal(counter, fKeys.length, 'iterated ' + fKeys.length + ' times')
125 t.test('first iterator argument', function (st) {
128 forEach(obj, function (item) {
129 st.equal(obj[keys[index]], item, 'item at key ' + keys[index] + ' is passed as first argument')
135 t.test('second iterator argument', function (st) {
138 forEach(obj, function (item, key) {
139 st.equal(keys[counter], key, 'key ' + key + ' is passed as second argument')
145 t.test('third iterator argument', function (st) {
147 forEach(obj, function (item, key, object) {
148 st.deepEqual(obj, object, 'object is passed as third argument')
153 t.test('context argument', function (st) {
155 forEach({}, function () {
156 st.equal(this, context, '"this" is the passed context')
165 test('string', function (t) {
167 t.test('second iterator argument', function (st) {
169 st.plan(str.length * 2 + 1)
170 forEach(str, function (item, index) {
171 st.equal(counter, index, 'index ' + index + ' is passed as second argument')
172 st.equal(str.charAt(index), item)
175 st.equal(counter, str.length, 'iterates ' + str.length + ' times')