Skip to content

Commit

Permalink
Reengineered EnvJasmine to better integrate with Maven
Browse files Browse the repository at this point in the history
  • Loading branch information
trevmex committed Mar 7, 2011
1 parent 164adab commit 4102009
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 66 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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)

19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 8 additions & 19 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.4
3 changes: 2 additions & 1 deletion bin/run_all_tests.sh
Original file line number Diff line number Diff line change
@@ -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`
3 changes: 2 additions & 1 deletion bin/run_test.sh
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 3 additions & 4 deletions include/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
63 changes: 47 additions & 16 deletions lib/envjasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
29 changes: 29 additions & 0 deletions lib/spanDir/spanDir.js
Original file line number Diff line number Diff line change
@@ -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());
}
}
9 changes: 9 additions & 0 deletions mocks/demo.mock.js
Original file line number Diff line number Diff line change
@@ -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"
};
22 changes: 22 additions & 0 deletions samples/ajaxDemo.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
9 changes: 9 additions & 0 deletions samples/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Sample demo

if (!this.Demo) {
Demo = {};
}

Demo.checkBirthdate = function (user) {
return !user.birthdate ? false : true;
}
25 changes: 1 addition & 24 deletions specs/ajaxDemo.spec.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
13 changes: 12 additions & 1 deletion specs/demo.spec.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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.