1 | 'use strict';
|
2 |
|
3 |
|
4 | var Underscore = require('underscore');
|
5 | var Assert = require('assert');
|
6 | var Parser = require('../lib/babelfish/parser');
|
7 |
|
8 |
|
9 | function LiteralNode(text) {
|
10 | this.type = 'literal';
|
11 | this.text = text;
|
12 | }
|
13 |
|
14 |
|
15 | function VariableNode(anchor) {
|
16 | this.type = 'variable';
|
17 | this.anchor = anchor;
|
18 | }
|
19 |
|
20 |
|
21 | function PluralNode(anchor, forms) {
|
22 | this.type = 'plural';
|
23 | this.forms = forms;
|
24 | this.anchor = anchor || 'count';
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 | function redistribute_ast(ast) {
|
30 | var nodes = [], last = {};
|
31 |
|
32 | Underscore.each(ast, function (node) {
|
33 | if ('literal' === last.type && 'literal' === node.type) {
|
34 | last.text += node.text;
|
35 | return;
|
36 | }
|
37 |
|
38 | nodes.push(node);
|
39 | last = node;
|
40 | });
|
41 |
|
42 | return nodes;
|
43 | }
|
44 |
|
45 |
|
46 | function testParsedNodes(definitions) {
|
47 | var tests = {};
|
48 |
|
49 | Object.getOwnPropertyNames(definitions).forEach(function (str) {
|
50 | tests[str] = function () {
|
51 | var expected, result;
|
52 |
|
53 | expected = definitions[str];
|
54 | result = redistribute_ast(Parser.parse(str));
|
55 |
|
56 |
|
57 | if (result.length !== expected.length) {
|
58 | Assert.ok(false, 'Unexpected amount of nodes.' +
|
59 | '\nExpected: ' + expected.length +
|
60 | '\nActual: ' + result.length +
|
61 | '\n' + result.map(function (o) {
|
62 | var ret = '\n - type: ' + o.type;
|
63 |
|
64 | if (o.anchor) { ret += '\n anchor: ' + o.anchor; }
|
65 | if (o.forms) { ret += '\n forms: ' + o.forms; }
|
66 | if (o.text) { ret += '\n text: ' + o.text; }
|
67 |
|
68 | return ret;
|
69 | }));
|
70 | }
|
71 |
|
72 | result.forEach(function (node, idx) {
|
73 | Assert.deepEqual(node, expected[idx]);
|
74 | });
|
75 | };
|
76 | });
|
77 |
|
78 | return tests;
|
79 | }
|
80 |
|
81 |
|
82 | require('vows').describe('BabelFish.Parser').addBatch({
|
83 | 'Parsing strings': testParsedNodes({
|
84 | 'Simple string }{ with \b brackets and \t special chars': [
|
85 | new LiteralNode('Simple string }{ with \b brackets and \t special chars')
|
86 | ],
|
87 |
|
88 | 'Quirky #{} #{1} #{ } (()):foo ((|)) (( )):bar mess': [
|
89 | new LiteralNode('Quirky #{} #{1} #{ } (()):foo ((|)) (( )):bar mess')
|
90 | ],
|
91 |
|
92 | 'String with simple #{variable}...': [
|
93 | new LiteralNode('String with simple '),
|
94 | new VariableNode('variable'),
|
95 | new LiteralNode('...')
|
96 | ],
|
97 |
|
98 | 'String with complex #{foo.bar.baz} variable': [
|
99 | new LiteralNode('String with complex '),
|
100 | new VariableNode('foo.bar.baz'),
|
101 | new LiteralNode(' variable')
|
102 | ],
|
103 |
|
104 | 'String with plurals ((a|b)):c': [
|
105 | new LiteralNode('String with plurals '),
|
106 | new PluralNode('c', ['a', 'b'])
|
107 | ],
|
108 |
|
109 | 'Plurals with ((a\\)b\\|c\\(d|e)):myvar, escaping': [
|
110 | new LiteralNode('Plurals with '),
|
111 | new PluralNode('myvar', ['a)b|c(d', 'e']),
|
112 | new LiteralNode(', escaping')
|
113 | ],
|
114 |
|
115 | 'Plurals with ((a|b)):_compl3x.$variable.': [
|
116 | new LiteralNode('Plurals with '),
|
117 | new PluralNode('_compl3x.$variable', ['a', 'b']),
|
118 | new LiteralNode('.')
|
119 | ],
|
120 |
|
121 | 'Plurals with empty (()):myvar forms': [
|
122 | new LiteralNode('Plurals with empty (()):myvar forms')
|
123 | ],
|
124 |
|
125 | 'Plurals with single ((abc)):$myvar forms': [
|
126 | new LiteralNode('Plurals with single '),
|
127 | new PluralNode('$myvar', ['abc']),
|
128 | new LiteralNode(' forms')
|
129 | ],
|
130 |
|
131 | 'Plurals with lots of forms ((b|c|d|e|f|g|h)):a': [
|
132 | new LiteralNode('Plurals with lots of forms '),
|
133 | new PluralNode('a', ['b', 'c', 'd', 'e', 'f', 'g', 'h'])
|
134 | ],
|
135 |
|
136 | 'Escape \\((a|b)):plurals and \\#{variables}': [
|
137 | new LiteralNode('Escape ((a|b)):plurals and #{variables}')
|
138 | ],
|
139 |
|
140 | 'Invalid variable #{n..e}': [
|
141 | new LiteralNode('Invalid variable #{n..e}')
|
142 | ],
|
143 |
|
144 | 'Escape backslash ((a\\\\|b)):c': [
|
145 | new LiteralNode('Escape backslash '),
|
146 | new PluralNode('c', ['a\\', 'b'])
|
147 | ],
|
148 |
|
149 | 'Automagically set ((anchor|to|count)) when plural have no anchor': [
|
150 | new LiteralNode('Automagically set '),
|
151 | new PluralNode('count', ['anchor', 'to', 'count']),
|
152 | new LiteralNode(' when plural have no anchor')
|
153 | ],
|
154 |
|
155 | 'Treat ((trailing|semicolumn)): literally and use automagic anchor': [
|
156 | new LiteralNode('Treat '),
|
157 | new PluralNode('count', ['trailing', 'semicolumn']),
|
158 | new LiteralNode(': literally and use automagic anchor')
|
159 | ]
|
160 | })
|
161 | }).export(module);
|