-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathparse_org.unit.test.js
308 lines (271 loc) · 11.8 KB
/
parse_org.unit.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { toJS, List } from 'immutable';
import {
parseOrg,
parseTodoKeywordConfig,
_parsePlanningItems,
parseMarkupAndCookies,
} from './parse_org';
import { exportOrg } from './export_org';
import readFixture from '../../test_helpers/index';
/**
* This is a convenience wrapper around parsing an org file using
* `parseOrg` and then export it using `exportOrg`.
* @param {String} testOrgFile - contents of an org file
*/
function parseAndExportOrgFile(testOrgFile) {
const parsedFile = parseOrg(testOrgFile);
const exportedFile = exportOrg({
headers: parsedFile.get('headers'),
todoKeywordSets: parsedFile.get('todoKeywordSets'),
fileConfigLines: parsedFile.get('fileConfigLines'),
linesBeforeHeadings: parsedFile.get('linesBeforeHeadings'),
dontIndent: false,
});
return exportedFile;
}
describe('Test the parser', () => {
const expectType = (result) => expect(result.map((x) => x.type));
describe('Parsing inline-markup', () => {
test('Parses inline-markup where closing delim is followed by ;', () => {
const result = parseMarkupAndCookies('*bold*;');
expectType(result).toEqual(['inline-markup', 'text']);
});
test('Parses inline-markup surrounded by text', () => {
const result = parseMarkupAndCookies(' *bold*;');
expectType(result).toEqual(['text', 'inline-markup', 'text']);
});
});
describe('Parse an header with empty description', () => {
const parseFirstHeaderFromOrg = (x) => parseOrg(x).toJS().headers[0];
test('Parse headline without trailing newline', () => {
const result = parseFirstHeaderFromOrg('* headline');
expect(result.description).toEqual([]);
expect(result.rawDescription).toEqual('');
});
test('Parse headline with trailing newline but no description', () => {
const result = parseFirstHeaderFromOrg('* headline\n');
expect(result.description).toEqual([]);
expect(result.rawDescription).toEqual('');
});
test('Parse headline with an empty line of description', () => {
const result = parseFirstHeaderFromOrg('* headline\n\n');
expect(result.description.length).toEqual(1);
expect(result.rawDescription).toEqual('\n');
});
test('Parse headline directly followed by next headline', () => {
const result = parseFirstHeaderFromOrg('* headline\n* headline 2');
expect(result.description).toEqual([]);
expect(result.rawDescription).toEqual('');
});
});
});
describe('Parsing and exporting should not alter the original file', () => {
test("Parsing and exporting shouldn't alter the original file", () => {
const testOrgFile = readFixture('indented_list');
const exportedFile = parseAndExportOrgFile(testOrgFile);
// Should have the same amount of lines. Safeguard for the next
// expectation.
const exportedFileLines = exportedFile.split('\n');
const testOrgFileLines = testOrgFile.split('\n');
expect(exportedFileLines.length).toEqual(testOrgFileLines.length);
exportedFileLines.forEach((line, index) => {
expect(line).toEqual(testOrgFileLines[index]);
});
});
test('Parses and exports a file which contains all features of organice', () => {
const testOrgFile = readFixture('all_the_features');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
describe('Boldness', () => {
test('Parsing lines with bold text', () => {
const testOrgFile = readFixture('bold_text');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
});
describe('Parsing inline-markup', () => {
test('Parses inline-markup where closing delim is followed by ;', () => {
const result = parseMarkupAndCookies('*bold*;');
expect(result.length).toEqual(2);
});
});
describe('HTTP URLs', () => {
test('Parse a line containing an URL but no /italic/ text before the URL', () => {
const testOrgFile = readFixture('url');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
});
describe('www URLs', () => {
const testOrgFile = readFixture('www_url');
test('Parse a line containing an URL starting with www', () => {
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
test('Parses all valid URLs starting with www', () => {
const parsedFile = parseOrg(testOrgFile);
const firstHeader = parsedFile.get('headers').first();
const parsedUrls = firstHeader.get('description').filter((x) => x.get('type') === 'www-url');
expect(parsedUrls.size).toEqual(2);
});
});
describe('E-mail address', () => {
test('Parse a line containing an e-mail address', () => {
const testOrgFile = readFixture('email');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
});
describe('Phone number in canonical format (+xxxxxx)', () => {
test('Parse a line containing a phone number but no +striked+ text after the number', () => {
const testOrgFile = readFixture('phonenumber');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
});
describe('Newlines', () => {
test('Newlines in between headers and items are preserved', () => {
const testOrgFile = readFixture('newlines');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
});
test('Config and content lines before first heading line are kept', () => {
const testOrgFile = readFixture('before-first-headline');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
describe('Planning items', () => {
describe('Formatting is the same as in Emacs', () => {
describe('List formatting', () => {
test('Parsing a basic list should not mangle the list', () => {
const testDescription = ' - indented list\n - Foo';
const parsedFile = _parsePlanningItems(testDescription);
expect(parsedFile.strippedDescription).toEqual(testDescription);
});
test('Parsing a list with planning items should not mangle the list', () => {
const testDescription = ' - indented list\n - Foo';
const parsedFile = _parsePlanningItems(`SCHEDULED: <2019-07-30 Tue>\n${testDescription}`);
expect(parsedFile.strippedDescription).toEqual(testDescription);
});
describe('Parses planning item with following checkmark', () => {
it('parses and exports without changes', () => {
const testOrgFile = readFixture('planning_item_with_following_checkmark');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
test('Parsing a planning items followed by a checklist must work', () => {
const testDescription = '- [ ] foo\n- [ ] bar';
const parsed = _parsePlanningItems(`SCHEDULED: <2019-07-30 Tue>\n${testDescription}`);
const parsedPlanningItem = parsed.planningItems.toJS();
expect(parsedPlanningItem[0].timestamp.dayName).toEqual('Tue');
expect(parsed.strippedDescription).toEqual(testDescription);
});
});
test('Planning items should contain active timestamps from title and description as well', () => {
const testOrgFile = readFixture('schedule_and_timestamps');
const parsedFile = parseOrg(testOrgFile);
const headers = parsedFile.get('headers').toJS();
expect(headers.length).toEqual(1);
const header = headers[0];
expect(header.planningItems.length).toEqual(3);
});
});
describe('Planning items are formatted as is default Emacs', () => {
test('For basic files', () => {
const testOrgFile = readFixture('schedule');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
test('For files with timestamps in title and description', () => {
const testOrgFile = readFixture('schedule_and_timestamps');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
test('For files with multiple planning items', () => {
const testOrgFile = readFixture('schedule_and_deadline');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
});
test('Properties are formatted as is default in Emacs', () => {
const testOrgFile = readFixture('properties');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
test('Tags are formatted as is default in Emacs', () => {
const testOrgFile = readFixture('tags');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
});
});
describe('Logbook entries', () => {
test('Logbook entries are formatted as is default in Emacs', () => {
const testOrgFile = readFixture('logbook');
const exportedFile = parseAndExportOrgFile(testOrgFile);
expect(exportedFile).toEqual(testOrgFile);
});
});
});
describe('Parse in-buffer TODO keyword settings', () => {
test('Normal headline', () => {
const result = parseTodoKeywordConfig('*** foo');
expect(result).toBeNull();
});
test('Normal text line', () => {
const result = parseTodoKeywordConfig('foo');
expect(result).toBeNull();
});
test('Other in-buffer setting', () => {
const result = parseTodoKeywordConfig('#+STARTUP: nologrepeat');
expect(result).toBeNull();
});
['#+TODO', '#+TYP_TODO'].forEach((t) => {
describe(t, () => {
const expectNewSetFromLine = (line) => {
const result = parseTodoKeywordConfig(line);
const expectedNewSet = {
completedKeywords: ['FINISHED'],
configLine: line,
default: false,
keywords: ['START', 'INPROGRESS', 'STALLED', 'FINISHED'],
};
expect(result.toJS()).toEqual(expectedNewSet);
};
test('no parentheses', () => {
const line = `${t}: START INPROGRESS STALLED | FINISHED`;
expectNewSetFromLine(line);
});
test('some (x) keyboard shortcuts', () => {
const line = `${t}: START INPROGRESS(i) STALLED(.) | FINISHED(f)`;
expectNewSetFromLine(line);
});
test('recording timestamp / note on entry', () => {
const line = `${t}: START INPROGRESS(!) STALLED | FINISHED(@)`;
expectNewSetFromLine(line);
});
test('shortcut plus recording timestamp / note on entry', () => {
const line = `${t}: START(s) INPROGRESS(i!) STALLED(.) | FINISHED(f@)`;
expectNewSetFromLine(line);
});
test('recording timestamp / note on exit', () => {
const line = `${t}: START(s) INPROGRESS(/!) STALLED | FINISHED(/@)`;
expectNewSetFromLine(line);
});
test('shortcut plus recording timestamp / note on exit', () => {
const line = `${t}: START(s) INPROGRESS(i/!) STALLED(.) | FINISHED(f/@)`;
expectNewSetFromLine(line);
});
test('recording timestamp / note on entry and exit', () => {
const line = `${t}: START(s) INPROGRESS(/!) STALLED | FINISHED(/@)`;
expectNewSetFromLine(line);
});
test('shortcut plus recording timestamp / note on entry and exit', () => {
const line = `${t}: START(s@/@) INPROGRESS(i!/!) STALLED(.@/!) | FINISHED(f!/@)`;
expectNewSetFromLine(line);
});
});
});
});