Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Clone in Desktop Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

190 lines (152 sloc) 7.75 KB
var Checker = require('../../../lib/checker');
var expect = require('chai').expect;
describe('rules/disallow-spaces-inside-object-brackets', function() {
var checker;
beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
});
describe('invalid options', function() {
it('should throw when given an number', function() {
expect(function() {
checker.configure({ disallowSpacesInsideObjectBrackets: 2 });
}).to.throw();
});
});
describe('true value', function() {
beforeEach(function() {
checker.configure({ disallowSpacesInsideObjectBrackets: true });
});
it('should report illegal space after opening brace, with true value', function() {
expect(checker.checkString('var x = { a: 1};'))
.to.have.one.validation.error.from('disallowSpacesInsideObjectBrackets');
});
it('should report illegal space before closing brace, with true value', function() {
expect(checker.checkString('var x = {a: 1 };'))
.to.have.one.validation.error.from('disallowSpacesInsideObjectBrackets');
});
it('should report illegal space in both cases, with true value', function() {
expect(checker.checkString('var x = { a: 1 };')).to.have.error.count.equal(2);
});
it('should report illegal space for nested braces too, with true value', function() {
expect(checker.checkString('var x = { test: { a: 1 } };')).to.have.error.count.equal(4);
});
it('should not report with no spaces, with true value', function() {
expect(checker.checkString('var x = {a: 1};')).to.have.no.errors();
});
});
describe('"all"', function() {
beforeEach(function() {
checker.configure({ disallowSpacesInsideObjectBrackets: 'all' });
});
it('should report illegal space after opening brace, with "all" value', function() {
expect(checker.checkString('var x = { a: 1};'))
.to.have.one.validation.error.from('disallowSpacesInsideObjectBrackets');
});
it('should report illegal space before closing brace, with "all" value', function() {
expect(checker.checkString('var x = {a: 1 };'))
.to.have.one.validation.error.from('disallowSpacesInsideObjectBrackets');
});
it('should report illegal space in both cases, with "all" value', function() {
expect(checker.checkString('var x = { a: 1 };')).to.have.error.count.equal(2);
});
it('should report illegal space for nested braces too, with "all" value', function() {
expect(checker.checkString('var x = { test: { a: 1 } };')).to.have.error.count.equal(4);
});
it('should not report with no spaces, with "all" value', function() {
expect(checker.checkString('var x = {a: 1};')).to.have.no.errors();
});
it('should report illegal spaces for destructive assignment', function() {
expect(checker.checkString('let { x } = {x: 1};')).to.have.error.count.equal(2);
});
describe.skip('import (#1524)', function() {
beforeEach(function() {
checker.configure({ disallowSpacesInsideObjectBrackets: true, esnext: true });
});
it('should not report with absent brackets', function() {
expect(checker.checkString('import myModule from "test";')).to.have.no.errors();
expect(checker.checkString('import "test";')).to.have.no.errors();
});
it('should report for import statements', function() {
expect(checker.checkString('import { myMember } from "test";')).to.have.error.count.equal(2);
expect(checker.checkString('import { myMember } from "test";')).to.have.error.count.equal(2);
expect(checker.checkString('import { foo, bar } from "test";')).to.have.error.count.equal(2);
expect(checker.checkString('import MyModule, { foo, bar } ' +
' from "test";')).to.have.error.count.equal(2);
expect(checker.checkString('import { a as b } from "test";')).to.have.error.count.equal(2);
});
});
});
describe('"nested"', function() {
beforeEach(function() {
checker.configure({ disallowSpacesInsideObjectBrackets: 'nested' });
});
it('should report illegal space after opening brace for nested object', function() {
expect(checker.checkString('var x = {1: { 1 : 2}};'))
.to.have.one.validation.error.from('disallowSpacesInsideObjectBrackets');
});
it('should report illegal space before closing brace for nested object', function() {
expect(checker.checkString('var x = {1: {1 : 2 }};'))
.to.have.one.validation.error.from('disallowSpacesInsideObjectBrackets');
});
it('should report illegal space in both cases for nested object', function() {
expect(checker.checkString('var x = {1: { 1 : 2 }};')).to.have.error.count.equal(2);
});
it('should report illegal space in both cases for multiple nested object', function() {
expect(checker.checkString('var x = {1: { 1 : 2 }, 2: { 3 : 4 }};')).to.have.error.count.equal(4);
});
it('should not report illegal space in both cases for nested object', function() {
expect(checker.checkString('var x = {1: {1 : 2}};')).to.have.no.errors();
});
});
describe('exceptions', function() {
it('should act like "true" when allExcept is false', function() {
checker.configure({
disallowSpacesInsideObjectBrackets: {
allExcept: false
}
});
expect(checker.checkString('var x = { a: 1};'))
.to.have.one.validation.error.from('disallowSpacesInsideObjectBrackets');
});
it('should report for function', function() {
checker.configure({
disallowSpacesInsideObjectBrackets: {
allExcept: ['}']
}
});
expect(checker.checkString('var x = {a: []};')).to.have.no.errors();
expect(checker.checkString('var x = { a: function() {} };'))
.to.have.one.validation.error.from('disallowSpacesInsideObjectBrackets');
});
it('should report for array literal', function() {
checker.configure({
disallowSpacesInsideObjectBrackets: {
allExcept: [']']
}
});
expect(checker.checkString('var x = {a: {b: 2} };'))
.to.have.one.validation.error.from('disallowSpacesInsideObjectBrackets');
expect(checker.checkString('var x = {a: [] };')).to.have.no.errors();
});
it('should report for parentheses', function() {
checker.configure({
disallowSpacesInsideObjectBrackets: {
allExcept: [')']
}
});
expect(checker.checkString('var x = {a: {b: 2} };'))
.to.have.one.validation.error.from('disallowSpacesInsideObjectBrackets');
expect(checker.checkString('var x = {a: (1) };')).to.have.no.errors();
});
it('should report for object literal', function() {
checker.configure({
disallowSpacesInsideObjectBrackets: {
allExcept: ['}']
}
});
expect(checker.checkString('var x = {a: {b: 2} };')).to.have.no.errors();
expect(checker.checkString('var x = { a: { b: 1} };')).to.have.error.count.equal(2);
});
});
});
Jump to Line
Something went wrong with that request. Please try again.