From b30aea25b5afc32023664f9b669d2f55df566c4d Mon Sep 17 00:00:00 2001 From: Alexander Slansky Date: Sun, 17 Aug 2014 12:13:33 +0200 Subject: [PATCH] added test case for custom mustache template --- .jscs.json | 8 ++++---- lib/templates/sprite.js | 5 ++--- test/css-sprite.js | 35 ++++++++++++++++++++++++++++++++- test/template/template.mustache | 24 ++++++++++++++++++++++ 4 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 test/template/template.mustache diff --git a/.jscs.json b/.jscs.json index 13754b3..59f3f4d 100644 --- a/.jscs.json +++ b/.jscs.json @@ -4,8 +4,10 @@ "requireSpaceBeforeBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!=="], "requireSpaceAfterBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!=="], "requireLineFeedAtFileEnd": true, - "requireRightStickedOperators": [ "!" ], - "requireLeftStickedOperators": [ "," ], + "requireSpaceBeforeBinaryOperators": true, + "requireSpaceBeforePostfixUnaryOperators": true, + "requireSpaceAfterBinaryOperators": true, + "requireSpaceBeforePostfixUnaryOperators": true, "requireKeywordsOnNewLine": ["else", "else if"], "requireSpacesInFunctionExpression": { "beforeOpeningCurlyBrace": true, @@ -14,8 +16,6 @@ "disallowSpacesInsideParentheses": true, "disallowSpaceAfterKeywords": [], "disallowSpaceAfterObjectKeys": true, - "disallowLeftStickedOperators": [ "?", "+", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<=" ], - "disallowRightStickedOperators": [ "?", "/", "*", ":", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<=" ], "disallowImplicitTypeConversion": [], "disallowMultipleLineBreaks": true, "disallowSpacesInsideArrayBrackets": true, diff --git a/lib/templates/sprite.js b/lib/templates/sprite.js index 3b93f6a..b4f9c37 100644 --- a/lib/templates/sprite.js +++ b/lib/templates/sprite.js @@ -2,6 +2,7 @@ // Load in local modules var fs = require('graceful-fs'); +var path = require('path'); var mustache = require('mustache'); var tmpl = { 'css': fs.readFileSync(__dirname + '/css.mustache', 'utf8'), @@ -48,9 +49,7 @@ function cssTemplate (params) { } }); // Render and return CSS - var tmplFile = options.template - ? fs.readFileSync(options.template, 'utf8') - : tmpl[options.processor]; + var tmplFile = options.template ? fs.readFileSync(path.join(process.cwd(), options.template), 'utf8') : tmpl[options.processor]; var css = mustache.render(tmplFile, tmplParams); return css; } diff --git a/test/css-sprite.js b/test/css-sprite.js index b0ed7eb..7ffa119 100644 --- a/test/css-sprite.js +++ b/test/css-sprite.js @@ -117,6 +117,40 @@ describe('css-sprite (lib/css-sprite.js)', function () { done(); }); }); + it('should return a object stream with a sprite and a css file (using a custom template)', function (done) { + var png, css; + vfs.src('./test/fixtures/**') + .pipe(sprite({ + out: './dist/img', + name: 'sprites.png', + style: './dist/css/sprites.css', + template: './test/template/template.mustache' + })) + .pipe(through2.obj(function (file, enc, cb) { + if (file.relative.indexOf('png') > -1) { + png = file; + } + else { + css = file; + } + cb(); + })) + .on('data', noop) + .on('end', function () { + png.should.be.ok; + png.path.should.equal('dist/img/sprites.png'); + png.relative.should.equal('sprites.png'); + css.should.be.ok; + css.path.should.equal('./dist/css/sprites.css'); + css.relative.should.equal('sprites.css'); + css.contents.toString('utf-8').should.containEql('.icon-camera'); + css.contents.toString('utf-8').should.containEql('.icon-cart'); + css.contents.toString('utf-8').should.containEql('.icon-command'); + css.contents.toString('utf-8').should.containEql('.icon-font'); + css.contents.toString('utf-8').should.containEql('custom: \'template\';'); + done(); + }); + }); it('should return a object stream with retina sprite, normal sprite and css with media query', function (done) { var png = [], css; vfs.src('./test/fixtures/**') @@ -255,7 +289,6 @@ describe('css-sprite (lib/css-sprite.js)', function () { }); }); it('should throw error when file stream', function (done) { - vfs.src('./test/fixtures/**', {buffer: false}) .pipe(sprite({ out: './dist/img', diff --git a/test/template/template.mustache b/test/template/template.mustache new file mode 100644 index 0000000..1bccb06 --- /dev/null +++ b/test/template/template.mustache @@ -0,0 +1,24 @@ +{{#sprite}} +{{class}} { + background-image: url('{{{escaped_image}}}'); +} + +{{/sprite}} +{{#retina}} +@media (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (-webkit-min-device-pixel-ratio: 1.5), (min-device-pixel-ratio: 1.5), (min-resolution: 1.5dppx) { + {{class}} { + background-image: url('{{{escaped_image}}}'); + background-size: {{px.total_width}} {{px.total_height}}; + } +} + +{{/retina}} +{{#items}} +{{class}} { + custom: 'template'; + background-position: {{px.offset_x}} {{px.offset_y}}; + width: {{px.width}}; + height: {{px.height}}; +} + +{{/items}}