-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdiagram.js
213 lines (182 loc) · 11.1 KB
/
diagram.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const outdent = require('outdent');
const color = require('ansi-colors');
const Diagram = require('../../src/diagram');
describe('Example diagrams', function() {
it('is a simple diagram of three boxes with arrows', function() {
const diagram = new Diagram()
.box('left')
.arrow(['<--'])
.box('middle')
.arrow(['-->', '<--'])
.box('right');
expect(diagram.toString()).to.equal(outdent`
┌──────────┐ ┌────────────┐ ┌───────────┐
│ │ │ │───▶│ │
│ left │◀───│ middle │ │ right │
│ │ │ │◀───│ │
└──────────┘ └────────────┘ └───────────┘
`);
});
it('is a diagram of three boxes of varying height from '
+ '(ltr) short to high', function() {
const diagram = new Diagram()
.box('short left')
.line(3)
.box('medium\nmiddle')
.line(1)
.box('high\n\nright');
expect(diagram.draw()).to.equal(outdent`
┌────────────────┐ ┌────────────┐ ┌───────────┐
│ │────│ │ │ │
│ short left │────│ medium │ │ high │
│ │────│ middle │────│ │
└────────────────┘ │ │ │ right │
└────────────┘ │ │
└───────────┘
`);
});
it('is a diagram of three boxes of varying height from '
+ '(ltr) short to high with different colors', function() {
const diagram = new Diagram()
.box('short left', {color: 'red'})
.line(3, {color: 'yellow'})
.box('medium\nmiddle', {color: 'green'})
.line(1, {color: 'magenta'})
.box('high\n\nright', {color: 'blue'});
expect(color.unstyle(diagram.draw())).to.equal(outdent`
┌────────────────┐ ┌────────────┐ ┌───────────┐
│ │────│ │ │ │
│ short left │────│ medium │ │ high │
│ │────│ middle │────│ │
└────────────────┘ │ │ │ right │
└────────────┘ │ │
└───────────┘
`);
});
it('is a diagram of three boxes of varying height from '
+ '(ltr) high to short', function() {
const diagram = new Diagram()
.box('high\n\nleft')
.line(3)
.box('medium\nmiddle')
.line(1)
.box('short right');
expect(diagram.draw()).to.equal(outdent`
┌──────────┐ ┌────────────┐ ┌─────────────────┐
│ │────│ │ │ │
│ high │ │ medium │────│ short right │
│ │────│ middle │ │ │
│ left │ │ │ └─────────────────┘
│ │────└────────────┘
└──────────┘
`);
});
it('is an Olympics podium', function() {
const diagram = new Diagram()
.box(' 2 ', {size: 2})
.box('1', {size: 3})
.box(' 3 ', {size: 1});
expect(diagram.draw()).to.equal(outdent`
┌───────────────────┐┌───────────────────┐┌───────────────────┐
│ ││ ││ │
│ ││ ││ 3 │
│ 2 ││ ││ │
│ ││ 1 │└───────────────────┘
│ ││ │
└───────────────────┘│ │
│ │
└───────────────────┘
`);
});
it('features some spaced boxes', function() {
const diagram = new Diagram()
.box(`I feel so small`, {size: 0})
.space(5)
.box('Give me some space, man!');
expect(diagram.draw()).to.equal(outdent`
┌───────────────┐ ┌──────────────────────────────┐
│I feel so small│ │ │
└───────────────┘ │ Give me some space, man! │
│ │
└──────────────────────────────┘
`);
});
it('is a nested diagram', function() {
const inner = new Diagram()
.box(`Yes yes girl`)
.arrow(['-->'])
.box('*Steal lemons*');
const outer = new Diagram()
.box(`Get lemons\n${inner}\n${inner}`)
.arrow(['-->'])
.box('Lemonade');
expect(outer.draw()).to.equal(outdent`
┌────────────────────────────────────────────────────┐ ┌──────────────┐
│ │ │ │
│ Get lemons │───▶│ Lemonade │
│ ┌──────────────────┐ ┌────────────────────┐ │ │ │
│ │ │ │ │ │ └──────────────┘
│ │ Yes yes girl │───▶│ *Steal lemons* │ │
│ │ │ │ │ │
│ └──────────────────┘ └────────────────────┘ │
│ ┌──────────────────┐ ┌────────────────────┐ │
│ │ │ │ │ │
│ │ Yes yes girl │───▶│ *Steal lemons* │ │
│ │ │ │ │ │
│ └──────────────────┘ └────────────────────┘ │
│ │
└────────────────────────────────────────────────────┘
`);
});
it('has no boxes', function() {
const diagram = new Diagram()
.arrow(['-->'])
.space(2)
.line(4)
.arrow(['<--', '--x'])
.space(10);
expect(diagram.draw()).to.equal(outdent`
───▶ ────
────◀───
────
───────X
`);
});
it('has labeled lines and arrows', function() {
const diagram = new Diagram()
.box(`box`)
.line(['label A', null, 'B'])
.box('box')
.arrow(['left:to the left', 'right:right'])
.box('box');
expect(diagram.draw()).to.equal(outdent`
┌─────────┐ ┌─────────┐ ┌─────────┐
│ │──┤label A├──│ │◀─┤to the left├──│ │
│ box │─────────────│ box │ │ box │
│ │──┤B ├──│ │──┤right ├─▶│ │
└─────────┘ └─────────┘ └─────────┘
`);
});
it('vertically aligns containers', function() {
const bottom = new Diagram()
.box('bottom');
const top = new Diagram()
.box('top');
const middle = new Diagram()
.box('middle');
const diagram = new Diagram()
.container(bottom, {verticalAlign: 'bottom'})
.box('a\npretty\nbig\nbox')
.container(middle, {verticalAlign: 'middle'})
.container(top, {verticalAlign: 'top'});
expect(diagram.draw()).to.equal(
' ┌────────────┐ ┌─────────┐\n' +
' │ │┌────────────┐│ │\n' +
' │ a ││ ││ top │\n' +
'┌────────────┐│ pretty ││ middle ││ │\n' +
'│ ││ big ││ │└─────────┘\n' +
'│ bottom ││ box │└────────────┘\n' +
'│ ││ │\n' +
'└────────────┘└────────────┘');
});
});