Skip to content

Commit 31c8729

Browse files
test: add tests for slice method in dstructs/named-typed-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 31c8729

File tree

1 file changed

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

1 file changed

+300
-0
lines changed
Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
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, 'toJSON' ), true, 'returns expected value' );
47+
t.strictEqual( isFunction( p.toJSON ), 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 namedtypedtuple 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 provided a first argument which is not an integer', 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+
3.14,
96+
NaN,
97+
true,
98+
false,
99+
null,
100+
void 0,
101+
{},
102+
[]
103+
];
104+
for ( i = 0; i < values.length; i++ ) {
105+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
106+
}
107+
t.end();
108+
109+
function badValue( value ) {
110+
return function badValue() {
111+
return p.slice( value );
112+
};
113+
}
114+
});
115+
116+
tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) {
117+
var values;
118+
var Point;
119+
var p;
120+
var i;
121+
122+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
123+
p = new Point( [ 10, 20, 30 ] );
124+
125+
values = [
126+
'5',
127+
3.14,
128+
NaN,
129+
true,
130+
false,
131+
null,
132+
void 0,
133+
{},
134+
[]
135+
];
136+
for ( i = 0; i < values.length; i++ ) {
137+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
138+
}
139+
t.end();
140+
141+
function badValue( value ) {
142+
return function badValue() {
143+
return p.slice( 0, value );
144+
};
145+
}
146+
});
147+
148+
tape( 'if called without arguments, the method returns a tuple containing the same elements as the original tuple', function test( t ) {
149+
var expected;
150+
var actual;
151+
var Point;
152+
var p;
153+
154+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
155+
p = new Point( [ 10, 20, 30 ] );
156+
expected = {
157+
'x': 10,
158+
'y': 20,
159+
'z': 30
160+
};
161+
actual = p.slice();
162+
163+
t.deepEqual({
164+
'x': actual.x,
165+
'y': actual.y,
166+
'z': actual.z
167+
}, expected, 'returns expected value' );
168+
t.notEqual( actual, p, 'returns a new instance' );
169+
t.end();
170+
});
171+
172+
tape( 'if called with one argument, the method returns a tuple containing elements starting from a specified beginning index (inclusive)', function test( t ) {
173+
var expected;
174+
var actual;
175+
var Point;
176+
var p;
177+
178+
Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] );
179+
p = new Point( [ 1, 2, 3, 4 ] );
180+
expected = {
181+
'y': 2,
182+
'z': 3,
183+
'w': 4
184+
};
185+
actual = p.slice( 1 );
186+
187+
t.deepEqual({
188+
'y': actual.y,
189+
'z': actual.z,
190+
'w': actual.w
191+
}, expected, 'returns expected value' );
192+
t.end();
193+
});
194+
195+
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 ) {
196+
var expected;
197+
var actual;
198+
var Point;
199+
var p;
200+
201+
Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] );
202+
p = new Point( [ 1, 2, 3, 4 ] );
203+
expected = {
204+
'y': 2,
205+
'z': 3
206+
};
207+
actual = p.slice( 1, 3 );
208+
209+
t.deepEqual({
210+
'y': actual.y,
211+
'z': actual.z
212+
}, expected, 'returns expected value' );
213+
214+
expected = {
215+
'y': 2,
216+
'z': 3,
217+
'w': 4
218+
};
219+
actual = p.slice( 1, 30 );
220+
221+
t.deepEqual({
222+
'y': actual.y,
223+
'z': actual.z,
224+
'w': actual.w
225+
}, expected, 'returns expected value' );
226+
t.end();
227+
});
228+
229+
tape( 'the method resolves negative indices relative to the last element', function test( t ) {
230+
var expected;
231+
var actual;
232+
var Point;
233+
var p;
234+
235+
Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] );
236+
p = new Point( [ 1, 2, 3, 4 ] );
237+
238+
expected = {
239+
'y': 2,
240+
'z': 3
241+
};
242+
actual = p.slice( -3, -1 );
243+
t.deepEqual({
244+
'y': actual.y,
245+
'z': actual.z
246+
}, expected, 'returns expected value' );
247+
248+
expected = {
249+
'x': 1,
250+
'y': 2
251+
};
252+
actual = p.slice( -30, -2 );
253+
t.deepEqual({
254+
'x': actual.x,
255+
'y': actual.y
256+
}, expected, 'returns expected value' );
257+
t.end();
258+
});
259+
260+
tape( 'the method returns null if a resolved beginning index exceeds a resolved ending index', function test( t ) {
261+
var actual;
262+
var Point;
263+
var p;
264+
265+
Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] );
266+
p = new Point( [ 1, 2, 3, 4 ] );
267+
actual = p.slice( 2, 0 );
268+
269+
t.strictEqual( actual, null, 'returns expected value' );
270+
t.end();
271+
});
272+
273+
tape( 'the method returns null if a resolved beginning index exceeds the maximum tuple index', function test( t ) {
274+
var actual;
275+
var Point;
276+
var p;
277+
278+
Point = namedtypedtuple( [ 'x', 'y', 'z' ] );
279+
p = new Point( [ 1, 2, 3 ] );
280+
actual = p.slice( 5 );
281+
282+
t.strictEqual( actual, null, 'returns expected value' );
283+
t.end();
284+
});
285+
286+
tape( 'the method returns null if a resolved ending index is less than or equal to zero', function test( t ) {
287+
var actual;
288+
var Point;
289+
var p;
290+
291+
Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] );
292+
p = new Point( [ 1, 2, 3, 4 ] );
293+
294+
actual = p.slice( 2, -8 );
295+
t.strictEqual( actual, null, 'returns expected value' );
296+
297+
actual = p.slice( 1, 0 );
298+
t.strictEqual( actual, null, 'returns expected value' );
299+
t.end();
300+
});

0 commit comments

Comments
 (0)