[gjs/wip/ptomato/classes: 8/8] tests: Add ES6 class inheriting from legacy class



commit f5ac6f799174b1bed455e328fca1b5a08f8a5e04
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Jul 23 19:24:25 2017 -0700

    tests: Add ES6 class inheriting from legacy class
    
    This should be supported, so add tests for it.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=785652

 installed-tests/js/testLegacyClass.js   |   68 ++++++++++++++++
 installed-tests/js/testLegacyGObject.js |  127 +++++++++++++++++++++++++++++++
 2 files changed, 195 insertions(+), 0 deletions(-)
---
diff --git a/installed-tests/js/testLegacyClass.js b/installed-tests/js/testLegacyClass.js
index 44fb3a4..6805532 100644
--- a/installed-tests/js/testLegacyClass.js
+++ b/installed-tests/js/testLegacyClass.js
@@ -660,3 +660,71 @@ describe('An interface', function () {
         expect(AnInterface.toString()).toEqual('[interface Interface for AnInterface]');
     });
 });
+
+describe('ES6 class inheriting from Lang.Class', function () {
+    let Shiny, Legacy;
+
+    beforeEach(function () {
+        Legacy = new Lang.Class({
+            Name: 'Legacy',
+            _init(someval) {
+                this.constructorCalledWith = someval;
+            },
+
+            instanceMethod() {},
+            chainUpToMe() {},
+            overrideMe() {},
+
+            get property() { return this._property + 1; },
+            set property(value) { this._property = value - 2; },
+        });
+        Legacy.staticMethod = function () {};
+        spyOn(Legacy, 'staticMethod');
+        spyOn(Legacy.prototype, 'instanceMethod');
+        spyOn(Legacy.prototype, 'chainUpToMe');
+        spyOn(Legacy.prototype, 'overrideMe');
+
+        Shiny = class Shiny extends Legacy {
+            constructor(someval) {
+                super(someval);
+            }
+
+            chainUpToMe() { super.chainUpToMe(); }
+            overrideMe() {}
+        };
+    });
+
+    it('calls a static method on the parent class', function () {
+        Shiny.staticMethod();
+        expect(Legacy.staticMethod).toHaveBeenCalled();
+    });
+
+    it('calls a method on the parent class', function () {
+        let instance = new Shiny();
+        instance.instanceMethod();
+        expect(Legacy.prototype.instanceMethod).toHaveBeenCalled();
+    });
+
+    it("passes arguments to the parent class's constructor", function () {
+        let instance = new Shiny(42);
+        expect(instance.constructorCalledWith).toEqual(42);
+    });
+
+    it('chains up to a method on the parent class', function () {
+        let instance = new Shiny();
+        instance.chainUpToMe();
+        expect(Legacy.prototype.chainUpToMe).toHaveBeenCalled();
+    });
+
+    it('overrides a method on the parent class', function () {
+        let instance = new Shiny();
+        instance.overrideMe();
+        expect(Legacy.prototype.overrideMe).not.toHaveBeenCalled();
+    });
+
+    it('sets and gets a property from the parent class', function () {
+        let instance = new Shiny();
+        instance.property = 42;
+        expect(instance.property).toEqual(41);
+    });
+});
diff --git a/installed-tests/js/testLegacyGObject.js b/installed-tests/js/testLegacyGObject.js
index 9e24dfd..35f82fb 100644
--- a/installed-tests/js/testLegacyGObject.js
+++ b/installed-tests/js/testLegacyGObject.js
@@ -851,3 +851,130 @@ describe('Legacy Gtk overrides', function () {
         expect(Gtk.Widget.get_css_name.call(MyComplexGtkSubclass)).toEqual('complex-subclass');
     });
 });
