990ad6f16b91bfd0a29861ae0541ce7585e1f914
[platform/framework/web/crosswalk-tizen.git] /
1 /*jshint node:true*/
2 /*global describe:false, it:false*/
3 "use strict";
4
5 var esprima = require('esprima');
6 var expect = require('chai').expect;
7 var _glob = require('glob');
8 var _path = require('path');
9
10 var esformatter = require('../lib/esformatter');
11
12 var _helpers = require('./helpers');
13 var readOut = _helpers.readOut;
14 var readIn = _helpers.readIn;
15 var readConfig = _helpers.readConfig;
16
17
18 // ---
19
20
21 describe('esformatter.format()', function() {
22
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
25
26   describe('default options', function() {
27
28     var pattern = _path.join(_helpers.COMPARE_FOLDER, 'default/*-in.js');
29     _glob.sync(pattern).forEach(function(fileName) {
30
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');
34
35       it(id, function() {
36         var input = readIn(id);
37         var compare = readOut(id);
38         var result = esformatter.format(input);
39
40         expect(result).to.equal(compare);
41
42         // result should be valid JS
43         expect(function() {
44           try {
45             esprima.parse(result);
46           } catch (e) {
47             throw new Error('esformatter.format() result produced a non-valid output.\n' + e);
48           }
49         }).to.not.Throw();
50
51         // make sure formatting can be applied multiple times
52         // (idempotent)
53         expect(esformatter.format(result)).to.equal(compare);
54       });
55
56     });
57
58   });
59
60
61   describe('custom options', function() {
62
63     var pattern = _path.join(_helpers.COMPARE_FOLDER, 'custom/*-in.js');
64     _glob.sync(pattern).forEach(function(fileName) {
65
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');
69
70       it(id, function() {
71         var input = readIn(id);
72         var options = readConfig(id);
73         var compare = readOut(id);
74         var result = esformatter.format(input, options);
75
76         expect(result).to.equal(compare);
77
78         // result should be valid JS
79         expect(function() {
80           try {
81             esprima.parse(result);
82           } catch (e) {
83             throw new Error('esformatter.format() result produced a non-valid output.\n' + e);
84           }
85         }).to.not.Throw();
86
87         // make sure formatting can be applied multiple times
88         // (idempotent)
89         expect(esformatter.format(result, options)).to.equal(compare);
90       });
91
92     });
93
94   });
95
96
97   describe('jquery preset', function() {
98
99     var pattern = _path.join(_helpers.COMPARE_FOLDER, 'jquery/*-in.js');
100     _glob.sync(pattern).forEach(function(fileName) {
101
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');
105
106       it(id, function() {
107         var input = readIn(id);
108         var compare = readOut(id);
109         var result = esformatter.format(input, {preset:'jquery'});
110
111         expect(result).to.equal(compare);
112
113         // result should be valid JS
114         expect(function() {
115           try {
116             esprima.parse(result);
117           } catch (e) {
118             throw new Error('esformatter.format() result produced a non-valid output.\n' + e);
119           }
120         }).to.not.Throw();
121
122         // make sure formatting can be applied multiple times
123         // (idempotent)
124         expect(esformatter.format(result, {preset:'jquery'})).to.equal(compare);
125       });
126
127     });
128
129   });
130
131   describe('shebang', function() {
132
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}');
136     });
137
138     it('should throw if not allowed', function() {
139       expect(function() {
140         esformatter.format('#!/usr/bin/env node\nif(foo){bar()}', {
141           esformatter: {
142             allowShebang: false
143           }
144         });
145       }).to.Throw();
146     });
147
148   });
149
150 });
151