|
1 | 1 | // LICENSE : MIT |
2 | | -"use strict"; |
3 | | -const assert = require("assert"); |
4 | | -const map = require("."); |
5 | | -const parse = require("@textlint/text-to-ast").parse; |
6 | | -const Syntax = require("@textlint/text-to-ast").Syntax; |
7 | | -const ObjectAssign = require("object-assign"); |
8 | | -describe('should not traverse into children of filtered out nodes', function (t) { |
9 | | - it("should map specified node", function () { |
10 | | - const ast = { |
11 | | - "type": "root", |
12 | | - "children": [ |
13 | | - { |
14 | | - "type": "node", |
15 | | - "children": [ |
16 | | - {"type": "leaf", "value": "1"} |
17 | | - ] |
18 | | - }, |
19 | | - {"type": "leaf", "value": "2"} |
20 | | - ] |
21 | | - }; |
22 | | - const actual = map(ast, function (node) { |
23 | | - if (node.type === "leaf") { |
24 | | - return ObjectAssign({}, node, { |
25 | | - value: "CHANGED" |
26 | | - }); |
27 | | - } |
28 | | - // no change |
29 | | - return node; |
30 | | - }); |
31 | | - const expected = { |
32 | | - "type": "root", |
33 | | - "children": [ |
34 | | - { |
35 | | - "type": "node", |
36 | | - "children": [ |
37 | | - {"type": "leaf", "value": "CHANGED"} |
38 | | - ] |
39 | | - }, |
40 | | - {"type": "leaf", "value": "CHANGED"} |
41 | | - ] |
42 | | - }; |
43 | | - assert.deepEqual(actual, expected); |
44 | | - }); |
45 | | - context("when return null", function () { |
46 | | - it("should map as empty object", function () { |
47 | | - const ast = { |
48 | | - "type": "root", |
49 | | - "children": [ |
50 | | - { |
51 | | - "type": "node", |
52 | | - "children": [ |
53 | | - {"type": "leaf", "value": "1"} |
54 | | - ] |
55 | | - }, |
56 | | - {"type": "leaf", "value": "2"} |
57 | | - ] |
58 | | - }; |
59 | | - const actual = map(ast, function (node) { |
60 | | - if (node.type === "leaf") { |
61 | | - return null; |
62 | | - } |
63 | | - // no change |
64 | | - return node; |
65 | | - }); |
66 | | - const expected = { |
67 | | - "type": "root", |
68 | | - "children": [ |
69 | | - { |
70 | | - "type": "node", |
71 | | - "children": [ |
72 | | - {} |
73 | | - ] |
74 | | - }, |
75 | | - {} |
76 | | - ] |
77 | | - }; |
78 | | - assert.deepEqual(actual, expected); |
79 | | - }); |
80 | | - }); |
| 2 | +'use strict' |
81 | 3 |
|
82 | | - context("when pass empty object", function () { |
83 | | - it("should work map", function () { |
84 | | - const ast = {}; |
85 | | - const actual = map(ast, function (node) { |
86 | | - return { |
87 | | - value: "test" |
88 | | - }; |
89 | | - }); |
90 | | - const expected = {}; |
91 | | - assert.deepEqual(actual, { |
92 | | - value: "test" |
93 | | - }); |
94 | | - }); |
95 | | - }); |
96 | | -}); |
| 4 | +/* eslint-env mocha */ |
97 | 5 |
|
| 6 | +const assert = require('assert') |
| 7 | +const assign = require('object-assign') |
| 8 | +const map = require('.') |
| 9 | + |
| 10 | +describe('should not traverse into children of filtered out nodes', function() { |
| 11 | + it('should map specified node', function() { |
| 12 | + const ast = { |
| 13 | + type: 'root', |
| 14 | + children: [ |
| 15 | + { |
| 16 | + type: 'node', |
| 17 | + children: [{type: 'leaf', value: '1'}] |
| 18 | + }, |
| 19 | + {type: 'leaf', value: '2'} |
| 20 | + ] |
| 21 | + } |
| 22 | + const actual = map(ast, function(node) { |
| 23 | + if (node.type === 'leaf') { |
| 24 | + return assign({}, node, { |
| 25 | + value: 'CHANGED' |
| 26 | + }) |
| 27 | + } |
| 28 | + // No change |
| 29 | + return node |
| 30 | + }) |
| 31 | + const expected = { |
| 32 | + type: 'root', |
| 33 | + children: [ |
| 34 | + { |
| 35 | + type: 'node', |
| 36 | + children: [{type: 'leaf', value: 'CHANGED'}] |
| 37 | + }, |
| 38 | + {type: 'leaf', value: 'CHANGED'} |
| 39 | + ] |
| 40 | + } |
| 41 | + assert.deepEqual(actual, expected) |
| 42 | + }) |
| 43 | + context('when return null', function() { |
| 44 | + it('should map as empty object', function() { |
| 45 | + const ast = { |
| 46 | + type: 'root', |
| 47 | + children: [ |
| 48 | + { |
| 49 | + type: 'node', |
| 50 | + children: [{type: 'leaf', value: '1'}] |
| 51 | + }, |
| 52 | + {type: 'leaf', value: '2'} |
| 53 | + ] |
| 54 | + } |
| 55 | + const actual = map(ast, function(node) { |
| 56 | + if (node.type === 'leaf') { |
| 57 | + return null |
| 58 | + } |
| 59 | + // No change |
| 60 | + return node |
| 61 | + }) |
| 62 | + const expected = { |
| 63 | + type: 'root', |
| 64 | + children: [ |
| 65 | + { |
| 66 | + type: 'node', |
| 67 | + children: [{}] |
| 68 | + }, |
| 69 | + {} |
| 70 | + ] |
| 71 | + } |
| 72 | + assert.deepEqual(actual, expected) |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + context('when pass empty object', function() { |
| 77 | + it('should work map', function() { |
| 78 | + const ast = {} |
| 79 | + const actual = map(ast, function() { |
| 80 | + return { |
| 81 | + value: 'test' |
| 82 | + } |
| 83 | + }) |
| 84 | + const expected = { |
| 85 | + value: 'test' |
| 86 | + } |
| 87 | + assert.deepEqual(actual, expected) |
| 88 | + }) |
| 89 | + }) |
| 90 | +}) |
0 commit comments