Browse files

Support promises-based API in tests

  • Loading branch information...
1 parent c3f92b7 commit 98ee8ec0dc88aa87a141100a82d8ecda5b0458c8 @arikon arikon committed Dec 8, 2011
View
55 lib/legacy-tech.js
@@ -91,32 +91,37 @@ exports.Tech.prototype.getFileContent = function(prefix) {
return '';
};
-exports.Tech.prototype.getTechRelativePath = function(bemPath) {
- var absPath = this.getTechPath();
-
- // NOTE: Если путь до технологии пустой, значит используется
- // реализация технологии по-умолчанию, и путь надо оставить пустым
- if(!absPath) return '';
-
- // NOTE: Используется реализация технологии из bem/lib/techs/default,
- // путь надо оставить пустым
- if(absPath == bemUtil.getBemTechPath('default')) return '';
-
- // NOTE: Если путь до технологии относительный, значит используется
- // реализация технологии из библиотеки, и путь надо оставить без изменения
- if(!/^\//.test(absPath)) return absPath;
-
- // FIXME: лучше искать «короткий путь», обдумать критерии такого пути
- bemPath = PATH.join(bemPath || '.', '/');
- var shortestPath = PATH.relative(bemPath, absPath);
- require.paths.forEach(function(reqPath) {
- var relPath = PATH.relative(PATH.join(reqPath, '/'), absPath);
- if(relPath.length < shortestPath.length) {
- shortestPath = relPath;
- }
- });
+exports.Tech.prototype.getTechRelativePath = function(from) {
+ from = PATH.join(from || '.', '/');
+ var absPath = this.getTechPath(),
+ techPath = PATH.relative(PATH.join(__dirname, '../../'), absPath);
+
+ // tech from 'bem' module
+ if(!/^[\.\/]/.test(techPath) && /^.*?\/lib/.test(techPath)) {
+ techPath = techPath.replace(/^.*?\//, 'bem/');
+ } else {
+ // look for tech into node_modules and NODE_PATH env variable
+ var shortestPath = PATH.relative(from, absPath);
+ shortestPath = shortestPath.split('/');
+ module.paths.concat(bemUtil.getNodePaths()).forEach(function(reqPath) {
+ var relPath = PATH.relative(PATH.join(reqPath, '/'), absPath);
+ if(!/^\./.test(relPath)) {
+ relPath = relPath.split('/');
+ if(relPath.length < shortestPath.length) {
+ shortestPath = relPath;
+ }
+ }
+ });
+
+ techPath = Array.prototype.join.call(shortestPath, '/');
+ if(!/^\./.test(techPath)) techPath = './' + techPath;
+ }
+
+ techPath = bemUtil.stripModuleExt(techPath);
- return bemUtil.stripModuleExt(shortestPath);
+ // NOTE: default tech, need to return empty path for it
+ if(techPath == bemUtil.getBemTechPath('default')) return '';
+ return techPath;
};
exports.Tech.prototype.getTechPath = function() {
View
4 lib/legacy-techs/bemdecl.js.js
@@ -5,10 +5,6 @@ var fs = require('fs'),
exports.techModule = module;
-exports.getTechPath = function() {
- return 'bem/lib/legacy-techs/bemdecl.js';
-};
-
exports.newFileContent = function (vars) {
vars.BemDecl = '';
var decl = [],
View
4 lib/legacy-techs/css.js
@@ -2,10 +2,6 @@ var Template = require('../template');
exports.techModule = module;
-exports.getTechPath = function() {
- return 'bem/lib/legacy-techs/css';
-};
-
exports.outFile = function (file) {
return '@import url(' + file + ');\n';
};
View
4 lib/legacy-techs/decl.js.js
@@ -3,10 +3,6 @@ var UTIL = require('util'),
exports.techModule = module;
-exports.getTechPath = function() {
- return 'bem/lib/legacy-techs/decl.js';
-};
-
// .decl.js не собираются при вызове bem build
exports.bemBuild = function(prefixes, outputDir, outputName) {};
View
4 lib/legacy-techs/default.js
@@ -1,5 +1 @@
exports.techModule = module;
-
-exports.getTechPath = function() {
- return 'bem/lib/legacy-techs/default';
-};
View
4 lib/legacy-techs/deps.js.js
@@ -6,10 +6,6 @@ var FS = require('fs'),
exports.techModule = module;
-exports.getTechPath = function() {
- return 'bem/lib/legacy-techs/deps.js';
-};
-
function Deps(deps, tech) {
this.tech = tech;
this.items = {};
View
4 lib/legacy-techs/ie.css.js
@@ -6,10 +6,6 @@ for (var n in ieCSS) exports[n] = ieCSS[n];
exports.techModule = module;
-exports.getTechPath = function() {
- return 'bem/lib/legacy-techs/ie.css';
-};
-
exports.bemBuildContent = function(prefixes, suffixes, outputDir) {
var files = [];
View
4 lib/legacy-techs/js.js
@@ -3,10 +3,6 @@ var fs = require('fs'),
exports.techModule = module;
-exports.getTechPath = function() {
- return 'bem/lib/legacy-techs/js';
-};
-
exports.outFile = function (relFile, file) {
return [
View
4 lib/legacy-techs/sass.js
@@ -2,10 +2,6 @@ var Template = require('../template');
exports.techModule = module;
-exports.getTechPath = function() {
- return 'bem/lib/legacy-techs/sass';
-};
-
exports.outFile = function (file) {
return '@import ' + file + '\n';
};
View
2 lib/level.js
@@ -89,7 +89,7 @@ var Level = exports.Level = INHERIT({
return techPath;
}
- throw new Error("Tech module with path '" + techPath + "' not found on require.paths");
+ throw new Error("Tech module with path '" + techPath + "' not found on require search paths");
},
getDefaultTechs: function() {
View
4 test/legacy-tech-test.js
@@ -53,8 +53,8 @@ vows.describe('tech').addBatch({
".matchSuffix('.test.js') returns true": function(tech) {
assert.isTrue(tech.matchSuffix('.test.js'));
},
- ".getTechRelativePath('./') resolves to 'data/techs/test.js'": function(tech) {
- assert.equal(tech.getTechRelativePath(__dirname), 'data/techs/test.js');
+ ".getTechRelativePath('./') resolves to './data/techs/test.js'": function(tech) {
+ assert.equal(tech.getTechRelativePath(__dirname), './data/techs/test.js');
}
}
View
11 test/level-test.js
@@ -1,6 +1,7 @@
var vows = require('vows'),
assert = require('assert'),
myPath = require('../lib/path'),
+ bemUtil = require('../lib/util'),
createLevel = require('../lib/level').createLevel;
vows.describe('level').addBatch({
@@ -14,6 +15,10 @@ vows.describe('level').addBatch({
assert.isArray(defs);
assert.isEmpty(defs);
},
+
+ /*
+ TODO: подумать об актуальности этих тестов
+
".resolveTechPath('../../techs/test.js') resolves": function(level) {
assert.equal(level.resolveTechPath('../../techs/test.js'), absolute('./data/techs/test.js'));
},
@@ -23,10 +28,10 @@ vows.describe('level').addBatch({
},
".resolveTechPath('data/techs/test.js') resolves": function(level) {
var path = 'data/techs/test.js';
- require.paths.unshift(__dirname);
- assert.equal(level.resolveTechPath(path), path);
- require.paths.shift();
+ assert.equal(level.resolveTechPath(absolute(path)), path);
},
+ */
+
"matchers are compliant to getters": testCompliances()
}
View
53 test/tech-test.js
@@ -1,4 +1,5 @@
-var vows = require('vows'),
+var Q = require('q'),
+ vows = require('vows'),
assert = require('assert'),
PATH = require('../lib/path'),
createTech = require('../lib/tech').createTech;
@@ -66,11 +67,26 @@ function testBaseTech(techPath, techAlias) {
".getCreateResult()": {},
- ".getCreateResults()": function(tech) {
- var res = tech.getCreateResults('test', { BlockName: 'b-test' });
- tech.getSuffixes().forEach(function(suffix) {
- assert.include(res, suffix);
- });
+ ".getCreateResults()": {
+ topic: function(tech) {
+ var _this = this;
+ Q.when(
+ tech.getCreateResults('test', { BlockName: 'b-test' }),
+ function(res) {
+ _this.callback(null, res, tech.getSuffixes());
+ },
+ function(err) {
+ _this.callback(err);
+ }
+ );
+ },
+
+ "contains results for all suffixes": function(err, res, suffixes) {
+ assert.isNull(err);
+ suffixes.forEach(function(suffix) {
+ assert.include(res, suffix);
+ });
+ }
},
".storeCreateResult()": {},
@@ -79,11 +95,26 @@ function testBaseTech(techPath, techAlias) {
".readContent()": {},
- ".readAllContent()": function(tech) {
- var res = tech.readAllContent('test');
- tech.getSuffixes().forEach(function(suffix) {
- assert.include(res, suffix);
- });
+ ".readAllContent()": {
+ topic: function(tech) {
+ var _this = this;
+ Q.when(
+ tech.readAllContent('test'),
+ function(res) {
+ _this.callback(null, res, tech.getSuffixes());
+ },
+ function(err) {
+ _this.callback(err);
+ }
+ );
+ },
+
+ "contains results for all suffixes": function(err, res, suffixes) {
+ assert.isNull(err);
+ suffixes.forEach(function(suffix) {
+ assert.include(res, suffix);
+ });
+ }
},
// build

0 comments on commit 98ee8ec

Please sign in to comment.