+
+const LegacyInterface1 = new Lang.Interface({
+    Name: 'LegacyInterface1',
+    Requires: [GObject.Object],
+    Signals: { 'legacy-iface1-signal': {} },
+});
+
+const LegacyInterface2 = new Lang.Interface({
+    Name: 'LegacyInterface2',
+    Requires: [GObject.Object],
+    Signals: { 'legacy-iface2-signal': {} },
+});
+
+const Legacy = new Lang.Class({
+    Name: 'Legacy',
+    Extends: GObject.Object,
+    Implements: [LegacyInterface1],
+    Properties: {
+        'property': GObject.ParamSpec.int('property', 'Property',
+            'A magic property', GObject.ParamFlags.READWRITE, 0, 100, 0),
+        'override-property': GObject.ParamSpec.int('override-property',
+            'Override property', 'Another magic property',
+            GObject.ParamFlags.READWRITE, 0, 100, 0),
+    },
+    Signals: {
+        'signal': {},
+    },
+
+    _init(someval) {
+        this.constructorCalledWith = someval;
+        this.parent();
+    },
+
+    instanceMethod() {},
+    chainUpToMe() {},
+    overrideMe() {},
+
+    get property() { return this._property + 1; },
+    set property(value) { this._property = value - 2; },
+
+    get override_property() { return this._override_property + 1; },
+    set override_property(value) { this._override_property = value - 2; },
+});
+Legacy.staticMethod = function () {};
+
+const Shiny = GObject.registerClass({
+    Implements: [LegacyInterface2],
+    Properties: {
+        'override-property': GObject.ParamSpec.override('override-property',
+            Legacy),
+    },
+}, class Shiny extends Legacy {
+    chainUpToMe() {
+        super.chainUpToMe();
+    }
+
+    overrideMe() {}
+
+    get override_property() { return this._override_property + 2; }
+    set override_property(value) { this._override_property = value - 1; }
+});
+
+fdescribe('ES6 GObject class inheriting from GObject.Class', function () {
+    let instance;
+
+    beforeEach(function () {
+        spyOn(Legacy, 'staticMethod');
+        spyOn(Legacy.prototype, 'instanceMethod');
+        spyOn(Legacy.prototype, 'chainUpToMe');
+        spyOn(Legacy.prototype, 'overrideMe');
+        instance = new Shiny();
+    });
+
+    it('calls a static method on the parent class', function () {
+        Shiny.staticMethod();
+        expect(Legacy.staticMethod).toHaveBeenCalled();
+    });
+
+    it('calls a method on the parent class', function () {
+        instance.instanceMethod();
+        expect(Legacy.prototype.instanceMethod).toHaveBeenCalled();
+    });
+
+    it("passes arguments to the parent class's constructor", function () {
+        let instance = new Shiny(42);
+        expect(instance.constructorCalledWith).toEqual(42);
+    });
+
+    it('chains up to a method on the parent class', function () {
+        instance.chainUpToMe();
+        expect(Legacy.prototype.chainUpToMe).toHaveBeenCalled();
+    });
+
+    it('overrides a method on the parent class', function () {
+        instance.overrideMe();
+        expect(Legacy.prototype.overrideMe).not.toHaveBeenCalled();
+    });
+
+    it('sets and gets a property from the parent class', function () {
+        instance.property = 42;
+        expect(instance.property).toEqual(41);
+    });
+
+    it('overrides a property from the parent class', function () {
+        instance.override_property = 42;
+        expect(instance.override_property).toEqual(43);
+    });
+
+    it('inherits a signal from the parent class', function () {
+        let signalSpy = jasmine.createSpy('signalSpy');
+        expect(() => {
+            instance.connect('signal', signalSpy);
+            instance.emit('signal');
+        }).not.toThrow();
+        expect(signalSpy).toHaveBeenCalled();
+    });
+
+    it('inherits legacy interfaces from the parent', function () {
+        expect(() => instance.emit('legacy-iface1-signal')).not.toThrow();
+        expect(instance instanceof LegacyInterface1).toBeTruthy();
+    });
+
+    it('can implement a legacy interface itself', function () {
+        expect(() => instance.emit('legacy-iface2-signal')).not.toThrow();
+        expect(instance instanceof LegacyInterface2).toBeTruthy();
+    });
+});


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]