NodeGuards.spec.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import * as ESTree from 'estree';
  2. import { assert } from 'chai';
  3. import { NodeGuards } from '../../../../src/node/NodeGuards';
  4. import { NodeFactory } from '../../../../src/node/NodeFactory';
  5. import { NodeUtils } from '../../../../src/node/NodeUtils';
  6. describe('NodeGuards', () => {
  7. describe('isNodeWithLexicalScopeStatements', () => {
  8. describe('truthful checks', () => {
  9. describe('Variant #1: block statement of function declaration', () => {
  10. const expectedResult: boolean = true;
  11. const node: ESTree.Node = NodeFactory.blockStatementNode();
  12. const parentNode: ESTree.FunctionDeclaration = NodeFactory.functionDeclarationNode(
  13. 'foo',
  14. [],
  15. node
  16. );
  17. let result: boolean;
  18. before(() => {
  19. NodeUtils.parentizeAst(parentNode);
  20. result = NodeGuards.isNodeWithLexicalScopeStatements(node, parentNode);
  21. });
  22. it('should check if node has statements', () => {
  23. assert.equal(result, expectedResult);
  24. });
  25. });
  26. describe('Variant #2: block statement of function expression', () => {
  27. const expectedResult: boolean = true;
  28. const node: ESTree.Node = NodeFactory.blockStatementNode();
  29. const parentNode: ESTree.FunctionExpression = NodeFactory.functionExpressionNode(
  30. [],
  31. node
  32. );
  33. let result: boolean;
  34. before(() => {
  35. NodeUtils.parentizeAst(parentNode);
  36. result = NodeGuards.isNodeWithLexicalScopeStatements(node, parentNode);
  37. });
  38. it('should check if node has statements', () => {
  39. assert.equal(result, expectedResult);
  40. });
  41. });
  42. });
  43. describe('false checks', () => {
  44. describe('Variant #1: switch-case node', () => {
  45. const expectedResult: boolean = false;
  46. const node: ESTree.Node = NodeFactory.switchCaseNode(
  47. NodeFactory.literalNode(1),
  48. []
  49. );
  50. const parentNode: ESTree.FunctionDeclaration = NodeFactory.functionDeclarationNode(
  51. 'foo',
  52. [],
  53. NodeFactory.blockStatementNode([
  54. NodeFactory.switchStatementNode(
  55. NodeFactory.memberExpressionNode(
  56. NodeFactory.identifierNode('bar'),
  57. NodeFactory.updateExpressionNode(
  58. '++',
  59. NodeFactory.identifierNode('baz')
  60. ),
  61. true
  62. ),
  63. [node]
  64. )
  65. ])
  66. );
  67. let result: boolean;
  68. before(() => {
  69. NodeUtils.parentizeAst(parentNode);
  70. result = NodeGuards.isNodeWithLexicalScopeStatements(node, parentNode);
  71. });
  72. it('should check if node has statements', () => {
  73. assert.equal(result, expectedResult);
  74. });
  75. });
  76. describe('Variant #2: literal node', () => {
  77. const expectedResult: boolean = false;
  78. const node: ESTree.Node = NodeFactory.literalNode(1);
  79. const parentNode: ESTree.FunctionDeclaration = NodeFactory.functionDeclarationNode(
  80. 'foo',
  81. [],
  82. NodeFactory.blockStatementNode([
  83. NodeFactory.expressionStatementNode(
  84. NodeFactory.callExpressionNode(
  85. NodeFactory.identifierNode('bar'),
  86. [node]
  87. )
  88. )
  89. ])
  90. );
  91. let result: boolean;
  92. before(() => {
  93. NodeUtils.parentizeAst(parentNode);
  94. result = NodeGuards.isNodeWithLexicalScopeStatements(node, parentNode);
  95. });
  96. it('should check if node has statements', () => {
  97. assert.equal(result, expectedResult);
  98. });
  99. });
  100. describe('Variant #3: block statement of if statement', () => {
  101. const expectedResult: boolean = false;
  102. const node: ESTree.Node = NodeFactory.blockStatementNode();
  103. const parentNode: ESTree.IfStatement = NodeFactory.ifStatementNode(
  104. NodeFactory.identifierNode('foo'),
  105. node
  106. );
  107. let result: boolean;
  108. before(() => {
  109. NodeUtils.parentizeAst(parentNode);
  110. result = NodeGuards.isNodeWithLexicalScopeStatements(node, parentNode);
  111. });
  112. it('should check if node has statements', () => {
  113. assert.equal(result, expectedResult);
  114. });
  115. });
  116. });
  117. });
  118. describe('isNodeWithStatements', () => {
  119. describe('truthful checks', () => {
  120. describe('Variant #1: program node', () => {
  121. const expectedResult: boolean = true;
  122. const node: ESTree.Node = NodeFactory.programNode();
  123. let result: boolean;
  124. before(() => {
  125. result = NodeGuards.isNodeWithStatements(node);
  126. });
  127. it('should check if node has statements', () => {
  128. assert.equal(result, expectedResult);
  129. });
  130. });
  131. describe('Variant #2: block statement node', () => {
  132. const expectedResult: boolean = true;
  133. const node: ESTree.Node = NodeFactory.blockStatementNode();
  134. let result: boolean;
  135. before(() => {
  136. result = NodeGuards.isNodeWithStatements(node);
  137. });
  138. it('should check if node has statements', () => {
  139. assert.equal(result, expectedResult);
  140. });
  141. });
  142. describe('Variant #3: switch case node', () => {
  143. const expectedResult: boolean = true;
  144. const node: ESTree.Node = NodeFactory.switchCaseNode(
  145. NodeFactory.literalNode(1),
  146. []
  147. );
  148. let result: boolean;
  149. before(() => {
  150. result = NodeGuards.isNodeWithStatements(node);
  151. });
  152. it('should check if node has statements', () => {
  153. assert.equal(result, expectedResult);
  154. });
  155. });
  156. });
  157. describe('false checks', () => {
  158. describe('Variant #1: literal node', () => {
  159. const expectedResult: boolean = false;
  160. const node: ESTree.Node = NodeFactory.literalNode(1);
  161. let result: boolean;
  162. before(() => {
  163. result = NodeGuards.isNodeWithStatements(node);
  164. });
  165. it('should check if node has statements', () => {
  166. assert.equal(result, expectedResult);
  167. });
  168. });
  169. describe('Variant #2: identifier node', () => {
  170. const expectedResult: boolean = false;
  171. const node: ESTree.Node = NodeFactory.identifierNode('foo');
  172. let result: boolean;
  173. before(() => {
  174. result = NodeGuards.isNodeWithStatements(node);
  175. });
  176. it('should check if node has statements', () => {
  177. assert.equal(result, expectedResult);
  178. });
  179. });
  180. describe('Variant #3: if-statement node', () => {
  181. const expectedResult: boolean = false;
  182. const node: ESTree.Node = NodeFactory.ifStatementNode(
  183. NodeFactory.identifierNode('foo'),
  184. NodeFactory.blockStatementNode()
  185. );
  186. let result: boolean;
  187. before(() => {
  188. result = NodeGuards.isNodeWithStatements(node);
  189. });
  190. it('should check if node has statements', () => {
  191. assert.equal(result, expectedResult);
  192. });
  193. });
  194. describe('Variant #4: switch-statement node', () => {
  195. const expectedResult: boolean = false;
  196. const node: ESTree.Node = NodeFactory.switchStatementNode(
  197. NodeFactory.identifierNode('foo'),
  198. []
  199. );
  200. let result: boolean;
  201. before(() => {
  202. result = NodeGuards.isNodeWithStatements(node);
  203. });
  204. it('should check if node has statements', () => {
  205. assert.equal(result, expectedResult);
  206. });
  207. });
  208. });
  209. });
  210. });