diff --git a/installed-tests/js/testPrint.js b/installed-tests/js/testPrint.js index 9b582c28c3d32a30b743f055bd853518eee8e004..153bc7e304bc48a4441c730692f008d0a9ad9eae 100644 --- a/installed-tests/js/testPrint.js +++ b/installed-tests/js/testPrint.js @@ -2,9 +2,10 @@ // SPDX-FileCopyrightText: 2020 Philip Chimento // SPDX-FileCopyrightText: 2022 Nasah Kuma -const GLib = imports.gi.GLib; imports.gi.versions.Gdk = '3.0'; const Gdk = imports.gi.Gdk; +const {getPrettyPrintFunction} = imports._print; +let prettyPrint = getPrettyPrintFunction(globalThis); describe('print', function () { it('can be spied upon', function () { @@ -39,121 +40,116 @@ describe('logError', function () { }); describe('prettyPrint', function () { - afterEach(function () { - GLib.test_assert_expected_messages_internal('Gjs', 'testPrint.js', 0, - 'pretty print'); - }); - it('property value primitive', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: { greeting: "hi" }'); - log({greeting: 'hi'}); + expect( + prettyPrint({greeting: 'hi'}) + ).toBe('{ greeting: "hi" }'); }); it('property value is object reference', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: { a: 5, b: [Circular] }'); let obj = {a: 5}; obj.b = obj; - log(obj); + expect( + prettyPrint(obj) + ).toBe('{ a: 5, b: [Circular] }'); }); it('more than one property', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: { a: 1, b: 2, c: 3 }'); - log({a: 1, b: 2, c: 3}); + expect( + prettyPrint({a: 1, b: 2, c: 3}) + ).toBe('{ a: 1, b: 2, c: 3 }'); }); it('add property value after property value object reference', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: { a: 5, b: [Circular], c: 4 }'); let obj = {a: 5}; obj.b = obj; obj.c = 4; - log(obj); + expect( + prettyPrint(obj) + ).toBe('{ a: 5, b: [Circular], c: 4 }'); }); it('array', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: [1, 2, 3, 4, 5]'); - log([1, 2, 3, 4, 5]); + expect( + prettyPrint([1, 2, 3, 4, 5]) + ).toBe('[1, 2, 3, 4, 5]'); }); it('property value array', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: { arr: [1, 2, 3, 4, 5] }'); - log({arr: [1, 2, 3, 4, 5]}); + expect( + prettyPrint({arr: [1, 2, 3, 4, 5]}) + ).toBe('{ arr: [1, 2, 3, 4, 5] }'); }); it('array reference is the only array element', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: [[Circular]]'); let arr = []; arr.push(arr); - log(arr); + expect( + prettyPrint(arr) + ).toBe('[[Circular]]'); }); it('array reference is one of multiple array elements', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: [4, [Circular], 5]'); let arr = []; arr.push(4); arr.push(arr); arr.push(5); - log(arr); + expect( + prettyPrint(arr) + ).toBe('[4, [Circular], 5]'); }); it('nested array', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: [1, 2, [3, 4], 5]'); - log([1, 2, [3, 4], 5]); + expect( + prettyPrint([1, 2, [3, 4], 5]) + ).toBe('[1, 2, [3, 4], 5]'); }); it('property value nested array', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: { arr: [1, 2, [3, 4], 5] }'); - log({arr: [1, 2, [3, 4], 5]}); + expect( + prettyPrint({arr: [1, 2, [3, 4], 5]}) + ).toBe('{ arr: [1, 2, [3, 4], 5] }'); }); it('function', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: [ Function: sum ]'); - log(function sum(a, b) { - return a + b; - }); + expect( + prettyPrint(function sum(a, b) { + return a + b; + }) + ).toBe('[ Function: sum ]'); }); it('property value function', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: { sum: [ Function: sum ] }'); - log({ - sum: function sum(a, b) { - return a + b; - }, - }); + expect( + prettyPrint({ + sum: function sum(a, b) { + return a + b; + }, + }) + ).toBe('{ sum: [ Function: sum ] }'); }); it('date', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: 2018-12-24T10:33:30.000Z'); - log(new Date(Date.UTC(2018, 11, 24, 10, 33, 30))); + expect( + prettyPrint(new Date(Date.UTC(2018, 11, 24, 10, 33, 30))) + ).toBe('2018-12-24T10:33:30.000Z'); }); it('property value date', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: { date: 2018-12-24T10:33:30.000Z }'); - log({date: new Date(Date.UTC(2018, 11, 24, 10, 33, 30))}); + expect( + prettyPrint({date: new Date(Date.UTC(2018, 11, 24, 10, 33, 30))}) + ).toBe('{ date: 2018-12-24T10:33:30.000Z }'); }); it('toString is overridden on object', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: [boxed instance wrapper GIName:*]'); - log(new Gdk.Rectangle()); + expect( + prettyPrint(new Gdk.Rectangle()) + ).toMatch(/\[boxed instance wrapper GIName:.*\]/); }); it('string tag supplied', function () { - GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE, - 'JS LOG: [object GIRepositoryNamespace]'); - log(Gdk); + expect( + prettyPrint(Gdk) + ).toMatch('[object GIRepositoryNamespace]'); }); }); diff --git a/modules/print.cpp b/modules/print.cpp index 2b07a27b0351689d4cea32a2cc469b0d9b631ed9..6454a526d6c3990d1ed88f6fbcbd2ddeb4de77b7 100644 --- a/modules/print.cpp +++ b/modules/print.cpp @@ -162,6 +162,24 @@ static bool set_pretty_print_function(JSContext*, unsigned argc, return true; } +GJS_JSAPI_RETURN_CONVENTION +static bool get_pretty_print_function(JSContext*, unsigned argc, + JS::Value* vp) { + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + + g_assert(args.length() == 1 && "getPrettyPrintFunction takes 1 arguments"); + + JS::Value v_global = args[0]; + + g_assert(v_global.isObject() && "argument must be an object"); + + JS::Value pretty_print = gjs_get_global_slot( + &v_global.toObject(), GjsGlobalSlot::PRETTY_PRINT_FUNC); + + args.rval().set(pretty_print); + return true; +} + // clang-format off static constexpr JSFunctionSpec funcs[] = { JS_FN("log", gjs_log, 1, GJS_MODULE_PROP_FLAGS), @@ -169,6 +187,7 @@ static constexpr JSFunctionSpec funcs[] = { JS_FN("print", gjs_print, 0, GJS_MODULE_PROP_FLAGS), JS_FN("printerr", gjs_printerr, 0, GJS_MODULE_PROP_FLAGS), JS_FN("setPrettyPrintFunction", set_pretty_print_function, 1, GJS_MODULE_PROP_FLAGS), + JS_FN("getPrettyPrintFunction", get_pretty_print_function, 1, GJS_MODULE_PROP_FLAGS), JS_FS_END}; // clang-format on