Compare commits

...

42 Commits

Author SHA1 Message Date
Christian Williams
1530ad25f9 Regenerate index.html with standalone download link. 2010-09-14 13:01:42 -07:00
Christian Williams
b2557e4ebe Build dist for 1.0.0. 2010-09-14 12:58:02 -07:00
Christian Williams
85c265e7c5 Bump version to 1.0.0! 2010-09-14 12:54:23 -07:00
Christian Williams
cc36a1808b Add link to jasmine site from html runner. 2010-09-12 13:11:33 -07:00
Christian Williams
685344342a Add MIT.LICENSE to standalone zip. 2010-09-08 13:38:55 -07:00
Christian Williams
a4eab045a0 Fix version format -- 1.0.0.rc1 instead of 1.0.0RC1, and include in standalone zip. 2010-08-26 13:15:15 -07:00
Christian Williams
cd0346b449 Allow version parts to have non-numeric components (e.g. "rc1"). 2010-08-26 13:11:54 -07:00
Christian Williams
f134c6e386 Regenerate download page. 2010-08-25 18:44:27 -07:00
Christian Williams
75238bd3b1 Update version to 1.0.0 for RC1. 2010-08-25 18:25:52 -07:00
Christian Williams
b8b4457b6b Regenerate jasmine.js. 2010-08-25 18:18:49 -07:00
Christian Williams
b33b2d2cf1 toThrow()'s (and some other matchers') .not message is misleading. 2010-08-25 18:16:37 -07:00
Christian Williams
3ec736f64c Regenerate jasmine.js. 2010-08-25 18:07:14 -07:00
Christian Williams
e7a57606a2 Allow waitsFor() arguments to be specified in arbitrary order.
Default waitsFor() timeout is now specified in env.defaultTimeoutInterval; defaults to 5 seconds.
Deprecate waits() block in favor of waitsFor().
2010-08-25 18:04:52 -07:00
Christian Williams
55149310c3 Fixed bug: waitsFor() hangs forever if latch function never returns true. 2010-08-25 17:16:19 -07:00
Christian Williams
254ebb8a03 Update jasmine.js. 2010-08-25 15:46:57 -07:00
Christian Williams
762f88e3c8 Some exploration and refactoring re waitsFor() bug: waitsFor() hangs forever if latch function never returns true. 2010-08-19 23:55:21 -07:00
Christian Williams
b3715075e3 Add custom messages for inverted spy matchers. 2010-08-13 00:32:32 -07:00
Christian Williams
9af81b627b Missing semicolons. 2010-08-05 23:10:09 -07:00
Christian Williams
5d44da0238 Remove acknowledgements and analytics code from README. 2010-08-05 00:54:54 -07:00
Christian Williams
93952890c3 Deprecate toNotBe, toNotEqual, toNotMatch, and toNotContain matchers. 2010-08-05 00:50:59 -07:00
Christian Williams
5182e00c66 Wrapped matcher functions return undefined after expect() for futureproofing; we might want to allow matcher chaining, e.g.:
expect(x).toBeGreaterThan(3).and.toBeLessThan(6));
or other stuff e.g.:
  expect(x).toBeTrue().because("it just needs to be");
2010-08-04 13:52:38 -07:00
Christian Williams
676af93bea Refactor toThrow() matcher specs. 2010-08-03 18:51:31 -07:00
Christian Williams
4d7b839473 Fix not.toThrow() matcher exception when used with no args. 2010-08-02 18:36:26 -07:00
Todd Persen
89afebd86b Update copyright years. 2010-07-30 11:59:30 -04:00
Christian Williams
31020bb51d Try adding analytics code. 2010-07-28 18:18:18 -07:00
Christian Williams
17e9a038a2 2010-07-28 18:17:10 -07:00
Todd Persen
1e01000c17 Update MIT License and add to README. 2010-07-28 11:20:29 -04:00
Christian Williams
6562b5a3b4 Add warning to SpecRunner.html when running from non-built source. 2010-07-27 18:47:12 -07:00
Christian Williams & Jay Phillips
15955df258 Styling. 2010-07-23 18:25:21 -07:00
Christian Williams & Jay Phillips
81bf4784cd Move User Guide to site. 2010-07-23 18:23:53 -07:00
Christian Williams & Jay Phillips
14486f5912 Add jekyll to Gemfile. 2010-07-23 18:19:16 -07:00
Christian Williams & Jay Phillips
9842c8ff81 Move User Guide from github README to here. 2010-07-23 18:08:33 -07:00
Christian Williams
feb4618c31 Add Gemfile. 2010-07-23 10:06:19 -07:00
Christian Williams & Jay Phillips
c6acfb3982 Merge branch 'master', remote branch 'origin' 2010-07-21 18:55:55 -07:00
Tom Crayford
ba5bd3f866 Jasmine.createSpy() throws a reference error, jasmine.createSpy() does not. 2010-07-18 00:45:23 +08:00
Christian Williams & Harry Ugol
0331209413 Merge branch 'master' of git://github.com/pivotal/jasmine 2010-07-09 14:51:13 -07:00
Christian Williams
57491a0088 README updates. 2010-07-05 22:56:37 -07:00
Christian Williams
e6b6d9ca95 2010-07-05 22:46:55 -07:00
Christian Williams
bf44d4b0db Change link to downloads. 2010-07-01 00:06:28 -07:00
Christian Williams
3da73bd519 Remove unused lib/consolex.js. 2010-06-30 23:49:34 -07:00
Christian Williams
61c0fe781c Styling fix. 2010-06-30 00:09:01 -07:00
Harry Ugol & Tyler Schultz
766f3546bf Add gem reference. 2010-06-28 10:52:22 -07:00
23 changed files with 673 additions and 842 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,6 @@
.idea/
.svn/
.DS_Store
_site/
.bundle/
.pairs

6
Gemfile Normal file
View File

@@ -0,0 +1,6 @@
source :gemcutter
gem "jekyll", "0.6.2"
gem "json_pure", "~>1.4.3"
gem "ragaskar-jsdoc_helper"
gem "rake", "0.8.7"

View File

