Repository navigation
Expand file tree
/
Copy pathconstruct_test.js
More file actions
167 lines (135 loc) · 3.6 KB
/
Copy pathconstruct_test.js
File metadata and controls
167 lines (135 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
steal('can/util', 'can/construct', function(can, Construct) {
module("can/construct",{
setup : function(){
var Animal = this.Animal = Construct({
count: 0,
test: function() {
return this.match ? true : false
}
},
{
init: function() {
this.constructor.count++;
this.eyes = false;
}
});
var Dog = this.Dog = this.Animal(
{
match : /abc/
},
{
init: function() {
Animal.prototype.init.apply(this, arguments);
},
talk: function() {
return "Woof";
}
});
this.Ajax = this.Dog({
count : 0
},
{
init: function( hairs ) {
Dog.prototype.init.apply(this, arguments);
this.hairs = hairs;
this.setEyes();
},
setEyes: function() {
this.eyes = true;
}
});
}
});
test("inherit", function(){
var Base = Construct({});
ok(new Base instanceof Construct)
var Inherit = Base({});
ok(new Inherit instanceof Base);
})
test("Creating", function(){
new this.Dog();
var a1 = new this.Animal();
new this.Animal();
var ajax = new this.Ajax(1000);
equals(2, this.Animal.count, "right number of animals");
equals(1, this.Dog.count, "right number of animals")
ok(this.Dog.match, "right number of animals")
ok(!this.Animal.match, "right number of animals")
ok(this.Dog.test(), "right number of animals")
ok(!this.Animal.test(), "right number of animals")
equals(1, this.Ajax.count, "right number of animals")
equals(2, this.Animal.count, "right number of animals");
equals(true, ajax.eyes, "right number of animals");
equals(1000, ajax.hairs, "right number of animals");
ok(a1 instanceof this.Animal)
ok(a1 instanceof Construct)
})
test("new instance",function(){
var d = this.Ajax.newInstance(6);
equals(6, d.hairs);
})
test("namespaces",function(){
var Todo = Construct("Todo",{},{})
var fb = Construct.extend("Foo.Bar")
ok(Foo.Bar === fb, "returns class")
equals(fb.shortName, "Bar", "short name is right");
equals(fb.fullName, "Foo.Bar","fullName is right")
})
test("setups", function(){
var order = 0,
staticSetup,
staticSetupArgs,
staticInit,
staticInitArgs,
protoSetup,
protoInitArgs,
protoInit,
staticProps = {
setup: function() {
staticSetup = ++order;
staticSetupArgs = arguments;
return ["something"]
},
init: function() {
staticInit = ++order;
staticInitArgs = arguments;
}
},
protoProps = {
setup: function( name ) {
protoSetup = ++order;
return ["Ford: "+name];
},
init: function() {
protoInit = ++order;
protoInitArgs = arguments;
}
}
Construct.extend("Car",staticProps,protoProps);
var geo = new Car("geo");
equals(staticSetup, 1);
equals(staticInit, 2);
equals(protoSetup, 3);
equals(protoInit, 4);
same(can.makeArray(staticInitArgs), ["something"] )
same(can.makeArray(protoInitArgs),["Ford: geo"] )
same(can.makeArray(staticSetupArgs),[Construct, "Car",staticProps, protoProps] ,"static construct");
//now see if staticSetup gets called again ...
Car.extend("Truck");
equals(staticSetup, 5, "Static setup is called if overwriting");
});
test("Creating without extend", function(){
Construct("Bar",{
ok : function(){
ok(true, "ok called")
}
});
new Bar().ok();
Bar("Foo",{
dude : function(){
ok(true, "dude called")
}
});
new Foo().dude(true);
});
});