Skip to content

Commit 65a3e80

Browse files
committed
style: align regexp/to-json with namespace conventions
- Added missing `test/test.main.js` (present in 25/27 siblings, 93%), mirroring `test/test.js` but requiring `./../lib/main.js` directly so that regressions in `lib/index.js` do not mask bugs in the main implementation file. https://claude.ai/code/session_0151TCuqxNJXJZfy4XEWqKhM
1 parent d0e7983 commit 65a3e80

1 file changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 vm = require( 'vm' );
24+
var tape = require( 'tape' );
25+
var IS_BROWSER = require( '@stdlib/assert/is-browser' );
26+
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
27+
var regexp2json = require( './../lib/main.js' );
28+
29+
30+
// VARIABLES //
31+
32+
var opts = {
33+
'skip': false
34+
};
35+
36+
37+
// TESTS //
38+
39+
tape( 'main export is a function', function test( t ) {
40+
t.ok( true, __filename );
41+
t.strictEqual( typeof regexp2json, 'function', 'main export is a function' );
42+
t.end();
43+
});
44+
45+
tape( 'if provided anything other than a regular expression, the function throws an error', function test( t ) {
46+
var values;
47+
var i;
48+
49+
values = [
50+
'5',
51+
5,
52+
NaN,
53+
null,
54+
void 0,
55+
true,
56+
false,
57+
[],
58+
{},
59+
function noop() {}
60+
];
61+
62+
for ( i = 0; i < values.length; i++ ) {
63+
t.throws( badValue( values[i] ), Error, 'throws when provided ' + values[i] );
64+
}
65+
t.end();
66+
67+
function badValue( value ) {
68+
return function badValue() {
69+
regexp2json( value );
70+
};
71+
}
72+
});
73+
74+
tape( 'the function returns a JSON object', function test( t ) {
75+
var json = regexp2json( /beep/ );
76+
t.strictEqual( isPlainObject( json ), true, 'returns expected value' );
77+
t.end();
78+
});
79+
80+
tape( 'the returned JSON object includes a `type` field whose value is equal to "RegExp"', function test( t ) {
81+
var json = regexp2json( /beep/ );
82+
t.strictEqual( json.type, 'RegExp', 'returns expected value' );
83+
t.end();
84+
});
85+
86+
tape( 'the returned JSON object includes a `pattern` field', function test( t ) {
87+
var expected;
88+
var values;
89+
var json;
90+
var i;
91+
92+
values = [
93+
/.*/,
94+
/ab+c/,
95+
/\/.*^1/
96+
];
97+
98+
expected = [
99+
'.*',
100+
'ab+c',
101+
'\\/.*^1'
102+
];
103+
104+
for ( i = 0; i < values.length; i++ ) {
105+
json = regexp2json( values[ i ] );
106+
t.strictEqual( json.pattern, expected[ i ], 'returns expected value when provided '+values[ i ].toString() );
107+
}
108+
t.end();
109+
});
110+
111+
tape( 'the returned JSON object includes a `flags` field', function test( t ) {
112+
var expected;
113+
var values;
114+
var json;
115+
var i;
116+
117+
values = [
118+
/.*/g,
119+
/ab+c/i,
120+
/\/.*^1/gi
121+
];
122+
123+
expected = [
124+
'g',
125+
'i',
126+
'gi'
127+
];
128+
129+
for ( i = 0; i < values.length; i++ ) {
130+
json = regexp2json( values[i] );
131+
t.strictEqual( json.flags, expected[ i ], 'returns expected value when provided '+values[ i ].toString() );
132+
}
133+
t.end();
134+
});
135+
136+
opts.skip = IS_BROWSER;
137+
tape( 'the function supports serializing a regular expression from a different realm', opts, function test( t ) {
138+
var json;
139+
var re;
140+
141+
re = vm.runInNewContext( '/ab+c/g' );
142+
json = regexp2json( re );
143+
144+
t.strictEqual( json.type, 'RegExp', 'returns expected value' );
145+
t.strictEqual( json.pattern, 'ab+c', 'returns expected value' );
146+
t.strictEqual( json.flags, 'g', 'returns expected value' );
147+
148+
t.end();
149+
});

0 commit comments

Comments
 (0)