Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/tests/typedObjects.js

Issue 5656302898380800: Issue 260 - [Typed objects] Implement type inheritance (Closed)
Patch Set: Renamed isinstance to isInstance Created May 2, 2014, 7:13 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/typedObjects/utils.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 }); 127 });
128 ok(type, "Type created"); 128 ok(type, "Type created");
129 129
130 let obj = type(4, 12.5); 130 let obj = type(4, 12.5);
131 equal(obj.foo, 4); 131 equal(obj.foo, 4);
132 equal(obj.bar, 12.5); 132 equal(obj.bar, 12.5);
133 }); 133 });
134 134
135 test("Object references", function() 135 test("Object references", function()
136 { 136 {
137 let {ObjectType, uint8} = require("typedObjects"); 137 let {ObjectType, ObjectBase, uint8} = require("typedObjects");
138 let type1 = new ObjectType({ 138 let type1 = new ObjectType({
139 foo: uint8 139 foo: uint8
140 }); 140 });
141 let type2 = new ObjectType({ 141 let type2 = new ObjectType({
142 bar: type1 142 bar: type1
143 }); 143 });
144 ok(type1 && type2, "Types created");
145 144
146 let obj1 = type1(); 145 let obj1 = type1();
147 let obj2 = type2(); 146 let obj2 = type2();
148 ok(obj1 && obj2, "Objects created");
149 147
150 obj2.bar = obj1; 148 obj2.bar = obj1;
151 ok(obj2.bar, "Object reference set"); 149 ok(obj1.equals(obj2.bar), "Object reference set");
152 equal(obj2.bar.typeId, obj1.typeId); 150
153 equal(obj2.bar.bufferIndex, obj1.bufferIndex); 151 let type3 = type1.extend({
154 equal(obj2.bar.byteOffset, obj1.byteOffset); 152 anyref: ObjectBase
153 });
154 let obj3 = type3();
155 obj2.bar = obj3;
156 ok(obj3.equals(obj2.bar), "Object reference set to a subclass");
157
158 throws(function()
159 {
160 obj2.bar = obj2;
161 }, "Object reference cannot be set to an unrelated class");
162 ok(obj3.equals(obj2.bar), "Object reference keeps value after unsuccessful a ssignment");
163
164 obj3.anyref = obj3;
165 ok(obj3.anyref.equals(obj3), "Assigned object itself to ObjectBase-typed ref erence");
166
167 obj3.anyref = obj2;
168 ok(obj3.anyref.equals(obj2), "Assigned object of unrelated type to ObjectBas e-typed reference");
155 169
156 obj2.bar = null; 170 obj2.bar = null;
157 ok(!obj2.bar, "Object reference unset"); 171 ok(!obj2.bar, "Object reference unset");
158 172
159 let obj3 = type2(); 173 let obj4 = type2();
160 obj3.bar = obj1; 174 obj4.bar = obj1;
161 ok(obj3.bar, "Object reference set on new object"); 175 ok(obj1.equals(obj4.bar), "Object reference set on new object");
162 equal(obj3.bar.typeId, obj1.typeId); 176 ok(!obj2.bar, "Reference on original object still unset");
163 equal(obj3.bar.bufferIndex, obj1.bufferIndex); 177 });
164 equal(obj3.bar.byteOffset, obj1.byteOffset); 178
165 ok(!obj2.bar); 179 test("Object equality", function()
180 {
181 let {ObjectType, uint8, float32} = require("typedObjects");
182
183 let type1 = new ObjectType({
184 foo: uint8
185 }, {bufferSize: 2});
186 let type2 = new ObjectType({
187 bar: type1
188 });
189
190 let obj1 = type1();
191 let obj2 = type2();
192 ok(obj1.equals(obj1), "Object equal to itself");
193 ok(obj2.equals(obj2), "Object equal to itself");
194 ok(!obj1.equals(obj2), "Object not equal to object of another type");
195 ok(!obj2.equals(obj1), "Object not equal to object of another type");
196
197 let obj3 = type1();
198 ok(!obj1.equals(obj3), "Object not equal to another object of same type in s ame buffer");
199 ok(!obj3.equals(obj1), "Object not equal to another object of same type in s ame buffer");
200
201 let obj4 = type1();
202 ok(!obj1.equals(obj4), "Object not equal to another object of same type at s ame offset");
203 ok(!obj4.equals(obj1), "Object not equal to another object of same type at s ame offset");
204
205 obj2.bar = obj1;
206 ok(obj1.equals(obj2.bar), "Object equal to reference to itself");
207 ok(obj2.bar.equals(obj1), "Object equal to reference to itself");
208 ok(obj2.bar.equals(obj2.bar), "Object reference equals to itself");
209
210 let obj5 = type2();
211 obj5.bar = null;
212 ok(!obj2.bar.equals(obj5.bar), "Object reference not equal to null reference ");
213
214 obj5.bar = obj3;
215 ok(!obj2.bar.equals(obj5.bar), "Object reference not equal to reference to a nother object")
216 ok(!obj5.bar.equals(obj2.bar), "Object reference not equal to reference to a nother object")
217 });
218
219 test("Object inheritance", function()
220 {
221 let {ObjectType, uint8, float32} = require("typedObjects");
222
223 // Property inheritance
224 let type1 = new ObjectType({
225 foo: uint8
226 });
227 let type2 = type1.extend({
228 bar: float32
229 });
230
231 let obj1 = type1();
232 let obj2 = type2();
233 ok("foo" in obj1, "Superclass property exists in superclass");
234 ok(!("bar" in obj1), "Subclass property doesn't exist in superclass");
235 ok("foo" in obj2, "Superclass property exists in subclass");
236 ok("bar" in obj2, "Subclass property exists in subclass");
237
238 ok(type1.isInstance(obj1), "Object is recognized as instance of its class");
239 ok(type1.isInstance(obj2), "Object is recognized as instance of its supercla ss");
240 ok(!type2.isInstance(obj1), "Object isn't an instance of its subclass");
241 ok(type2.isInstance(obj2), "Object is recognized as instance of its class");
242
243 // Method and constructor inheritance
244 let type3 = new ObjectType({
245 x: uint8,
246 foo: function(n) {return this.x * n;}
247 }, {
248 constructor: function(x)
249 {
250 this.x = x;
251 }
252 });
253 let type4 = type3.extend({
254 foo: function(super_, n) {return super_(n + 1);},
255 bar: function() {return 4;}
256 }, {
257 constructor: function(super_, x)
258 {
259 super_(x);
260 this.x *= 3;
261 }
262 });
263
264 let obj3 = type3(2);
265 let obj4 = type4(2);
266 equal(obj3.x, 2, "Superclass constructor executed correctly");
267 equal(obj4.x, 6, "Subclass constructor executed correctly");
268
269 equal(typeof obj3.foo, "function", "Superclass method exists in superclass") ;
270 equal(typeof obj3.bar, "undefined", "Subclass method doesn't exist in superc lass");
271 equal(typeof obj4.foo, "function", "Superclass method exists in subclass");
272 equal(typeof obj4.bar, "function", "Subclass method exists in subclass");
273
274 equal(obj3.foo(4), 8, "Superclass method executed correctly");
275 equal(obj4.foo(4), 30, "Overridden superclass method executed correctly")
276
277 let type5 = type3.extend({
278 y: uint8
279 });
280 let obj5 = type5(4);
281 equal(obj5.x, 4, "Superclass constructor is called even if subclass has no c onstructor");
282
283 // Untypical overrides
284 type3.extend({x: uint8});
285 ok(true, "Overriding property without changing type");
286
287 throws(function()
288 {
289 type3.extend({x: float32});
290 }, "Override changes property type");
291
292 throws(function()
293 {
294 type3.extend({foo: uint8});
295 }, "Property masks method");
296
297 throws(function()
298 {
299 type3.extend({x: function() {}});
300 }, "Method masks property");
166 }); 301 });
167 })(); 302 })();
OLDNEW
« no previous file with comments | « lib/typedObjects/utils.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld