[gjs/ewlsh/gobject-new: 122/122] Add override for GObject.Object.new_with_properties()
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/ewlsh/gobject-new: 122/122] Add override for GObject.Object.new_with_properties()
- Date: Fri, 14 Jan 2022 23:12:51 +0000 (UTC)
commit e3055d7881591dfc438b28d3e988fc3d758393a0
Author: Philip Chimento <philip chimento gmail com>
Date: Fri Jan 14 15:11:23 2022 -0800
Add override for GObject.Object.new_with_properties()
Called as in Vala, with two arrays: names and values, which must be the
same length.
Adds tests for GObject.Object.new() as well.
installed-tests/js/testGObject.js | 99 ++++++++++++++++++++++++++++++++++-----
modules/core/overrides/GObject.js | 10 ++++
2 files changed, 97 insertions(+), 12 deletions(-)
---
diff --git a/installed-tests/js/testGObject.js b/installed-tests/js/testGObject.js
index d6aafbbc..160f85ab 100644
--- a/installed-tests/js/testGObject.js
+++ b/installed-tests/js/testGObject.js
@@ -9,19 +9,19 @@
const {GLib, GObject} = imports.gi;
const {system: System} = imports;
-describe('GObject overrides', function () {
- const TestObj = GObject.registerClass({
- Properties: {
- int: GObject.ParamSpec.int('int', '', '', GObject.ParamFlags.READWRITE,
- 0, GLib.MAXINT32, 0),
- string: GObject.ParamSpec.string('string', '', '',
- GObject.ParamFlags.READWRITE, ''),
- },
- Signals: {
- test: {},
- },
- }, class TestObj extends GObject.Object {});
+const TestObj = GObject.registerClass({
+ Properties: {
+ int: GObject.ParamSpec.int('int', '', '', GObject.ParamFlags.READWRITE,
+ 0, GLib.MAXINT32, 0),
+ string: GObject.ParamSpec.string('string', '', '',
+ GObject.ParamFlags.READWRITE, ''),
+ },
+ Signals: {
+ test: {},
+ },
+}, class TestObj extends GObject.Object {});
+describe('GObject overrides', function () {
it('GObject.set()', function () {
const o = new TestObj();
o.set({string: 'Answer', int: 42});
@@ -80,3 +80,78 @@ describe('GObject should', function () {
expect(query.signal_id).toBe(1);
});
});
+
+describe('GObject.Object.new()', function () {
+ const gon = GObject.Object.new;
+
+ it('can be called with a property bag', function () {
+ const o = gon(TestObj, {
+ string: 'Answer',
+ int: 42,
+ });
+ expect(o.string).toBe('Answer');
+ expect(o.int).toBe(42);
+ });
+
+ it('can be called to construct an object without setting properties', function () {
+ const o1 = gon(TestObj);
+ expect(o1.string).toBe('');
+ expect(o1.int).toBe(0);
+
+ const o2 = gon(TestObj, {});
+ expect(o2.string).toBe('');
+ expect(o2.int).toBe(0);
+ });
+
+ it('complains about wrong types', function () {
+ expect(() => gon(TestObj, {
+ string: 42,
+ int: 'Answer',
+ })).toThrow();
+ });
+
+ it('complains about wrong properties', function () {
+ expect(() => gon(TestObj, {foo: 'bar'})).toThrow();
+ });
+
+ it('can construct C GObjects as well', function () {
+ const o = gon(GObject.Object, {});
+ expect(o.constructor.$gtype.name).toBe('GObject');
+ });
+});
+
+describe('GObject.Object.new_with_properties()', function () {
+ const gonwp = GObject.Object.new_with_properties;
+
+ it('can be called with two arrays', function () {
+ const o = gonwp(TestObj, ['string', 'int'], ['Answer', 42]);
+ expect(o.string).toBe('Answer');
+ expect(o.int).toBe(42);
+ });
+
+ it('can be called to construct an object without setting properties', function () {
+ const o = gonwp(TestObj, [], []);
+ expect(o.string).toBe('');
+ expect(o.int).toBe(0);
+ });
+
+ it('complains about various incorrect usages', function () {
+ expect(() => gonwp(TestObj)).toThrow();
+ expect(() => gonwp(TestObj, ['string', 'int'])).toThrow();
+ expect(() => gonwp(TestObj, ['string', 'int'], ['Answer'])).toThrow();
+ expect(() => gonwp(TestObj, {}, ['Answer', 42])).toThrow();
+ });
+
+ it('complains about wrong types', function () {
+ expect(() => gonwp(TestObj, ['string', 'int'], [42, 'Answer'])).toThrow();
+ });
+
+ it('complains about wrong properties', function () {
+ expect(() => gonwp(TestObj, ['foo'], ['bar'])).toThrow();
+ });
+
+ it('can construct C GObjects as well', function () {
+ const o = gonwp(GObject.Object, [], []);
+ expect(o.constructor.$gtype.name).toBe('GObject');
+ });
+});
diff --git a/modules/core/overrides/GObject.js b/modules/core/overrides/GObject.js
index b0d665f0..13f897d6 100644
--- a/modules/core/overrides/GObject.js
+++ b/modules/core/overrides/GObject.js
@@ -439,6 +439,16 @@ function _init() {
return new constructor(props);
};
+ GObject.Object.new_with_properties = function (gtype, names, values) {
+ if (!Array.isArray(names) || !Array.isArray(values))
+ throw new Error('new_with_properties takes two arrays (names, values)');
+ if (names.length !== values.length)
+ throw new Error('Arrays passed to new_with_properties must be the same length');
+
+ const props = Object.fromEntries(names.map((name, ix) => [name, values[ix]]));
+ return GObject.Object.new(gtype, props);
+ };
+
GObject.Object._classInit = function (klass) {
let gtypename = _createGTypeName(klass);
let gflags = klass.hasOwnProperty(GTypeFlags) ? klass[GTypeFlags] : 0;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]