forked from magcius/gjs
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lang: Add new "Class" framework adapted from MooTools
The current strategy of using __proto__ and _init to implement prototype chains is non-standard and error-prone. Use something a bit more automatic to help us along. Additionally, when patches come for inheriting from gi objects, we can use the existing framework to hoist ourselves without too much effort. https://bugzilla.gnome.org/show_bug.cgi?id=662582
- Loading branch information
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// application/javascript;version=1.8 | ||
|
||
const Lang = imports.lang; | ||
|
||
function assertArrayEquals(expected, got) { | ||
assertEquals(expected.length, got.length); | ||
for (let i = 0; i < expected.length; i ++) { | ||
assertEquals(expected[i], got[i]); | ||
} | ||
} | ||
|
||
const MagicBase = new Lang.Class({ | ||
Name: 'MagicBase', | ||
|
||
_init: function(a, buffer) { | ||
if (buffer) buffer.push(a); | ||
this.a = a; | ||
}, | ||
|
||
foo: function(a, buffer) { | ||
buffer.push(a); | ||
return a * 3; | ||
} | ||
}); | ||
|
||
const Magic = new Lang.Class({ | ||
Name: 'Magic', | ||
|
||
Extends: MagicBase, | ||
|
||
_init: function(a, b, buffer) { | ||
this.parent(a, buffer); | ||
if (buffer) buffer.push(b); | ||
this.b = b; | ||
}, | ||
|
||
foo: function(a, b, buffer) { | ||
let val = this.parent(a, buffer); | ||
buffer.push(b); | ||
return val * 2; | ||
} | ||
}); | ||
|
||
const ToStringOverride = new Lang.Class({ | ||
Name: 'ToStringOverride', | ||
|
||
toString: function() { | ||
let oldToString = this.parent(); | ||
return oldToString + '; hello'; | ||
} | ||
}); | ||
|
||
function testClassFramework() { | ||
let newMagic = new MagicBase('A'); | ||
assertEquals('A', newMagic.a); | ||
} | ||
|
||
function testInheritance() { | ||
let buffer = []; | ||
|
||
let newMagic = new Magic('a', 'b', buffer); | ||
assertArrayEquals(['a', 'b'], buffer); | ||
|
||
buffer = []; | ||
let val = newMagic.foo(10, 20, buffer); | ||
assertArrayEquals([10, 20], buffer); | ||
assertEquals(10*6, val); | ||
} | ||
|
||
function testConstructor() { | ||
assertEquals(Magic, Magic.prototype.constructor); | ||
|
||
let newMagic = new Magic(); | ||
assertEquals(Magic, newMagic.constructor); | ||
} | ||
|
||
function testInstanceOf() { | ||
let newMagic = new Magic(); | ||
|
||
assertTrue(newMagic instanceof Magic); | ||
assertTrue(newMagic instanceof MagicBase); | ||
} | ||
|
||
function testToString() { | ||
let newMagic = new MagicBase(); | ||
assertEquals('[object MagicBase]', newMagic.toString()); | ||
|
||
let override = new ToStringOverride(); | ||
assertEquals('[object ToStringOverride]; hello', override.toString()); | ||
} | ||
|
||
gjstestRun(); |