|
| 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 tape = require( 'tape' ); |
| 24 | +var parseJSON = require( '@stdlib/utils/parse-json' ); |
| 25 | +var reviveRegExp = require( './../lib/main.js' ); |
| 26 | + |
| 27 | + |
| 28 | +// FUNCTIONS // |
| 29 | + |
| 30 | +function setup( pattern, flags ) { |
| 31 | + return { |
| 32 | + 'type': 'RegExp', |
| 33 | + 'pattern': pattern, |
| 34 | + 'flags': flags || '' |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +// TESTS // |
| 40 | + |
| 41 | +tape( 'main export is a function', function test( t ) { |
| 42 | + t.ok( true, __filename ); |
| 43 | + t.strictEqual( typeof reviveRegExp, 'function', 'main export is a function' ); |
| 44 | + t.end(); |
| 45 | +}); |
| 46 | + |
| 47 | +tape( 'the function does not transform values which are not serialized RegExp objects', function test( t ) { |
| 48 | + var expected; |
| 49 | + var actual; |
| 50 | + |
| 51 | + expected = { |
| 52 | + 'beep': 'boop' |
| 53 | + }; |
| 54 | + actual = parseJSON( '{"beep":"boop"}', reviveRegExp ); |
| 55 | + |
| 56 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 57 | + |
| 58 | + // Null edge case: |
| 59 | + actual = parseJSON( 'null', reviveRegExp ); |
| 60 | + t.strictEqual( actual, null, 'returns expected value' ); |
| 61 | + |
| 62 | + t.end(); |
| 63 | +}); |
| 64 | + |
| 65 | +tape( 'an object must have a recognized `type` field in order to be revived', function test( t ) { |
| 66 | + var expected; |
| 67 | + var actual; |
| 68 | + |
| 69 | + expected = setup( 'ab+c' ); |
| 70 | + expected.type = 'Boop'; |
| 71 | + |
| 72 | + actual = parseJSON( JSON.stringify( expected ), reviveRegExp ); |
| 73 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 74 | + |
| 75 | + t.end(); |
| 76 | +}); |
| 77 | + |
| 78 | +tape( 'an object must have a `pattern` field in order to be revived', function test( t ) { |
| 79 | + var expected; |
| 80 | + var actual; |
| 81 | + |
| 82 | + expected = setup( 'ab+c' ); |
| 83 | + delete expected.pattern; |
| 84 | + |
| 85 | + actual = parseJSON( JSON.stringify( expected ), reviveRegExp ); |
| 86 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 87 | + |
| 88 | + t.end(); |
| 89 | +}); |
| 90 | + |
| 91 | +tape( 'the function revives a JSON-serialized regular expression', function test( t ) { |
| 92 | + var expected; |
| 93 | + var patterns; |
| 94 | + var actual; |
| 95 | + var flags; |
| 96 | + var json; |
| 97 | + var i; |
| 98 | + |
| 99 | + patterns = [ |
| 100 | + '.*', |
| 101 | + 'ab+c', |
| 102 | + '/.*^1', |
| 103 | + 'a+.b*' |
| 104 | + ]; |
| 105 | + |
| 106 | + flags = [ |
| 107 | + 'd', |
| 108 | + 'dy', |
| 109 | + 's', |
| 110 | + '' |
| 111 | + ]; |
| 112 | + |
| 113 | + for ( i = 0; i < patterns.length; i++ ) { |
| 114 | + json = JSON.stringify( setup( patterns[ i ], flags[ i ] ) ); |
| 115 | + |
| 116 | + expected = new RegExp( patterns[ i ], flags[ i ] ); |
| 117 | + actual = parseJSON( json, reviveRegExp ); |
| 118 | + |
| 119 | + t.ok( actual instanceof RegExp, 'returns expected value' ); |
| 120 | + t.strictEqual( actual.toString(), expected.toString(), 'returns expected value when provided ' + json ); |
| 121 | + } |
| 122 | + t.end(); |
| 123 | +}); |
0 commit comments