summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorMike Rennie2013-11-04 13:28:32 (EST)
committer Gerrit Code Review @ Eclipse.org2013-11-05 17:51:51 (EST)
commit7c62901ea20d8d6d0906743bc99508d512ae5d4f (patch)
tree41c5519853000cedc57baa8cf05a94ba36661dfd
parente543c98277c6d53a1e91604850cc67a8d1268924 (diff)
downloadorg.eclipse.orion.client-7c62901ea20d8d6d0906743bc99508d512ae5d4f.zip
org.eclipse.orion.client-7c62901ea20d8d6d0906743bc99508d512ae5d4f.tar.gz
org.eclipse.orion.client-7c62901ea20d8d6d0906743bc99508d512ae5d4f.tar.bz2
Bug 419856 - Document ASTManager and tests (was: ASTManager should cacherefs/changes/42/18042/3
ASTs by content type) Change-Id: I92862d6fbd27564bfa7e27b04d10218d7b2a28a2 Signed-off-by: Mike Rennie <Michael_Rennie@ca.ibm.com>
-rw-r--r--bundles/org.eclipse.orion.client.ui/web/js-tests/ast/astManagerTests.html41
-rw-r--r--bundles/org.eclipse.orion.client.ui/web/js-tests/ast/astManagerTests.js (renamed from bundles/org.eclipse.orion.client.ui/web/js-tests/ast/testcase.js)60
-rw-r--r--bundles/org.eclipse.orion.client.ui/web/js-tests/ast/test.html25
-rw-r--r--bundles/org.eclipse.orion.client.ui/web/js-tests/jsTestSuite.js2
-rw-r--r--bundles/org.eclipse.orion.client.ui/web/orion/edit/ast.js70
5 files changed, 127 insertions, 71 deletions
diff --git a/bundles/org.eclipse.orion.client.ui/web/js-tests/ast/astManagerTests.html b/bundles/org.eclipse.orion.client.ui/web/js-tests/ast/astManagerTests.html
new file mode 100644
index 0000000..f5f534f
--- /dev/null
+++ b/bundles/org.eclipse.orion.client.ui/web/js-tests/ast/astManagerTests.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta http-equiv="Content-Language" content="en-us">
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <script src="../../requirejs/require.js"></script>
+ <script>
+ /*global require window*/
+ require({
+ baseUrl: '../../',
+ paths: {
+ text: 'requirejs/text',
+ i18n: 'requirejs/i18n',
+ domReady: 'requirejs/domReady'
+ }
+ });
+
+ window.onload = function() {
+ require(["orion/test","js-tests/ast/astManagerTests.js"], function(test, testcase) {
+ test.run(testcase);
+ });
+ };
+ </script>
+</head>
+<body>
+<h3>AST Manager Unit Tests</h3>
+<p>
+ The tests cover the orion.edit.ASTManager.
+
+ The following tests are defined:
+ <ul>
+ <li><strong>test_getAST</strong> - tests asking the manager for an AST of a test content type <code>text/foo</code></li>
+ <li><strong>test_getAST_options</strong> - tests asking the manager for options used to create an AST for a test
+ content type <code>text/foo</code></li>
+ <li><strong>test_AST_cache_is_used</strong> - tests that the AST re-asked for is the cached copy</li>
+ <li><strong>test_AST_cache_is_invalidated</strong> - tests that the cache of ASTs is properly cleaned on changed events
+ and input loads</li>
+ </ul>
+</p>
+</body>
+</html>
diff --git a/bundles/org.eclipse.orion.client.ui/web/js-tests/ast/testcase.js b/bundles/org.eclipse.orion.client.ui/web/js-tests/ast/astManagerTests.js
index 10928f5..fefa52b 100644
--- a/bundles/org.eclipse.orion.client.ui/web/js-tests/ast/testcase.js
+++ b/bundles/org.eclipse.orion.client.ui/web/js-tests/ast/astManagerTests.js
@@ -19,12 +19,16 @@ define([
'orion/objects',
'orion/serviceregistry'
], function(MockEditor, assert, ASTManager, Deferred, EventTarget, mInputManager, objects, mServiceRegistry) {
- var InputManager = mInputManager.InputManager,
- ServiceRegistry = mServiceRegistry.ServiceRegistry;
+ /**
+ * @name setup
+ * @description Sets the test up prior to running
+ * @function
+ * @public
+ */
function setup() {
- var serviceRegistry = new ServiceRegistry();
- var inputManager = new InputManager({
+ var serviceRegistry = new mServiceRegistry.ServiceRegistry();
+ var inputManager = new mInputManager.InputManager({
serviceRegistry: serviceRegistry,
editor: new MockEditor()
});
@@ -39,10 +43,18 @@ define([
}
var tests = {};
+ /**
+ * @name test_getAST
+ * @descripion tests asking the manager for an AST of a test content type <code>text/foo</code>
+ * @function
+ * @public
+ * @returns {orion.Deferred}
+ */
tests.test_getAST = function() {
var result = setup(),
serviceRegistry = result.serviceRegistry,
- inputManager = result.inputManager;
+ inputManager = result.inputManager,
+ astManager = result.astManager;
serviceRegistry.registerService("orion.core.astprovider", {
computeAST: function(context) {
@@ -51,14 +63,22 @@ define([
}, { contentType: ["text/foo"] });
inputManager.setContentType({ id: "text/foo" });
- return serviceRegistry.getService("orion.core.astmanager").getAST().then(function(ast) {
+ return astManager.getAST().then(function(ast) {
assert.equal(ast.ast, "this is the AST");
});
};
+ /**
+ * @name test_getAST_options
+ * @descripion tests asking the manager for options used to create an AST for a test content type <code>text/foo</code>
+ * @function
+ * @public
+ * @returns {orion.Deferred}
+ */
tests.test_getAST_options = function() {
var result = setup(),
serviceRegistry = result.serviceRegistry,
- inputManager = result.inputManager;
+ inputManager = result.inputManager,
+ astManager = result.astManager;
var promise = new Deferred();
serviceRegistry.registerService("orion.core.astprovider", {
@@ -71,13 +91,21 @@ define([
inputManager.setContentType({ id: "text/foo" });
inputManager.getEditor().setText("the text");
- serviceRegistry.getService("orion.core.astmanager").getAST({ foo: "bar" });
+ astManager.getAST({ foo: "bar" });
return promise;
};
+ /**
+ * @name test_AST_cache_is_used
+ * @descripion tests that the AST re-asked for is the cached copy
+ * @function
+ * @public
+ * @returns {orion.Deferred}
+ */
tests.test_AST_cache_is_used = function() {
var result = setup(),
serviceRegistry = result.serviceRegistry,
- inputManager = result.inputManager;
+ inputManager = result.inputManager,
+ astManager = result.astManager;
var i = 0;
serviceRegistry.registerService("orion.core.astprovider", {
@@ -88,7 +116,6 @@ define([
inputManager.setContentType({ id: "text/foo" });
- var astManager = serviceRegistry.getService("orion.core.astmanager");
return astManager.getAST().then(function(ast) {
assert.equal(ast, "AST 0");
}).then(function() {
@@ -97,10 +124,18 @@ define([
});
});
};
+ /**
+ * @name test_AST_cache_is_invalidated
+ * @description tests that the cache of ASTs is properly cleaned on changed events and input loads
+ * @function
+ * @public
+ * @returns {orion.Deferred}
+ */
tests.test_AST_cache_is_invalidated = function() {
var result = setup(),
serviceRegistry = result.serviceRegistry,
- inputManager = result.inputManager;
+ inputManager = result.inputManager,
+ astManager = result.astManager;
var i = 0;
serviceRegistry.registerService("orion.core.astprovider", {
@@ -111,7 +146,6 @@ define([
inputManager.setContentType({ id: "text/foo" });
- var astManager = serviceRegistry.getService("orion.core.astmanager");
return astManager.getAST().then(function(ast) {
assert.equal(ast, "AST 0");
// Ensure we do not receive the cached "AST 0" after a model change
@@ -123,4 +157,4 @@ define([
};
return tests;
-}); \ No newline at end of file
+});
diff --git a/bundles/org.eclipse.orion.client.ui/web/js-tests/ast/test.html b/bundles/org.eclipse.orion.client.ui/web/js-tests/ast/test.html
deleted file mode 100644
index aeec6a8..0000000
--- a/bundles/org.eclipse.orion.client.ui/web/js-tests/ast/test.html
+++ /dev/null
@@ -1,25 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
- <script src="../../requirejs/require.js"></script>
- <script>
- /*global require window*/
- require({
- baseUrl: '../../',
- paths: {
- text: 'requirejs/text',
- i18n: 'requirejs/i18n',
- domReady: 'requirejs/domReady'
- }
- });
-
- window.onload = function() {
- require(["orion/test","testcase.js"], function(test, testcase) {
- test.run(testcase);
- });
- };
- </script>
-</head>
-<body>
-</body>
-</html> \ No newline at end of file
diff --git a/bundles/org.eclipse.orion.client.ui/web/js-tests/jsTestSuite.js b/bundles/org.eclipse.orion.client.ui/web/js-tests/jsTestSuite.js
index b4912e5..f00b0b1 100644
--- a/bundles/org.eclipse.orion.client.ui/web/js-tests/jsTestSuite.js
+++ b/bundles/org.eclipse.orion.client.ui/web/js-tests/jsTestSuite.js
@@ -105,7 +105,7 @@ OrionTestCase("setting", "/js-tests/settings/test.html");
OrionTestCase("esprima-content-assist", "/js-tests/esprima/esprimaJsContentAssistTests.html");
OrionTestCase("index-file-parsing", "/js-tests/esprima/indexFileParsingTests.html");
OrionTestCase("asyncStyler", "/js-tests/asyncStyler/test.html");
-//OrionTestCase("ast", "/js-tests/ast/test.html");
+//OrionTestCase("ast", "/js-tests/ast/astManagerTests.html");
OrionTestCase("commands", "/js-tests/commands/test.html");
//OrionTestCase("searchRendering", "/js-tests/searchRendering/test.html");
diff --git a/bundles/org.eclipse.orion.client.ui/web/orion/edit/ast.js b/bundles/org.eclipse.orion.client.ui/web/orion/edit/ast.js
index 6248f3f..2a5f47f 100644
--- a/bundles/org.eclipse.orion.client.ui/web/orion/edit/ast.js
+++ b/bundles/org.eclipse.orion.client.ui/web/orion/edit/ast.js
@@ -31,6 +31,7 @@ define([
this.serviceRegistry = serviceRegistry;
this.inputManager = inputManager;
this.start();
+ this.cache = null;
}
objects.mixin(ASTManager.prototype, /** @lends orion.edit.ASTManager.prototype */ {
/**
@@ -39,12 +40,12 @@ define([
* @function
* @public
* @memberof orion.edit.ASTManager.prototype
+ * @throws Error if the manager has already been started
*/
start: function() {
if (this.registration) {
- throw new Error("Already started");
+ throw new Error("The AST manager has already been started");
}
-
this.tracker = new ServiceTracker(this.serviceRegistry, "orion.core.astprovider"); //$NON-NLS-0$
this.tracker.open();
@@ -59,35 +60,40 @@ define([
* @function
* @public
* @memberof orion.edit.ASTManager.prototype
+ * @throws Error if the manager has already been stopped
*/
stop: function() {
- if(!this.registration) {
- throw new Error("Manager not running");
+ if(this.registration) {
+ this.registration.unregister();
+ this.contextRegistration.unregister();
+ this.tracker.close();
+ this.listener = null;
+ this.registration = null;
+ this.cache = null;
+ }
+ else {
+ throw new Error("The AST manager has laready been stopped");
}
- this.registration.unregister();
- this.contextRegistration.unregister();
- this.tracker.close();
- this.listener = this.cachedAST = null;
},
/**
* @name updated
- * @description Notifies the AST manager of a change to the model
+ * @description Notifies the AST manager of a change to the model.
* @function
* @public
* @memberof orion.edit.ASTManager.prototype
* @param {Object} event
*/
updated: function(event) {
- this.cachedAST = null;
+ this.cache = null;
},
/**
* @name _getASTProvider
- * @description Returns the AST provider for the given content type identifier
+ * @description Returns the AST provider for the given content type or <code>null</code> if it could not be computed
* @function
* @private
* @memberof orion.edit.ASTManager.prototype
- * @param {String} contentTypeId
- * @returns {Object} An AST provider capable of providing an AST for the given contentType.
+ * @param {String} contentTypeId the content type identifier
+ * @returns {Object|orion.Promise} An AST provider capable of providing an AST for the given contentType.
*/
_getASTProvider: function(contentTypeId) {
var providers = this.tracker.getServiceReferences().filter(function(serviceRef) {
@@ -102,18 +108,21 @@ define([
* @function
* @private
* @memberof orion.edit.ASTManager.prototype
- * @param {String} contentTypeId the identifier of the content type to compute the AST for
- * @param {Object} astContext the context in which to compute the AST
- * @returns {Object|orion.Promise} or <code>null</code> if there are no providers for an AST of the given content type
+ * @param {Object} options The object of options to pass to the AST provider.
+ * @returns {Object|orion.Promise}
*/
- _getAST: function(contentTypeId, astContext) {
- if (this.cachedAST) {
- return this.cachedAST;
+ _getAST: function(options) {
+ if (this.cache) {
+ return this.cache;
}
- var provider = this._getASTProvider(contentTypeId);
- if (provider) {
- astContext.text = this.inputManager.getEditor().getText();
- return provider.computeAST(astContext);
+ var contentType = this.inputManager.getContentType();
+ if(contentType && contentType.id) {
+ var provider = this._getASTProvider(contentType.id);
+ if (provider) {
+ options = options || {};
+ options.text = this.inputManager.getEditor().getText();
+ return provider.computeAST(options);
+ }
}
return null;
},
@@ -124,17 +133,14 @@ define([
* @public
* @memberof orion.edit.ASTManager.prototype
* @param {Object} [options={}] Options to be passed to the AST provider.
- * @returns {Object|orion.Promise} A promise that resolves to the AST. Resolves to <code>null</code> if no capable provider was found.
+ * @returns {orion.Promise} A promise that resolves to the AST. Resolves to <code>null</code> if no capable provider was found.
*/
getAST: function(options) {
- options = options || {};
- var _self = this;
- var contentType = this.inputManager.getContentType(),
- contentTypeId = contentType && contentType.id;
- return Deferred.when(this._getAST(contentTypeId, options), function(ast) {
- _self.cachedAST = ast;
- return ast;
- });
+ var that = this;
+ return Deferred.when(that._getAST(options), function(ast) {
+ that.cache = ast;
+ return ast;
+ });
}
});
return ASTManager;