Skip to content

Commit 9629e2e

Browse files
test: add tests for slice method in dstructs/named-typed-tuple
PR-URL: #8057 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 6fe6a73 commit 9629e2e

File tree

1 file changed

+382
-0
lines changed
  • lib/node_modules/@stdlib/dstructs/named-typed-tuple/test

1 file changed

+382
-0
lines changed
Lines changed: 382 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,382 @@
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 hasOwnProp = require( '@stdlib/assert/has-own-property' );
25+
var isFunction = require( '@stdlib/assert/is-function' );
26+
var namedtypedtuple = 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 namedtypedtuple, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'a tuple has a `slice` method', function test( t ) {
38+
var Point;
39+
var p;
40+
41+
Point = namedtypedtuple( [ 'x', 'y' ], {
42+
'name': 'Point'
43+
});
44+
p = new Point( [ 10, 20 ] );
45+
46+
t.strictEqual( hasOwnProp( p, 'slice' ), true, 'returns expected value' );
47+
t.strictEqual( isFunction( p.slice ), true, 'returns expected value' );
48+
t.end();
49+
});
50+
51+
tape( 'the method throws an error if invoked with a `this` context which is not a tuple instance', function test( t ) {
52+
var values;
53+
var Point;
54+
var p;
55+
var i;
56+
57+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
58+
p = new Point( [ 10, 20, 30 ] );
59+
60+
values = [
61+
'5',
62+
5,
63+
NaN,
64+
true,
65+
false,
66+
null,
67+
void 0,
68+
{},
69+
[],
70+
function noop() {}
71+
];
72+
for ( i = 0; i < values.length; i++ ) {
73+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
74+
}
75+
t.end();
76+
77+
function badValue( value ) {
78+
return function badValue() {
79+
return p.slice.call( value );
80+
};
81+
}
82+
});
83+
84+
tape( 'the method throws an error if invoked with a `this` context which is not a tuple instance (start)', function test( t ) {
85+
var values;
86+
var Point;
87+
var p;
88+
var i;
89+
90+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
91+
p = new Point( [ 10, 20, 30 ] );
92+
93+
values = [
94+
'5',
95+
5,
96+
NaN,
97+
true,
98+
false,
99+
null,
100+
void 0,
101+
{},
102+
[],
103+
function noop() {}
104+
];
105+
for ( i = 0; i < values.length; i++ ) {
106+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
107+
}
108+
t.end();
109+
110+
function badValue( value ) {
111+
return function badValue() {
112+
return p.slice.call( value, 0 );
113+
};
114+
}
115+
});
116+
117+
tape( 'the method throws an error if invoked with a `this` context which is not a tuple instance (start, end)', function test( t ) {
118+
var values;
119+
var Point;
120+
var p;
121+
var i;
122+
123+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
124+
p = new Point( [ 10, 20, 30 ] );
125+
126+
values = [
127+
'5',
128+
5,
129+
NaN,
130+
true,
131+
false,
132+
null,
133+
void 0,
134+
{},
135+
[],
136+
function noop() {}
137+
];
138+
for ( i = 0; i < values.length; i++ ) {
139+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
140+
}
141+
t.end();
142+
143+
function badValue( value ) {
144+
return function badValue() {
145+
return p.slice.call( value, 0, 1 );
146+
};
147+
}
148+
});
149+
150+
tape( 'the method throws an error if provided a first argument which is not an integer', function test( t ) {
151+
var values;
152+
var Point;
153+
var p;
154+
var i;
155+
156+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
157+
p = new Point( [ 10, 20, 30 ] );
158+
159+
values = [
160+
'5',
161+
3.14,
162+
NaN,
163+
true,
164+
false,
165+
null,
166+
void 0,
167+
{},
168+
[]
169+
];
170+
for ( i = 0; i < values.length; i++ ) {
171+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
172+
}
173+
t.end();
174+
175+
function badValue( value ) {
176+
return function badValue() {
177+
return p.slice( value );
178+
};
179+
}
180+
});
181+
182+
tape( 'the method throws an error if provided a first argument which is not an integer (end)', function test( t ) {
183+
var values;
184+
var Point;
185+
var p;
186+
var i;
187+
188+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
189+
p = new Point( [ 10, 20, 30 ] );
190+
191+
values = [
192+
'5',
193+
3.14,
194+
NaN,
195+
true,
196+
false,
197+
null,
198+
void 0,
199+
{},
200+
[]
201+
];
202+
for ( i = 0; i < values.length; i++ ) {
203+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
204+
}
205+
t.end();
206+
207+
function badValue( value ) {
208+
return function badValue() {
209+
return p.slice( value, 1 );
210+
};
211+
}
212+
});
213+
214+
tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) {
215+
var values;
216+
var Point;
217+
var p;
218+
var i;
219+
220+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
221+
p = new Point( [ 10, 20, 30 ] );
222+
223+
values = [
224+
'5',
225+
3.14,
226+
NaN,
227+
true,
228+
false,
229+
null,
230+
void 0,
231+
{},
232+
[]
233+
];
234+
for ( i = 0; i < values.length; i++ ) {
235+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
236+
}
237+
t.end();
238+
239+
function badValue( value ) {
240+
return function badValue() {
241+
return p.slice( 0, value );
242+
};
243+
}
244+
});
245+
246+
tape( 'if called without arguments, the method returns a tuple containing the same elements as the original tuple', function test( t ) {
247+
var expected;
248+
var actual;
249+
var Point;
250+
var p;
251+
252+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
253+
p = new Point( [ 10, 20, 30 ] );
254+
255+
expected = {
256+
'x': 10,
257+
'y': 20,
258+
'z': 30
259+
};
260+
actual = p.slice();
261+
262+
t.notEqual( actual, p, 'returns expected value' );
263+
t.deepEqual( actual.fields, [ 'x', 'y', 'z' ], 'returns expected value' );
264+
265+
actual = {
266+
'x': actual.x,
267+
'y': actual.y,
268+
'z': actual.z
269+
};
270+
t.deepEqual( actual, expected, 'returns expected value' );
271+
t.end();
272+
});
273+
274+
tape( 'if provided one argument, the method returns a tuple containing elements starting from a specified index (inclusive)', function test( t ) {
275+
var expected;
276+
var actual;
277+
var Point;
278+
var p;
279+
280+
Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] );
281+
p = new Point( [ 1, 2, 3, 4 ] );
282+
283+
expected = [ 2, 3, 4 ];
284+
actual = p.slice( 1 );
285+
286+
t.notEqual( actual, p, 'returns expected value' );
287+
t.deepEqual( actual, expected, 'returns expected value' );
288+
t.deepEqual( actual.fields, [ 'y', 'z', 'w' ], 'returns expected value' );
289+
t.end();
290+
});
291+
292+
tape( 'if provided two arguments, the method returns a tuple containing elements starting from a specified start index (inclusive) and ending at a specified end index (exclusive)', function test( t ) {
293+
var expected;
294+
var actual;
295+
var Point;
296+
var p;
297+
298+
Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] );
299+
p = new Point( [ 1, 2, 3, 4 ] );
300+
301+
expected = [ 2, 3 ];
302+
actual = p.slice( 1, 3 );
303+
304+
t.notEqual( actual, p, 'returns expected value' );
305+
t.deepEqual( actual, expected, 'returns expected value' );
306+
t.deepEqual( actual.fields, [ 'y', 'z' ], 'returns expected value' );
307+
308+
expected = [ 2, 3, 4 ];
309+
actual = p.slice( 1, 30 );
310+
311+
t.notEqual( actual, p, 'returns expected value' );
312+
t.deepEqual( actual, expected, 'returns expected value' );
313+
t.deepEqual( actual.fields, [ 'y', 'z', 'w' ], 'returns expected value' );
314+
t.end();
315+
});
316+
317+
tape( 'the method supports negative indices', function test( t ) {
318+
var expected;
319+
var actual;
320+
var Point;
321+
var p;
322+
323+
Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] );
324+
p = new Point( [ 1, 2, 3, 4 ] );
325+
326+
expected = [ 2, 3 ];
327+
actual = p.slice( -3, -1 );
328+
329+
t.notEqual( actual, p, 'returns expected value' );
330+
t.deepEqual( actual, expected, 'returns expected value' );
331+
t.deepEqual( actual.fields, [ 'y', 'z' ], 'returns expected value' );
332+
333+
expected = [ 1, 2 ];
334+
actual = p.slice( -30, -2 );
335+
336+
t.notEqual( actual, p, 'returns expected value' );
337+
t.deepEqual( actual, expected, 'returns expected value' );
338+
t.deepEqual( actual.fields, [ 'x', 'y' ], 'returns expected value' );
339+
t.end();
340+
});
341+
342+
tape( 'the method returns null if a resolved beginning index exceeds a resolved ending index', function test( t ) {
343+
var actual;
344+
var Point;
345+
var p;
346+
347+
Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] );
348+
p = new Point( [ 1, 2, 3, 4 ] );
349+
actual = p.slice( 2, 0 );
350+
351+
t.strictEqual( actual, null, 'returns expected value' );
352+
t.end();
353+
});
354+
355+
tape( 'the method returns null if a resolved beginning index exceeds the maximum tuple index', function test( t ) {
356+
var actual;
357+
var Point;
358+
var p;
359+
360+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
361+
p = new Point( [ 1, 2, 3 ] );
362+
actual = p.slice( 5 );
363+
364+
t.strictEqual( actual, null, 'returns expected value' );
365+
t.end();
366+
});
367+
368+
tape( 'the method returns null if a resolved ending index is less than or equal to zero', function test( t ) {
369+
var actual;
370+
var Point;
371+
var p;
372+
373+
Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] );
374+
p = new Point( [ 1, 2, 3, 4 ] );
375+
376+
actual = p.slice( 2, -8 );
377+
t.strictEqual( actual, null, 'returns expected value' );
378+
379+
actual = p.slice( 1, 0 );
380+
t.strictEqual( actual, null, 'returns expected value' );
381+
t.end();
382+
});

0 commit comments

Comments
 (0)