2 /*global describe:false, it:false*/
5 var esprima = require('esprima');
6 var expect = require('chai').expect;
7 var _glob = require('glob');
8 var _path = require('path');
10 var esformatter = require('../lib/esformatter');
12 var _helpers = require('./helpers');
13 var readOut = _helpers.readOut;
14 var readIn = _helpers.readIn;
15 var readConfig = _helpers.readConfig;
21 describe('esformatter.format()', function() {
23 // we generate the specs dynamically based on files inside the compare
24 // folder since it will be easier to write the tests and do the comparison
26 describe('default options', function() {
28 var pattern = _path.join(_helpers.COMPARE_FOLDER, 'default/*-in.js');
29 _glob.sync(pattern).forEach(function(fileName) {
31 // we read files before the test to avoid affecting the test
32 // benchmark, I/O operations are expensive.
33 var id = fileName.replace(/.+(default\/.+)-in\.js/, '$1');
36 var input = readIn(id);
37 var compare = readOut(id);
38 var result = esformatter.format(input);
40 expect(result).to.equal(compare);
42 // result should be valid JS
45 esprima.parse(result);
47 throw new Error('esformatter.format() result produced a non-valid output.\n' + e);
51 // make sure formatting can be applied multiple times
53 expect(esformatter.format(result)).to.equal(compare);
61 describe('custom options', function() {
63 var pattern = _path.join(_helpers.COMPARE_FOLDER, 'custom/*-in.js');
64 _glob.sync(pattern).forEach(function(fileName) {
66 // we read files before the test to avoid affecting the test
67 // benchmark, I/O operations are expensive.
68 var id = fileName.replace(/.+(custom\/.+)-in\.js/, '$1');
71 var input = readIn(id);
72 var options = readConfig(id);
73 var compare = readOut(id);
74 var result = esformatter.format(input, options);
76 expect(result).to.equal(compare);
78 // result should be valid JS
81 esprima.parse(result);
83 throw new Error('esformatter.format() result produced a non-valid output.\n' + e);
87 // make sure formatting can be applied multiple times
89 expect(esformatter.format(result, options)).to.equal(compare);
97 describe('jquery preset', function() {
99 var pattern = _path.join(_helpers.COMPARE_FOLDER, 'jquery/*-in.js');
100 _glob.sync(pattern).forEach(function(fileName) {
102 // we read files before the test to avoid affecting the test
103 // benchmark, I/O operations are expensive.
104 var id = fileName.replace(/.+(jquery\/.+)-in\.js/, '$1');
107 var input = readIn(id);
108 var compare = readOut(id);
109 var result = esformatter.format(input, {preset:'jquery'});
111 expect(result).to.equal(compare);
113 // result should be valid JS
116 esprima.parse(result);
118 throw new Error('esformatter.format() result produced a non-valid output.\n' + e);
122 // make sure formatting can be applied multiple times
124 expect(esformatter.format(result, {preset:'jquery'})).to.equal(compare);
131 describe('shebang', function() {
133 it('should allow by default', function() {
134 var result = esformatter.format('#!/usr/bin/env node\nif(foo){bar()}');
135 expect(result).to.equal('#!/usr/bin/env node\nif (foo) {\n bar()\n}');
138 it('should throw if not allowed', function() {
140 esformatter.format('#!/usr/bin/env node\nif(foo){bar()}', {