@@ -1,4 +1,4 @@
Copyright (c) 2008 Pivotal Labs
Copyright (c) 2008-2010 Pivotal Labs
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@@ -17,4 +17,4 @@ 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.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,463 +1,12 @@
[Jasmine](http://pivotal.github.com/jasmine)
<a name="README">[Jasmine](http://pivotal.github.com/jasmine/)</a>
=======
**A JavaScript Testing Framework**
Quick Start
----------
Want to use Jasmine in a project? Go HERE: [http://pivotal.github.com/jasmine/](http://pivotal.github.com/jasmine/)
1. Get the latest release from the [downloads page](http://github.com/pivotal/jasmine/downloads).
2. Open `SpecRunner.html` in your favorite browser.
Want to contribute to Jasmine? Read on...
For running within a Ruby environment, including automated execution with Selenium, please use
the [jasmine gem](http://github.com/pivotal/jasmine-gem).
### Which Release Should I Use?
Please use the latest version unless you have a good reason not to. Some of this documentation may not be applicable to older versions. Please see [[Release Notes]](http://wiki.github.com/pivotal/jasmine/release-notes) for change information.
Why Another JavaScript TDD/BDD Framework?
-----------
There are some great JavaScript testing frameworks out there already, so why did we write another?
None of the existing frameworks quite worked the way we wanted. Many only work from within a browser. Most don't support testing asynchronous code like event callbacks. Some have syntax that's hard for JS developers or IDEs to understand.
So we decided to start from scratch.
Enter Jasmine
------------
Jasmine is our dream JavaScript testing framework. It's heavily influenced by, and borrows the best parts of, ScrewUnit, JSSpec, [JSpec](http://github.com/visionmedia/jspec/tree/master), and of course RSpec.
Jasmine was designed with a few principles in mind. We believe that a good JavaScript testing framework:
* should not be tied to any browser, framework, platform, or host language.
* should have idiomatic and unsurprising syntax.
* should work anywhere JavaScript can run, including browsers, servers, phones, etc.
* shouldn't intrude in your application's territory (e.g. by cluttering the global namespace).
* should play well with IDEs (e.g. test code should pass static analysis).
Some of our goals while writing Jasmine:
* it should encourage good testing practices.
* it should integrate easily with continuous build systems.
* it should be simple to get started with.
The result is Jasmine, and we love test-driving our code with it. Enjoy.
How To
------
There is a simple example of how to use Jasmine in the /example directory. But here's more information.
### Specs
Each spec is, naturally, a JavaScript function. You tell Jasmine about this spec with a call to `it()` with a string and the function. The string is a description that will be helpful to you when reading a report.
it('should be a test', function () {
var foo = 0;
foo++;
});
### Expectations
Within your spec you will want to express expectations about the behavior of your application code. These are made with the `expect()` function and expectation matchers, like this:
it('should be a test', function () {
var foo = 0; // set up the world
foo++; // call your application code
expect(foo).toEqual(1); // passes because foo == 1
});
Results of the expectations are logged for later for reporting.
#### Expectation Matchers
Jasmine has several built-in matchers. Here are a few:
>`expect(x).toEqual(y);` compares objects or primitives `x` and `y` and passes if they are equivalent
>
>`expect(x).toMatch(pattern);` compares `x` to string or regular expression `pattern` and passes if they match
>
>`expect(x).toBeDefined();` passes if `x` is not `undefined`
>
>`expect(x).toBeNull();` passes if `x` is `null`
>
>`expect(x).toBeTruthy();` passes if `x` evaluates to true
>
>`expect(x).toBeFalsy();` passes if `x` evaluates to false
>
>`expect(x).toContain(y);` passes if array or string `x` contains `y`
Every matcher's criteria can be inverted by prepending `.not`:
>`expect(x).not.toEqual(y);` compares objects or primitives `x` and `y` and passes if they are *not* equivalent
#### Writing New Matchers
We've provided a small set of matchers that cover many common situations. However, we recommend that you write custom matchers when you want to assert a more specific sort of expectation. Custom matchers help to document the intent of your specs, and can help to remove code duplication in your specs.
It's extremely easy to create new matchers for your app. A matcher function receives the actual value as `this.actual`, and zero or more arguments may be passed in the function call. The function should return `true` if the actual value passes the matcher's requirements, and `false` if it does not.
Here's the definition of `toBeLessThan()`:
toBeLessThan: function(expected) {
return this.actual < expected;
};
To add the matcher to your suite, call `this.addMatchers()` from within a `before` or `it` block. Call it with an object mapping matcher name to function:
beforeEach(function() {
this.addMatchers({
toBeVisible: function() { return this.actual.isVisible(); }
});
});
### Suites
Specs are grouped in Suites. Suites are defined using the global `describe()` function:
describe('One suite', function () {
it('has a test', function () {
...
});
it('has another test', function () {
...
});
});
The Suite name is so that reporting is more descriptive.
Suites are executed in the order in which `describe()` calls are made, usually in the order in which their script files are included. Additionally, specs within a suite share a functional scope. So you may declare variables inside a describe block and they are accessible from within your specs. For example:
describe('A suite with some variables', function () {
var bar = 0
it('has a test', function () {
bar++;
expect(bar).toEqual(1);
});
it('has another test', function () {
bar++;
expect(bar).toEqual(2);
});
});
#### beforeEach
A suite can have a beforeEach declaration. It takes a function that is run before each spec. For example:
describe('some suite', function () {
var suiteWideFoo;
beforeEach(function () {
suiteWideFoo = 1;
});
it('should equal bar', function () {
expect(suiteWideFoo).toEqual(1);
});
});
A runner can also have beforeEach declarations. Runner beforeEach functions are executed before every spec in all suites, and execute BEFORE suite beforeEach functions. For example:
var runnerWideFoo = [];
beforeEach(function () {
runnerWideFoo.push('runner');
});
describe('some suite', function () {
beforeEach(function () {
runnerWideFoo.push('suite');
});
it('should equal bar', function () {
expect(runnerWideFoo).toEqual(['runner', 'suite']);
});
});
#### afterEach
Similarly, there is an afterEach declaration. It takes a function that is run after each spec. For example:
describe('some suite', function () {
var suiteWideFoo;
afterEach(function () {
suiteWideFoo = 0;
});
it('should equal 1', function () {
expect(suiteWideFoo).toEqual(1);
});
it('should equal 0 after', function () {
expect(suiteWideFoo).toEqual(0);
};
});
A runner can also have an afterEach declarations. Runner afterEach functions are executed after every spec in all suites, and execute AFTER suite afterEach functions. For example:
var runnerWideFoo = [];
afterEach(function () {
runnerWideFoo.push('runner');
});
describe('some suite', function () {
afterEach(function () {
runnerWideFoo.push('suite');
});
it('should be empty', function () {
expect(runnerWideFoo).toEqual([]);
});
it('should be populated after', function () {
expect(runnerWideFoo).toEqual(['suite', 'runner']);
};
});
### Single-spec After functions
A spec may ask Jasmine to execute some code after the spec has finished running; the code will run whether the spec finishes successfully or not. Multiple after functions may be given.
describe('some suite', function () {
it(function () {
var originalTitle = window.title;
this.after(function() { window.title = originalTitle; });
MyWindow.setTitle("new value");
expect(window.title).toEqual("new value");
});
### Nested Describes
Jasmine supports nested describes. An example:
describe('some suite', function () {
var suiteWideFoo;
beforeEach(function () {
suiteWideFoo = 0;
});
describe('some nested suite', function() {
var nestedSuiteBar;
beforeEach(function() {
nestedSuiteBar=1;
});
it('nested expectation', function () {
expect(suiteWideFoo).toEqual(0);
expect(nestedSuiteBar).toEqual(1);
});
});
it('top-level describe', function () {
expect(suiteWideFoo).toEqual(0);
expect(nestedSuiteBar).toEqual(undefined);
});
});
### Spies
Jasmine integrates 'spies' that permit many spying, mocking, and faking behaviors.
Here are a few examples:
var Klass = function () {
};
var Klass.prototype.method = function (arg) {
return arg;
};
var Klass.prototype.methodWithCallback = function (callback) {
return callback('foo');
};
...
it('should spy on Klass#method') {
spyOn(Klass, 'method');
Klass.method('foo argument');
expect(Klass.method).toHaveBeenCalledWith('foo argument');
});
it('should spy on Klass#methodWithCallback') {
var callback = Jasmine.createSpy();
Klass.methodWithCallback(callback);
expect(callback).toHaveBeenCalledWith('foo');
});
Spies can be very useful for testing AJAX or other asynchronous behaviors that take callbacks by faking the method firing an async call.
var Klass = function () {
};
var Klass.prototype.asyncMethod = function (callback) {
someAsyncCall(callback);
};
...
it('should test async call') {
spyOn(Klass, 'asyncMethod');
var callback = Jasmine.createSpy();
Klass.asyncMethod(callback);
expect(callback).not.toHaveBeenCalled();
var someResponseData = 'foo';
Klass.asyncMethod.mostRecentCall.args[0](someResponseData);
expect(callback).toHaveBeenCalledWith(someResponseData);
});
There are spy-specfic matchers that are very handy.
`expect(x).toHaveBeenCalled()` passes if `x` is a spy and was called
`expect(x).toHaveBeenCalledWith(arguments)` passes if `x` is a spy and was called with the specified arguments
`expect(x).not.toHaveBeenCalled()` passes if `x` is a spy and was not called
`expect(x).not.toHaveBeenCalledWith(arguments)` passes if `x` is a spy and was not called with the specified arguments
The old matchers `wasCalled`, `wasNotCalled`, `wasCalledWith`, and `wasNotCalledWith` have been deprecated and will be removed in a future release. Please change your specs to use `toHaveBeenCalled`, `not.toHaveBeenCalled`, `toHaveBeenCalledWith`, and `not.toHaveBeenCalledWith` respectively.
Spies can be trained to respond in a variety of ways when invoked:
`spyOn(x, 'method').andCallThrough()`: spies on AND calls the original function spied on
`spyOn(x, 'method').andReturn(arguments)`: returns passed arguments when spy is called
`spyOn(x, 'method').andThrow(exception)`: throws passed exception when spy is called
`spyOn(x, 'method').andCallFake(function)`: calls passed function when spy is called
Spies have some useful properties:
`callCount`: returns number of times spy was called
`mostRecentCall.args`: returns argument array from last call to spy.
`argsForCall[i]` returns arguments array for call `i` to spy.
Spies are automatically removed after each spec. They may be set in the beforeEach function.
### Disabling Tests & Suites
Specs may be disabled by calling `xit()` instead of `it()`. Suites may be disabled by calling `xdescribe()` instead of `describe()`. A simple find/replace in your editor of choice will allow you to run a subset of your specs.
### Asynchronous Specs
You may be thinking, "That's all very nice, but what's this about asynchronous tests?"
Well, say you need to make a call that is asynchronous - an AJAX API, event callback, or some other JavaScript library. That is, the call returns immediately, yet you want to make expectations 'at some point in the future' after some magic happens in the background.
Jasmine allows you to do this with `runs()` and `waits()` blocks.
`runs()` blocks by themselves simply run as if they were called directly. The following snippets of code should provide similar results:
it('should be a test', function () {
var foo = 0
foo++;
expect(foo).toEqual(1);
});
and
it('should be a test', function () {
runs( function () {
var foo = 0
foo++;
expect(foo).toEqual(1);
});
});
multiple `runs()` blocks in a spec will run serially. For example,
it('should be a test', function () {
runs( function () {
var foo = 0
foo++;
expect(foo).toEqual(1);
});
runs( function () {
var bar = 0
bar++;
expect(bar).toEqual(1);
});
});
`runs()` blocks share functional scope -- `this` properties will be common to all blocks, but declared `var`'s will not!
it('should be a test', function () {
runs( function () {
this.foo = 0
this.foo++;
var bar = 0;
bar++;
expect(this.foo).toEqual(1);
expect(bar).toEqual(1);
});
runs( function () {
this.foo++;
var bar = 0
bar++;
expect(foo).toEqual(2);
expect(bar).toEqual(1);
});
});
`runs()` blocks exist so you can test asynchronous processes. The function `waits()` works with `runs()` to provide a naive
timeout before the next block is run. You supply a time to wait before the next `runs()` function is executed. For example:
it('should be a test', function () {
runs(function () {
this.foo = 0;
var that = this;
setTimeout(function () {
that.foo++;
}, 250);
});
runs(function () {
this.expects(this.foo).toEqual(0);
});
waits(500);
runs(function () {
this.expects(this.foo).toEqual(1);
});
});
What's happening here?
* The first call to `runs()` sets call for 1/4 of a second in the future that increments `this.foo`.
* The second `runs()` is executed immediately and then verifies that `this.foo` was indeed initialized to zero in the previous `runs()`.
* Then we wait for half a second.
* Then the last call to `runs()` expects that `this.foo` was incremented by the `setTimeout`.
<i>(More developer docs to come...)</i>
## Support
We now have a Google Group for support & discussion.
@@ -476,7 +25,4 @@ We now have a Google Group for support & discussion.
## Developers
We welcome your contributions! Jasmine is currently maintained by Davis Frank ([infews](http://github.com/infews)), Rajan Agaskar ([ragaskar](http://github.com/ragaskar)), and Christian Williams ([Xian](http://github.com/Xian)). You can help us by removing all other recipients from your pull request.
## Acknowledgments
* A big shout out to the various JavaScript test framework authors, especially TJ for [JSpec](http://github.com/visionmedia/jspec/tree/master) - we played with it a bit before deciding that we really needed to roll our own.
* Thanks to Pivot [Jessica Miller](http://www.jessicamillerworks.com/) for our fancy pass/fail/pending icons
* Huge contributions have been made by [Adam Abrons](mailto:adam@pivotallabs.com), [Lee Byrd](mailto:lee@pivotallabs.com), [Erik Hanson](mailto:erik@pivotallabs.com), [Carl Jackson](mailto:carl@pivotallabs.com), and many other Pivots.
Copyright (c) 2008-2010 Pivotal Labs. This software is licensed under the MIT License.

View File

@@ -22,6 +22,7 @@ task :default => 'jasmine:dist'
def substitute_jasmine_version(filename)
contents = File.read(filename)
contents = contents.gsub(/##JASMINE_VERSION##/, (jasmine_version))
contents = contents.gsub(/[^\n]*REMOVE_THIS_LINE_FROM_BUILD[^\n]*/, '')
File.open(filename, 'w') { |f| f.write(contents) }
end
@@ -73,9 +74,9 @@ namespace :jasmine do
jasmine.puts %{
jasmine.version_= {
"major": #{version['major']},
"minor": #{version['minor']},
"build": #{version['build']},
"major": #{version['major'].to_json},
"minor": #{version['minor'].to_json},
"build": #{version['build'].to_json},
"revision": #{Time.now.to_i}
};
}
@@ -100,7 +101,6 @@ jasmine.version_= {
task :doc => :need_pages_submodule do
puts 'Creating Jasmine Documentation'
require 'rubygems'
#sudo gem install ragaskar-jsdoc_helper
require 'jsdoc_helper'
FileUtils.rm_r "pages/jsdoc", :force => true
@@ -131,7 +131,8 @@ jasmine.version_= {
{
"lib/jasmine.js" => "jasmine.js",
"lib/jasmine-html.js" => "jasmine-html.js",
"src/html/jasmine.css" => "jasmine.css"
"src/html/jasmine.css" => "jasmine.css",
"MIT.LICENSE" => "MIT.LICENSE"
}.each_pair do |src, dest|
FileUtils.cp(File.join(root, src), File.join(lib_dir, dest))
end
@@ -151,17 +152,19 @@ jasmine.version_= {
require 'digest/sha1'
download_html = "<!-- START_DOWNLOADS -->\n"
download_html += "<table>\n<tr><th/><th>Version</th><th>Size</th><th>Date</th><th>SHA1</th></tr>\n"
download_html += "<table>\n<tr><th></th><th>Version</th><th>Size</th><th>Date</th><th>SHA1</th></tr>\n"
Dir.glob('pages/downloads/*.zip').sort.reverse.each do |f|
sha1 = Digest::SHA1.hexdigest File.read(f)
fn = f.sub(/^pages\//, '')
version = /jasmine-standalone-(.*).zip/.match(f)[1]
download_html += "<td><a href='#{fn}'>#{fn.sub(/downloads\//, '')}</a></td>"
download_html += "<td>#{version}</td>\n"
download_html += "<td>#{File.size(f) / 1024}k</td>\n"
download_html += "<td>#{File.mtime(f).strftime("%Y/%m/%d %H:%M:%S")}</td>\n"
download_html += "<td>#{sha1}</td>\n"
download_html += "<tr>\n"
download_html += "<td class=\"link\"><a href='#{fn}'>#{fn.sub(/downloads\//, '')}</a></td>\n"
download_html += "<td class=\"version\">#{version}</td>\n"
download_html += "<td class=\"size\">#{File.size(f) / 1024}k</td>\n"
download_html += "<td class=\"date\">#{File.mtime(f).strftime("%Y/%m/%d %H:%M:%S %Z")}</td>\n"
download_html += "<td class=\"sha\">#{sha1}</td>\n"
download_html += "</tr>\n"
end
download_html += "</table>\n<!-- END_DOWNLOADS -->"

View File

@@ -17,6 +17,7 @@
</head>
<body>
<div id="REMOVE_THIS_LINE_FROM_BUILD"><p>You must be trying to look at examples in the Jasmine source tree.</p><p>Please download a distribution version of Jasmine at <a href="http://pivotal.github.com/jasmine/">http://pivotal.github.com/jasmine/</a>.</p></div>
<script type="text/javascript">
jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
jasmine.getEnv().execute();
@@ -24,4 +25,3 @@
</body>
</html>

View File

@@ -1,28 +0,0 @@
/** Console X
* http://github.com/deadlyicon/consolex.js
*
* By Jared Grippe <jared@jaredgrippe.com>
*
* Copyright (c) 2009 Jared Grippe
* Licensed under the MIT license.
*
* consolex avoids ever having to see javascript bugs in browsers that do not implement the entire
* firebug console suit
*
*/
(function(window) {
window.console || (window.console = {});
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
function emptyFunction(){}
for (var i = 0; i < names.length; ++i){
window.console[names[i]] || (window.console[names[i]] = emptyFunction);
if (typeof window.console[names[i]] !== 'function')
window.console[names[i]] = (function(method) {
return function(){ return Function.prototype.apply.apply(method, [console,arguments]); };
})(window.console[names[i]]);
}
})(this);

View File

@@ -34,7 +34,7 @@ jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) {
this.outerDiv = this.createDom('div', { className: 'jasmine_reporter' },
this.createDom('div', { className: 'banner' },
this.createDom('div', { className: 'logo' },
"Jasmine",
this.createDom('a', { href: 'http://pivotal.github.com/jasmine/', target: "_blank" }, "Jasmine"),
this.createDom('span', { className: 'version' }, runner.env.versionString())),
this.createDom('div', { className: 'options' },
"Show ",

View File

@@ -21,11 +21,16 @@ jasmine.unimplementedMethod_ = function() {
jasmine.undefined = jasmine.___undefined___;
/**
* Default interval for event loop yields. Small values here may result in slow test running. Zero means no updates until all tests have completed.
* Default interval in milliseconds for event loop yields (e.g. to allow network activity or to refresh the screen with the HTML-based runner). Small values here may result in slow test running. Zero means no updates until all tests have completed.
*
*/
jasmine.DEFAULT_UPDATE_INTERVAL = 250;
/**
* Default timeout interval in milliseconds for waitsFor() blocks.
*/
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
jasmine.getGlobal = function() {
function getGlobal() {
return this;
@@ -490,22 +495,24 @@ var runs = function(func) {
};
/**
* Waits for a timeout before moving to the next runs()-defined block.
* @param {Number} timeout
* Waits a fixed time period before moving to the next block.
*
* @deprecated Use waitsFor() instead
* @param {Number} timeout milliseconds to wait
*/
var waits = function(timeout) {
jasmine.getEnv().currentSpec.waits(timeout);
};
/**
* Waits for the latchFunction to return true before proceeding to the next runs()-defined block.
* Waits for the latchFunction to return true before proceeding to the next block.
*
* @param {Number} timeout
* @param {Function} latchFunction
* @param {String} message
* @param {String} optional_timeoutMessage
* @param {Number} optional_timeout
*/
var waitsFor = function(timeout, latchFunction, message) {
jasmine.getEnv().currentSpec.waitsFor(timeout, latchFunction, message);
var waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) {
jasmine.getEnv().currentSpec.waitsFor.apply(jasmine.getEnv().currentSpec, arguments);
};
/**
@@ -660,6 +667,7 @@ jasmine.Env = function() {
this.reporter = new jasmine.MultiReporter();
this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL;
this.defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL;
this.lastUpdate = 0;
this.specFilter = function() {
return true;
@@ -1135,7 +1143,7 @@ jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
message: message
});
this.spec.addMatcherResult(expectationResult);
return result;
return jasmine.undefined;
};
};
@@ -1153,6 +1161,7 @@ jasmine.Matchers.prototype.toBe = function(expected) {
/**
* toNotBe: compares the actual to the expected using !==
* @param expected
* @deprecated as of 1.0. Use not.toBe() instead.
*/
jasmine.Matchers.prototype.toNotBe = function(expected) {
return this.actual !== expected;
@@ -1170,6 +1179,7 @@ jasmine.Matchers.prototype.toEqual = function(expected) {
/**
* toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual
* @param expected
* @deprecated as of 1.0. Use not.toNotEqual() instead.
*/
jasmine.Matchers.prototype.toNotEqual = function(expected) {
return !this.env.equals_(this.actual, expected);
@@ -1188,6 +1198,7 @@ jasmine.Matchers.prototype.toMatch = function(expected) {
/**
* Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch
* @param expected
* @deprecated as of 1.0. Use not.toMatch() instead.
*/
jasmine.Matchers.prototype.toNotMatch = function(expected) {
return !(new RegExp(expected).test(this.actual));
@@ -1230,11 +1241,6 @@ jasmine.Matchers.prototype.toBeFalsy = function() {
};
/** @deprecated Use expect(xxx).toHaveBeenCalled() instead */
jasmine.Matchers.prototype.wasCalled = function() {
return this.toHaveBeenCalled();
};
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called.
*/
@@ -1248,12 +1254,18 @@ jasmine.Matchers.prototype.toHaveBeenCalled = function() {
}
this.message = function() {
return "Expected spy " + this.actual.identity + " to have been called.";
return [
"Expected spy " + this.actual.identity + " to have been called.",
"Expected spy " + this.actual.identity + " not to have been called."
];
};
return this.actual.wasCalled;
};
/** @deprecated Use expect(xxx).toHaveBeenCalled() instead */
jasmine.Matchers.prototype.wasCalled = jasmine.Matchers.prototype.toHaveBeenCalled;
/**
* Matcher that checks to see if the actual, a Jasmine spy, was not called.
*
@@ -1269,17 +1281,15 @@ jasmine.Matchers.prototype.wasNotCalled = function() {
}
this.message = function() {
return "Expected spy " + this.actual.identity + " to not have been called.";
return [
"Expected spy " + this.actual.identity + " to not have been called.",
"Expected spy " + this.actual.identity + " to have been called."
];
};
return !this.actual.wasCalled;
};
/** @deprecated Use expect(xxx).toHaveBeenCalledWith() instead */
jasmine.Matchers.prototype.wasCalledWith = function() {
return this.toHaveBeenCalledWith.apply(this, arguments);
};
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters.
*
@@ -1293,15 +1303,25 @@ jasmine.Matchers.prototype.toHaveBeenCalledWith = function() {
}
this.message = function() {
if (this.actual.callCount == 0) {
return "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.";
// todo: what should the failure message for .not.toHaveBeenCalledWith() be? is this right? test better. [xw]
return [
"Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.",
"Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was."
];
} else {
return "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall);
return [
"Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall),
"Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall)
];
}
};
return this.env.contains_(this.actual.argsForCall, expectedArgs);
};
/** @deprecated Use expect(xxx).toHaveBeenCalledWith() instead */
jasmine.Matchers.prototype.wasCalledWith = jasmine.Matchers.prototype.toHaveBeenCalledWith;
/** @deprecated Use expect(xxx).not.toHaveBeenCalledWith() instead */
jasmine.Matchers.prototype.wasNotCalledWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
@@ -1310,7 +1330,10 @@ jasmine.Matchers.prototype.wasNotCalledWith = function() {
}
this.message = function() {
return "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was";
return [
"Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was",
"Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was"
]
};
return !this.env.contains_(this.actual.argsForCall, expectedArgs);
@@ -1329,6 +1352,7 @@ jasmine.Matchers.prototype.toContain = function(expected) {
* Matcher that checks that the expected item is NOT an element in the actual Array.
*
* @param {Object} expected
* @deprecated as of 1.0. Use not.toNotContain() instead.
*/
jasmine.Matchers.prototype.toNotContain = function(expected) {
return !this.env.contains_(this.actual, expected);
@@ -1362,9 +1386,11 @@ jasmine.Matchers.prototype.toThrow = function(expected) {
result = (expected === jasmine.undefined || this.env.equals_(exception.message || exception, expected.message || expected));
}
var not = this.isNot ? "not " : "";
this.message = function() {
if (exception && (expected === jasmine.undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) {
return ["Expected function to throw", expected.message || expected, ", but it threw", exception.message || exception].join(' ');
return ["Expected function " + not + "to throw", expected ? expected.message || expected : " an exception", ", but it threw", exception.message || exception].join(' ');
} else {
return "Expected function to throw an exception.";
}
@@ -1644,6 +1670,7 @@ jasmine.Queue = function(env) {
this.running = false;
this.index = 0;
this.offset = 0;
this.abort = false;
};
jasmine.Queue.prototype.addBefore = function(block) {
@@ -1678,7 +1705,7 @@ jasmine.Queue.prototype.next_ = function() {
while (goAgain) {
goAgain = false;
if (self.index < self.blocks.length) {
if (self.index < self.blocks.length && !this.abort) {
var calledSynchronously = true;
var completedSynchronously = false;
@@ -1688,6 +1715,10 @@ jasmine.Queue.prototype.next_ = function() {
return;
}
if (self.blocks[self.index].abort) {
self.abort = true;
}
self.offset = 0;
self.index++;
@@ -1884,14 +1915,46 @@ jasmine.Spec.prototype.expect = function(actual) {
return positive;
};
/**
* Waits a fixed time period before moving to the next block.
*
* @deprecated Use waitsFor() instead
* @param {Number} timeout milliseconds to wait
*/
jasmine.Spec.prototype.waits = function(timeout) {
var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
this.addToQueue(waitsFunc);
return this;
};
jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) {
var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this);
/**
* Waits for the latchFunction to return true before proceeding to the next block.
*
* @param {Function} latchFunction
* @param {String} optional_timeoutMessage
* @param {Number} optional_timeout
*/
jasmine.Spec.prototype.waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) {
var latchFunction_ = null;
var optional_timeoutMessage_ = null;
var optional_timeout_ = null;
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
switch (typeof arg) {
case 'function':
latchFunction_ = arg;
break;
case 'string':
optional_timeoutMessage_ = arg;
break;
case 'number':
optional_timeout_ = arg;
break;
}
}
var waitsForFunc = new jasmine.WaitsForBlock(this.env, optional_timeout_, latchFunction_, optional_timeoutMessage_, this);
this.addToQueue(waitsForFunc);
return this;
};
@@ -2114,41 +2177,56 @@ jasmine.WaitsBlock.prototype.execute = function (onComplete) {
onComplete();
}, this.timeout);
};
/**
* A block which waits for some condition to become true, with timeout.
*
* @constructor
* @extends jasmine.Block
* @param {jasmine.Env} env The Jasmine environment.
* @param {Number} timeout The maximum time in milliseconds to wait for the condition to become true.
* @param {Function} latchFunction A function which returns true when the desired condition has been met.
* @param {String} message The message to display if the desired condition hasn't been met within the given time period.
* @param {jasmine.Spec} spec The Jasmine spec.
*/
jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) {
this.timeout = timeout;
this.timeout = timeout || env.defaultTimeoutInterval;
this.latchFunction = latchFunction;
this.message = message;
this.totalTimeSpentWaitingForLatch = 0;
jasmine.Block.call(this, env, null, spec);
};
jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block);
jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 100;
jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 10;
jasmine.WaitsForBlock.prototype.execute = function (onComplete) {
var self = this;
self.env.reporter.log('>> Jasmine waiting for ' + (self.message || 'something to happen'));
jasmine.WaitsForBlock.prototype.execute = function(onComplete) {
this.env.reporter.log('>> Jasmine waiting for ' + (this.message || 'something to happen'));
var latchFunctionResult;
try {
latchFunctionResult = self.latchFunction.apply(self.spec);
latchFunctionResult = this.latchFunction.apply(this.spec);
} catch (e) {
self.spec.fail(e);
this.spec.fail(e);
onComplete();
return;
}
if (latchFunctionResult) {
onComplete();
} else if (self.totalTimeSpentWaitingForLatch >= self.timeout) {
var message = 'timed out after ' + self.timeout + ' msec waiting for ' + (self.message || 'something to happen');
self.spec.fail({
} else if (this.totalTimeSpentWaitingForLatch >= this.timeout) {
var message = 'timed out after ' + this.timeout + ' msec waiting for ' + (this.message || 'something to happen');
this.spec.fail({
name: 'timeout',
message: message
});
this.abort = true;
onComplete();
} else {
self.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT;
self.env.setTimeout(function () { self.execute(onComplete); }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT);
this.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT;
var self = this;
this.env.setTimeout(function() {
self.execute(onComplete);
}, jasmine.WaitsForBlock.TIMEOUT_INCREMENT);
}
};
// Mock setTimeout, clearTimeout
@@ -2336,8 +2414,8 @@ jasmine.getGlobal().clearInterval = function(timeoutKey) {
jasmine.version_= {
"major": 0,
"minor": 11,
"build": 1,
"revision": 1277514571
"major": 1,
"minor": 0,
"build": 0,
"revision": 1284494074
};

2
pages

Submodule pages updated: 5e843e087b...60e240ad40

View File

@@ -29,7 +29,6 @@
<script type="text/javascript" src="../src/WaitsForBlock.js"></script>
<script type="text/javascript" src="../src/html/TrivialReporter.js"></script>
<script type="text/javascript" src="../lib/consolex.js"></script>

View File

@@ -11,15 +11,21 @@ describe("Custom Matchers", function() {
var spec1, spec2, spec1Matcher, spec2Matcher;
var suite = env.describe('some suite', function() {
env.beforeEach(function() {
this.addMatchers({ matcherForSuite: function(expected) {
return "matcherForSuite: actual: " + this.actual + "; expected: " + expected;
} });
this.addMatchers({
matcherForSuite: function(expected) {
this.message = "matcherForSuite: actual: " + this.actual + "; expected: " + expected;
return true;
}
});
});
spec1 = env.it('spec with an expectation').runs(function () {
this.addMatchers({ matcherForSpec: function(expected) {
return "matcherForSpec: actual: " + this.actual + "; expected: " + expected;
} });
this.addMatchers({
matcherForSpec: function(expected) {
this.message = "matcherForSpec: actual: " + this.actual + "; expected: " + expected;
return true;
}
});
spec1Matcher = this.expect("xxx");
});
@@ -30,10 +36,13 @@ describe("Custom Matchers", function() {
suite.execute();
expect(spec1Matcher.matcherForSuite("expected")).toEqual("matcherForSuite: actual: xxx; expected: expected");
expect(spec1Matcher.matcherForSpec("expected")).toEqual("matcherForSpec: actual: xxx; expected: expected");
spec1Matcher.matcherForSuite("expected");
expect(spec1Matcher.message).toEqual("matcherForSuite: actual: xxx; expected: expected");
spec1Matcher.matcherForSpec("expected");
expect(spec1Matcher.message).toEqual("matcherForSpec: actual: xxx; expected: expected");
expect(spec2Matcher.matcherForSuite("expected")).toEqual("matcherForSuite: actual: yyy; expected: expected");
spec2Matcher.matcherForSuite("expected");
expect(spec2Matcher.message).toEqual("matcherForSuite: actual: yyy; expected: expected");
expect(spec2Matcher.matcherForSpec).toBe(jasmine.undefined);
});

View File

@@ -10,6 +10,15 @@ describe("jasmine.Matchers", function() {
});
});
spyOn(spec, 'addMatcherResult');
this.addMatchers({
toPass: function() {
return lastResult().passed();
},
toFail: function() {
return !lastResult().passed();
}
})
});
function match(value) {
@@ -20,10 +29,19 @@ describe("jasmine.Matchers", function() {
return spec.addMatcherResult.mostRecentCall.args[0];
}
it("toEqual with primitives, objects, dates, etc.", function() {
expect(match(true).toEqual(true)).toEqual(true);
function catchException(fn) {
try {
fn.call();
} catch (e) {
return e;
}
throw new Error("expected function to throw an exception");
}
expect(match({foo:'bar'}).toEqual(null)).toEqual(false);
it("toEqual with primitives, objects, dates, etc.", function() {
expect(match(true).toEqual(true)).toPass();
expect(match({foo:'bar'}).toEqual(null)).toFail();
var functionA = function() {
return 'hi';
@@ -31,39 +49,39 @@ describe("jasmine.Matchers", function() {
var functionB = function() {
return 'hi';
};
expect(match({foo:functionA}).toEqual({foo:functionB})).toEqual(false);
expect(match({foo:functionA}).toEqual({foo:functionA})).toEqual(true);
expect(match({foo:functionA}).toEqual({foo:functionB})).toFail();
expect(match({foo:functionA}).toEqual({foo:functionA})).toPass();
expect((match(false).toEqual(true))).toEqual(false);
expect((match(false).toEqual(true))).toFail();
var circularGraph = {};
circularGraph.referenceToSelf = circularGraph;
expect((match(circularGraph).toEqual(circularGraph))).toEqual(true);
expect((match(circularGraph).toEqual(circularGraph))).toPass();
expect((match(new Date(2008, 1, 3, 15, 17, 19, 1234)).toEqual(new Date(2009, 1, 3, 15, 17, 19, 1234)))).toEqual(false);
expect((match(new Date(2008, 1, 3, 15, 17, 19, 1234)).toEqual(new Date(2008, 1, 3, 15, 17, 19, 1234)))).toEqual(true);
expect((match(new Date(2008, 1, 3, 15, 17, 19, 1234)).toEqual(new Date(2009, 1, 3, 15, 17, 19, 1234)))).toFail();
expect((match(new Date(2008, 1, 3, 15, 17, 19, 1234)).toEqual(new Date(2008, 1, 3, 15, 17, 19, 1234)))).toPass();
expect(match(true).toNotEqual(false)).toEqual(true);
expect((match(true).toNotEqual(true))).toEqual(false);
expect(match(true).toNotEqual(false)).toPass();
expect((match(true).toNotEqual(true))).toFail();
expect((match(['a', 'b']).toEqual(['a', jasmine.undefined]))).toEqual(false);
expect((match(['a', 'b']).toEqual(['a', 'b', jasmine.undefined]))).toEqual(false);
expect((match(['a', 'b']).toEqual(['a', jasmine.undefined]))).toFail();
expect((match(['a', 'b']).toEqual(['a', 'b', jasmine.undefined]))).toFail();
expect((match(new String("cat")).toEqual("cat"))).toBe(true);
expect((match(new String("cat")).toNotEqual("cat"))).toBe(false);
expect((match(new String("cat")).toEqual("cat"))).toPass();
expect((match(new String("cat")).toNotEqual("cat"))).toFail();
expect((match(new Number(5)).toEqual(5))).toBe(true);
expect((match(new Number('5')).toEqual(5))).toBe(true);
expect((match(new Number(5)).toNotEqual(5))).toBe(false);
expect((match(new Number('5')).toNotEqual(5))).toBe(false);
expect((match(new Number(5)).toEqual(5))).toPass();
expect((match(new Number('5')).toEqual(5))).toPass();
expect((match(new Number(5)).toNotEqual(5))).toFail();
expect((match(new Number('5')).toNotEqual(5))).toFail();
});
it("toEqual with DOM nodes", function() {
var nodeA = document.createElement('div');
var nodeB = document.createElement('div');
expect((match(nodeA).toEqual(nodeA))).toEqual(true);
expect((match(nodeA).toEqual(nodeB))).toEqual(false);
expect((match(nodeA).toEqual(nodeA))).toPass();
expect((match(nodeA).toEqual(nodeB))).toFail();
});
it("toEqual to build an Expectation Result", function() {
@@ -75,7 +93,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toEqual");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual));
expect(result.message).toMatch(jasmine.pp(expected));
expect(result.expected).toEqual(expected);
@@ -90,7 +108,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toNotEqual");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(str));
expect(result.message).toMatch('not');
expect(result.expected).toEqual(str);
@@ -102,12 +120,12 @@ describe("jasmine.Matchers", function() {
var b = {};
//noinspection UnnecessaryLocalVariableJS
var c = a;
expect((match(a).toBe(b))).toEqual(false);
expect((match(a).toBe(a))).toEqual(true);
expect((match(a).toBe(c))).toEqual(true);
expect((match(a).toNotBe(b))).toEqual(true);
expect((match(a).toNotBe(a))).toEqual(false);
expect((match(a).toNotBe(c))).toEqual(false);
expect((match(a).toBe(b))).toFail();
expect((match(a).toBe(a))).toPass();
expect((match(a).toBe(c))).toPass();
expect((match(a).toNotBe(b))).toPass();
expect((match(a).toNotBe(a))).toFail();
expect((match(a).toNotBe(c))).toFail();
});
it("toBe to build an ExpectationResult", function() {
@@ -119,7 +137,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toBe");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual));
expect(result.message).toMatch(jasmine.pp(expected));
expect(result.expected).toEqual(expected);
@@ -134,24 +152,24 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toNotBe");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(str);
expect(result.expected).toEqual(str);
expect(result.actual).toEqual(str);
});
it("toMatch and #toNotMatch should perform regular expression matching on strings", function() {
expect((match('foobarbel').toMatch(/bar/))).toEqual(true);
expect((match('foobazbel').toMatch(/bar/))).toEqual(false);
expect((match('foobarbel').toMatch(/bar/))).toPass();
expect((match('foobazbel').toMatch(/bar/))).toFail();
expect((match('foobarbel').toMatch("bar"))).toEqual(true);
expect((match('foobazbel').toMatch("bar"))).toEqual(false);
expect((match('foobarbel').toMatch("bar"))).toPass();
expect((match('foobazbel').toMatch("bar"))).toFail();
expect((match('foobarbel').toNotMatch(/bar/))).toEqual(false);
expect((match('foobazbel').toNotMatch(/bar/))).toEqual(true);
expect((match('foobarbel').toNotMatch(/bar/))).toFail();
expect((match('foobazbel').toNotMatch(/bar/))).toPass();
expect((match('foobarbel').toNotMatch("bar"))).toEqual(false);
expect((match('foobazbel').toNotMatch("bar"))).toEqual(true);
expect((match('foobarbel').toNotMatch("bar"))).toFail();
expect((match('foobazbel').toNotMatch("bar"))).toPass();
});
it("toMatch w/ RegExp to build an ExpectationResult", function() {
@@ -163,7 +181,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toMatch");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual));
expect(result.message).toMatch(expected.toString());
expect(result.expected).toEqual(expected);
@@ -179,7 +197,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toMatch");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toEqual("Expected 'a' to match 'b'.");
expect(result.expected).toEqual(expected);
expect(result.actual).toEqual(actual);
@@ -194,7 +212,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toNotMatch");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toEqual("Expected 'a' to not match /a/.");
expect(result.expected).toEqual(expected);
expect(result.actual).toEqual(actual);
@@ -208,15 +226,15 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toNotMatch");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toEqual("Expected 'a' to not match 'a'.");
expect(result.expected).toEqual(str);
expect(result.actual).toEqual(str);
});
it("toBeDefined", function() {
expect(match('foo').toBeDefined()).toEqual(true);
expect(match(jasmine.undefined).toBeDefined()).toEqual(false);
expect(match('foo').toBeDefined()).toPass();
expect(match(jasmine.undefined).toBeDefined()).toFail();
});
it("toBeDefined to build an ExpectationResult", function() {
@@ -226,20 +244,20 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toBeDefined");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toEqual('Expected undefined to be defined.');
expect(result.actual).toEqual(jasmine.undefined);
});
it("toBeUndefined", function() {
expect(match('foo').toBeUndefined()).toEqual(false);
expect(match(jasmine.undefined).toBeUndefined()).toEqual(true);
expect(match('foo').toBeUndefined()).toFail();
expect(match(jasmine.undefined).toBeUndefined()).toPass();
});
it("toBeNull", function() {
expect(match(null).toBeNull()).toEqual(true);
expect(match(jasmine.undefined).toBeNull()).toEqual(false);
expect(match("foo").toBeNull()).toEqual(false);
expect(match(null).toBeNull()).toPass();
expect(match(jasmine.undefined).toBeNull()).toFail();
expect(match("foo").toBeNull()).toFail();
});
it("toBeNull w/ String to build an ExpectationResult", function() {
@@ -250,7 +268,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toBeNull");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual));
expect(result.message).toMatch('null');
expect(result.actual).toEqual(actual);
@@ -264,18 +282,18 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toBeNull");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual));
expect(result.message).toMatch('null');
expect(result.actual).toEqual(actual);
});
it("toBeFalsy", function() {
expect(match(false).toBeFalsy()).toEqual(true);
expect(match(true).toBeFalsy()).toEqual(false);
expect(match(jasmine.undefined).toBeFalsy()).toEqual(true);
expect(match(0).toBeFalsy()).toEqual(true);
expect(match("").toBeFalsy()).toEqual(true);
expect(match(false).toBeFalsy()).toPass();
expect(match(true).toBeFalsy()).toFail();
expect(match(jasmine.undefined).toBeFalsy()).toPass();
expect(match(0).toBeFalsy()).toPass();
expect(match("").toBeFalsy()).toPass();
});
it("toBeFalsy to build an ExpectationResult", function() {
@@ -286,21 +304,21 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toBeFalsy");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual));
expect(result.message).toMatch('falsy');
expect(result.actual).toEqual(actual);
});
it("toBeTruthy", function() {
expect(match(false).toBeTruthy()).toEqual(false);
expect(match(true).toBeTruthy()).toEqual(true);
expect(match(jasmine.undefined).toBeTruthy()).toEqual(false);
expect(match(0).toBeTruthy()).toEqual(false);
expect(match("").toBeTruthy()).toEqual(false);
expect(match("hi").toBeTruthy()).toEqual(true);
expect(match(5).toBeTruthy()).toEqual(true);
expect(match({foo: 1}).toBeTruthy()).toEqual(true);
expect(match(false).toBeTruthy()).toFail();
expect(match(true).toBeTruthy()).toPass();
expect(match(jasmine.undefined).toBeTruthy()).toFail();
expect(match(0).toBeTruthy()).toFail();
expect(match("").toBeTruthy()).toFail();
expect(match("hi").toBeTruthy()).toPass();
expect(match(5).toBeTruthy()).toPass();
expect(match({foo: 1}).toBeTruthy()).toPass();
});
it("toBeTruthy to build an ExpectationResult", function() {
@@ -310,69 +328,69 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toBeTruthy");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toEqual("Expected false to be truthy.");
expect(result.actual).toEqual(false);
expect(result.actual).toFail();
});
it("toEqual", function() {
expect(match(jasmine.undefined).toEqual(jasmine.undefined)).toEqual(true);
expect(match({foo:'bar'}).toEqual({foo:'bar'})).toEqual(true);
expect(match("foo").toEqual({bar: jasmine.undefined})).toEqual(false);
expect(match({foo: jasmine.undefined}).toEqual("goo")).toEqual(false);
expect(match({foo: {bar :jasmine.undefined}}).toEqual("goo")).toEqual(false);
expect(match(jasmine.undefined).toEqual(jasmine.undefined)).toPass();
expect(match({foo:'bar'}).toEqual({foo:'bar'})).toPass();
expect(match("foo").toEqual({bar: jasmine.undefined})).toFail();
expect(match({foo: jasmine.undefined}).toEqual("goo")).toFail();
expect(match({foo: {bar :jasmine.undefined}}).toEqual("goo")).toFail();
});
it("toEqual with jasmine.any()", function() {
expect(match("foo").toEqual(jasmine.any(String))).toEqual(true);
expect(match(3).toEqual(jasmine.any(Number))).toEqual(true);
expect(match("foo").toEqual(jasmine.any(Function))).toEqual(false);
expect(match("foo").toEqual(jasmine.any(Object))).toEqual(false);
expect(match({someObj:'foo'}).toEqual(jasmine.any(Object))).toEqual(true);
expect(match({someObj:'foo'}).toEqual(jasmine.any(Function))).toEqual(false);
expect(match("foo").toEqual(jasmine.any(String))).toPass();
expect(match(3).toEqual(jasmine.any(Number))).toPass();
expect(match("foo").toEqual(jasmine.any(Function))).toFail();
expect(match("foo").toEqual(jasmine.any(Object))).toFail();
expect(match({someObj:'foo'}).toEqual(jasmine.any(Object))).toPass();
expect(match({someObj:'foo'}).toEqual(jasmine.any(Function))).toFail();
expect(match(function() {
}).toEqual(jasmine.any(Object))).toEqual(false);
expect(match(["foo", "goo"]).toEqual(["foo", jasmine.any(String)])).toEqual(true);
}).toEqual(jasmine.any(Object))).toFail();
expect(match(["foo", "goo"]).toEqual(["foo", jasmine.any(String)])).toPass();
expect(match(function() {
}).toEqual(jasmine.any(Function))).toEqual(true);
}).toEqual(jasmine.any(Function))).toPass();
expect(match(["a", function() {
}]).toEqual(["a", jasmine.any(Function)])).toEqual(true);
}]).toEqual(["a", jasmine.any(Function)])).toPass();
});
it("toEqual handles circular objects ok", function() {
expect(match({foo: "bar", baz: jasmine.undefined}).toEqual({foo: "bar", baz: jasmine.undefined})).toEqual(true);
expect(match({foo:['bar','baz','quux']}).toEqual({foo:['bar','baz','quux']})).toEqual(true);
expect(match({foo: {bar:'baz'}, quux:'corge'}).toEqual({foo:{bar:'baz'}, quux:'corge'})).toEqual(true);
expect(match({foo: "bar", baz: jasmine.undefined}).toEqual({foo: "bar", baz: jasmine.undefined})).toPass();
expect(match({foo:['bar','baz','quux']}).toEqual({foo:['bar','baz','quux']})).toPass();
expect(match({foo: {bar:'baz'}, quux:'corge'}).toEqual({foo:{bar:'baz'}, quux:'corge'})).toPass();
var circularObject = {};
var secondCircularObject = {};
circularObject.field = circularObject;
secondCircularObject.field = secondCircularObject;
expect(match(circularObject).toEqual(secondCircularObject)).toEqual(true);
expect(match(circularObject).toEqual(secondCircularObject)).toPass();
});
it("toNotEqual as slightly surprising behavior, but is it intentional?", function() {
expect(match({x:"x", y:"y", z:"w"}).toNotEqual({x:"x", y:"y", z:"z"})).toEqual(true);
expect(match({x:"x", y:"y", w:"z"}).toNotEqual({x:"x", y:"y", z:"z"})).toEqual(true);
expect(match({x:"x", y:"y", z:"z"}).toNotEqual({w: "w", x:"x", y:"y", z:"z"})).toEqual(true);
expect(match({w: "w", x:"x", y:"y", z:"z"}).toNotEqual({x:"x", y:"y", z:"z"})).toEqual(true);
expect(match({x:"x", y:"y", z:"w"}).toNotEqual({x:"x", y:"y", z:"z"})).toPass();
expect(match({x:"x", y:"y", w:"z"}).toNotEqual({x:"x", y:"y", z:"z"})).toPass();
expect(match({x:"x", y:"y", z:"z"}).toNotEqual({w: "w", x:"x", y:"y", z:"z"})).toPass();
expect(match({w: "w", x:"x", y:"y", z:"z"}).toNotEqual({x:"x", y:"y", z:"z"})).toPass();
});
it("toEqual handles arrays", function() {
expect(match([1, "A"]).toEqual([1, "A"])).toEqual(true);
expect(match([1, "A"]).toEqual([1, "A"])).toPass();
});
it("toContain and toNotContain", function() {
expect(match('ABC').toContain('A')).toEqual(true);
expect(match('ABC').toContain('X')).toEqual(false);
expect(match('ABC').toContain('A')).toPass();
expect(match('ABC').toContain('X')).toFail();
expect(match(['A', 'B', 'C']).toContain('A')).toEqual(true);
expect(match(['A', 'B', 'C']).toContain('F')).toEqual(false);
expect(match(['A', 'B', 'C']).toNotContain('F')).toEqual(true);
expect(match(['A', 'B', 'C']).toNotContain('A')).toEqual(false);
expect(match(['A', 'B', 'C']).toContain('A')).toPass();
expect(match(['A', 'B', 'C']).toContain('F')).toFail();
expect(match(['A', 'B', 'C']).toNotContain('F')).toPass();
expect(match(['A', 'B', 'C']).toNotContain('A')).toFail();
expect(match(['A', {some:'object'}, 'C']).toContain({some:'object'})).toEqual(true);
expect(match(['A', {some:'object'}, 'C']).toContain({some:'other object'})).toEqual(false);
expect(match(['A', {some:'object'}, 'C']).toContain({some:'object'})).toPass();
expect(match(['A', {some:'object'}, 'C']).toContain({some:'other object'})).toFail();
});
it("toContain to build an ExpectationResult", function() {
@@ -384,7 +402,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toContain");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual));
expect(result.message).toMatch('contain');
expect(result.message).toMatch(jasmine.pp(expected));
@@ -401,7 +419,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toNotContain");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual));
expect(result.message).toMatch('not contain');
expect(result.message).toMatch(jasmine.pp(expected));
@@ -410,9 +428,9 @@ describe("jasmine.Matchers", function() {
});
it("toBeLessThan should pass if actual is less than expected", function() {
expect(match(37).toBeLessThan(42)).toEqual(true);
expect(match(37).toBeLessThan(-42)).toEqual(false);
expect(match(37).toBeLessThan(37)).toEqual(false);
expect(match(37).toBeLessThan(42)).toPass();
expect(match(37).toBeLessThan(-42)).toFail();
expect(match(37).toBeLessThan(37)).toFail();
});
it("toBeLessThan to build an ExpectationResult", function() {
@@ -424,7 +442,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toBeLessThan");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual) + ' to be less than');
expect(result.message).toMatch(jasmine.pp(expected));
expect(result.actual).toEqual(actual);
@@ -432,9 +450,9 @@ describe("jasmine.Matchers", function() {
});
it("toBeGreaterThan should pass if actual is greater than expected", function() {
expect(match(37).toBeGreaterThan(42)).toEqual(false);
expect(match(37).toBeGreaterThan(-42)).toEqual(true);
expect(match(37).toBeGreaterThan(37)).toEqual(false);
expect(match(37).toBeGreaterThan(42)).toFail();
expect(match(37).toBeGreaterThan(-42)).toPass();
expect(match(37).toBeGreaterThan(37)).toFail();
});
it("toBeGreaterThan to build an ExpectationResult", function() {
@@ -446,54 +464,95 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toBeGreaterThan");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toMatch(jasmine.pp(actual) + ' to be greater than');
expect(result.message).toMatch(jasmine.pp(expected));
expect(result.actual).toEqual(actual);
expect(result.expected).toEqual(expected);
});
it("toThrow", function() {
var expected = match(function() {
throw new Error("Fake Error");
describe("toThrow", function() {
describe("when code block throws an exception", function() {
var throwingFn;
beforeEach(function() {
throwingFn = function() {
throw new Error("Fake Error");
};
});
it("should match any exception", function() {
expect(match(throwingFn).toThrow()).toPass();
});
it("should match exceptions specified by message", function() {
expect(match(throwingFn).toThrow("Fake Error")).toPass();
expect(match(throwingFn).toThrow("Other Error")).toFail();
expect(lastResult().message).toMatch("Other Error");
});
it("should match exceptions specified by Error", function() {
expect(match(throwingFn).toThrow(new Error("Fake Error"))).toPass();
expect(match(throwingFn).toThrow(new Error("Other Error"))).toFail();
expect(lastResult().message).toMatch("Other Error");
});
describe("and matcher is inverted with .not", function() {
it("should match any exception", function() {
expect(match(throwingFn).not.toThrow()).toFail();
expect(lastResult().message).toMatch(/Expected function not to throw an exception/);
});
it("should match exceptions specified by message", function() {
expect(match(throwingFn).not.toThrow("Fake Error")).toFail();
// expect(lastResult().message).toMatch(/Expected function not to throw Fake Error./);
expect(match(throwingFn).not.toThrow("Other Error")).toPass();
});
it("should match exceptions specified by Error", function() {
expect(match(throwingFn).not.toThrow(new Error("Fake Error"))).toFail();
// expect(lastResult().message).toMatch("Other Error");
expect(match(throwingFn).not.toThrow(new Error("Other Error"))).toPass();
});
});
});
expect(expected.toThrow()).toEqual(true);
expect(expected.toThrow("Fake Error")).toEqual(true);
expect(expected.toThrow(new Error("Fake Error"))).toEqual(true);
expect(expected.toThrow("Other Error")).toEqual(false);
var result = lastResult();
expect(result.message).toMatch("Other Error");
describe("when actual is not a function", function() {
it("should fail with an exception", function() {
var exception = catchException(function() {
match('not-a-function').toThrow();
});
expect(exception).toBeDefined();
expect(exception.message).toEqual('Actual is not a function');
});
expect(expected.toThrow(new Error("Other Error"))).toEqual(false);
result = lastResult();
expect(result.message).toMatch("Other Error");
var exception;
try {
(function () {
new jasmine.Matchers(env, 'not-a-function', spec).toThrow();
})();
} catch (e) {
exception = e;
}
expect(exception).toBeDefined();
expect(exception.message).toEqual('Actual is not a function');
describe("and matcher is inverted with .not", function() {
it("should fail with an exception", function() {
var exception = catchException(function() {
match('not-a-function').not.toThrow();
});
expect(exception).toBeDefined();
expect(exception.message).toEqual('Actual is not a function');
});
});
});
expect(match(function() {
}).toThrow()).toEqual(false);
result = lastResult();
expect(result.message).toEqual('Expected function to throw an exception.');
describe("when code block does not throw an exception", function() {
it("should fail (or pass when inverted with .not)", function() {
expect(match(function() {
}).toThrow()).toFail();
expect(lastResult().message).toEqual('Expected function to throw an exception.');
});
});
});
describe(".not.matcher", function() {
it("should invert the sense of any matcher", function() {
expect(match(37).not.toBeGreaterThan(42)).toEqual(true);
expect(match(42).not.toBeGreaterThan(37)).toEqual(false);
expect(match("abc").not.toEqual("def")).toEqual(true);
expect(match("abc").not.toEqual("abc")).toEqual(false);
expect(match(37).not.toBeGreaterThan(42)).toPass();
expect(match(42).not.toBeGreaterThan(37)).toFail();
expect(match("abc").not.toEqual("def")).toPass();
expect(match("abc").not.toEqual("abc")).toFail();
});
it("should provide an inverted default message", function() {
@@ -557,10 +616,10 @@ describe("jasmine.Matchers", function() {
describe("toHaveBeenCalled", function() {
it("should pass if the spy was called", function() {
expect(match(TestClass.spyFunction).toHaveBeenCalled()).toEqual(false);
expect(match(TestClass.spyFunction).toHaveBeenCalled()).toFail();
TestClass.spyFunction();
expect(match(TestClass.spyFunction).toHaveBeenCalled()).toEqual(true);
expect(match(TestClass.spyFunction).toHaveBeenCalled()).toPass();
});
it("should throw an exception when invoked with any arguments", function() {
@@ -585,10 +644,10 @@ describe("jasmine.Matchers", function() {
describe("wasNotCalled", function() {
it("should pass iff the spy was not called", function() {
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(true);
expect(match(TestClass.spyFunction).wasNotCalled()).toPass();
TestClass.spyFunction();
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(false);
expect(match(TestClass.spyFunction).wasNotCalled()).toFail();
});
it("should throw an exception when invoked with any arguments", function() {
@@ -603,15 +662,15 @@ describe("jasmine.Matchers", function() {
describe("toHaveBeenCalledWith", function() {
it('toHaveBeenCalledWith should return true if it was called with the expected args', function() {
TestClass.spyFunction('a', 'b', 'c');
expect(match(TestClass.spyFunction).toHaveBeenCalledWith('a', 'b', 'c')).toEqual(true);
expect(match(TestClass.spyFunction).toHaveBeenCalledWith('a', 'b', 'c')).toPass();
});
it('should return false if it was not called with the expected args', function() {
TestClass.spyFunction('a', 'b', 'c');
var expected = match(TestClass.spyFunction);
expect(expected.toHaveBeenCalledWith('c', 'b', 'a')).toEqual(false);
expect(expected.toHaveBeenCalledWith('c', 'b', 'a')).toFail();
var result = lastResult();
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.expected).toEqual(['c', 'b', 'a']);
expect(result.actual.mostRecentCall.args).toEqual(['a', 'b', 'c']);
expect(result.message).toContain(jasmine.pp(result.expected));
@@ -620,21 +679,37 @@ describe("jasmine.Matchers", function() {
it('should return false if it was not called', function() {
var expected = match(TestClass.spyFunction);
expect(expected.toHaveBeenCalledWith('c', 'b', 'a')).toEqual(false);
expect(expected.toHaveBeenCalledWith('c', 'b', 'a')).toFail();
var result = lastResult();
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.expected).toEqual(['c', 'b', 'a']);
expect(result.actual.argsForCall).toEqual([]);
expect(result.message).toContain(jasmine.pp(result.expected));
});
it('should allow matches across multiple calls', function() {
var expected = match(TestClass.spyFunction);
TestClass.spyFunction('a', 'b', 'c');
TestClass.spyFunction('d', 'e', 'f');
expect(expected.toHaveBeenCalledWith('a', 'b', 'c')).toEqual(true);
expect(expected.toHaveBeenCalledWith('d', 'e', 'f')).toEqual(true);
expect(expected.toHaveBeenCalledWith('x', 'y', 'z')).toEqual(false);
var expected = match(TestClass.spyFunction);
expect(expected.toHaveBeenCalledWith('a', 'b', 'c')).toPass();
expect(expected.toHaveBeenCalledWith('d', 'e', 'f')).toPass();
expect(expected.toHaveBeenCalledWith('x', 'y', 'z')).toFail();
});
it("should return a decent message", function() {
TestClass.spyFunction('a', 'b', 'c');
TestClass.spyFunction('d', 'e', 'f');
var expected = match(TestClass.spyFunction);
expect(expected.toHaveBeenCalledWith('a', 'b')).toFail();
expect(lastResult().message).toEqual("Expected spy to have been called with [ 'a', 'b' ] but was called with [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ]");
});
it("should return a decent message when inverted", function() {
TestClass.spyFunction('a', 'b', 'c');
TestClass.spyFunction('d', 'e', 'f');
var expected = match(TestClass.spyFunction);
expect(expected.not.toHaveBeenCalledWith('a', 'b', 'c')).toFail();
expect(lastResult().message).toEqual("Expected spy not to have been called with [ 'a', 'b', 'c' ] but was called with [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ]");
});
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('toHaveBeenCalledWith'));
@@ -658,7 +733,7 @@ describe("jasmine.Matchers", function() {
var result = lastResult();
expect(result.matcherName).toEqual("toHaveBeenCalledWith");
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.message).toContain(jasmine.pp(['a', 'b']));
expect(result.message).toContain(jasmine.pp(['a', 'c']));
expect(result.actual).toEqual(TestClass.someFunction);
@@ -680,15 +755,15 @@ describe("jasmine.Matchers", function() {
describe("wasNotCalledWith", function() {
it('should return true if the spy was NOT called with the expected args', function() {
TestClass.spyFunction('a', 'b', 'c');
expect(match(TestClass.spyFunction).wasNotCalledWith('c', 'b', 'a')).toEqual(true);
expect(match(TestClass.spyFunction).wasNotCalledWith('c', 'b', 'a')).toPass();
});
it('should return false if it WAS called with the expected args', function() {
TestClass.spyFunction('a', 'b', 'c');
var expected = match(TestClass.spyFunction);
expect(expected.wasNotCalledWith('a', 'b', 'c')).toEqual(false);
expect(expected.wasNotCalledWith('a', 'b', 'c')).toFail();
var result = lastResult();
expect(result.passed()).toEqual(false);
expect(result.passed()).toFail();
expect(result.expected).toEqual(['a', 'b', 'c']);
expect(result.actual.mostRecentCall.args).toEqual(['a', 'b', 'c']);
expect(result.message).toContain(jasmine.pp(result.expected));
@@ -696,21 +771,25 @@ describe("jasmine.Matchers", function() {
it('should return true if it was not called', function() {
var expected = match(TestClass.spyFunction);
expect(expected.wasNotCalledWith('c', 'b', 'a')).toEqual(true);
expect(expected.wasNotCalledWith('c', 'b', 'a')).toPass();
});
it('should allow matches across multiple calls', function() {
var expected = match(TestClass.spyFunction);
TestClass.spyFunction('a', 'b', 'c');
TestClass.spyFunction('d', 'e', 'f');
expect(expected.wasNotCalledWith('a', 'b', 'c')).toEqual(false);
expect(expected.wasNotCalledWith('d', 'e', 'f')).toEqual(false);
expect(expected.wasNotCalledWith('x', 'y', 'z')).toEqual(true);
expect(expected.wasNotCalledWith('a', 'b', 'c')).toFail();
expect(expected.wasNotCalledWith('d', 'e', 'f')).toFail();
expect(expected.wasNotCalledWith('x', 'y', 'z')).toPass();
});
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasNotCalledWith'));
});
});
describe("all matchers", function() {
it("should return null, for future-proofing, since we might eventually allow matcher chaining", function() {
expect(match(true).toBe(true)).toBeUndefined();
});
});
});

View File

@@ -258,86 +258,146 @@ describe("jasmine spec running", function () {
expect(another_spec.results().getItems()[0].passed()).toEqual(true);
});
it("testWaitsFor", function() {
var doneWaiting = false;
var runsBlockExecuted = false;
describe("waitsFor", function() {
var latchFunction = function() {
return true;
};
var spec;
env.describe('foo', function() {
spec = env.it('has a waits for', function() {
this.runs(function() {
});
this.waitsFor(500, function() {
return doneWaiting;
});
this.runs(function() {
runsBlockExecuted = true;
function makeWaitsForSpec() {
var args = jasmine.util.argsToArray(arguments);
env.describe('suite', function() {
spec = env.it('spec', function() {
this.waitsFor.apply(this, args);
});
});
env.execute();
}
it("should accept args (latchFunction, timeoutMessage, timeout)", function() {
makeWaitsForSpec(latchFunction, "message", 123);
var block = spec.queue.blocks[1];
expect(block.latchFunction).toBe(latchFunction);
expect(block.timeout).toEqual(123);
expect(block.message).toEqual('message');
});
spec.execute();
expect(runsBlockExecuted).toEqual(false); //, 'should not have executed runs block yet');
fakeTimer.tick(100);
doneWaiting = true;
fakeTimer.tick(100);
expect(runsBlockExecuted).toEqual(true); //, 'should have executed runs block');
});
it("should accept args (latchFunction, timeout)", function() {
makeWaitsForSpec(latchFunction, 123);
var block = spec.queue.blocks[1];
expect(block.latchFunction).toBe(latchFunction);
expect(block.timeout).toEqual(123);
expect(block.message).toEqual(null);
});
it("testWaitsForFailsWithMessage", function() {
var spec;
env.describe('foo', function() {
spec = env.it('has a waits for', function() {
this.runs(function() {
});
it("should accept args (latchFunction, timeoutMessage)", function() {
env.defaultTimeoutInterval = 4321;
makeWaitsForSpec(latchFunction, "message");
var block = spec.queue.blocks[1];
expect(block.latchFunction).toBe(latchFunction);
expect(block.timeout).toEqual(4321);
expect(block.message).toEqual('message');
});
this.waitsFor(500, function() {
return false; // force a timeout
}, 'my awesome condition');
it("should accept args (latchFunction)", function() {
env.defaultTimeoutInterval = 4321;
makeWaitsForSpec(latchFunction);
var block = spec.queue.blocks[1];
expect(block.latchFunction).toBe(latchFunction);
expect(block.timeout).toEqual(4321);
expect(block.message).toEqual(null);
});
this.runs(function() {
it("should accept deprecated args order (timeout, latchFunction, timeoutMessage)", function() {
makeWaitsForSpec(123, latchFunction, "message");
var block = spec.queue.blocks[1];
expect(block.latchFunction).toBe(latchFunction);
expect(block.timeout).toEqual(123);
expect(block.message).toEqual('message');
});
it("testWaitsFor", function() {
var doneWaiting = false;
var runsBlockExecuted = false;
var spec;
env.describe('foo', function() {
spec = env.it('has a waits for', function() {
this.runs(function() {
});
this.waitsFor(500, function() {
return doneWaiting;
});
this.runs(function() {
runsBlockExecuted = true;
});
});
});
spec.execute();
expect(runsBlockExecuted).toEqual(false); //, 'should not have executed runs block yet');
fakeTimer.tick(100);
doneWaiting = true;
fakeTimer.tick(100);
expect(runsBlockExecuted).toEqual(true); //, 'should have executed runs block');
});
spec.execute();
fakeTimer.tick(1000);
var actual = spec.results().getItems()[0].message;
var expected = 'timeout: timed out after 500 msec waiting for my awesome condition';
expect(actual).toEqual(expected);
});
it("fails with message", function() {
var spec;
env.describe('foo', function() {
spec = env.it('has a waits for', function() {
this.runs(function() {
});
it("waitsFor fails and skips the rest of the spec if timeout is reached and the latch function is still false", function() {
var runsBlockExecuted = false;
this.waitsFor(500, function() {
return false; // force a timeout
}, 'my awesome condition');
var spec;
env.describe('foo', function() {
spec = env.it('has a waits for', function() {
this.runs(function() {
});
this.waitsFor(500, function() {
return false;
});
this.runs(function() {
runsBlockExecuted = true;
this.runs(function() {
});
});
});
spec.execute();
fakeTimer.tick(1000);
expect(spec.results().getItems()[0].message).toEqual('timeout: timed out after 500 msec waiting for my awesome condition');
});
spec.execute();
expect(runsBlockExecuted).toEqual(false);
fakeTimer.tick(100);
expect(runsBlockExecuted).toEqual(false);
fakeTimer.tick(400);
expect(runsBlockExecuted).toEqual(false);
var actual = spec.results().getItems()[0].message;
var expected = 'timeout: timed out after 500 msec waiting for something to happen';
expect(actual).toEqual(expected,
'expected "' + expected + '" but found "' + actual + '"');
it("fails and skips the rest of the spec if timeout is reached and the latch function hasn't returned true", function() {
var runsBlockExecuted = false;
var subsequentSpecRan = false;
var timeoutSpec, subsequentSpec;
var suite = env.describe('foo', function() {
timeoutSpec = env.it('has a waits for', function() {
this.runs(function() {
});
this.waitsFor(500, function() {
return false;
});
this.runs(function() {
runsBlockExecuted = true;
});
});
subsequentSpec = env.it('then carries on to the next test', function() {
subsequentSpecRan = true;
});
});
env.execute();
expect(runsBlockExecuted).toEqual(false);
fakeTimer.tick(100);
expect(runsBlockExecuted).toEqual(false);
fakeTimer.tick(400);
expect(runsBlockExecuted).toEqual(false);
expect(timeoutSpec.results().getItems()[0].message).toEqual('timeout: timed out after 500 msec waiting for something to happen');
expect(subsequentSpecRan).toEqual(true);
});
});
it("testSpecAfter", function() {
@@ -520,17 +580,15 @@ describe("jasmine spec running", function () {
});
describe('#waitsFor should allow consecutive calls', function () {
var foo;
beforeEach(function () {
foo = 0;
});
it('exits immediately (does not stack) if the latchFunction times out', function () {
var reachedFirstWaitsFor = false;
var reachedSecondWaitsFor = false;
var waitsSuite = env.describe('suite that waits', function () {
env.describe('suite that waits', function () {
env.it('should stack timeouts', function() {
this.waitsFor(500, function () {
reachedFirstWaitsFor = true;
@@ -546,7 +604,7 @@ describe("jasmine spec running", function () {
});
expect(reachedFirstWaitsFor).toEqual(false);
waitsSuite.execute();
env.execute();
expect(reachedFirstWaitsFor).toEqual(true);
expect(foo).toEqual(0);

View File

@@ -15,7 +15,7 @@ describe('WaitsForBlock', function () {
return true;
};
var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
expect(onComplete).wasNotCalled();
expect(onComplete).not.toHaveBeenCalled();
block.execute(onComplete);
expect(onComplete).toHaveBeenCalled();
});
@@ -51,22 +51,22 @@ describe('WaitsForBlock', function () {
env.clearInterval = fakeTimer.clearInterval;
});
it('latchFunction should be retried after 100 ms', function () {
it('latchFunction should be retried after 10 ms', function () {
var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
expect(latchFunction).wasNotCalled();
expect(latchFunction).not.toHaveBeenCalled();
block.execute(onComplete);
expect(latchFunction.callCount).toEqual(1);
fakeTimer.tick(50);
fakeTimer.tick(5);
expect(latchFunction.callCount).toEqual(1);
fakeTimer.tick(50);
fakeTimer.tick(5);
expect(latchFunction.callCount).toEqual(2);
});
it('onComplete should be called if latchFunction returns true before timeout', function () {
var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
expect(onComplete).wasNotCalled();
expect(onComplete).not.toHaveBeenCalled();
block.execute(onComplete);
expect(onComplete).wasNotCalled();
expect(onComplete).not.toHaveBeenCalled();
latchFunction.andReturn(true);
fakeTimer.tick(100);
expect(onComplete).toHaveBeenCalled();
@@ -76,12 +76,12 @@ describe('WaitsForBlock', function () {
spyOn(spec, 'fail');
var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
block.execute(onComplete);
expect(spec.fail).wasNotCalled();
expect(spec.fail).not.toHaveBeenCalled();
fakeTimer.tick(timeout);
expect(spec.fail).toHaveBeenCalled();
var failMessage = spec.fail.mostRecentCall.args[0].message;
expect(failMessage).toMatch(message);
expect(onComplete).wasNotCalled();
expect(onComplete).toHaveBeenCalled();
});
});
});

View File

@@ -11,6 +11,7 @@ jasmine.Env = function() {
this.reporter = new jasmine.MultiReporter();
this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL;
this.defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL;
this.lastUpdate = 0;
this.specFilter = function() {
return true;

View File

@@ -68,7 +68,7 @@ jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
message: message
});
this.spec.addMatcherResult(expectationResult);
return result;
return jasmine.undefined;
};
};
@@ -86,6 +86,7 @@ jasmine.Matchers.prototype.toBe = function(expected) {
/**
* toNotBe: compares the actual to the expected using !==
* @param expected
* @deprecated as of 1.0. Use not.toBe() instead.
*/
jasmine.Matchers.prototype.toNotBe = function(expected) {
return this.actual !== expected;
@@ -103,6 +104,7 @@ jasmine.Matchers.prototype.toEqual = function(expected) {
/**
* toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual
* @param expected
* @deprecated as of 1.0. Use not.toNotEqual() instead.
*/
jasmine.Matchers.prototype.toNotEqual = function(expected) {
return !this.env.equals_(this.actual, expected);
@@ -121,6 +123,7 @@ jasmine.Matchers.prototype.toMatch = function(expected) {
/**
* Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch
* @param expected
* @deprecated as of 1.0. Use not.toMatch() instead.
*/
jasmine.Matchers.prototype.toNotMatch = function(expected) {
return !(new RegExp(expected).test(this.actual));
@@ -163,11 +166,6 @@ jasmine.Matchers.prototype.toBeFalsy = function() {
};
/** @deprecated Use expect(xxx).toHaveBeenCalled() instead */
jasmine.Matchers.prototype.wasCalled = function() {
return this.toHaveBeenCalled();
};
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called.
*/
@@ -181,12 +179,18 @@ jasmine.Matchers.prototype.toHaveBeenCalled = function() {
}
this.message = function() {
return "Expected spy " + this.actual.identity + " to have been called.";
return [
"Expected spy " + this.actual.identity + " to have been called.",
"Expected spy " + this.actual.identity + " not to have been called."
];
};
return this.actual.wasCalled;
};
/** @deprecated Use expect(xxx).toHaveBeenCalled() instead */
jasmine.Matchers.prototype.wasCalled = jasmine.Matchers.prototype.toHaveBeenCalled;
/**
* Matcher that checks to see if the actual, a Jasmine spy, was not called.
*
@@ -202,17 +206,15 @@ jasmine.Matchers.prototype.wasNotCalled = function() {
}
this.message = function() {
return "Expected spy " + this.actual.identity + " to not have been called.";
return [
"Expected spy " + this.actual.identity + " to not have been called.",
"Expected spy " + this.actual.identity + " to have been called."
];
};
return !this.actual.wasCalled;
};
/** @deprecated Use expect(xxx).toHaveBeenCalledWith() instead */
jasmine.Matchers.prototype.wasCalledWith = function() {
return this.toHaveBeenCalledWith.apply(this, arguments);
};
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters.
*
@@ -226,15 +228,25 @@ jasmine.Matchers.prototype.toHaveBeenCalledWith = function() {
}
this.message = function() {
if (this.actual.callCount == 0) {
return "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.";
// todo: what should the failure message for .not.toHaveBeenCalledWith() be? is this right? test better. [xw]
return [
"Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.",
"Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was."
];
} else {
return "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall);
return [
"Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall),
"Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall)
];
}
};
return this.env.contains_(this.actual.argsForCall, expectedArgs);
};
/** @deprecated Use expect(xxx).toHaveBeenCalledWith() instead */
jasmine.Matchers.prototype.wasCalledWith = jasmine.Matchers.prototype.toHaveBeenCalledWith;
/** @deprecated Use expect(xxx).not.toHaveBeenCalledWith() instead */
jasmine.Matchers.prototype.wasNotCalledWith = function() {
var expectedArgs = jasmine.util.argsToArray(arguments);
@@ -243,7 +255,10 @@ jasmine.Matchers.prototype.wasNotCalledWith = function() {
}
this.message = function() {
return "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was";
return [
"Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was",
"Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was"
]
};
return !this.env.contains_(this.actual.argsForCall, expectedArgs);
@@ -262,6 +277,7 @@ jasmine.Matchers.prototype.toContain = function(expected) {
* Matcher that checks that the expected item is NOT an element in the actual Array.
*
* @param {Object} expected
* @deprecated as of 1.0. Use not.toNotContain() instead.
*/
jasmine.Matchers.prototype.toNotContain = function(expected) {
return !this.env.contains_(this.actual, expected);
@@ -295,9 +311,11 @@ jasmine.Matchers.prototype.toThrow = function(expected) {
result = (expected === jasmine.undefined || this.env.equals_(exception.message || exception, expected.message || expected));
}
var not = this.isNot ? "not " : "";
this.message = function() {
if (exception && (expected === jasmine.undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) {
return ["Expected function to throw", expected.message || expected, ", but it threw", exception.message || exception].join(' ');
return ["Expected function " + not + "to throw", expected ? expected.message || expected : " an exception", ", but it threw", exception.message || exception].join(' ');
} else {
return "Expected function to throw an exception.";
}

View File

@@ -4,6 +4,7 @@ jasmine.Queue = function(env) {
this.running = false;
this.index = 0;
this.offset = 0;
this.abort = false;
};
jasmine.Queue.prototype.addBefore = function(block) {
@@ -38,7 +39,7 @@ jasmine.Queue.prototype.next_ = function() {
while (goAgain) {
goAgain = false;
if (self.index < self.blocks.length) {
if (self.index < self.blocks.length && !this.abort) {
var calledSynchronously = true;
var completedSynchronously = false;
@@ -48,6 +49,10 @@ jasmine.Queue.prototype.next_ = function() {
return;
}
if (self.blocks[self.index].abort) {
self.abort = true;
}
self.offset = 0;
self.index++;

View File

@@ -73,14 +73,46 @@ jasmine.Spec.prototype.expect = function(actual) {
return positive;
};
/**
* Waits a fixed time period before moving to the next block.
*
* @deprecated Use waitsFor() instead
* @param {Number} timeout milliseconds to wait
*/
jasmine.Spec.prototype.waits = function(timeout) {
var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
this.addToQueue(waitsFunc);
return this;
};
jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) {
var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this);
/**
* Waits for the latchFunction to return true before proceeding to the next block.
*
* @param {Function} latchFunction
* @param {String} optional_timeoutMessage
* @param {Number} optional_timeout
*/
jasmine.Spec.prototype.waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) {
var latchFunction_ = null;
var optional_timeoutMessage_ = null;
var optional_timeout_ = null;
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
switch (typeof arg) {
case 'function':
latchFunction_ = arg;
break;
case 'string':
optional_timeoutMessage_ = arg;
break;
case 'number':
optional_timeout_ = arg;
break;
}
}
var waitsForFunc = new jasmine.WaitsForBlock(this.env, optional_timeout_, latchFunction_, optional_timeoutMessage_, this);
this.addToQueue(waitsForFunc);
return this;
};

View File

@@ -1,37 +1,52 @@
/**
* A block which waits for some condition to become true, with timeout.
*
* @constructor
* @extends jasmine.Block
* @param {jasmine.Env} env The Jasmine environment.
* @param {Number} timeout The maximum time in milliseconds to wait for the condition to become true.
* @param {Function} latchFunction A function which returns true when the desired condition has been met.
* @param {String} message The message to display if the desired condition hasn't been met within the given time period.
* @param {jasmine.Spec} spec The Jasmine spec.
*/
jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) {
this.timeout = timeout;
this.timeout = timeout || env.defaultTimeoutInterval;
this.latchFunction = latchFunction;
this.message = message;
this.totalTimeSpentWaitingForLatch = 0;
jasmine.Block.call(this, env, null, spec);
};
jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block);
jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 100;
jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 10;
jasmine.WaitsForBlock.prototype.execute = function (onComplete) {
var self = this;
self.env.reporter.log('>> Jasmine waiting for ' + (self.message || 'something to happen'));
jasmine.WaitsForBlock.prototype.execute = function(onComplete) {
this.env.reporter.log('>> Jasmine waiting for ' + (this.message || 'something to happen'));
var latchFunctionResult;
try {
latchFunctionResult = self.latchFunction.apply(self.spec);
latchFunctionResult = this.latchFunction.apply(this.spec);
} catch (e) {
self.spec.fail(e);
this.spec.fail(e);
onComplete();
return;
}
if (latchFunctionResult) {
onComplete();
} else if (self.totalTimeSpentWaitingForLatch >= self.timeout) {
var message = 'timed out after ' + self.timeout + ' msec waiting for ' + (self.message || 'something to happen');
self.spec.fail({
} else if (this.totalTimeSpentWaitingForLatch >= this.timeout) {
var message = 'timed out after ' + this.timeout + ' msec waiting for ' + (this.message || 'something to happen');
this.spec.fail({
name: 'timeout',
message: message
});
this.abort = true;
onComplete();
} else {
self.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT;
self.env.setTimeout(function () { self.execute(onComplete); }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT);
this.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT;
var self = this;
this.env.setTimeout(function() {
self.execute(onComplete);
}, jasmine.WaitsForBlock.TIMEOUT_INCREMENT);
}
};

View File

@@ -21,11 +21,16 @@ jasmine.unimplementedMethod_ = function() {
jasmine.undefined = jasmine.___undefined___;
/**
* Default interval for event loop yields. Small values here may result in slow test running. Zero means no updates until all tests have completed.
* Default interval in milliseconds for event loop yields (e.g. to allow network activity or to refresh the screen with the HTML-based runner). Small values here may result in slow test running. Zero means no updates until all tests have completed.
*
*/
jasmine.DEFAULT_UPDATE_INTERVAL = 250;
/**
* Default timeout interval in milliseconds for waitsFor() blocks.
*/
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
jasmine.getGlobal = function() {
function getGlobal() {
return this;
@@ -490,22 +495,24 @@ var runs = function(func) {
};
/**
* Waits for a timeout before moving to the next runs()-defined block.
* @param {Number} timeout
* Waits a fixed time period before moving to the next block.
*
* @deprecated Use waitsFor() instead
* @param {Number} timeout milliseconds to wait
*/
var waits = function(timeout) {
jasmine.getEnv().currentSpec.waits(timeout);
};
/**
* Waits for the latchFunction to return true before proceeding to the next runs()-defined block.
* Waits for the latchFunction to return true before proceeding to the next block.
*
* @param {Number} timeout
* @param {Function} latchFunction
* @param {String} message
* @param {String} optional_timeoutMessage
* @param {Number} optional_timeout
*/
var waitsFor = function(timeout, latchFunction, message) {
jasmine.getEnv().currentSpec.waitsFor(timeout, latchFunction, message);
var waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) {
jasmine.getEnv().currentSpec.waitsFor.apply(jasmine.getEnv().currentSpec, arguments);
};
/**

View File

@@ -34,7 +34,7 @@ jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) {
this.outerDiv = this.createDom('div', { className: 'jasmine_reporter' },
this.createDom('div', { className: 'banner' },
this.createDom('div', { className: 'logo' },
"Jasmine",
this.createDom('a', { href: 'http://pivotal.github.com/jasmine/', target: "_blank" }, "Jasmine"),
this.createDom('span', { className: 'version' }, runner.env.versionString())),
this.createDom('div', { className: 'options' },
"Show ",

View File

@@ -1,5 +1,5 @@
{
"major": 0,
"minor": 11,
"build": 1
"major": 1,
"minor": 0,
"build": 0
}