1 | fs = require 'fs'
|
2 | should = require 'should'
|
3 | escodegen = require 'escodegen'
|
4 | esprima = require 'esprima'
|
5 | wrench = require 'wrench'
|
6 | coffee = require 'coffee-script'
|
7 | jscov = require('./coverage').require 'jscov'
|
8 | conditionals = require('./coverage').require 'conditionals'
|
9 |
|
10 | cases = [{
|
11 | name: 'One comment'
|
12 | input: '''
|
13 | var a = 1 + 2;
|
14 | // JSCOV: a > 5
|
15 | '''
|
16 | output: '''
|
17 | var __noop__cc__ = function() {};
|
18 | var a = 1 + 2;
|
19 | if (a > 5) { __noop__cc__() } else { __noop__cc__() };
|
20 | '''
|
21 | }, {
|
22 | name: 'No content'
|
23 | input: '''
|
24 | var a = 1 + 2;
|
25 | // JSCOV:
|
26 | '''
|
27 | output: '''
|
28 | var a = 1 + 2;
|
29 | // JSCOV:
|
30 | '''
|
31 | }, {
|
32 | name: 'No comments'
|
33 | input: '''
|
34 | var a = 1 + 2;
|
35 | '''
|
36 | output: '''
|
37 | var a = 1 + 2;
|
38 | '''
|
39 | }, {
|
40 | name: 'Two comments'
|
41 | input: '''
|
42 | var a = 1 + 2;
|
43 | // JSCOV: a > 5
|
44 | // JSCOV: a < 5
|
45 | '''
|
46 | output: '''
|
47 | var __noop__cc__ = function() {};
|
48 | var a = 1 + 2;
|
49 | if (a > 5) { __noop__cc__() } else { __noop__cc__() };
|
50 | if (a < 5) { __noop__cc__() } else { __noop__cc__() };
|
51 | '''
|
52 | }, {
|
53 | name: 'Coffee script'
|
54 | coffee: true
|
55 | input: '''
|
56 | a = 1 + 2
|
57 | # JSCOV: a > 5
|
58 | '''
|
59 | output: '''
|
60 | __noop__cc__ = (->)
|
61 | a = 1 + 2
|
62 | if (a > 5) then __noop__cc__() else __noop__cc__()
|
63 | '''
|
64 | }, {
|
65 | name: 'Coffee script param with JS code'
|
66 | coffee: true
|
67 | input: '''
|
68 | var a = 1 + 2;
|
69 | // JSCOV: a > 5
|
70 | '''
|
71 | output: '''
|
72 | var a = 1 + 2;
|
73 | // JSCOV: a > 5
|
74 | '''
|
75 | }, {
|
76 | name: 'Comment not on its own line'
|
77 | input: '''
|
78 | var a = 1 + 2;
|
79 | // JSCOV: a > 5
|
80 | var b = 2; // JSCOV: a < 5
|
81 | '''
|
82 | output: '''
|
83 | var __noop__cc__ = function() {};
|
84 | var a = 1 + 2;
|
85 | if (a > 5) { __noop__cc__() } else { __noop__cc__() };
|
86 | var b = 2; // JSCOV: a < 5
|
87 | '''
|
88 | }, {
|
89 | name: 'Minimal space'
|
90 | input: '//JSCOV:a>5'
|
91 | output: '''
|
92 | var __noop__cc__ = function() {};
|
93 | if (a>5) { __noop__cc__() } else { __noop__cc__() };
|
94 | '''
|
95 | }, {
|
96 | name: 'More space'
|
97 | input: '''
|
98 | // JSCOV: a > 5
|
99 | '''
|
100 | output: '''
|
101 | var __noop__cc__ = function() {};
|
102 | if (a > 5) { __noop__cc__() } else { __noop__cc__() };
|
103 | '''
|
104 | }, {
|
105 | name: 'Separating colon and identifier'
|
106 | input: '''
|
107 | // JSCOV : a > 5
|
108 | '''
|
109 | output: '''
|
110 | // JSCOV : a > 5
|
111 | '''
|
112 | }]
|
113 |
|
114 |
|
115 |
|
116 | cases.forEach (data) ->
|
117 | it "should transform #{data.name} properly", (done) ->
|
118 | if data.coffee
|
119 | output = conditionals.expand(data.input, { lang: 'coffee' })
|
120 | else
|
121 | output = conditionals.expand(data.input)
|
122 | output.should.eql data.output
|
123 | done()
|
124 |
|
125 |
|
126 |
|
127 | it "should work on all testfiles", (done) ->
|
128 | jscov.rewriteFolder 'spec/scaffolding/scaffold', 'spec/.output/cc', { conditionals: true }, (err) ->
|
129 | should.not.exist err
|
130 | done()
|
131 |
|
132 |
|
133 |
|
134 | it "should be possible to enable conditional comments", (done) ->
|
135 | jscov.rewriteFolder 'spec/scaffolding/conditionals', 'spec/.output/cc1', { conditionals: true }, (err) ->
|
136 | should.not.exist err
|
137 |
|
138 | expecting = {
|
139 | 'cond-coffee.coffee': false
|
140 | 'cond.js': false
|
141 | 'nocond.js': true
|
142 | 'nocond-coffee.coffee': true
|
143 | }
|
144 |
|
145 | Object.keys(expecting).forEach (file) ->
|
146 | input = fs.readFileSync("spec/scaffolding/conditionals/#{file}", 'utf8')
|
147 | input = coffee.compile(input) if file.match(/\.coffee$/)
|
148 | input = jscov.rewriteSource(input, file)
|
149 | output = fs.readFileSync("spec/.output/cc1/#{file.replace(/\.coffee$/, '.js')}", 'utf8')
|
150 | (input == output).should.eql expecting[file]
|
151 |
|
152 | done()
|
153 |
|
154 |
|
155 |
|
156 | it "should be possible to disable conditional comments", (done) ->
|
157 | jscov.rewriteFolder 'spec/scaffolding/conditionals', 'spec/.output/cc2', { conditionals: false }, (err) ->
|
158 | should.not.exist err
|
159 |
|
160 | expecting = {
|
161 | 'cond-coffee.coffee': true
|
162 | 'cond.js': true
|
163 | 'nocond.js': true
|
164 | 'nocond-coffee.coffee': true
|
165 | }
|
166 |
|
167 | Object.keys(expecting).forEach (file) ->
|
168 | input = fs.readFileSync("spec/scaffolding/conditionals/#{file}", 'utf8')
|
169 | input = coffee.compile(input) if file.match(/\.coffee$/)
|
170 | input = jscov.rewriteSource(input, file)
|
171 | output = fs.readFileSync("spec/.output/cc2/#{file.replace(/\.coffee$/, '.js')}", 'utf8')
|
172 | (input == output).should.eql expecting[file]
|
173 |
|
174 | done() |
\ | No newline at end of file |