|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* @license |
| 3 |
* Copyright (c) 2016 IBM Corporation and others. |
| 4 |
* All rights reserved. This program and the accompanying materials are made |
| 5 |
* available under the terms of the Eclipse Public License v1.0 |
| 6 |
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution |
| 7 |
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). |
| 8 |
* |
| 9 |
* Contributors: |
| 10 |
* IBM Corporation - initial API and implementation |
| 11 |
******************************************************************************/ |
| 12 |
/*eslint-env amd, mocha*/ |
| 13 |
/* eslint-disable missing-nls */ |
| 14 |
define([ |
| 15 |
'javascript/quickFixes', |
| 16 |
'javascript/validator', |
| 17 |
'chai/chai', |
| 18 |
'orion/Deferred', |
| 19 |
'javascript/astManager', |
| 20 |
'javascript/cuProvider', |
| 21 |
'javascript/commands/renameCommand', |
| 22 |
'javascript/commands/generateDocCommand', |
| 23 |
'orion/serviceregistry', |
| 24 |
'javascript/javascriptProject', |
| 25 |
'mocha/mocha', //must stay at the end, not a module |
| 26 |
], function(QuickFixes, Validator, chai, Deferred, ASTManager, CUProvider, RenameCommand, GenerateDocCommand, mServiceRegistry, JSProject) { |
| 27 |
var assert = chai.assert; |
| 28 |
|
| 29 |
return function(worker) { |
| 30 |
describe('ES6 Quick Fix Tests',function() { |
| 31 |
this.timeout(10000); |
| 32 |
before('Reset Tern Server', function(done) { |
| 33 |
worker.start(done, {options:{ecmaVersion:6, sourceType:"module"}}); |
| 34 |
}); |
| 35 |
|
| 36 |
/** |
| 37 |
* @description Sets up the test |
| 38 |
* |
| 39 |
* Supported options include: |
| 40 |
* - `buffer` - the source to process |
| 41 |
* - `contentType` - the content type to load, defaults to application/javascript if not specified |
| 42 |
* - `callback` - the 'done' param from the tests, its required for all worker-based tests |
| 43 |
* - `rule` - the rule object. {@link #createTestRule(...)} |
| 44 |
* - `expected` - the array of expected fixes |
| 45 |
* - `fixid` - the id of the rule to use if not the same as the one created by default - allows testing multifixes |
| 46 |
* @param {Object} options {buffer, contentType} |
| 47 |
* @returns {Object} The object with the initialized values |
| 48 |
*/ |
| 49 |
function setup(options) { |
| 50 |
var buffer = options.buffer; |
| 51 |
var contentType = options.contentType ? options.contentType : 'application/javascript'; |
| 52 |
var astManager = new ASTManager.ASTManager(); |
| 53 |
var serviceRegistry = new mServiceRegistry.ServiceRegistry(); |
| 54 |
var validator = new Validator(worker); |
| 55 |
var state = Object.create(null); |
| 56 |
var loc = contentType === 'text/html' ? 'es6quickfix_test_script.html' : 'es6quickfix_test_script.js'; |
| 57 |
assert(options.callback, "You must provide a callback for a worker-based test"); |
| 58 |
state.callback = options.callback; |
| 59 |
worker.setTestState(state); |
| 60 |
var rule = options.rule; |
| 61 |
validator._enableOnly(rule.id, rule.severity, rule.opts); |
| 62 |
var renameCommand = new RenameCommand.RenameCommand(worker, {setSearchLocation: function(){}}); |
| 63 |
var generateDocCommand = new GenerateDocCommand.GenerateDocCommand(astManager, CUProvider); |
| 64 |
var fixComputer = new QuickFixes.JavaScriptQuickfixes(astManager, renameCommand, generateDocCommand, new JSProject(serviceRegistry), worker); |
| 65 |
var editorContext = { |
| 66 |
/*override*/ |
| 67 |
getText: function(start, end) { |
| 68 |
if(typeof start === 'undefined' && typeof end === 'undefined') { |
| 69 |
return new Deferred().resolve(buffer); |
| 70 |
} |
| 71 |
return new Deferred().resolve(buffer.slice(start, end)); |
| 72 |
}, |
| 73 |
|
| 74 |
setText: function(text, start, end) { |
| 75 |
return new Deferred().resolve(assertFixes(text, start, end, options.expected)); |
| 76 |
}, |
| 77 |
|
| 78 |
getSelections: function(){ |
| 79 |
return new Deferred().resolve([]); |
| 80 |
}, |
| 81 |
|
| 82 |
getFileMetadata:function() { |
| 83 |
var o = Object.create(null); |
| 84 |
o.contentType = Object.create(null); |
| 85 |
o.contentType.id = contentType; |
| 86 |
o.location = loc; |
| 87 |
return new Deferred().resolve(o); |
| 88 |
}, |
| 89 |
exitLinkedMode: function() { |
| 90 |
return new Deferred().resolve(); |
| 91 |
}, |
| 92 |
enterLinkedMode: function(linkModel) { |
| 93 |
return new Deferred().resolve(assertLinkedModel(linkModel, options.expected)); |
| 94 |
}, |
| 95 |
setCaretOffset: function(caretOffset) { |
| 96 |
return new Deferred().resolve(caretOffset); |
| 97 |
}, |
| 98 |
openEditor: function(file, opts){ |
| 99 |
return new Deferred().resolve(assertFixes({file: file, start: opts.start, end: opts.end}, null, null, options.expected)); |
| 100 |
} |
| 101 |
}; |
| 102 |
return { |
| 103 |
validator: validator, |
| 104 |
fixComputer: fixComputer, |
| 105 |
editorContext: editorContext, |
| 106 |
contentType: contentType, |
| 107 |
loc: loc |
| 108 |
}; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @callback from Mocha after each test run |
| 113 |
*/ |
| 114 |
afterEach(function() { |
| 115 |
CUProvider.onModelChanging({file: {location: 'es6quickfix_test_script.js'}}); |
| 116 |
CUProvider.onModelChanging({file: {location: 'es6quickfix_test_script.html'}}); |
| 117 |
}); |
| 118 |
|
| 119 |
/** |
| 120 |
* @description Checks the state of the linked models |
| 121 |
* @param {Object} linkedModel The linked model from the platform |
| 122 |
* @param {Object} expected The expected linked model from the test |
| 123 |
* @since 11.0 |
| 124 |
*/ |
| 125 |
function assertLinkedModel(linkedModel, expected) { |
| 126 |
try { |
| 127 |
assert(expected, "There must be an expected linkedModel"); |
| 128 |
assert(expected.groups, "There must be a groups node in the expected linked model"); |
| 129 |
assert(Array.isArray(expected.groups), "Groups must be an array in the expected linked model"); |
| 130 |
assert(linkedModel, "There must be a linkedModel"); |
| 131 |
assert(linkedModel.groups, "There must be a groups node in the linked model"); |
| 132 |
assert(Array.isArray(linkedModel.groups), "Groups must be an array in the linked model"); |
| 133 |
assert.equal(linkedModel.groups.length, expected.groups.length, "The linked mode groups should be the same length"); |
| 134 |
expected.groups.forEach(function(group, index) { |
| 135 |
//groups have data and positions: [{offset, length}] |
| 136 |
var g = linkedModel.groups[index]; |
| 137 |
assert.equal(typeof g.data, typeof group.data, "The type of the data of the groups is not the same"); |
| 138 |
assert(Array.isArray(g.positions), "There should be a positions array"); |
| 139 |
assert(Array.isArray(group.positions), "There must be an expected positions array"); |
| 140 |
assert.equal(g.positions.length, group.positions.length, "The position arrays should be the same size"); |
| 141 |
group.positions.forEach(function(pos, index2) { |
| 142 |
var p = g.positions[index2]; |
| 143 |
assert(p, "There should be a position"); |
| 144 |
assert.equal(p.offset, pos.offset, "The position offsets do not match"); |
| 145 |
assert.equal(p.length, pos.length, "The position lengths do not match"); |
| 146 |
}); |
| 147 |
}); |
| 148 |
} |
| 149 |
catch(err) { |
| 150 |
worker.getTestState().callback(err); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @description Runs the validator on the given options and computes fixes for those problems |
| 156 |
* @param {Object} options {buffer, contentType, rule} |
| 157 |
* @returns {orion.Promise} The validation promise |
| 158 |
*/ |
| 159 |
function getFixes(options) { |
| 160 |
var obj = setup(options); |
| 161 |
return obj.validator.computeProblems(obj.editorContext, {contentType: obj.contentType, rule: options.rule}).then( |
| 162 |
function(problems) { |
| 163 |
try { |
| 164 |
var pbs = problems.problems; |
| 165 |
var annot = pbs[0]; |
| 166 |
if(options.pid) { |
| 167 |
for(var i = 0; i < pbs.length; i++) { |
| 168 |
if(pbs[i].id === options.pid) { |
| 169 |
annot = pbs[i]; |
| 170 |
break; |
| 171 |
} |
| 172 |
} |
| 173 |
assert(i !== pbs.length, "Did not find any problems for the expected id: "+ options.pid); |
| 174 |
} else { |
| 175 |
assert(pbs, "There should always be problems"); |
| 176 |
// Some quick fixes may provide multiple expected text edits per problem |
| 177 |
if (!Array.isArray(options.expected)){ |
| 178 |
assert.equal(pbs.length, 1, 'Expected only one problem per test'); |
| 179 |
} |
| 180 |
assert(annot.id, "No problem id is reported"); |
| 181 |
assert(annot.id.indexOf(options.rule.id) === 0, "The problem id should start with the enabled rule id"); |
| 182 |
} |
| 183 |
annot.title = annot.description; |
| 184 |
if(options.fixid) { |
| 185 |
annot.fixid = options.fixid; |
| 186 |
} |
| 187 |
var annotations; |
| 188 |
if (Array.isArray(options.expected)){ |
| 189 |
annotations = pbs; |
| 190 |
for (i=0; i<annotations.length; i++) { |
| 191 |
annotations[i].title = annotations[i].description; |
| 192 |
} |
| 193 |
} |
| 194 |
return obj.fixComputer.execute(obj.editorContext, {annotation: annot, annotations: annotations, input: obj.loc}).then(function() { |
| 195 |
worker.getTestState().callback(); |
| 196 |
}, |
| 197 |
function(err) { |
| 198 |
if(err instanceof Error) { |
| 199 |
worker.getTestState().callback(err); |
| 200 |
} else if(typeof err.Message === 'string') { |
| 201 |
worker.getTestState().callback(err.Message); |
| 202 |
} else { |
| 203 |
worker.getTestState().callback("Test rejected with unknown error"); |
| 204 |
} |
| 205 |
}); |
| 206 |
} |
| 207 |
catch(err) { |
| 208 |
worker.getTestState().callback(err); |
| 209 |
} |
| 210 |
}, |
| 211 |
function (error) { |
| 212 |
worker.getTestState().callback(error); |
| 213 |
}); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @description Compares the computed fixes set against the expected ones |
| 218 |
* @param {Array.<orion.Fix>} computed The computed set of fixes |
| 219 |
* @param {Array.<Object>} expected The expected set of fixes |
| 220 |
*/ |
| 221 |
function assertFixes(computed, start, end, expected) { |
| 222 |
try { |
| 223 |
assert(computed !== null && typeof computed !== 'undefined', 'There should be fixes'); |
| 224 |
if (Array.isArray(expected)){ |
| 225 |
assert(Array.isArray(computed.text), "Expected multiple quick fix text edits"); |
| 226 |
assert(Array.isArray(computed.selection), "Expected multiple quick fix selections"); |
| 227 |
assert.equal(computed.text.length, expected.length, "Wrong number of quick fix text edits"); |
| 228 |
assert.equal(computed.selection.length, expected.length, "Wrong number of quick fix selections"); |
| 229 |
for (var i=0; i<expected.length; i++) { |
| 230 |
assert(computed.text[i] === expected[i].value, 'The fix: \"'+computed.text[i]+'\" does not match the expected fix of: \"'+expected[i].value + '\"'); |
| 231 |
assert.equal(computed.selection[i].start, expected[i].start, 'The fix starts do not match'); |
| 232 |
assert.equal(computed.selection[i].end, expected[i].end, 'The fix ends do not match'); |
| 233 |
} |
| 234 |
} else if (typeof computed === 'object' && computed.file && expected.file){ |
| 235 |
assert.equal(computed.file, expected.file, 'Navigation fix found but to wrong file.\nExpected: ' + expected.file + ' (' + expected.start + ',' + expected.end + ')\nActual: ' + computed.file + ' (' + computed.start + ',' + computed.end + ')'); |
| 236 |
assert.equal(computed.start, expected.start, 'Navigation fix found but to wrong start.\nExpected: ' + expected.file + ' (' + expected.start + ',' + expected.end + ')\nActual: ' + computed.file + ' (' + computed.start + ',' + computed.end + ')'); |
| 237 |
assert.equal(computed.end, expected.end, 'Navigation fix found but to wrong end.\nExpected: ' + expected.file + ' (' + expected.start + ',' + expected.end + ')\nActual: ' + computed.file + ' (' + computed.start + ',' + computed.end + ')'); |
| 238 |
} else if (typeof computed === 'object' && Array.isArray(computed.text)){ |
| 239 |
assert.equal(computed.text.length, 1, 'Was expecting one quick fix text edit'); |
| 240 |
assert.equal(computed.selection.length, 1, 'Was expected one quick fix selection range'); |
| 241 |
assert(computed.text[0].indexOf(expected.value) > -1, 'The fix: \"'+computed.text[0]+'\"" does not match the expected fix of: '+expected.value); |
| 242 |
assert.equal(computed.selection[0].start, expected.start, 'The fix starts do not match'); |
| 243 |
assert.equal(computed.selection[0].end, expected.end, 'The fix ends do not match'); |
| 244 |
} else { |
| 245 |
assert(computed.indexOf(expected.value) > -1, 'The fix: '+computed+' does not match the expected fix of: '+expected.value); |
| 246 |
assert.equal(start, expected.start, 'The fix starts do not match'); |
| 247 |
assert.equal(end, expected.end, 'The fix ends do not match'); |
| 248 |
} |
| 249 |
} |
| 250 |
catch(err) { |
| 251 |
worker.getTestState().callback(err); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* @description Creates a test rule object for the test set up |
| 257 |
* @param {String} id The id of the rule used to update the preferences in javascript/validator#updated |
| 258 |
* @param {Number} severity The severity of the problem or null (which defaults to '2') |
| 259 |
* @param {String} opts The optional args for a rule. For example no-missing-doc has 'decl' and 'expr' as optional args |
| 260 |
* @returns {Object} Returns a new rule object for testing with |
| 261 |
*/ |
| 262 |
function createTestRule(id, severity, opts) { |
| 263 |
var rule = Object.create(null); |
| 264 |
rule.id = id; |
| 265 |
rule.severity = severity ? severity : 2; |
| 266 |
rule.opts = opts; |
| 267 |
return rule; |
| 268 |
} |
| 269 |
//MISSING-DOC |
| 270 |
describe("missing-doc", function() { |
| 271 |
it("export named declaration", function(done) { |
| 272 |
var rule = createTestRule("missing-doc"); |
| 273 |
var expected = { |
| 274 |
value: "/**\n"+ |
| 275 |
" * @name myFunc\n"+ |
| 276 |
" * @description description\n"+ |
| 277 |
" * @returns returns\n"+ |
| 278 |
" */\n", |
| 279 |
start: 21, |
| 280 |
end: 21 |
| 281 |
}; |
| 282 |
return getFixes({ |
| 283 |
buffer: "var MYCONSTANT = \"\";\nexport function myFunc() { return MYCONSTANT; }", |
| 284 |
rule: rule, |
| 285 |
expected: expected, |
| 286 |
callback: done |
| 287 |
}); |
| 288 |
}); |
| 289 |
}); |
| 290 |
}); |
| 291 |
}; |
| 292 |
}); |