|
| 1 | +// test/checkFunctions.spec.js |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const { |
| 5 | + checkVarKindMatch, |
| 6 | + checkCalleeMatch, |
| 7 | + checkOperatorMatch, |
| 8 | + checkDefault, |
| 9 | + checkMap, |
| 10 | +} = require('./utils'); |
| 11 | + |
| 12 | +describe('Check Functions', function () { |
| 13 | + describe('checkVarKindMatch', function () { |
| 14 | + it('should return false if astInfo.kind is not provided', function () { |
| 15 | + const node = { kind: 'const' }; |
| 16 | + const astInfo = {}; // no "kind" |
| 17 | + assert.strictEqual(checkVarKindMatch(node, astInfo), false); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should return true if node.kind equals astInfo.kind', function () { |
| 21 | + const node = { kind: 'const' }; |
| 22 | + const astInfo = { kind: 'const' }; |
| 23 | + assert.strictEqual(checkVarKindMatch(node, astInfo), true); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should return false if node.kind does not match astInfo.kind', function () { |
| 27 | + const node = { kind: 'let' }; |
| 28 | + const astInfo = { kind: 'const' }; |
| 29 | + assert.strictEqual(checkVarKindMatch(node, astInfo), false); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('checkCalleeMatch', function () { |
| 34 | + it('should return false if astInfo.callee is not provided', function () { |
| 35 | + const node = { |
| 36 | + callee: { type: 'Identifier', name: 'Promise' }, |
| 37 | + }; |
| 38 | + const astInfo = {}; // no "callee" |
| 39 | + assert.strictEqual(checkCalleeMatch(node, astInfo), false); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return false if node.callee is missing', function () { |
| 43 | + const node = {}; |
| 44 | + const astInfo = { callee: 'Promise' }; |
| 45 | + assert.strictEqual(checkCalleeMatch(node, astInfo), false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return false if node.callee.type !== "Identifier"', function () { |
| 49 | + const node = { |
| 50 | + callee: { type: 'MemberExpression', name: 'Promise' }, |
| 51 | + }; |
| 52 | + const astInfo = { callee: 'Promise' }; |
| 53 | + assert.strictEqual(checkCalleeMatch(node, astInfo), false); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should return false if the callee name does not match astInfo.callee', function () { |
| 57 | + const node = { |
| 58 | + callee: { type: 'Identifier', name: 'WeakRef' }, |
| 59 | + }; |
| 60 | + const astInfo = { callee: 'Promise' }; |
| 61 | + assert.strictEqual(checkCalleeMatch(node, astInfo), false); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return true if callee name matches astInfo.callee', function () { |
| 65 | + const node = { |
| 66 | + callee: { type: 'Identifier', name: 'Promise' }, |
| 67 | + }; |
| 68 | + const astInfo = { callee: 'Promise' }; |
| 69 | + assert.strictEqual(checkCalleeMatch(node, astInfo), true); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('checkOperatorMatch', function () { |
| 74 | + it('should return false if astInfo.operator is not provided', function () { |
| 75 | + const node = { operator: '??' }; |
| 76 | + const astInfo = {}; // no "operator" |
| 77 | + assert.strictEqual(checkOperatorMatch(node, astInfo), false); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return false if node.operator does not match astInfo.operator', function () { |
| 81 | + const node = { operator: '**' }; |
| 82 | + const astInfo = { operator: '??' }; |
| 83 | + assert.strictEqual(checkOperatorMatch(node, astInfo), false); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return true if node.operator matches astInfo.operator', function () { |
| 87 | + const node = { operator: '??' }; |
| 88 | + const astInfo = { operator: '??' }; |
| 89 | + assert.strictEqual(checkOperatorMatch(node, astInfo), true); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('checkDefault', function () { |
| 94 | + it('should always return true', function () { |
| 95 | + assert.strictEqual(checkDefault(), true); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('checkMap usage examples', function () { |
| 100 | + it('checkMap.VariableDeclaration should call checkVarKindMatch internally and return correct boolean', function () { |
| 101 | + const node = { kind: 'const' }; |
| 102 | + const astInfo = { kind: 'const' }; |
| 103 | + const result = checkMap.VariableDeclaration(node, astInfo); |
| 104 | + assert.strictEqual(result, true); |
| 105 | + |
| 106 | + const nodeMismatch = { kind: 'let' }; |
| 107 | + const result2 = checkMap.VariableDeclaration(nodeMismatch, astInfo); |
| 108 | + assert.strictEqual(result2, false); |
| 109 | + }); |
| 110 | + |
| 111 | + it('checkMap.LogicalExpression should call checkOperatorMatch internally', function () { |
| 112 | + const node = { operator: '??' }; |
| 113 | + const astInfo = { operator: '??' }; |
| 114 | + const result = checkMap.LogicalExpression(node, astInfo); |
| 115 | + assert.strictEqual(result, true); |
| 116 | + }); |
| 117 | + |
| 118 | + it('checkMap.ArrowFunctionExpression should call checkDefault internally', function () { |
| 119 | + const result = checkMap.ArrowFunctionExpression(); |
| 120 | + assert.strictEqual(result, true); |
| 121 | + }); |
| 122 | + |
| 123 | + it('checkMap.default should return false if node type is unsupported', function () { |
| 124 | + const result = checkMap.default(); |
| 125 | + assert.strictEqual(result, false); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | +}); |
0 commit comments