diff --git a/lib/node_modules/@stdlib/dstructs/named-typed-tuple/test/test.slice.js b/lib/node_modules/@stdlib/dstructs/named-typed-tuple/test/test.slice.js new file mode 100644 index 000000000000..366fc7f73338 --- /dev/null +++ b/lib/node_modules/@stdlib/dstructs/named-typed-tuple/test/test.slice.js @@ -0,0 +1,300 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var namedtypedtuple = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof namedtypedtuple, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'a tuple has a `slice` method', function test( t ) { + var Point; + var p; + + Point = namedtypedtuple( [ 'x', 'y' ], { + 'name': 'Point' + }); + p = new Point( [ 10, 20 ] ); + + t.strictEqual( hasOwnProp( p, 'slice' ), true, 'returns expected value' ); + t.strictEqual( isFunction( p.slice ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a namedtypedtuple instance', function test( t ) { + var values; + var Point; + var p; + var i; + + Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); + p = new Point( [ 10, 20, 30 ] ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return p.slice.call( value ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not an integer', function test( t ) { + var values; + var Point; + var p; + var i; + + Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); + p = new Point( [ 10, 20, 30 ] ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return p.slice( value ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var Point; + var p; + var i; + + Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); + p = new Point( [ 10, 20, 30 ] ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return p.slice( 0, value ); + }; + } +}); + +tape( 'if called without arguments, the method returns a tuple containing the same elements as the original tuple', function test( t ) { + var expected; + var actual; + var Point; + var p; + + Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); + p = new Point( [ 10, 20, 30 ] ); + expected = { + 'x': 10, + 'y': 20, + 'z': 30 + }; + actual = p.slice(); + + t.deepEqual({ + 'x': actual.x, + 'y': actual.y, + 'z': actual.z + }, expected, 'returns expected value' ); + t.notEqual( actual, p, 'returns a new instance' ); + t.end(); +}); + +tape( 'if called with one argument, the method returns a tuple containing elements starting from a specified beginning index (inclusive)', function test( t ) { + var expected; + var actual; + var Point; + var p; + + Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] ); + p = new Point( [ 1, 2, 3, 4 ] ); + expected = { + 'y': 2, + 'z': 3, + 'w': 4 + }; + actual = p.slice( 1 ); + + t.deepEqual({ + 'y': actual.y, + 'z': actual.z, + 'w': actual.w + }, expected, 'returns expected value' ); + t.end(); +}); + +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 ) { + var expected; + var actual; + var Point; + var p; + + Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] ); + p = new Point( [ 1, 2, 3, 4 ] ); + expected = { + 'y': 2, + 'z': 3 + }; + actual = p.slice( 1, 3 ); + + t.deepEqual({ + 'y': actual.y, + 'z': actual.z + }, expected, 'returns expected value' ); + + expected = { + 'y': 2, + 'z': 3, + 'w': 4 + }; + actual = p.slice( 1, 30 ); + + t.deepEqual({ + 'y': actual.y, + 'z': actual.z, + 'w': actual.w + }, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method resolves negative indices relative to the last element', function test( t ) { + var expected; + var actual; + var Point; + var p; + + Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] ); + p = new Point( [ 1, 2, 3, 4 ] ); + + expected = { + 'y': 2, + 'z': 3 + }; + actual = p.slice( -3, -1 ); + t.deepEqual({ + 'y': actual.y, + 'z': actual.z + }, expected, 'returns expected value' ); + + expected = { + 'x': 1, + 'y': 2 + }; + actual = p.slice( -30, -2 ); + t.deepEqual({ + 'x': actual.x, + 'y': actual.y + }, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns null if a resolved beginning index exceeds a resolved ending index', function test( t ) { + var actual; + var Point; + var p; + + Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] ); + p = new Point( [ 1, 2, 3, 4 ] ); + actual = p.slice( 2, 0 ); + + t.strictEqual( actual, null, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns null if a resolved beginning index exceeds the maximum tuple index', function test( t ) { + var actual; + var Point; + var p; + + Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); + p = new Point( [ 1, 2, 3 ] ); + actual = p.slice( 5 ); + + t.strictEqual( actual, null, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns null if a resolved ending index is less than or equal to zero', function test( t ) { + var actual; + var Point; + var p; + + Point = namedtypedtuple( [ 'x', 'y', 'z', 'w' ] ); + p = new Point( [ 1, 2, 3, 4 ] ); + + actual = p.slice( 2, -8 ); + t.strictEqual( actual, null, 'returns expected value' ); + + actual = p.slice( 1, 0 ); + t.strictEqual( actual, null, 'returns expected value' ); + t.end(); +});