GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: A JavaScript BDD Testing Library
Homepage: http://www.yehudakatz.com
Clone URL: git://github.com/wycats/jspec.git
Boooo... some necessary changes.
wycats (author)
Thu Jan 17 00:51:01 -0800 2008
commit  7860e59d803f330f3c3955eae7cde9e7994bfda9
tree    0f1bbd0354ffa21c3e0e28419e001e76c0003a1e
parent  faaae412902604f844dc18fc52b0ff17727aabaf
...
27
28
29
30
 
31
32
 
 
33
34
 
35
36
37
38
39
40
 
41
42
43
44
45
46
 
47
48
49
50
51
 
 
 
52
53
54
55
56
57
 
 
58
59
60
...
65
66
67
 
 
 
68
69
70
...
75
76
77
78
79
 
 
80
81
82
 
 
83
84
 
85
86
 
87
88
 
89
90
91
92
93
94
 
 
95
96
97
 
 
98
99
100
101
102
103
 
 
104
105
106
107
108
109
 
 
110
111
 
112
113
114
115
 
 
116
117
118
...
27
28
29
 
30
31
 
32
33
34
 
35
36
37
38
39
40
 
41
42
43
44
45
46
 
47
48
 
 
 
 
49
50
51
52
53
 
 
 
 
54
55
56
57
58
...
63
64
65
66
67
68
69
70
71
...
76
77
78
 
 
79
80
81
 
 
82
83
84
 
85
86
 
87
88
 
89
90
91
92
93
 
 
94
95
96
 
 
97
98
99
100
101
102
 
 
103
104
105
106
107
108
 
 
109
110
111
 
112
113
114
 
 
115
116
117
118
119
0
@@ -27,34 +27,32 @@ jspec = {
0
     jspec.logger(jspec.TOP_LEVEL, str);
0
     var it = function(str, fn) {
0
       jspec.logger(jspec.DESCRIBE, str);
0
- fn.call();
0
+ fn();
0
     };
0
- Object.prototype.should = function(fn_str, to_compare, not) {
0
+ var Expectation = function(p) { this.expectation = p; };
0
+ Expectation.prototype.to = function(fn_str, to_compare, not) {
0
      try {
0
- var pass = jspec.matchers[fn_str].matches.call(this, to_compare);
0
+ var pass = jspec.matchers[fn_str].matches(this.expectation, to_compare);
0
         if(not) var pass = !pass;
0
       } catch(e) {
0
        var pass = null;
0
       }
0
       var should_string = (jspec.matchers[fn_str].describe &&
0
- jspec.matchers[fn_str].describe.call(this, to_compare, not)) ||
0
+ jspec.matchers[fn_str].describe(this.expectation, to_compare, not)) ||
0
        this.toString() + " should " + (not ? "not " : "") + fn_str + " " + to_compare;
0
       if(pass) {
0
         jspec.logger(jspec.IT_SHOULD, should_string + " (PASS)");
0
       }  else {
0
         jspec.logger(jspec.IT_SHOULD, should_string + (pass == false ? " (FAIL)" : " (ERROR)"));
0
- jspec.logger(jspec.FAILURE, jspec.matchers[fn_str].failure_message.call(this, to_compare, not))
0
+ jspec.logger(jspec.FAILURE, jspec.matchers[fn_str].failure_message(this.expectation, to_compare, not))
0
       }
0
- };
0
- Object.prototype.should_not = function(fn_str, to_compare) {
0
- this.should(fn_str, to_compare, true);
0
- };
0
+ }
0
+ Expectation.prototype.not_to = function(fn_str, to_compare) { this.to(fn_str, to_compare, true) }
0
+ var expect = function(p) { return new Expectation(p) };
0
     x = desc.toString()
0
     var fn_body = this.fn_contents(desc);
0
- var fn = new Function("it", fn_body);
0
- fn.call(this, it);
0
- delete Object.prototype.should
0
- delete Object.prototype.should_not
0
+ var fn = new Function("it", "expect", fn_body);
0
+ fn.call(this, it, expect);
0
   }
0
 }
0
 
