Skip to content

Commit d2fa0ab

Browse files
test: add a test file for toString() method of dstructs/named-typed-tuple
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent fe96d97 commit d2fa0ab

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/main.js' );
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( 'an instance 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, 'has property' );
47+
t.strictEqual( isFunction( p.toString ), true, 'has method' );
48+
t.end();
49+
});
50+
51+
tape( 'the method throws an error if invoked with a `this` context which is not the host 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)