-
-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathtest.js
133 lines (108 loc) · 2.92 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
// MODULES //
var tape = require( 'tape' );
var proxyquire = require( 'proxyquire' );
var detect = require( './../lib' );
// VARIABLES //
var hasUint8ClampedArray = ( typeof Uint8ClampedArray === 'function' );
// TESTS //
tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.strictEqual( typeof detect, 'function', 'main export is a function' );
t.end();
});
tape( 'feature detection result is a boolean', function test( t ) {
t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' );
t.end();
});
tape( 'if `Uint8ClampedArray` is supported, detection result is `true`', function test( t ) {
var mocked;
if ( hasUint8ClampedArray ) {
t.strictEqual( detect(), true, 'detection result is `true`' );
} else {
t.strictEqual( detect(), false, 'detection result is `false`' );
}
mocked = proxyquire( './../lib/detect_uint8clampedarray_support.js', {
'./uint8clampedarray.js': Mock,
'@stdlib/utils/is-uint8array-clamped': isArray
});
t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' );
t.end();
function isArray() {
return true;
}
function Mock( arr ) {
var out = [];
var v;
var i;
for ( i = 0; i < arr.length; i++ ) {
v = arr[ i ];
if ( v < 0 ) {
v = 0;
} else if ( v > 255 ) {
v = 255;
}
out.push( v );
}
return out;
}
});
tape( 'if `Uint8ClampedArray` is not supported, detection result is `false`', function test( t ) {
var mocked;
if ( hasUint8ClampedArray ) {
t.strictEqual( detect(), true, 'detection result is `true`' );
} else {
t.strictEqual( detect(), false, 'detection result is `false`' );
}
mocked = proxyquire( './../lib/detect_uint8clampedarray_support.js', {
'./uint8clampedarray.js': {}
});
t.strictEqual( mocked(), false, 'detection result is `false`' );
mocked = proxyquire( './../lib/detect_uint8clampedarray_support.js', {
'./uint8clampedarray.js': Mock1
});
t.strictEqual( mocked(), false, 'detection result is `false`' );
mocked = proxyquire( './../lib/detect_uint8clampedarray_support.js', {
'./uint8clampedarray.js': Mock2,
'@stdlib/utils/is-uint8array-clamped': isArray
});
t.strictEqual( mocked(), false, 'detection result is `false`' );
mocked = proxyquire( './../lib/detect_uint8clampedarray_support.js', {
'./uint8clampedarray.js': Mock3,
'@stdlib/utils/is-uint8array-clamped': isArray
});
t.strictEqual( mocked(), false, 'detection result is `false`' );
t.end();
function isArray() {
return true;
}
function Mock1() {
// Not a typed array:
return [];
}
function Mock2( arr ) {
var out = [];
var v;
var i;
// Does not clamp...
for ( i = 0; i < arr.length; i++ ) {
v = arr[ i ];
out.push( v );
}
return out;
}
function Mock3( arr ) {
var out = [];
var v;
var i;
// Does not clamp...
for ( i = 0; i < arr.length; i++ ) {
v = arr[ i ];
if ( v < 0 ) {
v = 0;
}
out.push( v );
}
return out;
}
});