Skip to content

Commit e2cda3f

Browse files
test: create a test file for forEach() method of dstructs/named-type-tuple
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent fe96d97 commit e2cda3f

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var tape = require( 'tape' );
24+
var hasProp = require( '@stdlib/assert/has-property' );
25+
var isFunction = require( '@stdlib/assert/is-function' );
26+
var namedtypetuple = require( './../lib' );
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.strictEqual( typeof namedtypetuple, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'a tuple has a `forEach` method', function test( t ) {
38+
var Point;
39+
var p;
40+
41+
Point = namedtypetuple( [ 'x', 'y' ] );
42+
p = new Point();
43+
44+
t.strictEqual( hasProp( p, 'forEach' ), true, 'has property' );
45+
t.strictEqual( isFunction( p.forEach ), true, 'has method' );
46+
t.end();
47+
});
48+
49+
tape( 'the method throws an error if invoked with a `this` context which is not a namedtypetuple instance', function test( t ) {
50+
var values;
51+
var Point;
52+
var p;
53+
var i;
54+
55+
Point = namedtypetuple( [ 'x', 'y' ] );
56+
p = new Point( [ 1, 2 ] );
57+
58+
values = [
59+
'5',
60+
5,
61+
NaN,
62+
true,
63+
false,
64+
null,
65+
void 0,
66+
{},
67+
[],
68+
function noop() {}
69+
];
70+
for ( i = 0; i < values.length; i++ ) {
71+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
72+
}
73+
t.end();
74+
75+
function badValue( value ) {
76+
return function badValue() {
77+
return p.forEach.call( value, fcn );
78+
};
79+
}
80+
81+
function fcn( v ) {
82+
void v;
83+
}
84+
});
85+
86+
tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) {
87+
var values;
88+
var Point;
89+
var p;
90+
var i;
91+
92+
Point = namedtypetuple( [ 'x', 'y' ] );
93+
p = new Point( [ 1, 2 ] );
94+
95+
values = [
96+
'5',
97+
3.14,
98+
NaN,
99+
true,
100+
false,
101+
null,
102+
void 0,
103+
{},
104+
[]
105+
];
106+
for ( i = 0; i < values.length; i++ ) {
107+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
108+
}
109+
t.end();
110+
111+
function badValue( value ) {
112+
return function badValue() {
113+
return p.forEach( value );
114+
};
115+
}
116+
});
117+
118+
tape( 'the method returns `undefined`', function test( t ) {
119+
var Point;
120+
var out;
121+
var p;
122+
123+
Point = namedtypetuple( [ 'x', 'y' ] );
124+
p = new Point( [ 10, 20 ] );
125+
out = p.forEach( fcn );
126+
127+
t.strictEqual( out, void 0, 'returns expected value' );
128+
t.end();
129+
130+
function fcn( v ) {
131+
void v;
132+
}
133+
});
134+
135+
tape( 'the method invokes a provided function for each element in a tuple', function test( t ) {
136+
var fieldNames;
137+
var indices;
138+
var values;
139+
var tuples;
140+
var Point;
141+
var p;
142+
143+
indices = [];
144+
values = [];
145+
fieldNames = [];
146+
tuples = [];
147+
148+
Point = namedtypetuple( [ 'x', 'y', 'z' ] );
149+
p = new Point( [ 1, 2, 3 ] );
150+
p.forEach( fcn );
151+
152+
t.deepEqual( values, [ 1, 2, 3 ], 'returns expected values' );
153+
t.deepEqual( indices, [ 0, 1, 2 ], 'returns expected indices' );
154+
t.deepEqual( fieldNames, [ 'x', 'y', 'z' ], 'returns expected field names' );
155+
t.strictEqual( tuples[ 0 ], p, 'returns expected tuple reference' );
156+
t.strictEqual( tuples[ 1 ], p, 'returns expected tuple reference' );
157+
t.strictEqual( tuples[ 2 ], p, 'returns expected tuple reference' );
158+
159+
t.end();
160+
161+
function fcn( v, i, fieldName, tuple ) {
162+
values.push( v );
163+
indices.push( i );
164+
fieldNames.push( fieldName );
165+
tuples.push( tuple );
166+
}
167+
});
168+
169+
tape( 'the method supports providing an execution context', function test( t ) {
170+
var Point;
171+
var ctx;
172+
var p;
173+
174+
ctx = {
175+
'count': 0
176+
};
177+
Point = namedtypetuple( [ 'x', 'y', 'z' ] );
178+
p = new Point( [ 1, 2, 3 ] );
179+
p.forEach( fcn, ctx );
180+
181+
t.strictEqual( ctx.count, 3, 'returns expected value' );
182+
183+
t.end();
184+
185+
function fcn() {
186+
this.count += 1; // eslint-disable-line no-invalid-this
187+
}
188+
});

0 commit comments

Comments
 (0)