Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.vscode
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ sudo: false
language: node_js
node_js:
- "0.12"
- "0.10"
- "0.10"
env:
- NODE_ENV=test
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,46 @@ sassaby.includedMixin('appearance').calledWithArgs('button').hasNumDeclarations(
```

#### declares
Assert that the mixin makes a declaration of the given rule-property pair.
Can be either called with a string as property value or with an Array object.

Called with a String it asserts that the mixin makes a declaration of the given rule-property pair.
```js
sassaby.includedMixin('appearance').calledWithArgs('button').declares('-webkit-appearance', 'button');
```
Called with an Object it asserts that the mixin makes a declaration of the given property with the provided fallback values.
E.g. for the mixin:
```
@mixin font-size($font-size: 1.6) {
font-size: $font-size * 16 + px;
font-size: $font-size + rem;
}
```
```js
sassaby.includedMixin('font-size').calledWithArgs('2').declares('max-height', ['32px', '2rem']);
```

#### declaresInSelector
Assert that the mixin makes a declaration of the given rule-property pair inside the provided selector.
E.g. for the mixin:
```
@mixin clearfix() {
&:before,
&:after {
content: " "; // 1
display: table; // 2
}
&:after {
clear: both;
}
}
```
```js
sassaby.includedMixin('clearfix').called().declaresInSelector(':before', 'content', " ");
sassaby.includedMixin('clearfix').called().declaresInSelector(':before', 'display', 'table');
sassaby.includedMixin('clearfix').called().declaresInSelector(':after', 'content', " ");
sassaby.includedMixin('clearfix').called().declaresInSelector(':after', 'display', 'table');
sassaby.includedMixin('clearfix').called().declaresInSelector(':after', 'clear', 'both');
```

#### doesNotDeclare
Assert that the mixin does not make a declaration of the given rule-property pair.
Expand Down
61 changes: 36 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
{
"name": "sassaby",
"version": "2.1.1",
"description": "A unit testing library for SASS",
"keywords": ["sass", "scss", "testing", "unit testing", "bdd", "tdd", "test"],
"author": "Ryan Bahniuk <ryan.bahniuk@gmail.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/ryanbahniuk/sassaby"
},
"main": "src/sassaby",
"dependencies": {
"css": "^2.2.0",
"cssmin": "^0.4.3",
"mocha": "^2.2.5",
"node-sass": "^3.1.2"
},
"devDependencies": {
"proxyquire": "^1.5.0",
"sinon": "^1.14.1"
},
"scripts": {
"test": "NODE_ENV=test mocha"
}
}
"name": "sassaby",
"version": "2.1.1",
"description": "A unit testing library for SASS",
"keywords": [
"sass",
"scss",
"testing",
"unit testing",
"bdd",
"tdd",
"test"
],
"author": "Ryan Bahniuk <ryan.bahniuk@gmail.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/ryanbahniuk/sassaby"
},
"main": "src/sassaby",
"dependencies": {
"css": "^2.2.0",
"cssmin": "^0.4.3",
"mocha": "^2.2.5",
"node-sass": "2.0.1",
"request": "2.81.0",
"normalize-path": "^2.1.1"
},
"devDependencies": {
"cross-env": "^5.1.0",
"proxyquire": "^1.5.0",
"sinon": "^1.14.1"
},
"scripts": {
"test": "cross-env NODE_ENV=test & mocha"
}
}
20 changes: 18 additions & 2 deletions src/mixinResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,25 @@ MixinResult.prototype = {
},

declares: function(property, value) {
var declaration = parsers.findDeclaration(this.ast, property);
var declarationValue = declaration ? utilities.scrubQuotes(declaration.value) : '';
var declaration, declarationValue;
if (Array.isArray(value) && value.length > 1) {
declaration = parsers.findDeclarations(this.ast, property);
declarationValue = [];
declaration.forEach(function(declaration){
declarationValue.push(declaration.value);
});
} else {
declaration = parsers.findDeclaration(this.ast, property);
declarationValue = declaration ? utilities.scrubQuotes(declaration.value) : '';
}
var message = 'Value: ' + declarationValue + ' does not equal value: ' + value + '.';
assert.equal(declarationValue.toString(), value.toString(), message);
},

declaresInSelector: function(selector, property, value) {
var declaration = parsers.findDeclarationInSelector(this.ast, selector, property);
var declarationValue = declaration ? utilities.scrubQuotes(declaration.value) : '';
var message = 'In Selector:'+ selector + ' the value: ' + declarationValue + ' does not equal value: ' + value + '.';
assert.equal(declarationValue, value.toString(), message);
},

Expand Down
55 changes: 51 additions & 4 deletions src/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,28 @@ function hasSelectorValue(rule, selectorValue) {

function findDeclarationProperty(rule, declarationProperty) {
var foundDeclaration;

if (rule.declarations) {
rule.declarations.forEach(function(declaration) {
if (declaration.property === declarationProperty) {
foundDeclaration = declaration;
}
});
}

return foundDeclaration;
}

function findDeclarationsProperties(rule, declarationProperty) {
var foundDeclarations = [];
if (rule.declarations) {
rule.declarations.forEach(function(declaration) {
if (declaration.property === declarationProperty) {
foundDeclarations.push(declaration);
}
});
}
return foundDeclarations;
}

function isFontFace(rule) {
if (rule.type === 'font-face') {
return true;
Expand All @@ -57,10 +67,8 @@ var Parsers = {
});
return count;
},

findDeclaration: function(ast, property) {
var found;

ast.stylesheet.rules.forEach(function(rule) {
if (rule.type === 'media') {
rule.rules.forEach(function(rule) {
Expand All @@ -74,6 +82,45 @@ var Parsers = {
return found;
},

findDeclarations: function(ast, property) {
var found = [];
ast.stylesheet.rules.forEach(function(rule) {
if (rule.type === 'media') {
rule.rules.forEach(function(rule) {
found.push(findDeclarationsProperties(rule, property));
});
} else {
found.push(findDeclarationsProperties(rule, property));
}
});
var declarationsArray;
found.forEach(function(value) {
if(value.length !== 0) {
declarationsArray = value;
}
})
return declarationsArray;
},

findDeclarationInSelector: function(ast, selector, property) {
var found;
if (selector.indexOf("&") > -1) {
selector = selector.replace("&", ".test");
}
if (selector.indexOf("@at-root") > -1) {
selector = selector.replace("@at-root ", "");
}
ast.stylesheet.rules.forEach(function(rule) {
for(var i = 0; i < rule.selectors.length; i++) {
if(rule.selectors[i].indexOf(selector) >= 0 ) {
found = found || findDeclarationProperty(rule, property);
}
}
});
console.log(found);
return found;
},

findMedia: function(ast) {
var found = [];

Expand Down
5 changes: 3 additions & 2 deletions src/sassaby.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var assert = require('assert');
var Mixin = require('./types/mixin');
var Func = require('./types/func');
var parsers = require('./parsers');
var normalize = require('normalize-path');

function setVariables(varz) {
var sassVariables = '';
Expand All @@ -17,15 +18,15 @@ function setVariables(varz) {
function setDependencies(dependencies) {
var sassImports = '';
dependencies.forEach(function(fileName) {
sassImports = sassImports + "@import '" + fileName + "';";
sassImports = sassImports + "@import '" + normalize(fileName) + "';";
});
return sassImports;
}

function Sassaby(path, options) {
options = options || {};
this.path = path;
this.file = fs.readFileSync(path).toString();
this.file = fs.readFileSync(normalize(path), 'utf-8').toString();
this.variables = '';
this.dependencies = '';

Expand Down
65 changes: 64 additions & 1 deletion test/fixtures/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,69 @@
"column": 18
}
}
},
{
"type": "declaration",
"property": "max-height",
"value": "16px",
"position": {
"start": {
"line": 9,
"column": 3
},
"end": {
"line": 9,
"column": 18
}
}
},
{
"type": "declaration",
"property": "max-height",
"value": "1rem",
"position": {
"start": {
"line": 10,
"column": 3
},
"end": {
"line": 10,
"column": 18
}
}
}
],
"position": {
"start": {
"line": 11,
"column": 1
},
"end": {
"line": 13,
"column": 2
}
}
},
{
"type": "rule",
"selectors": [
".test.testInSelector"
],
"declarations": [
{
"type": "declaration",
"property": "color",
"value": "red",
"position": {
"start": {
"line": 11,
"column": 3
},
"end": {
"line": 11,
"column": 13
}
}
}
],
"position": {
Expand All @@ -94,7 +157,7 @@
"column": 1
},
"end": {
"line": 9,
"line": 14,
"column": 2
}
}
Expand Down
Loading