NodeUtils.spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { IBlockStatementNode } from "../src/interfaces/nodes/IBlockStatementNode";
  2. import { IIdentifierNode } from "../src/interfaces/nodes/IIdentifierNode";
  3. import { NodeType } from "../src/enums/NodeType";
  4. import { NodeUtils } from '../src/NodeUtils';
  5. import {ILiteralNode} from "../src/interfaces/nodes/ILiteralNode";
  6. let assert: any = require('chai').assert;
  7. describe('NodeUtils', () => {
  8. describe('addXVerbatimPropertyToLiterals (node: INode): void', () => {
  9. let node: any;
  10. beforeEach(() => {
  11. node = {
  12. type: NodeType.Literal,
  13. value: 'string',
  14. raw: `'string'`
  15. };
  16. NodeUtils.addXVerbatimPropertyToLiterals(node)
  17. });
  18. it('should add `x-verbatim-property` to `Literal` node', () => {
  19. assert.deepEqual(node, {
  20. type: NodeType.Literal,
  21. value: 'string',
  22. raw: `'string'`,
  23. 'x-verbatim-property': `'string'`
  24. });
  25. });
  26. });
  27. describe('appendNode (blockScopeBody: INode[], node: INode): void', () => {
  28. let blockStatementNode: IBlockStatementNode,
  29. identifierNode: IIdentifierNode;
  30. beforeEach(() => {
  31. blockStatementNode = {
  32. type: NodeType.Literal,
  33. body: []
  34. };
  35. identifierNode = {
  36. type: NodeType.Identifier,
  37. name: 'identifier'
  38. };
  39. NodeUtils.appendNode(blockStatementNode.body, identifierNode)
  40. });
  41. it('should append given node to a `BlockStatement` node body', () => {
  42. assert.deepEqual(blockStatementNode, {
  43. type: NodeType.Literal,
  44. body: [identifierNode]
  45. });
  46. });
  47. });
  48. describe('getBlockScopeNodeByIndex (node: INode, index: number = 0): INode', () => {
  49. let blockStatementNode: IBlockStatementNode,
  50. identifierNode: IIdentifierNode,
  51. literalNode: ILiteralNode;
  52. beforeEach(() => {
  53. identifierNode = {
  54. type: NodeType.Identifier,
  55. name: 'identifier'
  56. };
  57. literalNode = {
  58. type: NodeType.Identifier,
  59. value: 'string',
  60. raw: `'string'`,
  61. 'x-verbatim-property': `'string'`
  62. };
  63. blockStatementNode = {
  64. type: NodeType.Literal,
  65. body: [
  66. identifierNode,
  67. literalNode
  68. ]
  69. };
  70. });
  71. it('should return block-scope node of given node by given index if node has block-scope', () => {
  72. assert.deepEqual(NodeUtils.getBlockScopeNodeByIndex(blockStatementNode), identifierNode);
  73. assert.deepEqual(NodeUtils.getBlockScopeNodeByIndex(blockStatementNode, 1), literalNode);
  74. });
  75. it('should return root node if index is out of boundaries', () => {
  76. assert.deepEqual(NodeUtils.getBlockScopeNodeByIndex(blockStatementNode, 2), blockStatementNode);
  77. });
  78. it('should return root node if node has not block-scope', () => {
  79. assert.deepEqual(NodeUtils.getBlockScopeNodeByIndex(identifierNode, 1), identifierNode);
  80. });
  81. });
  82. });