New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

eyeglass-dev-testutils

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eyeglass-dev-testutils - npm Package Compare versions

Comparing version

to
1.0.0

6

CHANGELOG.md

@@ -0,1 +1,7 @@

<a name="1.0.0"></a>
# [1.0.0](https://github.com/sass-eyeglass/eyeglass-dev-testutils/compare/v0.1.0...v1.0.0) (2015-11-17)
<a name="0.1.0"></a>

@@ -2,0 +8,0 @@ # [0.1.0](https://github.com/sass-eyeglass/eyeglass-dev-testutils/compare/v0.0.7...v0.1.0) (2015-11-17)

249

lib/testutils.js

@@ -33,8 +33,9 @@ "use strict";

function Testutils(options) {
if (!(this instanceof Testutils)) {
return new Testutils(options);
}
options = merge({
// engines
engines: {
eyeglass: require("eyeglass"),
sass: require("node-sass")
},
engines: {},
options: {}

@@ -48,135 +49,139 @@ }, options);

};
if (!this.engines.eyeglass || !this.engines.sass) {
throw new Error("you must now pass your sass and eyeglass engine explicitly to eyeglass-dev-testutils");
}
}
var methods = {
getSassFixtures: function(fixtureDir) {
var rExt = /\.s[ac]ss$/;
var encoding = "utf8";
Testutils.prototype.getSassFixtures = function(fixtureDir) {
var rExt = /\.s[ac]ss$/;
var encoding = "utf8";
var files = glob.sync(fixtureDir + "**/[^_]*.s[ac]ss");
var files = glob.sync(fixtureDir + "**/[^_]*.s[ac]ss");
return files.reduce(function(obj, sourceFile) {
var expectedFile = sourceFile.replace(rExt, ".css");
var dataFile = sourceFile.replace(rExt, ".json");
var name = sourceFile.replace(fixtureDir, "").replace(rExt, "")
.replace(/^\//, "");
return files.reduce(function(obj, sourceFile) {
var expectedFile = sourceFile.replace(rExt, ".css");
var dataFile = sourceFile.replace(rExt, ".json");
var name = sourceFile.replace(fixtureDir, "").replace(rExt, "")
.replace(/^\//, "");
var current = obj[name] = {
source: readFile(sourceFile, encoding),
expected: readFile(expectedFile, encoding),
data: JSON.parse(readFile(dataFile, encoding) || null)
};
var current = obj[name] = {
source: readFile(sourceFile, encoding),
expected: readFile(expectedFile, encoding),
data: JSON.parse(readFile(dataFile, encoding) || null)
};
if (current.data && current.data.expectedError) {
current.error = getError(current.data.expectedError);
}
return obj;
}, {});
},
assertCompilesSync: function(options, expectedOutput, name) {
try {
var result = this.compileSync(options);
this.assertExpectedCSS(result.css.toString(), expectedOutput, name);
if (current.data && current.data.expectedError) {
current.error = getError(current.data.expectedError);
}
catch (err) {
assert(!err, err.toString());
}
},
assertCompiles: function(options, expectedOutput, done, name) {
this.compile(options, function(err, result) {
assert(!err, err && err.message);
this.assertExpectedCSS(result.css.toString(), expectedOutput, name);
done();
}.bind(this));
},
assertCompilationError: function(options, expectedError, done) {
this.compile(options, function(err, result) {
assert(err);
assert(!result);
this.assertErrorMessage(err.message, expectedError);
done();
}.bind(this));
},
compile: function(options, cb, name) {
this.engines.sass.render(this.sassOptions(options), cb, name);
},
compileSync: function(options, name) {
return this.engines.sass.renderSync(this.sassOptions(options), name);
},
sassOptions: function(options) {
if (typeof options === "string") {
options = merge({}, this.options, {
data: options
});
}
if (typeof options.sassOptions === "function") {
return options.sassOptions();
}
else {
options = merge({}, this.options, options);
return new this.engines.eyeglass.Eyeglass(options, this.engines.sass).sassOptions();
}
},
assertCompilesFixture: function(fixture, done) {
if (fixture.data && fixture.error) {
this.assertCompilationError(fixture.source, fixture.error, done);
}
else {
this.assertCompiles(fixture.source, fixture.expected || "", done);
}
},
assertExpectedCSS: function(output, expected, name) {
var diffStr = "";
var isSame = true;
if (expected instanceof RegExp) {
isSame = expected.test(normalizeCss(output));
}
else {
var diff = diffy.diffCss(normalizeCss(expected), normalizeCss(output));
diff.forEach(function(part) {
var value = part.value;
if (part.added || part.removed) {
isSame = false;
value = (part.added ? chalk.red.inverse : chalk.green.inverse)(value);
}
diffStr += value;
});
}
return obj;
}, {});
};
if (name) {
diffStr = "[" + chalk.gray(name) + "]\n" + diffStr;
}
diffStr = chalk.reset(diffStr);
var msg = "the output does not match\n" + diffStr;
assert.ok(isSame, msg);
},
assertMultilineEqual: function(string1, string2) {
var lines1 = string1.split("\n");
var lines2 = string2.split("\n");
lines1.forEach(function(line1, lineNum) {
var line2 = lines2[lineNum];
var msg = "expected line #" + (lineNum + 1) + " to be the same";
assert.equal(line1, line2, msg);
Testutils.prototype.assertCompilesSync = function(options, expectedOutput, name) {
try {
var result = this.compileSync(options);
this.assertExpectedCSS(result.css.toString(), expectedOutput, name);
}
catch (err) {
assert(!err, err.toString());
}
};
Testutils.prototype.assertCompiles = function(options, expectedOutput, done, name) {
this.compile(options, function(err, result) {
assert(!err, err && err.message);
this.assertExpectedCSS(result.css.toString(), expectedOutput, name);
done();
}.bind(this));
};
Testutils.prototype.assertCompilationError = function(options, expectedError, done) {
this.compile(options, function(err, result) {
assert(err);
assert(!result);
this.assertErrorMessage(err.message, expectedError);
done();
}.bind(this));
};
Testutils.prototype.compile = function(options, cb, name) {
this.engines.sass.render(this.sassOptions(options), cb, name);
};
Testutils.prototype.compileSync = function(options, name) {
return this.engines.sass.renderSync(this.sassOptions(options), name);
};
Testutils.prototype.sassOptions = function(options) {
if (typeof options === "string") {
options = merge({}, this.options, {
data: options
});
},
assertErrorMessage: function(message, expected) {
if (expected instanceof RegExp) {
assert.ok(expected.test(message));
}
else {
this.assertMultilineEqual(message, expected);
}
}
if (typeof options.sassOptions === "function") {
return options.sassOptions();
}
else {
options = merge({}, this.options, options);
return new this.engines.eyeglass.Eyeglass(options, this.engines.sass).sassOptions();
}
};
var testutilsInstance = new Testutils();
Testutils.prototype.assertCompilesFixture = function(fixture, done) {
if (fixture.data && fixture.error) {
this.assertCompilationError(fixture.source, fixture.error, done);
}
else {
this.assertCompiles(fixture.source, fixture.expected || "", done);
}
};
Object.keys(methods).forEach(function(methodName) {
var method = methods[methodName];
Testutils.prototype[methodName] = method;
Testutils[methodName] = method.bind(testutilsInstance);
});
Testutils.prototype.assertExpectedCSS = function(output, expected, name) {
var diffStr = "";
var isSame = true;
if (expected instanceof RegExp) {
isSame = expected.test(normalizeCss(output));
}
else {
var diff = diffy.diffCss(normalizeCss(expected), normalizeCss(output));
diff.forEach(function(part) {
var value = part.value;
if (part.added || part.removed) {
isSame = false;
value = (part.added ? chalk.red.inverse : chalk.green.inverse)(value);
}
diffStr += value;
});
}
if (name) {
diffStr = "[" + chalk.gray(name) + "]\n" + diffStr;
}
diffStr = chalk.reset(diffStr);
var msg = "the output does not match\n" + diffStr;
assert.ok(isSame, msg);
};
Testutils.prototype.assertMultilineEqual = function(string1, string2) {
var lines1 = string1.split("\n");
var lines2 = string2.split("\n");
lines1.forEach(function(line1, lineNum) {
var line2 = lines2[lineNum];
var msg = "expected line #" + (lineNum + 1) + " to be the same";
assert.equal(line1, line2, msg);
});
};
Testutils.prototype.assertErrorMessage = function(message, expected) {
if (expected instanceof RegExp) {
assert.ok(expected.test(message));
}
else {
this.assertMultilineEqual(message, expected);
}
};
module.exports = Testutils;
{
"name": "eyeglass-dev-testutils",
"version": "0.1.0",
"version": "1.0.0",
"description": "Testing Utilities. For Eyeglass.",

@@ -25,8 +25,7 @@ "license": "Apache-2.0",

"diff": "^2.1.1",
"eyeglass": "^0.7.1",
"glob": "^5.0.14",
"lodash.merge": "^3.3.2",
"node-sass": "^3.4.2"
"lodash.merge": "^3.3.2"
},
"devDependencies": {
"eyeglass": "^0.7.1",
"eyeglass-dev-eslint": "2.0.0",

@@ -45,4 +44,5 @@ "gulp": "^3.9.0",

"mocha": "^2.3.2",
"node-sass": "^3.4.2",
"should": "^7.1.0"
}
}

@@ -16,4 +16,12 @@ # eyeglass-dev-testutils [![Build Status][travis-ci-badge]][travis-ci] [![Version][npm-version-badge]][npm-version] [![License][license-badge]][license]

```js
var testutils = require("eyeglass-dev-testutils");
var path = require("path");
var sass = require("node-sass");
var eyeglass = require("eyeglass");
var Testutils = require("eyeglass-dev-testutils");
var testutils = new Testutils({
engines: {
sass: sass,
eyeglass: eyeglass
}
});

@@ -49,18 +57,2 @@ var fixtureDir = path.join(__dirname, "fixtures"); // ./test/fixtures/

## Custom engines
If you need to use a specific version of Sass or eyeglass, you can pass it via the engine option:
```js
var Testutils = require("eyeglass-dev-testutils");
var sass = require("node-sass");
var eyeglass = require("eyeglass");
var testutils = new Testutils({
engines: {
sass: sass,
eyeglass: eyeglass
}
});
```
## License

@@ -67,0 +59,0 @@

"use strict";
var testutils = require("../index");
var path = require("path");
var assert = require("assert");
var sass = require("node-sass");
var eyeglass = require("eyeglass");
var Testutils = require("../index");
var testutils = new Testutils({
engines: {
sass: sass,
eyeglass: eyeglass
}
});
var fixtureDir = path.join(__dirname, "fixtures");

@@ -8,0 +17,0 @@