Permalink
Browse files

Fixed error messages for new'd objects & errors

  • Loading branch information...
1 parent 122f4b9 commit 1f2443920bcfa3883efcd96974ab754b69d0a7a9 @spmason committed May 4, 2012
Showing with 44 additions and 6 deletions.
  1. +1 −0 .gitignore
  2. +8 −0 expectations.sublime-project
  3. +13 −6 index.js
  4. +22 −0 test/expect.tests.js
View
@@ -1 +1,2 @@
node_modules
+*.sublime-workspace
@@ -0,0 +1,8 @@
+{
+ "folders":
+ [
+ {
+ "path": "/home/stevem/Development/expectations"
+ }
+ ]
+}
View
@@ -23,12 +23,12 @@
})(this, function(root, AssertionError) {
'use strict';
var assertions = {
- pass: function(message){
- },
- fail: function(message){
- throw new AssertionError({message: message});
- }
- };
+ pass: function(message){
+ },
+ fail: function(message){
+ throw new AssertionError({message: message});
+ }
+ };
function formatValue(value, ignoreUndefined, stack){
if(typeof value === 'undefined'){
@@ -55,6 +55,13 @@
stack = stack || [];
if(typeof value === 'object' && stack.indexOf(value) === -1 && stack.length < 5){
+ if(value.toString() !== '[object Object]'){
+ if(value instanceof Error){
+ return '[Error: ' + value.toString() + ']';
+ }
+ return '[' + value.toString() + ']';
+ }
+
return '{' + Object.keys(value).map(function(key){
return ['"', key, '": ', formatValue(value[key], false, stack.concat(value))].join('');
}).join(', ') + '}';
View
@@ -194,6 +194,28 @@
}
}
});
+ it('can generate correct message for Function with custom toString()', function(){
+ function Obj(){}
+ Obj.prototype.toString = function(){
+ return "testing";
+ };
+ try{
+ expect(new Obj()).not.toBeDefined();
+ }catch(err){
+ if(err.message !== 'expected [testing] not to be defined'){
+ throw new Error('Expected error message is not correct: ' + err.message);
+ }
+ }
+ });
+ it('can generate correct message for Errors', function(){
+ try{
+ expect(new Error('text')).not.toBeDefined();
+ }catch(err){
+ if(err.message !== 'expected [Error: text] not to be defined'){
+ throw new Error('Expected error message is not correct: ' + err.message);
+ }
+ }
+ });
it('can generate correct message for objects with circular references', function(){
try{
var obj = {abc: 'def'};

0 comments on commit 1f24439

Please sign in to comment.