0
@@ -65,6 +63,9 @@ jspec.print_object = function(obj) {
0
     return obj.toString().match(/^([^\{]*) {/)[1];
0
   } else if(obj instanceof Array) {
0
     return "[" + obj.toString() + "]";
0
+ } else if(obj instanceof HTMLElement) {
0
+ return "<" + obj.tagName + " " + (obj.className != "" ? "class='" + obj.className + "'" : "") +
0
+ (obj.id != "" ? "id='" + obj.id + "'" : "") + ">";
0
   } else {
0
     return obj.toString().replace(/\n\s*/g, "");
0
   }
0
@@ -75,43 +76,43 @@ jspec.print_object = function(obj) {
0
 jspec.matchers = {};
0
 
0
 jspec.matchers["=="] = {
0
- describe: function(target, not) {
0
- return jspec.print_object(this) + " should " + (not ? "not " : "") + "equal " + jspec.print_object(target)
0
+ describe: function(self, target, not) {
0
+ return jspec.print_object(self) + " should " + (not ? "not " : "") + "equal " + jspec.print_object(target)
0
   },
0
- matches: function(target) {
0
- return this == target;
0
+ matches: function(self, target) {
0
+ return self == target;
0
   },
0
- failure_message: function(target, not) {
0
+ failure_message: function(self, target, not) {
0
     if (not)
0
- return "Expected " + jspec.print_object(this) + " not to equal " + jspec.print_object(target);
0
+ return "Expected " + jspec.print_object(self) + " not to equal " + jspec.print_object(target);
0
     else
0
- return "Expected " + jspec.print_object(this) + ". Got " + jspec.print_object(target);
0
+ return "Expected " + jspec.print_object(self) + ". Got " + jspec.print_object(target);
0
   }
0
 }
0
 
0
 jspec.matchers["include"] = {
0
- matches: function(target) {
0
- if(Array.prototype.indexOf) return Array.prototype.indexOf.call(this, target) != -1;
0
+ matches: function(self, target) {
0
+ if(Array.prototype.indexOf) return Array.prototype.indexOf.call(self, target) != -1;
0
     else {
0
- for(i=0,j=this.length;i<j;i++) {
0
- if(target == this[i]) return true;
0
+ for(i=0,j=self.length;i<j;i++) {
0
+ if(target == self[i]) return true;
0
       }
0
       return false;
0
     }
0
   },
0
- failure_message: function(target, not) {
0
- return "Expected [" + jspec.print_object(this) + "] " + (not ? "not " : "") + "to include " + target;
0
+ failure_message: function(self, target, not) {
0
+ return "Expected [" + jspec.print_object(self) + "] " + (not ? "not " : "") + "to include " + target;
0
   }
0
 }
0
 
0
 jspec.matchers["exist"] = {
0
- describe: function(target, not) {
0
- return jspec.print_object(this) + " should " + (not ? "not " : "") + "exist."
0
+ describe: function(self, target, not) {
0
+ return jspec.print_object(self) + " should " + (not ? "not " : "") + "exist."
0
   },
0
- matches: function(target) {
0
+ matches: function(self, target) {
0
     return !!this;
0
   },
0
- failure_message: function(target, not) {
0
- return "Expected " + (not ? "not " : "") + "to exist, but was " + jspec.print_object(this);
0
+ failure_message: function(self, target, not) {
0
+ return "Expected " + (not ? "not " : "") + "to exist, but was " + jspec.print_object(self);
0
   }
0
 }
0
\ No newline at end of file
...
12
13
14
15
 
16
17
 
18
19
20
 
 
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
52
53
...
12
13
14
 
15
16
 
17
18
 
 
19
20
21
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
0
@@ -12,42 +12,42 @@
0
     window.onload = function() {
0
       jspec.describe("JSpec", function() {
0
         it("should support ==", function() {
0
- (1).should("==", 1);
0
+ expect(1).to("==", 1);
0
           var arr = [];
0
- arr.should("==", arr);
0
+ expect(arr).to("==", arr);
0
           var obj = new Object;
0
- obj.should("==", obj);
0
- document.should("==", document);
0
+ expect(obj).to("==", obj);
0
+ expect(document).to("==", document);
0
         });
0
       
0
- it("should support include", function() {
0
- [1,2,3,4,5].should("include", 3);
0
- [1,2,3,4,5].should_not("include", 3);
0
- document.getElementsByTagName("div").should("include", document.getElementById("hello"))
0
- });
0
-
0
- it("should support exists", function() {
0
- document.should("exist");
0
- });
0
-
0
- jspec.matchers["have_tag_name"] = {
0
- describe: function(target, not) {
0
- return jspec.print_object(this) + " should " + (not ? "not " : "") + "have " + target + " as its tag name."
0
- },
0
- matches: function(target) {
0
- return (this.tagName && this.tagName == target) ? true : false;
0
- },
0
- failure_message: function(target, not) {
0
- return "Expected " + this.toString() + (not ? " not " : " ") + "to have " + target + " as its tag name," +
0
- " but was " + this.tagName;
0
- }
0
- };
0
-
0
- it("should support custom matchers", function() {
0
- document.getElementById("wrapper").should("have_tag_name", "DIV");
0
- document.getElementById("wrapper").should_not("have_tag_name", "SPAN");
0
- document.getElementById("wrapper").should("have_tag_name", "SPAN");
0
- });
0
+ it("should support include", function() {
0
+ expect([1,2,3,4,5]).to("include", 3);
0
+ expect([1,2,3,4,5]).not_to("include", 3);
0
+ expect(document.getElementsByTagName("div")).to("include", document.getElementById("hello"))
0
+ });
0
+
0
+ it("should support exists", function() {
0
+ expect(document).to("exist");
0
+ });
0
+
0
+ jspec.matchers["have_tag_name"] = {
0
+ describe: function(self, target, not) {
0
+ return jspec.print_object(self) + " should " + (not ? "not " : "") + "have " + target + " as its tag name."
0
+ },
0
+ matches: function(self, target) {
0
+ return (self.tagName && self.tagName == target) ? true : false;
0
+ },
0
+ failure_message: function(self, target, not) {
0
+ return "Expected " + jspec.print_object(self) + (not ? " not " : " ") + "to have " + target + " as its tag name," +
0
+ " but was " + self.tagName;
0
+ }
0
+ };
0
+
0
+ it("should support custom matchers", function() {
0
+ expect(document.getElementById("wrapper")).to("have_tag_name", "DIV");
0
+ expect(document.getElementById("wrapper")).to("have_tag_name", "SPAN");
0
+ expect(document.getElementById("wrapper")).not_to("have_tag_name", "SPAN");
0
+ });
0
       });
0
     };
0
   </script>  

Comments

    No one has commented yet.