From 3aa7b6d571aa274eeb6b43070b35e999d43005f9 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 --- lib/assets/it-i18n.jst | 13 ---------- lib/assets/it.jst | 52 +++++++++++++++++++++++++++++++++------- lib/assets/tmpl-spec.jst | 7 ++++-- lib/techs/tmpl-spec.js | 2 ++ 4 files changed, 50 insertions(+), 24 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..93ca7c4 100644 --- a/lib/assets/it.jst +++ b/lib/assets/it.jst @@ -1,13 +1,47 @@ -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) { %> +function saveHtmlFile(filename, actual, done) { + fs.writeFile(path.join(__dirname, filename), beautifyHtml(actual), done); +}<% +}%> +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 (saveHtml) {%> + var filename = [it, prettifyEngineName(engine.name), 'html'].join('.'); +}<% + +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 {%> + // async mode + engines['${ engine.name }']${ subreference }.apply(bemjson, function (errs, actual) { + if (errs !== null) { + done(errs); + return; + } + + assertHtml(actual, expected, function (err) { + err && done(err);%> +if (saveHtml) {%> + saveHtmlFile('${ filename }', actual, done); +%>} + }); + });<% +}%> }); diff --git a/lib/assets/tmpl-spec.jst b/lib/assets/tmpl-spec.jst index 2826fec..0b51005 100644 --- a/lib/assets/tmpl-spec.jst +++ b/lib/assets/tmpl-spec.jst @@ -2,7 +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', @@ -72,9 +74,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/tmpl-spec.js b/lib/techs/tmpl-spec.js index d239c11..17cc161 100644 --- a/lib/techs/tmpl-spec.js +++ b/lib/techs/tmpl-spec.js @@ -98,6 +98,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 +108,7 @@ module.exports = require('enb/lib/build-flow').create() return { name: engine.name, + isAsync: engine.options.isAsync, target: target, exportName: exportName };