Skip to content

Commit

Permalink
Added infinity methods, with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teobais committed Mar 26, 2017
1 parent 7f8f2b5 commit 08f046c
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
31 changes: 31 additions & 0 deletions spec/core/matchers/toBeNegativeInfinitySpec.js
@@ -0,0 +1,31 @@
describe("toBeNegativeInfinity", function() {
it("fails for anything that isn't -Infinity", function() {
var matcher = jasmineUnderTest.matchers.toBeNegativeInfinity(),
result;

result = matcher.compare(1);
expect(result.pass).toBe(false);

result = matcher.compare(Number.NaN);
expect(result.pass).toBe(false);

result = matcher.compare(null);
expect(result.pass).toBe(false);
});

it("has a custom message on failure", function() {
var matcher = jasmineUnderTest.matchers.toBeNegativeInfinity(),
result = matcher.compare(0);

expect(result.message()).toEqual("Expected 0 not to be -Infinity.")
});

it("succeeds for -Infinity", function() {
var matcher = jasmineUnderTest.matchers.toBeNegativeInfinity(),
result = matcher.compare(Number.NEGATIVE_INFINITY);

expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected actual to be -Infinity.")
});

});
31 changes: 31 additions & 0 deletions spec/core/matchers/toBePositiveInfinitySpec.js
@@ -0,0 +1,31 @@
describe("toBePositiveInfinity", function() {
it("fails for anything that isn't Infinity", function() {
var matcher = jasmineUnderTest.matchers.toBePositiveInfinity(),
result;

result = matcher.compare(1);
expect(result.pass).toBe(false);

result = matcher.compare(Number.NaN);
expect(result.pass).toBe(false);

result = matcher.compare(null);
expect(result.pass).toBe(false);
});

it("has a custom message on failure", function() {
var matcher = jasmineUnderTest.matchers.toBePositiveInfinity(),
result = matcher.compare(0);

expect(result.message()).toEqual("Expected 0 not to be Infinity.")
});

it("succeeds for Infinity", function() {
var matcher = jasmineUnderTest.matchers.toBePositiveInfinity(),
result = matcher.compare(Number.POSITIVE_INFINITY);

expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected actual to be Infinity.")
});

});
6 changes: 4 additions & 2 deletions src/core/matchers/requireMatchers.js
Expand Up @@ -6,18 +6,20 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) {
'toBeFalsy',
'toBeGreaterThan',
'toBeGreaterThanOrEqual',
'toBeLessThanOrEqual',
'toBeLessThan',
'toBeLessThanOrEqual',
'toBeNaN',
'toBeNegativeInfinity',
'toBeNull',
'toBePositiveInfinity',
'toBeTruthy',
'toBeUndefined',
'toContain',
'toEqual',
'toHaveBeenCalled',
'toHaveBeenCalledBefore',
'toHaveBeenCalledWith',
'toHaveBeenCalledTimes',
'toHaveBeenCalledWith',
'toMatch',
'toThrow',
'toThrowError'
Expand Down
28 changes: 28 additions & 0 deletions src/core/matchers/toBeNegativeInfinity.js
@@ -0,0 +1,28 @@
getJasmineRequireObj().toBeNegativeInfinity = function(j$) {
/**
* {@link expect} the actual value to be `-Infinity` (-infinity).
* @function
* @name matchers#toBeNegativeInfinity
* @example
* expect(thing).toBeNegativeInfinity();
*/
function toBeNegativeInfinity() {
return {
compare: function(actual) {
var result = {
pass: (actual === Number.NEGATIVE_INFINITY)
};

if (result.pass) {
result.message = 'Expected actual to be -Infinity.';
} else {
result.message = function() { return 'Expected ' + j$.pp(actual) + ' not to be -Infinity.'; };
}

return result;
}
};
}

return toBeNegativeInfinity;
};
28 changes: 28 additions & 0 deletions src/core/matchers/toBePositiveInfinity.js
@@ -0,0 +1,28 @@
getJasmineRequireObj().toBePositiveInfinity = function(j$) {
/**
* {@link expect} the actual value to be `Infinity` (infinity).
* @function
* @name matchers#toBePositiveInfinity
* @example
* expect(thing).toBePositiveInfinity();
*/
function toBePositiveInfinity() {
return {
compare: function(actual) {
var result = {
pass: (actual === Number.POSITIVE_INFINITY)
};

if (result.pass) {
result.message = 'Expected actual to be Infinity.';
} else {
result.message = function() { return 'Expected ' + j$.pp(actual) + ' not to be Infinity.'; };
}

return result;
}
};
}

return toBePositiveInfinity;
};

0 comments on commit 08f046c

Please sign in to comment.