Skip to content

Commit 96a1cb4

Browse files
feat(iter/cartesian-square): add test/test.js
--- 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: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 86d4ec0 commit 96a1cb4

1 file changed

Lines changed: 225 additions & 0 deletions

File tree

  • lib/node_modules/@stdlib/iter/cartesian-square/test
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 proxyquire = require( 'proxyquire' );
25+
var linspace = require( '@stdlib/array/linspace' );
26+
var iteratorSymbol = require( '@stdlib/symbol/iterator' );
27+
var iterCartesianSquare = require( './../lib' );
28+
29+
30+
// TESTS //
31+
32+
tape( 'main export is a function', function test( t ) {
33+
t.ok( true, __filename );
34+
t.strictEqual( typeof iterCartesianSquare, 'function', 'main export is a function' );
35+
t.end();
36+
});
37+
38+
tape( 'the function throws an error if provided an argument which is not a collection', function test( t ) {
39+
var values;
40+
var i;
41+
42+
values = [
43+
'5',
44+
5,
45+
NaN,
46+
true,
47+
false,
48+
null,
49+
void 0,
50+
{}
51+
];
52+
53+
for ( i = 0; i < values.length; i++ ) {
54+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
55+
}
56+
t.end();
57+
58+
function badValue( value ) {
59+
return function badValue() {
60+
iterCartesianSquare( value );
61+
};
62+
}
63+
});
64+
65+
tape( 'the function returns an iterator protocol-compliant object (cartesian-square)', function test( t ) {
66+
var expected;
67+
var values;
68+
var actual;
69+
var it;
70+
var v;
71+
72+
values = [ 1, 2 ];
73+
expected = [
74+
{
75+
'value': [ 1, 1 ],
76+
'done': false
77+
},
78+
{
79+
'value': [ 1, 2 ],
80+
'done': false
81+
},
82+
{
83+
'value': [ 2, 1 ],
84+
'done': false
85+
},
86+
{
87+
'value': [ 2, 2 ],
88+
'done': false
89+
},
90+
{
91+
'done': true
92+
}
93+
];
94+
95+
it = iterCartesianSquare( values );
96+
t.strictEqual( it.next.length, 0, 'has zero arity' );
97+
98+
actual = [];
99+
while ( true ) {
100+
v = it.next();
101+
if ( v.done ) {
102+
break;
103+
}
104+
actual.push( v );
105+
}
106+
actual.push( it.next() );
107+
108+
t.deepEqual( actual, expected, 'returns expected values' );
109+
t.end();
110+
});
111+
112+
tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) {
113+
var it;
114+
var r;
115+
116+
it = iterCartesianSquare( linspace( 0, 5, 6 ) );
117+
118+
r = it.next();
119+
t.strictEqual( typeof r.value, 'object', 'returns expected value' );
120+
t.strictEqual( r.done, false, 'returns expected value' );
121+
122+
r = it.next();
123+
t.strictEqual( typeof r.value, 'object', 'returns expected value' );
124+
t.strictEqual( r.done, false, 'returns expected value' );
125+
126+
r = it.return();
127+
t.strictEqual( r.value, void 0, 'returns expected value' );
128+
t.strictEqual( r.done, true, 'returns expected value' );
129+
130+
r = it.next();
131+
t.strictEqual( r.value, void 0, 'returns expected value' );
132+
t.strictEqual( r.done, true, 'returns expected value' );
133+
134+
t.end();
135+
});
136+
137+
tape( 'the returned iterator has a `return` method for closing an iterator (argument)', function test( t ) {
138+
var it;
139+
var r;
140+
141+
it = iterCartesianSquare( linspace( 0, 5, 6 ) );
142+
143+
r = it.next();
144+
t.strictEqual( typeof r.value, 'object', 'returns expected value' );
145+
t.strictEqual( r.done, false, 'returns expected value' );
146+
147+
r = it.next();
148+
t.strictEqual( typeof r.value, 'object', 'returns expected value' );
149+
t.strictEqual( r.done, false, 'returns expected value' );
150+
151+
r = it.return( 'finished' );
152+
t.strictEqual( r.value, 'finished', 'returns expected value' );
153+
t.strictEqual( r.done, true, 'returns expected value' );
154+
155+
r = it.next();
156+
t.strictEqual( r.value, void 0, 'returns expected value' );
157+
t.strictEqual( r.done, true, 'returns expected value' );
158+
159+
t.end();
160+
});
161+
162+
tape( 'if an environment supports `Symbol.iterator` and the provided iterator is iterable, the returned iterator is iterable', function test( t ) {
163+
var iterCartesianSquare;
164+
var it1;
165+
var it2;
166+
var x;
167+
var i;
168+
169+
iterCartesianSquare = proxyquire( './../lib/main.js', {
170+
'@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__'
171+
});
172+
173+
x = linspace( 0, 5, 6 );
174+
x[ '__ITERATOR_SYMBOL__' ] = factory;
175+
176+
it1 = iterCartesianSquare( x );
177+
t.strictEqual( typeof it1[ '__ITERATOR_SYMBOL__' ], 'function', 'has method' );
178+
t.strictEqual( it1[ '__ITERATOR_SYMBOL__' ].length, 0, 'has zero arity' );
179+
180+
it2 = it1[ '__ITERATOR_SYMBOL__' ]();
181+
t.strictEqual( typeof it2, 'object', 'returns expected value' );
182+
t.strictEqual( typeof it2.next, 'function', 'has method' );
183+
t.strictEqual( typeof it2.return, 'function', 'has method' );
184+
185+
for ( i = 0; i < 100; i++ ) {
186+
t.deepEqual( it2.next().value, it1.next().value, 'returns expected value' );
187+
}
188+
t.end();
189+
190+
function factory() {
191+
return linspace( 0, 5, 6 );
192+
}
193+
});
194+
195+
tape( 'if an environment does not support `Symbol.iterator`, the returned iterator is not "iterable"', function test( t ) {
196+
var iterCartesianSquare;
197+
var it;
198+
199+
iterCartesianSquare = proxyquire( './../lib/main.js', {
200+
'@stdlib/symbol/iterator': false
201+
});
202+
203+
it = iterCartesianSquare( linspace( 0, 5, 6 ) );
204+
t.strictEqual( it[ iteratorSymbol ], void 0, 'does not have property' );
205+
206+
t.end();
207+
});
208+
209+
tape( 'if a provided iterator is not iterable, the returned iterator is not iterable', function test( t ) {
210+
var iterCartesianSquare;
211+
var it;
212+
var x;
213+
214+
iterCartesianSquare = proxyquire( './../lib/main.js', {
215+
'@stdlib/symbol/iterator': '__ITERATOR_SYMBOL__'
216+
});
217+
218+
x = linspace( 0, 5, 6 );
219+
x[ '__ITERATOR_SYMBOL__' ] = null;
220+
221+
it = iterCartesianSquare( x );
222+
t.strictEqual( it[ iteratorSymbol ], void 0, 'does not have property' );
223+
224+
t.end();
225+
});

0 commit comments

Comments
 (0)