Skip to content

Commit

Permalink
Merge pull request enb#40 from zxqfox/feature/isAsync
Browse files Browse the repository at this point in the history
isAsync feature and a bunch of lang bugs
  • Loading branch information
Andrew Abramov committed Feb 13, 2015
2 parents 42257ef + 10d99ee commit 8bdd510
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 87 deletions.
13 changes: 0 additions & 13 deletions lib/assets/it-i18n.jst

This file was deleted.

51 changes: 42 additions & 9 deletions lib/assets/it.jst
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
it('should be equal `${ it }` by ${ engine.name }', function (<% if (saveHtml) { %>done<% } %>) {
var bemjson = references['${ it }'].bemjson,
expected = references['${ it }'].html,
actual = engines['${ engine.name }'].apply(bemjson);
<%
// prepares
var titleSuffix = lang ? ' in `' + lang + '` lang' : '';
var subreference = lang ? '[\'' + lang + '\']' : '';
var prettifyEngineName = function (ngn) {
return lang ? String(ngn).toLowerCase().replace(' ', '-') + '.' + lang : ngn;
};
if (saveHtml) {
var filename = [it, prettifyEngineName(engine.name), 'html'].join('.');
}
%>
it('should be equal `${ it }` by ${ engine.name }${ titleSuffix }', function (<% if (saveHtml || engine.async) { %>done<% } %>) {
var bemjson = references['${ it }']${ subreference }.bemjson,
expected = references['${ it }']${ subreference }.html;<%
if (!engine.async) { %>
// sync mode
var actual = engines['${ engine.name }']${ subreference }.apply(bemjson);
assertHtml(actual, expected);<%
if (saveHtml) {
var filename = [it, engine.name, 'html'].join('.'),
sep = '\n ';
print(sep + 'fs.writeFile(path.join(__dirname, \'' + filename + '\'), beautifyHtml(actual), done);');
} %>
if (saveHtml) { %>
saveHtmlFile('${ filename }', actual, done);
<% }
} else { // engine.async %>
// async mode
engines['${ engine.name }']${ subreference }.apply(bemjson, function(errs, actual) {
if (errs !== null) {
done(errs);
return;
}
assertHtml(actual, expected, function(err) {
<% if (saveHtml) {%>
saveHtmlFile('${ filename }', actual, function() {
done(err || null);
});
<% } else { %>
done(err || null);
<% } %>
});
});<%
} %>

});
116 changes: 76 additions & 40 deletions lib/assets/tmpl-spec.jst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ var assert = require('assert'),
path = require('path'),
<% if (saveHtml) { %>
fs = require('fs'),
beautifyHtml = require('${ paths['js-beautify'] }').html,
<% } %>
beautifyHtml = function(html) {
return require('${ paths['js-beautify'] }').html(html, beautifyHtmlConfig);
},
beautifyHtmlConfig = { unformatted: [
'a', 'span', 'img', 'address', 'script',
'h1', 'h2', 'h3', 'h4', 'h5','h6',
Expand All @@ -13,68 +14,103 @@ var assert = require('assert'),
'font', 'ins', 'del', 'pre', 'address', 'dt',
'q', 'i', 'b', 'u', 's', 'bdo', 'em'
]},
saveHtmlFile = function(filename, actual, done) {
fs.writeFile(path.join(__dirname, filename), beautifyHtml(actual), done);
},
<% } %>
dropRequireCache = require('enb/lib/fs/drop-require-cache'),
HtmlDiffer = require('${ paths['html-differ'] }').HtmlDiffer,
htmlDiffer = new HtmlDiffer('bem'),
referencesFilename = require.resolve('${ paths.references }'),
engines,
references;
referencesFilename = require.resolve('${ paths.references }');

describe('${ describe }', function() {
var engines = {},
references;

beforeEach(function () {
// reload references
dropRequireCache(require, referencesFilename);
references = require(referencesFilename);
});

<% if (!(langs && langs.length)) { %>
//
// no langs mode
//
beforeEach(function () {
// reload engines artifacts
engines = {};
<% _.forEach(engines, function(engine) {
function reRequire(where, what) {
var name = what.name,
target = what.target,
exportName = what.exportName;
print('dropRequireCache(require, require.resolve(\'' + target + '\'));\n');
print(where + '[\'' + name + '\'] = require(\'' + target + '\')' + exportName + ';\n');
}
if (langs.length) {
var engineObject = 'engines[\'' + engine.name + '\']';
print(engineObject + ' = {};\n');
_.forEach(engine.langs, function(lang) {
reRequire(engineObject, lang);
});
} else {
reRequire('engines', engine);
}
reRequire('engines', engine);
}); %>
});
<% _.forEach(its, function(it) { %>
describe('${ it }', function() {
<% _.forEach(its, function(it) { %>
describe('${ it }', function() {
<% _.forEach(engines, function(engine) {
if (langs.length) {
_.forEach(langs, function(lang) {
print(template('it-i18n', {
<% _.forEach(engines, function(engine) {
print(template('it', {
it: it,
engine: engine,
lang: lang,
lang: null,
saveHtml: saveHtml
}));
}); %>
}); // describe ${ it }
<% }); // _.forEach its %>
<% } else { // langs.length > 0 %>
//
// langs mode
//
<% _.forEach(langs, function (lang) { %>
describe('with lang `${ lang }`', function() {
beforeEach(function () {
<% _.forEach(engines, function(engine) {
var engineObject = 'engines[\'' + engine.name + '\']';
print(engineObject + ' = {};\n');
reRequire(engineObject, engine.langs.filter(function (_lang) { return lang === _lang.name; })[0]);
}); %>
});
} else {
print(template('it', {
it: it,
engine: engine,
saveHtml: saveHtml
}));
}
}); %>
});<% }); %>
});
<% _.forEach(its, function(it) { %>
describe('${ it }', function() {
<% _.forEach(engines, function(engine) {
print(template('it', {
it: it,
engine: engine,
lang: lang,
saveHtml: saveHtml
}));
}); %>
function assertHtml(actual, expected) {
});
<% }); %>
});
<% }); %>
<% }; // /if langs %>
}); // /describe ${ describe }
<%
function reRequire(where, what) {
var name = what.name,
target = what.target,
exportName = what.exportName;
print('dropRequireCache(require, require.resolve(\'' + target + '\'));\n');
print(where + '[\'' + name + '\'] = require(\'' + target + '\')' + exportName + ';\n');
}
%>
function assertHtml(actual, expected, done) {
if (htmlDiffer.isEqual(actual, expected)) {
assert.ok(actual);
done && done(null);
} else {
assert.fail(actual, expected, null, '\n');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/node-configurator.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ exports.configure = function (config, options) {
engineTargets.push(instrumented);
nodeConfig.addTarget(instrumented);
} else {
engineTargets.push(engine.target);
nodeConfig.addTarget(engine.target);
engineTargets.push(target);
nodeConfig.addTarget(target);
}
});
});
Expand Down
4 changes: 3 additions & 1 deletion lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ module.exports = function (helper) {

return level;
}),
langs: options.langs || [],
langs: options.langs,
engines: _.map(engines, function (engine, name) {
var techPath = engine.tech,
tech = require(techPath),
async = !!engine.async,
options = _.cloneDeep(engine.options || {}),
target = '?.' + name.toLowerCase().replace(' ', '-') + '.js',
exportName = options && options.exportName;
Expand All @@ -62,6 +63,7 @@ module.exports = function (helper) {
target: target,
exportName: exportName,
tech: tech,
async: async,
options: options
};
}),
Expand Down
4 changes: 2 additions & 2 deletions lib/techs/mock-lang-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = require('enb/lib/build-flow').create()
return vfs.read(source, 'utf8')
.then(function (content) {
var mock = [
'(function(global, bem_) {',
';(function(global, bem_) {',
' global.BEM = bem_;',
' var i18n = bem_.I18N = function(keyset, key, param) {',
' var result = key;',
Expand All @@ -20,7 +20,7 @@ module.exports = require('enb/lib/build-flow').create()
' i18n.keyset = function() { return i18n }',
' i18n.key = function(key) { return key }',
' i18n.lang = function() { return }',
'})(this, typeof BEM === \'undefined\' ? {} : BEM);'
'}(this, typeof BEM === \'undefined\' ? {} : BEM));'
].join('\n'),
mapIndex = content.lastIndexOf('//# sourceMappingURL='),
map;
Expand Down
32 changes: 20 additions & 12 deletions lib/techs/references.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,34 @@ module.exports = require('enb/lib/build-flow').create()
.useDirList('tmpl-specs')
.builder(function (dirs) {
var references = {},
bemjsons = {},
htmls = {};
bemjsons = [],
htmls = [];

dirs.forEach(function (dir) {
dir.files.forEach(function (file) {
var filename = file.name,
name = filename.split('.')[0];
fullname = file.fullname,
name = filename.split('.')[0],
lang = getLang(fullname),
item = {
name: name,
fullname: fullname,
lang: lang
};

if (hasExt(filename, '.bemjson.js')) {
bemjsons[name] = file.fullname;
bemjsons.push(item);
} else if (hasExt(filename, '.html')) {
htmls[name] = file.fullname;
htmls.push(item);
}
});
});

return vow.all([
vow.all(Object.keys(bemjsons).map(function (name) {
var filename = bemjsons[name],
lang = getLang(filename);
vow.all(bemjsons.map(function (file) {
var name = file.name,
lang = file.lang,
filename = file.fullname;

dropRequireCache(require, filename);

Expand All @@ -45,11 +53,11 @@ module.exports = require('enb/lib/build-flow').create()
}
});
})),
vow.all(Object.keys(htmls).map(function (name) {
var filename = htmls[name],
lang = getLang(filename);
vow.all(htmls.map(function (file) {
var name = file.name,
lang = file.lang;

return vfs.read(filename, 'utf-8')
return vfs.read(file.fullname, 'utf-8')
.then(function (source) {
var reference = references[name] || (references[name] = {});

Expand Down
Loading

0 comments on commit 8bdd510

Please sign in to comment.