Skip to content
Browse files

cactus to Cactus, and update module pattern.

  • Loading branch information...
1 parent b6d51e4 commit 226351b5b76b0df91f7c37098da53957d794efff Winston Teo committed Mar 19, 2012
Showing with 50 additions and 48 deletions.
  1. +11 −11 spec/javascripts/cactusSpec.js
  2. +39 −37 vendor/assets/javascripts/cactus.js
View
22 spec/javascripts/cactusSpec.js
@@ -1,15 +1,15 @@
-describe("cactus", function() {
+describe("Cactus", function() {
var expectation;
describe("css verification for one element", function() {
describe("expect", function() {
beforeEach(function() {
- expectation = cactus.expect(".banner", "display");
+ expectation = Cactus.expect(".banner", "display");
});
it("returns cactus object for chaining", function() {
- expect(expectation).toBe(cactus);
+ expect(expectation).toBe(Cactus);
});
it("sets variables", function() {
@@ -23,7 +23,7 @@ describe("cactus", function() {
describe("matchers", function() {
describe("toEqual", function() {
beforeEach(function() {
- expectation = cactus.expect(".banner", "display");
+ expectation = Cactus.expect(".banner", "display");
});
it("is equal", function() {
@@ -39,7 +39,7 @@ describe("cactus", function() {
describe("toContain", function() {
beforeEach(function() {
- expectation = cactus.expect(".banner", "display");
+ expectation = Cactus.expect(".banner", "display");
});
it("is equal", function() {
@@ -55,7 +55,7 @@ describe("cactus", function() {
describe("toHaveColor", function() {
beforeEach(function() {
- expectation = cactus.expect(".banner", "background-color");
+ expectation = Cactus.expect(".banner", "background-color");
});
it("is equal (case insensitive)", function() {
@@ -74,11 +74,11 @@ describe("cactus", function() {
describe("css verification for all elements", function() {
describe("expectEvery", function() {
beforeEach(function() {
- expectation = cactus.expectEvery("label", "text-align");
+ expectation = Cactus.expectEvery("label", "text-align");
});
it("returns cactus object for chaining", function() {
- expect(expectation).toBe(cactus);
+ expect(expectation).toBe(Cactus);
});
it("sets variables", function() {
@@ -92,7 +92,7 @@ describe("cactus", function() {
describe("matchers", function() {
describe("toEqual", function() {
beforeEach(function() {
- expectation = cactus.expectEvery("label", "text-align");
+ expectation = Cactus.expectEvery("label", "text-align");
});
it("is equal", function() {
@@ -108,7 +108,7 @@ describe("cactus", function() {
describe("toContain", function() {
beforeEach(function() {
- expectation = cactus.expect("label", "text-align");
+ expectation = Cactus.expect("label", "text-align");
});
it("is equal", function() {
@@ -124,7 +124,7 @@ describe("cactus", function() {
describe("toHaveColor", function() {
beforeEach(function() {
- expectation = cactus.expectEvery("label", "color");
+ expectation = Cactus.expectEvery("label", "color");
});
it("is equal (case insensitive)", function() {
View
76 vendor/assets/javascripts/cactus.js
@@ -1,58 +1,50 @@
-var cactus = (function() {
+var Cactus = (function() {
+ // Public Accessor
+ var _cactus = {};
+
+ // Private Variables
var tag_name = null;
var property = null;
var styles = null;
- function debug() {
+
+ // Debug
+ _cactus.debug = function() {
return {
tag_name : tag_name,
property : property,
styles : styles
};
- }
+ };
- function expect(elem, attr) {
+ // Expectations
+ _cactus.expect = function(elem, attr) {
tag_name = elem;
property = attr
styles = [ $(tag_name).css(property) ];
return this;
- }
+ };
- function expectEvery(elem, attr) {
+ _cactus.expectEvery = function(elem, attr) {
tag_name = elem;
property = attr
styles = $.map( $(tag_name), function(elem, i) { return $(elem).css(property); } );
return this;
- }
-
- function expectationResult(computed, expected, comparator) {
- // Defaults to equality comparator
- if (comparator === undefined) { comparator = function(x, y) { return x === y; }; }
-
- var result = true;
- $.each(computed, function(index, style) {
- if (!comparator(style, expected)) {
- jq_selector = "$('" + tag_name + "')" + "[" + index + "]";
- console.log("Cactus expected " + jq_selector + " " + property + " to equal " + expected + ", but got " + style + " instead.");
- result = result && false;
- }
- });
-
- return result;
- }
+ };
- function toEqual(expected_style) {
+ // Matchers
+ _cactus.toEqual = function(expected_style) {
return expectationResult(styles, expected_style);
- }
+ };
- function toContain(expected_style) {
+ _cactus.toContain = function(expected_style) {
return expectationResult(styles, expected_style, function(x, y) { return x.match(y); });
- }
+ };
- function toHaveColor(expected_style) {
+ _cactus.toHaveColor = function(expected_style) {
// Source: http://stackoverflow.com/questions/1740700/get-hex-value-rather-than-rgb-value-using-jquery
function rgb2hex(rgb) {
function hex(x) { return ("0" + parseInt(x).toString(16)).slice(-2); }
@@ -65,15 +57,25 @@ var cactus = (function() {
expected = expected_style.toLowerCase();
return expectationResult(computed, expected);
- }
-
- return {
- debug : debug,
- expect : expect,
- expectEvery : expectEvery,
- toEqual : toEqual,
- toContain : toContain,
- toHaveColor : toHaveColor
};
+ function expectationResult(computed, expected, comparator) {
+ // Defaults to equality comparator
+ if (comparator === undefined) { comparator = function(x, y) { return x === y; }; }
+
+ var result = true;
+ $.each(computed, function(index, style) {
+ if (!comparator(style, expected)) {
+ jq_selector = "$('" + tag_name + "')" + "[" + index + "]";
+ console.log("Cactus expected " + jq_selector + " " + property + " to equal " + expected + ", but got " + style + " instead.");
+ result = result && false;
+ }
+ });
+
+ return result;
+ };
+
+ // Return Accessor
+ return _cactus;
+
})();

0 comments on commit 226351b

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