-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test: add missing tests to dstructs/named-typed-tuple
#10941
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
Open
headlessNode
wants to merge
4
commits into
stdlib-js:develop
Choose a base branch
from
headlessNode:test-nmd-tt
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+6,087
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1920b07
test: add missing tests to dstructs/named-typed-tuple
headlessNode f0f8986
Merge branch 'stdlib-js:develop' into test-nmd-tt
headlessNode beae3d8
test: add tests
headlessNode bf4697a
Merge remote-tracking branch 'upstream/develop' into test-nmd-tt
stdlib-bot 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
149 changes: 149 additions & 0 deletions
149
lib/node_modules/@stdlib/dstructs/named-typed-tuple/test/test.at.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,149 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 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 hasProp = require( '@stdlib/assert/has-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 an `at` method', function test( t ) { | ||
| var Point; | ||
| var p; | ||
|
|
||
| Point = namedtypedtuple( [ 'x', 'y' ] ); | ||
| p = new Point(); | ||
|
|
||
| t.strictEqual( hasProp( p, 'at' ), true, 'returns expected value' ); | ||
| t.strictEqual( isFunction( p.at ), true, 'returns expected value' ); | ||
| t.end(); | ||
| }); | ||
|
|
||
| tape( 'the method throws an error if invoked with a `this` context which is not a tuple instance', function test( t ) { | ||
| var values; | ||
| var Point; | ||
| var p; | ||
| var i; | ||
|
|
||
| Point = namedtypedtuple( [ 'x', 'y' ] ); | ||
| p = new Point( [ 1, 2 ] ); | ||
|
|
||
| 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.at.call( value, 0 ); | ||
| }; | ||
| } | ||
| }); | ||
|
|
||
| tape( 'the method returns the tuple element at a given index', function test( t ) { | ||
| var Point; | ||
| var v; | ||
| var p; | ||
|
|
||
| Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); | ||
| p = new Point( [ 1, 2, 3 ] ); | ||
|
|
||
| v = p.at( 0 ); | ||
| t.strictEqual( v, 1, 'returns expected value' ); | ||
|
|
||
| v = p.at( 1 ); | ||
| t.strictEqual( v, 2, 'returns expected value' ); | ||
|
|
||
| v = p.at( 2 ); | ||
| t.strictEqual( v, 3, 'returns expected value' ); | ||
|
|
||
| // Field values remain unchanged: | ||
| t.strictEqual( p.x, 1, 'returns expected value' ); | ||
| t.strictEqual( p.y, 2, 'returns expected value' ); | ||
| t.strictEqual( p.z, 3, 'returns expected value' ); | ||
| t.end(); | ||
| }); | ||
|
|
||
| tape( 'the method supports negative indices', function test( t ) { | ||
| var Point; | ||
| var v; | ||
| var p; | ||
|
|
||
| Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); | ||
| p = new Point( [ 1, 2, 3 ] ); | ||
|
|
||
| v = p.at( -1 ); | ||
| t.strictEqual( v, 3, 'returns expected value' ); | ||
|
|
||
| v = p.at( -2 ); | ||
| t.strictEqual( v, 2, 'returns expected value' ); | ||
|
|
||
| v = p.at( -3 ); | ||
| t.strictEqual( v, 1, 'returns expected value' ); | ||
|
|
||
| // Field values remain unchanged: | ||
| t.strictEqual( p.x, 1, 'returns expected value' ); | ||
| t.strictEqual( p.y, 2, 'returns expected value' ); | ||
| t.strictEqual( p.z, 3, 'returns expected value' ); | ||
|
Comment on lines
+123
to
+126
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment. |
||
| t.end(); | ||
| }); | ||
|
|
||
| tape( 'the method returns `undefined` if the index is out of bounds', function test( t ) { | ||
| var Point; | ||
| var v; | ||
| var p; | ||
|
|
||
| Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); | ||
| p = new Point( [ 1, 2, 3 ] ); | ||
|
|
||
| v = p.at( 10 ); | ||
| t.strictEqual( v, void 0, 'returns expected value' ); | ||
|
|
||
| v = p.at( -10 ); | ||
| t.strictEqual( v, void 0, 'returns expected value' ); | ||
|
|
||
| // Field values remain unchanged: | ||
| t.strictEqual( p.x, 1, 'returns expected value' ); | ||
| t.strictEqual( p.y, 2, 'returns expected value' ); | ||
| t.strictEqual( p.z, 3, 'returns expected value' ); | ||
|
Comment on lines
+144
to
+147
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment. |
||
| t.end(); | ||
| }); | ||
189 changes: 189 additions & 0 deletions
189
lib/node_modules/@stdlib/dstructs/named-typed-tuple/test/test.entries.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,189 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 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 hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); | ||
| var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); | ||
| var namedtypedtuple = require( './../lib' ); | ||
|
|
||
|
|
||
| // VARIABLES // | ||
|
|
||
| var HAS_ITERATOR_SYMBOL = hasIteratorSymbolSupport(); | ||
|
|
||
|
|
||
| // 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 an `entries` method', function test( t ) { | ||
| var Point; | ||
| var p; | ||
|
|
||
| Point = namedtypedtuple( [ 'x', 'y' ] ); | ||
| p = new Point(); | ||
|
|
||
| t.strictEqual( hasOwnProp( p, 'entries' ), true, 'returns expected value' ); | ||
| t.strictEqual( isFunction( p.entries ), true, 'returns expected value' ); | ||
| t.end(); | ||
| }); | ||
|
|
||
| tape( 'the method throws an error if invoked with a `this` context which is not a tuple instance', function test( t ) { | ||
| var values; | ||
| var Point; | ||
| var p; | ||
| var i; | ||
|
|
||
| Point = namedtypedtuple( [ 'x', 'y' ] ); | ||
| p = new Point( [ 1, 2 ] ); | ||
|
|
||
| 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.entries.call( value ); | ||
| }; | ||
| } | ||
| }); | ||
|
|
||
| tape( 'the method returns an iterator protocol-compliant object which iterates over tuple key-value pairs', function test( t ) { | ||
| var Point; | ||
| var iter; | ||
| var v; | ||
| var p; | ||
|
|
||
| Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); | ||
| p = new Point( [ 10, 20, 30 ] ); | ||
| iter = p.entries(); | ||
|
|
||
| v = iter.next(); | ||
| t.strictEqual( v.done, false, 'returns expected value' ); | ||
| t.deepEqual( v.value, [ 0, 'x', 10 ], 'returns expected value' ); | ||
|
|
||
| v = iter.next(); | ||
| t.strictEqual( v.done, false, 'returns expected value' ); | ||
| t.deepEqual( v.value, [ 1, 'y', 20 ], 'returns expected value' ); | ||
|
|
||
| v = iter.next(); | ||
| t.strictEqual( v.done, false, 'returns expected value' ); | ||
| t.deepEqual( v.value, [ 2, 'z', 30 ], 'returns expected value' ); | ||
|
|
||
| v = iter.next(); | ||
| t.strictEqual( v.done, true, 'returns expected value' ); | ||
| t.strictEqual( v.value, void 0, 'returns expected value' ); | ||
|
|
||
| t.end(); | ||
| }); | ||
|
|
||
| tape( 'the returned iterator has a `return` method for closing an iterator', function test( t ) { | ||
| var Point; | ||
| var iter; | ||
| var v; | ||
| var p; | ||
|
|
||
| Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); | ||
| p = new Point( [ 10, 20, 30 ] ); | ||
| iter = p.entries(); | ||
|
|
||
| v = iter.next(); | ||
| t.strictEqual( v.done, false, 'returns expected value' ); | ||
|
|
||
| v = iter.return(); | ||
| t.strictEqual( v.done, true, 'returns expected value' ); | ||
|
|
||
| v = iter.next(); | ||
| t.strictEqual( v.done, true, 'returns expected value' ); | ||
|
|
||
| t.end(); | ||
| }); | ||
|
|
||
| tape( 'the returned iterator `return` method supports providing a value', function test( t ) { | ||
| var Point; | ||
| var iter; | ||
| var v; | ||
| var p; | ||
|
|
||
| Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); | ||
| p = new Point( [ 10, 20, 30 ] ); | ||
| iter = p.entries(); | ||
|
|
||
| v = iter.next(); | ||
| t.strictEqual( v.done, false, 'returns expected value' ); | ||
|
|
||
| v = iter.return( 'finished' ); | ||
| t.strictEqual( v.done, true, 'returns expected value' ); | ||
| t.strictEqual( v.value, 'finished', 'returns expected value' ); | ||
|
|
||
| v = iter.next(); | ||
| t.strictEqual( v.done, true, 'returns expected value' ); | ||
|
|
||
| t.end(); | ||
| }); | ||
|
|
||
| tape( 'if the environment supports `Symbol.iterator`, the returned iterator is iterable', function test( t ) { | ||
| var Point; | ||
| var iter1; | ||
| var iter2; | ||
| var v1; | ||
| var v2; | ||
| var p; | ||
|
|
||
| if ( !HAS_ITERATOR_SYMBOL ) { | ||
| t.pass( 'environment does not support Symbol.iterator' ); | ||
| return t.end(); | ||
| } | ||
| Point = namedtypedtuple( [ 'x', 'y', 'z' ] ); | ||
| p = new Point( [ 10, 20, 30 ] ); | ||
| iter1 = p.entries(); | ||
|
|
||
| t.strictEqual( isFunction( iter1[ ITERATOR_SYMBOL ] ), true, 'returns expected value' ); | ||
|
|
||
| iter2 = iter1[ ITERATOR_SYMBOL ](); | ||
|
|
||
| v1 = iter1.next(); | ||
| v2 = iter2.next(); | ||
|
|
||
| t.deepEqual( v1.value, v2.value, 'returns expected value' ); | ||
|
|
||
| t.end(); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these assertions needed?