Skip to content
Browse files

Reengineered EnvJasmine to better integrate with Maven

  • Loading branch information...
1 parent 164adab commit 41020097fc644dc12a0ee2fc626d46855101589c @trevmex committed
Showing with 174 additions and 66 deletions.
  1. +10 −0 CHANGELOG
  2. +19 −0 LICENSE
  3. +8 −19 README
  4. +1 −0 VERSION
  5. +2 −1 bin/run_all_tests.sh
  6. +2 −1 bin/run_test.sh
  7. +3 −4 include/dependencies.js
  8. +47 −16 lib/envjasmine.js
  9. +29 −0 lib/spanDir/spanDir.js
  10. +9 −0 mocks/demo.mock.js
  11. +22 −0 samples/ajaxDemo.js
  12. +9 −0 samples/demo.js
  13. +1 −24 specs/ajaxDemo.spec.js
  14. +12 −1 specs/demo.spec.js
View
10 CHANGELOG
@@ -0,0 +1,10 @@
+Version 1.4: Reengineered system to better integrate with maven (03/07/2011)
+
+Version 1.3: Added support for arbitrary file structures in specs directory (02/15/2011)
+
+Version 1.2: Improved Maven integration support (01/27/2011)
+
+Version 1.1: Added support for AJAX calls (01/24/2011)
+
+Version 1.0: Initial Release (01/07/2011)
+
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010-2011 Jeff Avallone and Trevor Lalish-Menagh
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
27 README
@@ -34,23 +34,12 @@ https://github.com/velesin/jasmine-jquery
----
Originally created by Jeff Avallone and Trevor Lalish-Menagh
+See LICENSE for lincensing information.
-Copyright (c) 2010 Jeff Avallone and Trevor Lalish-Menagh
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
+----
+
+See CHANGELOG for changelog information.
+
+----
+
+Current version is found in the VERSION file.
View
1 VERSION
@@ -0,0 +1 @@
+1.4
View
3 bin/run_all_tests.sh
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
cd "`dirname \"$0\"`"
-java -Duser.timezone="US/Eastern" -jar ../lib/rhino/js.jar ../lib/envjasmine.js `find ../specs -name *.spec.js`
+PWD=$(pwd)
+java -Duser.timezone="US/Eastern" -jar "$PWD/../lib/rhino/js.jar" "$PWD/../lib/envjasmine.js" "$PWD/.." `find "$PWD/../specs" -name *.spec.js`
View
3 bin/run_test.sh
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
cd "`dirname \"$0\"`"
-java -Duser.timezone="US/Eastern" -jar ../lib/rhino/js.jar ../lib/envjasmine.js ../$1
+PWD=$(pwd)
+java -Duser.timezone="US/Eastern" -jar "$PWD/../lib/rhino/js.jar" "$PWD/../lib/envjasmine.js" "$PWD/.." "$PWD/../$1"
View
7 include/dependencies.js
@@ -3,8 +3,7 @@
// NOTE: Load order does matter.
// This is your main JavaScript directory in your project.
-// EnvJasmine.jsDirectory = "../../../../main/webapp/js/";
+EnvJasmine.jsDir = EnvJasmine.rootDir + "/samples/"; // TODO: Change this to your project's main js directory.
-// Examples
-load("../include/jquery-1.4.4.js");
-// load(EnvJasmine.jsDirectory + "vendor/jquery.json-2.2.min.js");
+load(EnvJasmine.includeDir + "jquery-1.4.4.js"); // for example, load jquery.
+// TODO: Add your own
View
63 lib/envjasmine.js
@@ -14,30 +14,61 @@
Packages.org.mozilla.javascript.Context
.getCurrentContext().setOptimizationLevel(-1);
+if (arguments.length < 1) {
+ print("Usage: envjasmine.js <EnvJasmine Root Directory> [<js spec files>...]");
+ exit(1);
+}
+
// Create the EnvJasmine namespace
if (!this.EnvJasmine) {
this.EnvJasmine = {};
}
+// These are standard driectories in the EnvJasmine project.
+EnvJasmine.rootDir = java.io.File(arguments[0]).getCanonicalPath() || '';
+EnvJasmine.libDir = EnvJasmine.rootDir + "/lib/";
+EnvJasmine.includeDir = EnvJasmine.rootDir + "/include/";
+EnvJasmine.mocksDir = EnvJasmine.rootDir + "/mocks/";
+EnvJasmine.specsDir = EnvJasmine.rootDir + "/specs/";
+EnvJasmine.specSuffix = new RegExp(/.spec.js$/);
+
// Load the envjasmine environment
-load("../lib/envjs/env.rhino.1.2.js");
-load("../lib/jasmine/jasmine.js");
-load("../lib/jasmine-ajax/mock-ajax.js");
-load("../lib/jasmine-ajax/spec-helper.js");
-load("../lib/jasmine-jquery/jasmine-jquery-1.1.3.js"); // include this to use test jQuery tests
-load("../lib/jasmine-rhino-reporter/jasmine-rhino-reporter.js");
+load(EnvJasmine.libDir + "spanDir/spanDir.js");
+load(EnvJasmine.libDir + "envjs/env.rhino.1.2.js");
+load(EnvJasmine.libDir + "jasmine/jasmine.js");
+load(EnvJasmine.libDir + "jasmine-ajax/mock-ajax.js");
+load(EnvJasmine.libDir + "jasmine-ajax/spec-helper.js");
+load(EnvJasmine.libDir + "jasmine-jquery/jasmine-jquery-1.1.3.js");
+load(EnvJasmine.libDir + "jasmine-rhino-reporter/jasmine-rhino-reporter.js");
// Load external dependencies
-load("../include/dependencies.js");
-
-// Load the specs from the commandline
-var spec;
-for (var i = 0; i < arguments.length; i++) {
- spec = arguments[i];
- print("Loading: " + spec);
- load("../specs/" + spec);
+load(EnvJasmine.includeDir + "dependencies.js");
+
+if (arguments.length > 1) {
+ // Load the specs from the commandline
+ var spec = '';
+ for (var i = 1; i < arguments.length; i++) {
+ if (arguments[i][arguments[i].length - 3] !== "." && arguments[i][arguments[i].length - 2] !== "j" && arguments[i][arguments[i].length - 1] !== "s") {
+ // if this is not a JavaScript file (ending in '.js'), then the stupid command line parsed a directory with a space (' ') in it.
+ // When that happens, add the directory segment to the spec path with a space character and continue.
+ spec += arguments[i] + ' ';
+ continue;
+ } else {
+ spec += arguments[i];
+ print("Loading: " + spec);
+ load(spec);
+ spec = '';
+ }
+ }
+} else {
+ // Load specs from the specs dir
+ spanDir(EnvJasmine.specsDir, function (spec) {
+ if(EnvJasmine.specSuffix.test(spec)) {
+ print("Loading: " + spec);
+ load(spec);
+ }
+ });
}
// Execute the specs
-window.location = "../lib/envjasmine.html";
-
+window.location = EnvJasmine.libDir + "envjasmine.html";
View
29 lib/spanDir/spanDir.js
@@ -0,0 +1,29 @@
+// spanDir - a directory traversal function for
+// Rhino JavaScript
+// Copyright 2010 by James K. Lawless
+// See MIT/X11 license at
+// http://www.mailsend-online.com/wp/license.php
+
+importPackage(java.io);
+
+// spanDir() accepts two parameters
+// The first is a string representing a directory path
+// The second is a closure that accepts a parameter of type
+// java.io.File
+function spanDir(dir,dirHandler) {
+ var lst=new File(dir).listFiles(),
+ i;
+
+ for(i=0;i<lst.length;i++) {
+ // If it's a directory, recursive call spanDir()
+ // so that we end up doing a scan of
+ // the directory tree
+ if(lst[i].isDirectory()) {
+ spanDir(lst[i].getCanonicalPath(),dirHandler);
+ }
+ // Pass the File object to the handler that
+ // the caller has specified regardless of whether
+ // the File object is a directory.
+ dirHandler(lst[i].getCanonicalPath());
+ }
+}
View
9 mocks/demo.mock.js
@@ -0,0 +1,9 @@
+// Add mocks here for use in your specs.
+// A mock is a simple JavaScript object that you will use to fake data.
+var demoUser = {
+ name: "Trevor",
+ birthdate: "3/2/1977"
+ },
+ badUser = {
+ name: "BadGuy"
+ };
View
22 samples/ajaxDemo.js
@@ -0,0 +1,22 @@
+// This is the contents of ajaxDemo.js, the file to test.
+var TwitterWidget = {
+ makeRequest: function() {
+ var self = this;
+
+ $.ajax({
+ method: "GET",
+ url: "http://api.twitter.com/1/statuses/show/trevmex.json",
+ datatype: "json",
+ success: function (data) {
+ self.addDataToDOM(data);
+ }
+ });
+ },
+
+ addDataToDOM: function(data) {
+ // does something
+ // We will mock this behavior with a spy.
+
+ return data;
+ }
+};
View
9 samples/demo.js
@@ -0,0 +1,9 @@
+// Sample demo
+
+if (!this.Demo) {
+ Demo = {};
+}
+
+Demo.checkBirthdate = function (user) {
+ return !user.birthdate ? false : true;
+}
View
25 specs/ajaxDemo.spec.js
@@ -1,30 +1,7 @@
// Load the file to test here.
//
// Example:
-// load(EnvJasmine.jsDirectory + "ajaxDemo.js");
-
-// This is the contents of ajaxDemo.js, the file to test.
-var TwitterWidget = {
- makeRequest: function() {
- var self = this;
-
- $.ajax({
- method: "GET",
- url: "http://api.twitter.com/1/statuses/show/trevmex.json",
- datatype: "json",
- success: function (data) {
- self.addDataToDOM(data);
- }
- });
- },
-
- addDataToDOM: function(data) {
- // does something
- // We will mock this behavior with a spy.
-
- return data;
- }
-};
+load(EnvJasmine.jsDir + "ajaxDemo.js");
// This is the test code.
describe("AjaxDemo", function () {
View
13 specs/demo.spec.js
@@ -1,7 +1,10 @@
// Load the file to test here.
//
// Example:
-// load(EnvJasmine.jsDirectory + "demo.js");
+load(EnvJasmine.jsDir + "demo.js");
+
+// Load mocks for this spec
+load(EnvJasmine.mocksDir + "demo.mock.js");
describe("Demo", function () {
it("asserts that one plus one equals two", function () {
@@ -11,4 +14,12 @@ describe("Demo", function () {
it("asserts that 1 + 1 does not equal 3", function () {
expect(1 + 1 == 3).toEqual(false);
});
+
+ it("asserts that a user has a birthdate", function () {
+ expect(Demo.checkBirthdate(demoUser)).toEqual(true);
+ });
+
+ it("asserts that an ill-formed user has no birthdate", function () {
+ expect(Demo.checkBirthdate(badUser)).toEqual(false);
+ });
});

0 comments on commit 4102009

Please sign in to comment.
Something went wrong with that request. Please try again.