|
21 | 21 | // MODULES //
|
22 | 22 |
|
23 | 23 | var tape = require( 'tape' );
|
| 24 | +var proxyquire = require( 'proxyquire' ); |
24 | 25 | var indexOf = require( '@stdlib/utils/index-of' );
|
25 | 26 | var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' );
|
| 27 | +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); |
26 | 28 | var objectKeys = require( './../lib/polyfill.js' );
|
27 | 29 |
|
28 | 30 |
|
@@ -95,6 +97,145 @@ tape( 'the function returns an array of an object\'s own enumerable property nam
|
95 | 97 | t.end();
|
96 | 98 | });
|
97 | 99 |
|
| 100 | +tape( 'the function returns an array of an object\'s own enumerable property names (string; non-enumerable character indices)', function test( t ) { |
| 101 | + var objectKeys; |
| 102 | + var expected; |
| 103 | + var actual; |
| 104 | + var idx; |
| 105 | + var i; |
| 106 | + |
| 107 | + objectKeys = proxyquire( './../lib/polyfill.js', { |
| 108 | + '@stdlib/assert/has-own-property': mock |
| 109 | + }); |
| 110 | + |
| 111 | + expected = [ '0', '1', '2' ]; |
| 112 | + actual = objectKeys( 'foo' ); |
| 113 | + |
| 114 | + for ( i = 0; i < expected.length; i++ ) { |
| 115 | + idx = indexOf( actual, expected[ i ] ); |
| 116 | + t.strictEqual( idx !== -1, true, 'contains property name: '+expected[i] ); |
| 117 | + } |
| 118 | + t.end(); |
| 119 | + |
| 120 | + function mock( obj, prop ) { |
| 121 | + if ( obj === 'foo' ) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + return hasOwnProp( obj, prop ); |
| 125 | + } |
| 126 | +}); |
| 127 | + |
| 128 | +tape( 'the function returns an array of an object\'s own enumerable property names (function; environments having enumerable prototype bug)', function test( t ) { |
| 129 | + var objectKeys; |
| 130 | + var expected; |
| 131 | + var actual; |
| 132 | + |
| 133 | + function noop() { |
| 134 | + // No-op... |
| 135 | + } |
| 136 | + |
| 137 | + noop.a = 'b'; |
| 138 | + |
| 139 | + objectKeys = proxyquire( './../lib/polyfill.js', { |
| 140 | + './has_enumerable_prototype_bug.js': true |
| 141 | + }); |
| 142 | + |
| 143 | + expected = [ 'a' ]; |
| 144 | + actual = objectKeys( noop ); |
| 145 | + |
| 146 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 147 | + t.end(); |
| 148 | +}); |
| 149 | + |
| 150 | +tape( 'the function returns an array of an object\'s own enumerable property names (environments having non-enumerable properties bug)', function test( t ) { |
| 151 | + var objectKeys; |
| 152 | + var expected; |
| 153 | + var actual; |
| 154 | + var FLG; |
| 155 | + var obj; |
| 156 | + var idx; |
| 157 | + var i; |
| 158 | + |
| 159 | + FLG = true; |
| 160 | + |
| 161 | + objectKeys = proxyquire( './../lib/polyfill.js', { |
| 162 | + './has_non_enumerable_properties_bug.js': true, |
| 163 | + '@stdlib/assert/has-own-property': mock |
| 164 | + }); |
| 165 | + |
| 166 | + obj = { |
| 167 | + 'a': 'b', |
| 168 | + 'toString': toString |
| 169 | + }; |
| 170 | + expected = [ 'a', 'toString' ]; |
| 171 | + actual = objectKeys( obj ); |
| 172 | + |
| 173 | + for ( i = 0; i < expected.length; i++ ) { |
| 174 | + idx = indexOf( actual, expected[ i ] ); |
| 175 | + t.strictEqual( idx !== -1, true, 'contains property name: '+expected[i] ); |
| 176 | + } |
| 177 | + t.end(); |
| 178 | + |
| 179 | + function toString() { |
| 180 | + return 'beep'; |
| 181 | + } |
| 182 | + |
| 183 | + function mock( obj, prop ) { |
| 184 | + if ( prop === 'toString' && hasOwnProp( obj, prop ) ) { |
| 185 | + FLG = !FLG; |
| 186 | + return FLG; |
| 187 | + } |
| 188 | + return hasOwnProp( obj, prop ); |
| 189 | + } |
| 190 | +}); |
| 191 | + |
| 192 | +tape( 'the function returns an array of an object\'s own enumerable property names (environments having non-enumerable properties and constructor equality bugs)', function test( t ) { |
| 193 | + var objectKeys; |
| 194 | + var expected; |
| 195 | + var actual; |
| 196 | + var FLG; |
| 197 | + var obj; |
| 198 | + var idx; |
| 199 | + var i; |
| 200 | + |
| 201 | + FLG = true; |
| 202 | + |
| 203 | + objectKeys = proxyquire( './../lib/polyfill.js', { |
| 204 | + './has_non_enumerable_properties_bug.js': true, |
| 205 | + '@stdlib/assert/has-own-property': mock |
| 206 | + }); |
| 207 | + |
| 208 | + obj = { |
| 209 | + 'a': 'b', |
| 210 | + 'toString': toString, |
| 211 | + 'constructor': { |
| 212 | + 'prototype': null |
| 213 | + } |
| 214 | + }; |
| 215 | + obj.constructor.prototype = obj; // circular |
| 216 | + |
| 217 | + expected = [ 'a', 'toString' ]; |
| 218 | + actual = objectKeys( obj ); |
| 219 | + |
| 220 | + for ( i = 0; i < expected.length; i++ ) { |
| 221 | + idx = indexOf( actual, expected[ i ] ); |
| 222 | + t.strictEqual( idx !== -1, true, 'contains property name: '+expected[i] ); |
| 223 | + } |
| 224 | + t.end(); |
| 225 | + |
| 226 | + function toString() { |
| 227 | + return 'beep'; |
| 228 | + } |
| 229 | + |
| 230 | + function mock( obj, prop ) { |
| 231 | + if ( prop === 'toString' && hasOwnProp( obj, prop ) ) { |
| 232 | + FLG = !FLG; |
| 233 | + return FLG; |
| 234 | + } |
| 235 | + return hasOwnProp( obj, prop ); |
| 236 | + } |
| 237 | +}); |
| 238 | + |
98 | 239 | tape( 'the function ignores non-enumerable properties', function test( t ) {
|
99 | 240 | var expected;
|
100 | 241 | var actual;
|
|
0 commit comments