Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toBeGreaterThan matcher #1

Merged
merged 2 commits into from Sep 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions spec/javascripts/cactusSpec.js
Expand Up @@ -40,6 +40,28 @@ describe("Cactus", function() {
// Reset
$("label:first").css("text-align", "right");
});

describe("toBeGreaterThan", function() {
beforeEach(function() {
$("label").css("font-size", "12px");
});

it("returns true when result is true for all matched elements", function() {
var result = Cactus.expect("label", "font-size").toBeGreaterThan("10px");
expect(result).toBe(true);
});

it("returns false when result is false for one of the matched elements", function() {
// Setup
$("label:first").css("font-size", "9px");

var result = Cactus.expect("label", "font-size").toBeGreaterThan("10px");
expect(result).toBe(false);

// Reset
$("label").css("font-size", "12px");
});
});
});

describe("toContain", function() {
Expand Down
10 changes: 7 additions & 3 deletions vendor/assets/javascripts/cactus.js
Expand Up @@ -35,6 +35,10 @@ Cactus = (function() {
return compare(styles, expected_style, function(x, y) { return x === y; });
};

_cactus.toBeGreaterThan = function(expected_style) {
return compare(styles, expected_style, function(x, y) { return parseInt(x) > parseInt(y); }, "exceed");
};

_cactus.toContain = function(expected_style) {
return compare(styles, expected_style, function(x, y) { return x.match(y) ? true : false; });
};
Expand Down Expand Up @@ -86,13 +90,13 @@ Cactus = (function() {
return str;
}

function compare(computed, expected, comparator) {
function compare(computed, expected, comparator, operation) {
var result = true, status, message;

if ($(tag_name).is("*")) {
$.each(computed, function(index, style) {
status = comparator(style, expected);
message = "Expected " + selector(index) + " " + property + " to equal " + expected + ". Got " + style + ".";
message = "Expected " + selector(index) + " " + property + " to " + (operation ? operation : "equal") + " " + expected + ". Got " + style + ".";

result = result && status;

Expand All @@ -101,7 +105,7 @@ Cactus = (function() {
});
} else {
status = "skip";
message = "Expected " + selector() + " " + property + " to equal " + expected + ".";
message = "Expected " + selector() + " " + property + " to " + (operation ? operation : "equal") + " " + expected + ".";

result = status;

Expand Down