aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Rennie2013-12-09 15:33:49 (EST)
committerGerrit Code Review @ Eclipse.org2013-12-11 12:36:11 (EST)
commite9525f46da066ea5d462b8d73e57f58551205148 (patch)
treefa57b4036c1d2207d6aad8172b7d16934c9c2d3d
parentc76a20afb1d0e607af1fe36f434bf0cc2b0640c2 (diff)
downloadorg.eclipse.orion.client-e9525f46da066ea5d462b8d73e57f58551205148.zip
org.eclipse.orion.client-e9525f46da066ea5d462b8d73e57f58551205148.tar.gz
org.eclipse.orion.client-e9525f46da066ea5d462b8d73e57f58551205148.tar.bz2
Bug 423008 - Add tests for mark occurrencesrefs/changes/35/19535/2
Change-Id: I83288a446f79b6028559ec5cb95bf23a3f1b5d4f Signed-off-by: Mike Rennie <Michael_Rennie@ca.ibm.com>
-rw-r--r--bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.html32
-rw-r--r--bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.js150
2 files changed, 182 insertions, 0 deletions
diff --git a/bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.html b/bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.html
new file mode 100644
index 0000000..b64454f
--- /dev/null
+++ b/bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.html
@@ -0,0 +1,32 @@
+<!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',
+ estraverse: 'estraverse/estraverse',
+ esprima: 'esprima/esprima'
+ },
+ isTest: true
+ });
+
+ window.onload = function() {
+ require(["orion/test","js-tests/occurrencesTests.js"], function(test, testcase) {
+ test.run(testcase);
+ });
+ };
+ </script>
+</head>
+<body>
+<h2>JavaScript Mark Occurrences Tests</h2>
+<p>
+This suite tests <code>javascript.JavaScriptOccurrences</code>
+</p>
+</body>
+</html> \ No newline at end of file
diff --git a/bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.js b/bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.js
new file mode 100644
index 0000000..51a3068
--- /dev/null
+++ b/bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.js
@@ -0,0 +1,150 @@
+/*******************************************************************************
+ * @license
+ * Copyright (c) 2013 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0
+ * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
+ * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
+ *
+ * Contributors: IBM Corporation - initial API and implementation
+ ******************************************************************************/
+/*global console:true define*/
+define([
+ 'orion/assert',
+ 'javascript/astManager',
+ 'orion/Deferred',
+ 'javascript/occurrences'
+], function(Assert, ASTManager, Deferred, Occurrences) {
+
+ var astManager = new ASTManager();
+ var occurrences = new Occurrences.JavaScriptOccurrences(astManager);
+ var editorContext = {
+ text: "",
+ getText: function() {
+ return new Deferred().resolve(this.text);
+ }
+ };
+ var context = {
+ selection: {
+ start:-1,
+ end: -1
+ }
+ };
+
+ /**
+ * @name tearDown
+ * @description Resets the test state between runs, must explicitly be called per-test
+ * @function
+ * @public
+ */
+ function tearDown() {
+ editorContext.text = "";
+ astManager.updated();
+ context.selection.start = -1;
+ context.selection.end = -1;
+ };
+
+ /**
+ * @name assertOccurrence
+ * @description Checks the given occurrence against the expected start and end to make sure it is marked correctly
+ * @function
+ * @public
+ * @param {Array} results The computed occurrence elements to check
+ * @param {Array} expected The array of expected start/end pairs
+ */
+ function assertOccurrences(results, expected) {
+ if(!results) {
+ Assert.fail("The occurrence array cannot be null");
+ }
+ Assert.equal(results.length, expected.length, "The wrong number of occurrences was returned");
+ for(var i = 0; i < expected.length; i++) {
+ //for each expected result try to find it in the results, and remove it if it is found
+ for(var j = 0; j < results.length; j++) {
+ if(!results[j]) {
+ continue;
+ }
+ if((expected[i].start === results[j].start) && (expected[i].end === results[j].end)) {
+ results[j] = null;
+ }
+ }
+ }
+ for(var k = 0; k < results.length; k++) {
+ if(results[k]) {
+ Assert.fail("Found an unknown occurrence: [start "+results[k].start+"][end "+results[k].end+"]");
+ }
+ }
+ };
+
+ /**
+ * @name setContext
+ * @description Delegate helper to set and return the context
+ * @function
+ * @public
+ * @param {Number} start The start of the editor selection
+ * @param {Number} end The end of thhe editor selection
+ * @returns {Object} the modified context object
+ */
+ function setContext(start, end) {
+ context.selection.start = start;
+ context.selection.end = end;
+ return context;
+ }
+
+ var Tests = {};
+
+ /**
+ * Tests a function declaration
+ */
+ Tests.test_funcDeclaration1 = function() {
+ editorContext.text = "function F1(p1, p2) {\n"+
+ "\tvar out = p1;\n"+
+ "};";
+ return occurrences.computeOccurrences(editorContext, setContext(13, 13)).then(function(results) {
+ try {
+ //expected to fail until https://bugs.eclipse.org/bugs/show_bug.cgi?id=423634 is fixed
+ assertOccurrences(results, [{start:12, end:14}, {start:33, end:35}]);
+ }
+ finally {
+ tearDown();
+ }
+ });
+ };
+
+ /**
+ * Tests a function expression
+ */
+ Tests.test_funcExpression1 = function() {
+ editorContext.text = "var obj = {\n"+
+ "\titem: function(p1, p2) {\n"+
+ "\t\tvar out = p1;\n"+
+ "\t}"+
+ "};";
+ return occurrences.computeOccurrences(editorContext, setContext(30, 30)).then(function(results) {
+ try {
+ //expected to fail until https://bugs.eclipse.org/bugs/show_bug.cgi?id=423634 is fixed
+ assertOccurrences(results, [{start:28, end:30}, {start:50, end:52}]);
+ }
+ finally {
+ tearDown();
+ }
+ });
+ };
+
+ /**
+ * Tests an object expression
+ */
+ Tests.test_objExpression1 = function() {
+ editorContext.text = "var object = {};"+
+ "var newobject = object;";
+ return occurrences.computeOccurrences(editorContext, setContext(5, 5)).then(function(results) {
+ try {
+ assertOccurrences(results, [{start:4, end:10}, {start:32, end:38}]);
+ }
+ finally {
+ tearDown();
+ }
+ });
+ };
+
+ return Tests;
+}); \ No newline at end of file