-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
62 lines (53 loc) · 1.18 KB
/
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
var assert = require('assert');
var punc = require('./');
var Grammar = require('grammarjs-grammar');
var Parser = require('grammarjs-recursive-parser');
var fs = require('fs');
describe('punctuation', function(){
it('should extend a new grammar', function(){
var grammar = new Grammar('random');
grammar.use(punc());
var parser = new Parser(grammar);
});
});
/**
* Test helper.
*
* @api private
*/
function test(str, log) {
return it(str, function(){
assert.equal(str, compile(str, log));
});
}
/**
* Parse `str` to ast, then stringify back.
*
* @param {String} str
* @return {String}
*/
function compile(str, log) {
var ast = parser.parse(str);
if (log) console.log(JSON.stringify(ast, null, 2));
return stringify(ast);
}
/**
* For testing, it should generate the original string.
*
* @param {Token} token
* @api public
*/
function stringify(token) {
if (!token) return '';
var html = [];
if (Array.isArray(token.content)) {
token.content.forEach(function(child){
html.push(stringify(child));
});
} else if (null != token.content) {
html.push(stringify(token.content));
} else {
html.push(token);
}
return html.join('');
}