Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions lib/node_modules/@stdlib/dstructs/named-typed-tuple/test/test.at.js
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' );
Comment on lines +99 to +102
Copy link
Member

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?

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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment.

t.end();
});
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();
});
Loading