-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten.test.js
139 lines (114 loc) 路 2.81 KB
/
flatten.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import test from 'ava'
import balance from '.'
// DEBUG
// import chromafi from 'chromafi'
// import util from 'util'
test('flatten siblings w/ body replacements', t => {
const source = '0{1}{2}3'
const result = balance(source, {
open: '{',
close: '}'
})
result.blocks[0].children[0].updateBody('x')
result.blocks[0].children[1].updateBody('y')
const flattened = result.flatten()
t.is(flattened, '0xy3')
})
test('flatten deep tree w/ body replacements', t => {
const source = 'A{B{C}B}A'
const result = balance(source, {
open: '{',
close: '}'
})
result.blocks[0].children[0].children[0].updateBody('_')
const flattened = result.flatten()
t.is(flattened, 'A{B_B}A')
})
test('flatten deep tree w/ simblings and body replacements', t => {
const source = 'A{B{C}D{C}B}A'
const result = balance(source, {
open: '{',
close: '}'
})
result.blocks[0].children[0].children[0].updateBody('_')
const flattened = result.flatten()
t.is(flattened, 'A{B_D_B}A')
})
test('flatten tree w/o body replacements', t => {
const source = 'A{B{C}D{C}B}A'
const result = balance(source, {
open: '{',
close: '}'
})
const flattened = result.flatten()
t.is(flattened, 'A{B{C}D{C}B}A')
})
test('flatten tree shallow', t => {
const source = '{A}'
const result = balance(source, {
open: '{',
close: '}'
})
result.blocks[0].children[0].updateBody('X')
const flattened = result.flatten()
t.is(flattened, 'X')
})
test('flatten tree multi-delims', t => {
const source = '{{A}}'
const result = balance(source, {
open: '{',
close: '}'
})
result.blocks[0].children[0].children[0].updateBody('X')
const flattened = result.flatten()
t.is(flattened, '{X}')
})
test('flatten tree deep w/ colons', t => {
const source = '{A:B{C:D{E:F{G:H}}}}'
const result = balance(source, {
open: '{',
close: '}'
})
result.blocks[0]
.children[0]
.children[0]
.children[0]
.children[0].updateBody('_')
const flattened = result.flatten()
t.is(flattened, '{A:B{C:D{E:F_}}}')
})
test('flatten tree deep readme demo', t => {
const source = '0{1{2{3{4}}}}'
const result = balance(source, {
open: '{',
close: '}'
})
const block4 = result.blocks[0]
.children[0]
.children[0]
.children[0]
.children[0]
block4.updateBody('[BEEP!]')
const flattened = result.flatten()
t.is(flattened, '0{1{2{3[BEEP!]}}}')
})
test('flatten inner content', t => {
const source = '<p>{foo: {bar: "baz"}}</p>'
const result = balance(source, {
open: '{',
close: '}'
})
result.blocks[0]
.children[0]
.children[0].updateBody('YYY')
// DEBUG
// console.log(util.inspect(result.blocks, {
// showHidden: false,
// depth: null
// }))
const flattened1 = result.flatten()
t.is(flattened1, '<p>{foo: YYY}</p>')
result.blocks[0].children[0].updateBody('XXX')
const flattened2 = result.flatten()
t.is(flattened2, '<p>XXX</p>')
})