From b49e2f5f0f5fb5b9fe26ef4f1f5ec9cd62a185d6 Mon Sep 17 00:00:00 2001 From: Andrey Sitnik Date: Tue, 22 Dec 2015 18:02:23 +0300 Subject: [PATCH] Rewrite tests to AVA --- .eslintrc | 9 ++-- package.json | 11 ++--- test.js | 131 +++++++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 135 --------------------------------------------------- 4 files changed, 141 insertions(+), 145 deletions(-) create mode 100644 test.js delete mode 100644 test/test.js diff --git a/.eslintrc b/.eslintrc index 3325819..6ba6231 100644 --- a/.eslintrc +++ b/.eslintrc @@ -87,7 +87,7 @@ "comma-style": [2], "brace-style": [2], "no-debugger": [2], - "quote-props": [2, "as-needed"], + "quote-props": [2, "consistent-as-needed"], "no-iterator": [2], "no-new-func": [2], "key-spacing": [2, { "align": "value" }], @@ -119,8 +119,11 @@ "semi": [2, "always"], "yoda": [2, "never"] }, + "ecmaFeatures": { + "modules": true + }, "env": { - "mocha": true, - "node": true + "node": true, + "es6": true } } diff --git a/package.json b/package.json index 5d4162b..3b1652d 100644 --- a/package.json +++ b/package.json @@ -17,16 +17,13 @@ "url": "https://github.com/postcss/postcss-simple-vars.git" }, "dependencies": { - "postcss": "^5.0.10" + "postcss": "^5.0.13" }, "devDependencies": { - "gulp-eslint": "1.0.0", - "gulp-mocha": "2.1.3", - "mocha": "2.3.3", - "chai": "3.4.0", - "gulp": "3.9.0" + "eslint": "1.10.3", + "ava": "0.8.0" }, "scripts": { - "test": "gulp" + "test": "ava && eslint *.js" } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..8880a96 --- /dev/null +++ b/test.js @@ -0,0 +1,131 @@ +import postcss from 'postcss'; +import test from 'ava'; + +import vars from './'; + +function run(t, input, output, opts = { }) { + t.same(postcss(vars(opts)).process(input).css, output); +} + +test('replaces variables in values', t => { + run(t, '$size: 10px;\na{ width: $size; height: $size }', + 'a{ width: 10px; height: 10px }'); +}); + +test('replaces vars in property names', t => { + run(t, '$prop: width; a{ $(prop): 1px }', 'a{ width: 1px }'); +}); + +test('replaces vars inside property names', t => { + run(t, '$dir: top; a{ margin-$(dir): 1px }', 'a{ margin-top: 1px }'); +}); + +test('allows dashes and digits in variable name', t => { + run(t, '$a-b_10: 1;\na{ one: $a-b_10 a$(a-b_10) }', 'a{ one: 1 a1 }'); +}); + +test('needs space before variable', t => { + run(t, '$size: 10px; a { width: one$size }', 'a { width: one$size }'); +}); + +test('does not remove first symbol', t => { + run(t, 'a{ a: 1 $a }', 'a{ a: 1 1 }', { variables: { a: 1 } }); +}); + +test('allows to use in negative numbers', t => { + run(t, 'a{ a: -$a }', 'a{ a: -1 }', { variables: { a: 1 } }); +}); + +test('replaces multiple variables', t => { + run(t, 'a{ a: $a $a }', 'a{ a: 1 1 }', { variables: { a: 1 } }); +}); + +test('has second syntax for varibles', t => { + run(t, '$size: 10; a { width: $(size)px }', 'a { width: 10px }'); +}); + +test('replaces variables in selector', t => { + run(t, '$name: a; $name $(name)b { }', 'a ab { }'); +}); + +test('replaces variables in at-rule params', t => { + run(t, '$name: a; @at $name; @at $(name)b { }', '@at a; @at ab { }'); +}); + +test('parses at-rule without params', t => { + run(t, '@atrule{}', '@atrule{}'); +}); + +test('overrides variables', t => { + run(t, '$var: 1; a{ one: $var } b{ $var: 2; two: $var } c{ two: $var }', + 'a{ one: 1 } b{ two: 2 } c{ two: 2 }'); +}); + +test('throws an error on unknown variable', t => { + t.throws( () => { + run(t, 'a{ width: -$size }'); + }, 'postcss-simple-vars: :1:4: Undefined variable $size'); +}); + +test('allows to silent errors', t => { + run(t, 'a{ width: $size }', 'a{ width: $size }', { silent: true }); +}); + +test('gets variables from options', t => { + run(t, 'a{ width: $one }', 'a{ width: 1 }', { variables: { one: 1 } }); +}); + +test('works with any syntax in option', t => { + run(t, 'a{ width: $one }', 'a{ width: 1 }', { variables: { $one: 1 } }); +}); + +test('cans get variables only from option', t => { + run(t, '$one: 2; $two: 2; a{ one: $one $two }', + '$one: 2; $two: 2; a{ one: 1 $two }', + { only: { one: 1 } }); +}); + +test('works with false value', t => { + run(t, 'a{ zero: $zero }', 'a{ zero: 0 }', { variables: { zero: 0 } }); +}); + +test('allows to use var in other vars', t => { + run(t, '$one: 1; $two: $one 2; a{ value: $two }', 'a{ value: 1 2 }'); +}); + +test('set default values by function', t => { + let value; + let config = () => { + return { config: value }; + }; + + value = 1; + run(t, 'a{ width: $config }', 'a{ width: 1 }', { variables: config }); + + value = 2; + run(t, 'a{ width: $config }', 'a{ width: 2 }', { variables: config }); +}); + +test('has callback for unknown variable', t => { + let result = []; + let unknown = (node, name) => { + result.push([node.prop, name]); + }; + + run(t, 'a{width:$one}', 'a{width:$one}', { unknown: unknown }); + t.same(result, [['width', 'one']]); +}); + +test.cb('has callback for exporting variables', t => { + run(t, '$one: 1;', '', { + onVariables: (variables) => { + t.same(variables.one, '1'); + t.end(); + } + }); +}); + +test('overrides unknown variable', t => { + let unknown = () => 'unknown'; + run(t, 'a{width:$one}', 'a{width:unknown}', { unknown: unknown }); +}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index a425596..0000000 --- a/test/test.js +++ /dev/null @@ -1,135 +0,0 @@ -var postcss = require('postcss'); -var expect = require('chai').expect; - -var vars = require('../'); - -var test = function (input, output, opts) { - expect(postcss(vars(opts)).process(input).css).to.eql(output); -}; - -describe('postcss-simple-vars', function () { - - it('replaces variables in values', function () { - test('$size: 10px;\na{ width: $size; height: $size }', - 'a{ width: 10px; height: 10px }'); - }); - - it('replaces vars in property names', function () { - test('$prop: width; a{ $(prop): 1px }', 'a{ width: 1px }'); - }); - - it('replaces vars inside property names', function () { - test('$dir: top; a{ margin-$(dir): 1px }', 'a{ margin-top: 1px }'); - }); - - it('allows dashes and digits in variable name', function () { - test('$a-b_10: 1;\na{ one: $a-b_10 a$(a-b_10) }', 'a{ one: 1 a1 }'); - }); - - it('needs space before variable', function () { - test('$size: 10px; a { width: one$size }', 'a { width: one$size }'); - }); - - it('does not remove first symbol', function () { - test('a{ a: 1 $a }', 'a{ a: 1 1 }', { variables: { a: 1 } }); - }); - - it('allows to use in negative numbers', function () { - test('a{ a: -$a }', 'a{ a: -1 }', { variables: { a: 1 } }); - }); - - it('replaces multiple variables', function () { - test('a{ a: $a $a }', 'a{ a: 1 1 }', { variables: { a: 1 } }); - }); - - it('has second syntax for varibles', function () { - test('$size: 10; a { width: $(size)px }', 'a { width: 10px }'); - }); - - it('replaces variables in selector', function () { - test('$name: a; $name $(name)b { }', 'a ab { }'); - }); - - it('replaces variables in at-rule params', function () { - test('$name: a; @at $name; @at $(name)b { }', '@at a; @at ab { }'); - }); - - it('parses at-rule without params', function () { - test('@atrule{}', '@atrule{}'); - }); - - it('overrides variables', function () { - test('$var: 1; a{ one: $var } b{ $var: 2; two: $var } c{ two: $var }', - 'a{ one: 1 } b{ two: 2 } c{ two: 2 }'); - }); - - it('throws an error on unknown variable', function () { - expect(function () { - test('a{ width: -$size }'); - }).to.throw(':1:4: Undefined variable $size'); - }); - - it('allows to silent errors', function () { - test('a{ width: $size }', 'a{ width: $size }', { silent: true }); - }); - - it('gets variables from options', function () { - test('a{ width: $one }', 'a{ width: 1 }', { variables: { one: 1 } }); - }); - - it('works with any syntax in option', function () { - test('a{ width: $one }', 'a{ width: 1 }', { variables: { $one: 1 } }); - }); - - it('cans get variables only from option', function () { - test('$one: 2; $two: 2; a{ one: $one $two }', - '$one: 2; $two: 2; a{ one: 1 $two }', - { only: { one: 1 } }); - }); - - it('works with false value', function () { - test('a{ zero: $zero }', 'a{ zero: 0 }', { variables: { zero: 0 } }); - }); - - it('allows to use var in other vars', function () { - test('$one: 1; $two: $one 2; a{ value: $two }', 'a{ value: 1 2 }'); - }); - - it('set default values by function', function () { - var value; - var config = function () { - return { config: value }; - }; - - value = 1; - test('a{ width: $config }', 'a{ width: 1 }', { variables: config }); - - value = 2; - test('a{ width: $config }', 'a{ width: 2 }', { variables: config }); - }); - - it('has callback for unknown variable', function () { - var result = []; - var unknown = function (node, name) { - result.push([node.prop, name]); - }; - - test('a{width:$one}', 'a{width:$one}', { unknown: unknown }); - expect(result).to.eql([['width', 'one']]); - }); - - it('has callback for exporting variables', function (done) { - test('$one: 1;', '', { onVariables: function (variables) { - expect(variables.one).to.eql('1'); - done(); - } }); - }); - - it('overrides unknown variable', function () { - var unknown = function () { - return 'unknown'; - }; - test('a{width:$one}', 'a{width:unknown}', { unknown: unknown }); - }); - -});