Pull merge.
[yaffs-website] / node_modules / for-each / test / test.js
1 var test = require("tape")
2     , forEach = require("..")
3
4 test("forEach calls each iterator", function (t) {
5     var count = 0
6     t.plan(4)
7     forEach({ a: 1, b: 2 }, function (value, key) {
8         if (count === 0) {
9             t.equal(value, 1)
10             t.equal(key, "a")
11         } else {
12             t.equal(value, 2)
13             t.equal(key, "b")
14         }
15         count++
16     })
17 })
18
19 test("forEach calls iterator with correct this value", function (t) {
20     var thisValue = {}
21
22     t.plan(1)
23
24     forEach([0], function () {
25         t.equal(this, thisValue)
26     }, thisValue)
27 })
28
29 test('second argument: iterator', function (t) {
30     var arr = []
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')
43     }
44     t.end()
45 })
46
47 test('array', function (t) {
48     var arr = [1, 2, 3]
49
50     t.test('iterates over every item', function (st) {
51         var index = 0
52         forEach(arr, function () { index += 1 })
53         st.equal(index, arr.length, 'iterates ' + arr.length + ' times')
54         st.end()
55     })
56
57     t.test('first iterator argument', function (st) {
58         var index = 0
59         st.plan(arr.length)
60         forEach(arr, function (item) {
61             st.equal(arr[index], item, 'item ' + index + ' is passed as first argument')
62             index += 1
63         })
64         st.end();
65     })
66
67     t.test('second iterator argument', function (st) {
68         var counter = 0
69         st.plan(arr.length)
70         forEach(arr, function (item, index) {
71             st.equal(counter, index, 'index ' + index + ' is passed as second argument')
72             counter += 1
73         })
74         st.end();
75     })
76
77     t.test('third iterator argument', function (st) {
78         st.plan(arr.length)
79         forEach(arr, function (item, index, array) {
80             st.deepEqual(arr, array, 'array is passed as third argument')
81         })
82         st.end();
83     })
84
85     t.test('context argument', function (st) {
86         var context = {}
87         forEach([], function () {
88             st.equal(this, context, '"this" is the passed context')
89         }, context)
90         st.end()
91     })
92
93     t.end()
94 })
95
96 test('object', function (t) {
97     var obj = {
98         a: 1,
99         b: 2,
100         c: 3
101     }
102     var keys = ['a', 'b', 'c']
103
104     var F = function () {
105         this.a = 1
106         this.b = 2
107     }
108     F.prototype.c = 3
109     var fKeys = ['a', 'b']
110
111     t.test('iterates over every object literal key', function (st) {
112         var counter = 0
113         forEach(obj, function () { counter += 1 })
114         st.equal(counter, keys.length, 'iterated ' + counter + ' times')
115         st.end()
116     })
117
118     t.test('iterates only over own keys', function (st) {
119         var counter = 0
120         forEach(new F(), function () { counter += 1 })
121         st.equal(counter, fKeys.length, 'iterated ' + fKeys.length + ' times')
122         st.end()
123     })
124
125     t.test('first iterator argument', function (st) {
126         var index = 0
127         st.plan(keys.length)
128         forEach(obj, function (item) {
129             st.equal(obj[keys[index]], item, 'item at key ' + keys[index] + ' is passed as first argument')
130             index += 1
131         })
132         st.end()
133     })
134
135     t.test('second iterator argument', function (st) {
136         var counter = 0
137         st.plan(keys.length)
138         forEach(obj, function (item, key) {
139             st.equal(keys[counter], key, 'key ' + key + ' is passed as second argument')
140             counter += 1
141         })
142         st.end()
143     })
144
145     t.test('third iterator argument', function (st) {
146         st.plan(keys.length)
147         forEach(obj, function (item, key, object) {
148             st.deepEqual(obj, object, 'object is passed as third argument')
149         })
150         st.end()
151     })
152
153     t.test('context argument', function (st) {
154         var context = {}
155         forEach({}, function () {
156             st.equal(this, context, '"this" is the passed context')
157         }, context)
158         st.end()
159     })
160
161     t.end()
162 })
163
164
165 test('string', function (t) {
166     var str = 'str'
167     t.test('second iterator argument', function (st) {
168         var counter = 0
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)
173             counter += 1
174         })
175         st.equal(counter, str.length, 'iterates ' + str.length + ' times')
176         st.end()
177     })
178     t.end()
179 })
180