-
-
Notifications
You must be signed in to change notification settings - Fork 896
test: add tests for slice
method in dstructs/named-typed-tuple
#8057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kgryte
merged 6 commits into
stdlib-js:develop
from
Gauravkaushik-1206:create-test-file-slice-method
Sep 17, 2025
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
31c8729
test: add tests for slice method in dstructs/named-typed-tuple
Gauravkaushik-1206 b595c37
test: fix method name
kgryte 6603d5e
test: update tests
kgryte 3b2db58
test: update tests
kgryte 90e2c7a
test: add assertion
kgryte fdb5e03
test: fix broken tests
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
300 changes: 300 additions & 0 deletions
300
lib/node_modules/@stdlib/dstructs/named-typed-tuple/test/test.slice.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, 'toJSON' ), true, 'returns expected value' ); | ||
t.strictEqual( isFunction( p.toJSON ), 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(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.