From 92491a916f7600e461f936e1cb038dee127c99d5 Mon Sep 17 00:00:00 2001 From: Alexej Yaroshevich Date: Wed, 11 Feb 2015 23:57:15 +0400 Subject: [PATCH] isAsync and some refactoring - restored htmlBeautifier func-ty - reworked langs it render - added isAsync flag to force calling .apply on engine - added safer call of i18n --- lib/assets/it-i18n.jst | 13 ---------- lib/assets/it.jst | 51 ++++++++++++++++++++++++++++++++------- lib/assets/tmpl-spec.jst | 13 +++++++--- lib/techs/mock-lang-js.js | 4 +-- lib/techs/tmpl-spec.js | 10 ++++---- 5 files changed, 59 insertions(+), 32 deletions(-) delete mode 100644 lib/assets/it-i18n.jst diff --git a/lib/assets/it-i18n.jst b/lib/assets/it-i18n.jst deleted file mode 100644 index b44aad1..0000000 --- a/lib/assets/it-i18n.jst +++ /dev/null @@ -1,13 +0,0 @@ -it('should be equal `${ it }` by ${ engine.name } in `${ lang }` lang', function (<% if (saveHtml) { %>done<% } %>) { - var bemjson = references['${ it }']['${ lang }'].bemjson, - expected = references['${ it }']['${ lang }'].html, - actual = engines['${ engine.name }']['${ lang }'].apply(bemjson); - - assertHtml(actual, expected);<% - if (saveHtml) { - var filename = [it, engine.name.toLowerCase().replace(' ', '-'), lang, 'html'].join('.'), - sep = '\n '; - - print(sep + 'fs.writeFile(path.join(__dirname, \'' + filename + '\'), beautifyHtml(actual), done);'); - } %> -}); diff --git a/lib/assets/it.jst b/lib/assets/it.jst index 3fa3b00..94314ca 100644 --- a/lib/assets/it.jst +++ b/lib/assets/it.jst @@ -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.isAsync) { %>done<% } %>) { + var bemjson = references['${ it }']${ subreference }.bemjson, + expected = references['${ it }']${ subreference }.html;<% + +if (!engine.isAsync) { %> + // 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.isAsync %> + // 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); +<% } %> + }); + });<% +} %> + }); diff --git a/lib/assets/tmpl-spec.jst b/lib/assets/tmpl-spec.jst index 2826fec..99b6fa7 100644 --- a/lib/assets/tmpl-spec.jst +++ b/lib/assets/tmpl-spec.jst @@ -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', @@ -13,6 +14,10 @@ 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'), @@ -64,6 +69,7 @@ describe('${ it }', function() { print(template('it', { it: it, engine: engine, + lang: null, saveHtml: saveHtml })); } @@ -72,9 +78,10 @@ describe('${ it }', function() { });<% }); %> }); -function assertHtml(actual, expected) { +function assertHtml(actual, expected, done) { if (htmlDiffer.isEqual(actual, expected)) { assert.ok(actual); + done && done(null); } else { assert.fail(actual, expected, null, '\n'); } diff --git a/lib/techs/mock-lang-js.js b/lib/techs/mock-lang-js.js index 47c9e8a..a9b30f4 100644 --- a/lib/techs/mock-lang-js.js +++ b/lib/techs/mock-lang-js.js @@ -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;', @@ -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; diff --git a/lib/techs/tmpl-spec.js b/lib/techs/tmpl-spec.js index d239c11..54321b9 100644 --- a/lib/techs/tmpl-spec.js +++ b/lib/techs/tmpl-spec.js @@ -4,8 +4,7 @@ var path = require('path'), assetsDirname = path.join(__dirname, '..', 'assets'), readAssets = vow.all([ vfs.read(path.join(assetsDirname, 'tmpl-spec.jst'), 'utf-8'), - vfs.read(path.join(assetsDirname, 'it.jst'), 'utf-8'), - vfs.read(path.join(assetsDirname, 'it-i18n.jst'), 'utf-8') + vfs.read(path.join(assetsDirname, 'it.jst'), 'utf-8') ]), lodash = require('lodash'), template = lodash.template, @@ -69,10 +68,9 @@ module.exports = require('enb/lib/build-flow').create() } its = Object.keys(its); - return readAssets.spread(function (asset, it, iti18n) { + return readAssets.spread(function (asset, it) { var templates = { - it: it, - 'it-i18n': iti18n + it: it }, data = { describe: path.basename(nodePath) + ' (' + path.dirname(nodePath) + ')', @@ -98,6 +96,7 @@ module.exports = require('enb/lib/build-flow').create() var langTarget = target.replace('.js', '.' + lang + '.js'); return { name: lang, + isAsync: engine.options.isAsync, target: langTarget, exportName: exportName }; @@ -107,6 +106,7 @@ module.exports = require('enb/lib/build-flow').create() return { name: engine.name, + isAsync: engine.options.isAsync, target: target, exportName: exportName };