Skip to content

Commit dbe212c

Browse files
test: create a test file for toJSON() 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 dbe212c

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 `toJSON` 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, 'toJSON' ), true, 'returns expected value' );
47+
t.strictEqual( isFunction( p.toJSON ), 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.toJSON.call( value );
85+
};
86+
}
87+
});
88+
89+
tape( 'the method serializes a tuple as JSON', function test( t ) {
90+
var expected;
91+
var actual;
92+
var Point;
93+
var p;
94+
95+
Point = namedtypedtuple( [ 'x', 'y' ] );
96+
p = new Point( [ 10, 20 ] );
97+
98+
expected = {
99+
'x': 10,
100+
'y': 20
101+
};
102+
actual = p.toJSON();
103+
104+
t.deepEqual( actual, expected, 'returns expected value' );
105+
t.end();
106+
});
107+
108+
tape( 'the method serializes a tuple with multiple fields as JSON', function test( t ) {
109+
var expected;
110+
var actual;
111+
var Point;
112+
var p;
113+
114+
Point = namedtypedtuple( [ 'price', 'quantity' ] );
115+
p = new Point( [ 123456.789, 9876 ] );
116+
117+
expected = {
118+
'price': 123456.789,
119+
'quantity': 9876
120+
};
121+
actual = p.toJSON();
122+
123+
t.deepEqual( actual, expected, 'returns expected value' );
124+
t.end();
125+
});

0 commit comments

Comments
 (0)