Skip to content
Browse files

Add a basic is_similar matcher with specs

  • Loading branch information...
1 parent aee5c07 commit b4904db9dea759e70b05831ea0f54358d4b9551c @leto committed Apr 12, 2010
Showing with 24 additions and 6 deletions.
  1. +16 −6 lib/hump.matchers.js
  2. +8 −0 spec/matchers_spec.js
View
22 lib/hump.matchers.js
@@ -46,12 +46,23 @@ Hump.Matchers = (function($) {
return 'expected ' + $.print(actual) + (not ? ' to not equal ' : ' to equal ') + $.print(expected);
}
},
-
+
+ be_similar: {
+ match: function(expected, actual) {
+ var eps = 1e-3;
+ return Math.abs(actual - expected) < eps;
+ },
+
+ failure_message: function(expected, actual, not) {
+ return 'expected ' + $.print(actual) + (not ? ' to not ' : ' to ') + 'be similar to ' + $.print(expected);
+ }
+ },
+
be_gt: {
match: function(expected, actual) {
return actual > expected;
},
-
+
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not ' : ' to ') + 'be greater than ' + $.print(expected);
}
@@ -61,7 +72,7 @@ Hump.Matchers = (function($) {
match: function(expected, actual) {
return actual >= expected;
},
-
+
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not ' : ' to ') + 'be greater than or equal to ' + $.print(expected);
}
@@ -71,7 +82,7 @@ Hump.Matchers = (function($) {
match: function(expected, actual) {
return actual < expected;
},
-
+
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not ' : ' to ') + 'be less than ' + $.print(expected);
}
@@ -81,7 +92,7 @@ Hump.Matchers = (function($) {
match: function(expected, actual) {
return actual <= expected;
},
-
+
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not ' : ' to ') + 'be less than or equal to ' + $.print(expected);
}
@@ -223,7 +234,6 @@ Hump.Matchers = (function($) {
return 'expected ' + $.print(actual) + (not ? ' to not throw error ' : ' to throw error ') + $.print(expected);
}
},
-
throw_error_matching: {
match: function(expected, actual) {
try {
View
8 spec/matchers_spec.js
@@ -9,6 +9,14 @@ Hump.Unit(function() {
expect(a).to_not(eq, b);
});
});
+ describe("#similar", function() {
+ it("compares numbers that are similar", function() {
+
+ expect(1).to(be_similar, 1);
+ expect(1).to(be_similar, 1.0001);
+ expect(1).to_not(be_similar, 1.1);
+ });
+ });
describe('#equal', function() {
it("invokes the provided matcher on a call to expect", function() {
expect(true).to(equal, true);

0 comments on commit b4904db

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