1 var JS
= this.JS
|| require('jstest');
2 var yT
= this.yT
|| require('yuu/yT');
4 JS
.Test
.describe('yT', function() { with (this) {
6 it("makes Object subclasses", function () { with (this) {
7 var A
= yT(Object
, {});
8 assertKindOf(Object
, new A());
10 it("makes Object subclasses from a prototype", function () { with (this) {
11 var A
= yT(Object
.prototype, {});
12 assertKindOf(Object
, new A());
14 it("makes a non-Object subclass", function () { with (this) {
17 assertKindOf("object", a
);
18 assertNot(a
instanceof Object
);
20 it("makes Object subclasses by default", function () { with (this) {
22 assertKindOf(Object
, new A());
24 it("makes nested subclasses", function () { with (this) {
27 assertKindOf(Object
, new B());
28 assertKindOf(A
, new B());
29 assertKindOf(B
, new B());
31 it("makes nested subclasses from a prototype", function () { with (this) {
33 var B
= yT(A
.prototype, {});
34 assertKindOf(Object
, new B());
35 assertKindOf(A
, new B());
36 assertKindOf(B
, new B());
39 it("sets constructors", function () { with (this) {
41 var A
= yT({ constructor: ctor
});
43 assertSame(A
, a
.constructor);
44 assertSame(ctor
, a
.constructor);
46 it("auto-creates root constructors", function () { with (this) {
49 assertSame(A
, a
.constructor);
51 it("auto-creates parent-calling constructors", function () { with (this) {
52 var A
= yT({ constructor: function (x
, y
) { this.i
= x
+ y
; } });
56 assertSame(B
, b
.constructor);
57 assertNotSame(A
, b
.constructor);
60 it("creates data descriptors", function () { with (this) {
61 var A
= yT({ X
: { value
: 1 } });
65 assertEqual(1, a
.X
, "X should not be writable");
67 it("creates writable data descriptors", function () { with (this) {
68 var A
= yT({ x
: { value
: 1, writable
: true } });
74 it("creates bare value descriptors", function () { with (this) {
79 assertEqual(1, a
.X
, "X should not be writable");
81 it("creates read-only aliases", function () { with (this) {
82 var A
= yT({ a
: { alias
: "b", readonly
: true } });
85 assertEqual("hello", a
.a
);
87 assertEqual("world", a
.a
);
89 assertEqual("world", a
.a
);
90 assertEqual("world", a
.b
);
92 it("creates read-write aliases", function () { with (this) {
93 var A
= yT({ a
: { alias
: "b", readonly
: false } });
96 assertEqual("hello", a
.a
);
98 assertEqual("world", a
.a
);
100 assertEqual("goodbye", a
.a
);
101 assertEqual("goodbye", a
.b
);
103 it("creates read-write aliases by default", function () { with (this) {
104 var A
= yT({ a
: { alias
: "b" } });
107 assertEqual("hello", a
.a
);
109 assertEqual("world", a
.a
);
111 assertEqual("goodbye", a
.a
);
112 assertEqual("goodbye", a
.b
);
116 JS
.Test
.describe('an example Point type', function() { with (this) {
118 constructor: function (x
, y
) {
123 x
: { value
: 0, chainable
: true },
124 y
: { value
: 0, chainable
: true },
127 xy
: { swizzle
: "xy" },
128 yx
: { swizzle
: "yx" },
133 return Math
.atan2(this.y
, this.x
);
135 set: function (angle
) {
136 var magnitude
= this.magnitude
;
137 this.x
= Math
.cos(angle
) * magnitude
;
138 this.y
= Math
.sin(angle
) * magnitude
;
145 return Math
.sqrt(this.x
* this.x
+ this.y
* this.y
);
147 set: function (magnitude
) {
148 var angle
= this.angle
;
149 this.x
= Math
.cos(angle
) * magnitude
;
150 this.y
= Math
.sin(angle
) * magnitude
;
157 it("constructs 0, 0 by default", function () { with (this) {
161 assertEqual(2, p
.length
);
165 it("has chainable setters", function () { with (this) {
166 var p
= new Point().setX(1).setY(2);
171 it("x is also 0", function () { with (this) {
174 assertEqual(0, p
[0]);
177 assertEqual(1, p
[0]);
180 assertEqual(2, p
[0]);
182 it("y is also 1", function () { with (this) {
185 assertEqual(0, p
[1]);
188 assertEqual(1, p
[1]);
191 assertEqual(2, p
[1]);
194 it("x and y have swizzling", function () { with (this) {
196 assertEqual([0, 0], p
.xy
);
197 assertEqual([0, 0], p
.yx
);
199 assertEqual([1, 0], p
.xy
);
200 assertEqual([0, 1], p
.yx
);
202 assertEqual([0, 1], p
.xy
);
203 assertEqual([1, 0], p
.yx
);
206 it("can change magnitude", function () { with (this) {
207 var p
= new Point(1, 0);
208 assertEqual(1, p
.magnitude
);
210 assertEqual([10, 0], p
.xy
);
212 it("can change angle", function () { with (this) {
214 var p
= new Point(1, 0);
215 assertEqual(0, p
.angle
);
216 p
.angle
= Math
.PI
/ 2;
217 assertInDelta(Math
.PI
/ 2, p
.angle
, delta
);
218 assertInDelta(0, p
.x
, delta
);
219 assertInDelta(1, p
.y
, delta
);
220 p
.angle
= Math
.PI
/ 4;
221 assertInDelta(Math
.PI
/ 4, p
.angle
, delta
);
222 assertInDelta(Math
.sqrt(2) / 2, p
.x
, delta
);
223 assertInDelta(Math
.sqrt(2) / 2, p
.y
, delta
);
224 assertInDelta(p
.x
, p
.y
, delta
);