Skip to content

Commit f501596

Browse files
test: add tests for toString method in dstructs/named-typed-tuple
PR-URL: #8041 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 7533913 commit f501596

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 `toString` 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, 'toString' ), true, 'returns expected value' );
47+
t.strictEqual( isFunction( p.toString ), 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 tuple instance', function test( t ) {
52+
var values;
53+
var Point;
54+
var p;
55+
var i;
56+
57+
Point = namedtypedtuple( [ 'x', 'y' ], {
58+
'name': 'Point'
59+
});
60+
p = new Point( [ 10, 20 ] );
61+
62+
values = [
63+
'5',
64+
5,
65+
NaN,
66+
true,
67+
false,
68+
null,
69+
void 0,
70+
{},
71+
[],
72+
function noop() {},
73+
{
74+
'name': 'Bob'
75+
}
76+
];
77+
for ( i = 0; i < values.length; i++ ) {
78+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
79+
}
80+
t.end();
81+
82+
function badValue( value ) {
83+
return function badValue() {
84+
return p.toString.call( value );
85+
};
86+
}
87+
});
88+
89+
tape( 'the method returns a string representation for a tuple with one field', function test( t ) {
90+
var expected;
91+
var Single;
92+
var actual;
93+
var s;
94+
95+
Single = namedtypedtuple( [ 'id' ], {
96+
'name': 'Single'
97+
});
98+
s = new Single( [ 12345 ] );
99+
100+
expected = 'Single(id=12345)';
101+
actual = s.toString();
102+
103+
t.strictEqual( actual, expected, 'returns expected value' );
104+
t.end();
105+
});
106+
107+
tape( 'the method returns a string representation of a tuple with multiple fields', function test( t ) {
108+
var expected;
109+
var actual;
110+
var Point;
111+
var p;
112+
113+
Point = namedtypedtuple( [ 'price', 'quantity' ] );
114+
p = new Point( [ 123456.789, 9876 ] );
115+
116+
expected = 'tuple(price=123456.789, quantity=9876)';
117+
actual = p.toString();
118+
119+
t.strictEqual( actual, expected, 'returns expected value' );
120+
t.end();
121+
});

0 commit comments

Comments
 (0)