[gjs/wip/ptomato/mozjs52: 30/37] tests: Refactor to avoid error reporter



commit 6ec1576d6df229623b79eb808cdcdcd3d146a46a
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon Jun 5 23:10:24 2017 -0700

    tests: Refactor to avoid error reporter
    
    Error reporter callbacks and JS_ReportPendingException() are going away
    in SpiderMonkey 52. We used these to determine the message from a thrown
    exception. Luckily, it's quite easy to do so without an error reporter
    callback: simply create an error report from the exception object which
    you can get from JS_GetPendingException().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=784196

 test/gjs-test-call-args.cpp |   11 +++------
 test/gjs-test-rooting.cpp   |   10 +-------
 test/gjs-test-utils.cpp     |   50 ++++++++++++++++++++----------------------
 test/gjs-test-utils.h       |    5 +--
 4 files changed, 31 insertions(+), 45 deletions(-)
---
diff --git a/test/gjs-test-call-args.cpp b/test/gjs-test-call-args.cpp
index 1331210..601a14f 100644
--- a/test/gjs-test-call-args.cpp
+++ b/test/gjs-test-call-args.cpp
@@ -274,9 +274,8 @@ run_code(GjsUnitTestFixture *fx,
 
     JS::RootedValue ignored(fx->cx);
     bool ok = JS::Evaluate(fx->cx, options, script, strlen(script), &ignored);
-    JS_ReportPendingException(fx->cx);
 
-    g_assert_null(fx->message);
+    g_assert_null(gjs_unit_test_exception_message(fx));
     g_assert_true(ok);
 }
 
@@ -292,17 +291,15 @@ run_code_expect_exception(GjsUnitTestFixture *fx,
     JS::RootedValue ignored(fx->cx);
     bool ok = JS::Evaluate(fx->cx, options, script, strlen(script), &ignored);
     g_assert_false(ok);
-    g_assert_true(JS_IsExceptionPending(fx->cx));
-    JS_ReportPendingException(fx->cx);
-    g_assert_nonnull(fx->message);
+    GjsAutoChar message = gjs_unit_test_exception_message(fx);
+    g_assert_nonnull(message);
 
     /* Cheap way to shove an expected exception message into the data argument */
     const char *expected_msg = strstr((const char *) code, "//");
     if (expected_msg != NULL) {
         expected_msg += 2;
-        assert_match(fx->message, expected_msg);
+        assert_match(message, expected_msg);
     }
-    g_clear_pointer(&fx->message, g_free);
 }
 
 void
diff --git a/test/gjs-test-rooting.cpp b/test/gjs-test-rooting.cpp
index 3c90077..2ad199c 100644
--- a/test/gjs-test-rooting.cpp
+++ b/test/gjs-test-rooting.cpp
@@ -230,13 +230,6 @@ context_destroyed(JS::HandleObject obj,
 }
 
 static void
-teardown_context_already_destroyed(GjsRootingFixture *fx,
-                                   gconstpointer      unused)
-{
-    gjs_unit_test_teardown_context_already_destroyed(PARENT(fx));
-}
-
-static void
 test_maybe_owned_notify_callback_called_on_context_destroy(GjsRootingFixture *fx,
                                                            gconstpointer      unused)
 {
@@ -288,8 +281,7 @@ gjs_test_add_tests_for_rooting(void)
 #undef ADD_ROOTING_TEST
 
 #define ADD_CONTEXT_DESTROY_TEST(path, f) \
-    g_test_add("/rooting/" path, GjsRootingFixture, NULL, setup, f, \
-               teardown_context_already_destroyed);
+    g_test_add("/rooting/" path, GjsRootingFixture, nullptr, setup, f, nullptr);
 
     ADD_CONTEXT_DESTROY_TEST("maybe-owned/notify-callback-called-on-context-destroy",
                              test_maybe_owned_notify_callback_called_on_context_destroy);
diff --git a/test/gjs-test-utils.cpp b/test/gjs-test-utils.cpp
index 8c39dde..e3d8934 100644
--- a/test/gjs-test-utils.cpp
+++ b/test/gjs-test-utils.cpp
@@ -36,19 +36,6 @@
 #include "gjs/jsapi-wrapper.h"
 #include "gjs-test-utils.h"
 
-static void
-test_error_reporter(JSContext     *context,
-                    const char    *message,
-                    JSErrorReport *report)
-{
-    GjsContext *gjs_context = gjs_context_get_current();
-    GjsUnitTestFixture *fx =
-        (GjsUnitTestFixture *) g_object_get_data(G_OBJECT(gjs_context),
-                                                 "test fixture");
-    g_free(fx->message);
-    fx->message = g_strdup(message);
-}
-
 void
 gjs_unit_test_fixture_setup(GjsUnitTestFixture *fx,
                             gconstpointer       unused)
@@ -56,10 +43,6 @@ gjs_unit_test_fixture_setup(GjsUnitTestFixture *fx,
     fx->gjs_context = gjs_context_new();
     fx->cx = (JSContext *) gjs_context_get_native_context(fx->gjs_context);
 
-    /* This is for shoving private data into the error reporter callback */
-    g_object_set_data(G_OBJECT(fx->gjs_context), "test fixture", fx);
-    JS_SetErrorReporter(JS_GetRuntime(fx->cx), test_error_reporter);
-
     JS_BeginRequest(fx->cx);
 
     JS::RootedObject global(fx->cx, gjs_get_import_global(fx->cx));
@@ -69,6 +52,10 @@ gjs_unit_test_fixture_setup(GjsUnitTestFixture *fx,
 void
 gjs_unit_test_destroy_context(GjsUnitTestFixture *fx)
 {
+    GjsAutoChar message = gjs_unit_test_exception_message(fx);
+    if (message)
+        g_printerr("**\n%s\n", message.get());
+
     JS_LeaveCompartment(fx->cx, fx->compartment);
     JS_EndRequest(fx->cx);
 
@@ -76,19 +63,30 @@ gjs_unit_test_destroy_context(GjsUnitTestFixture *fx)
 }
 
 void
-gjs_unit_test_teardown_context_already_destroyed(GjsUnitTestFixture *fx)
-{
-    if (fx->message != NULL)
-        g_printerr("**\n%s\n", fx->message);
-    g_free(fx->message);
-}
-
-void
 gjs_unit_test_fixture_teardown(GjsUnitTestFixture *fx,
                                gconstpointer      unused)
 {
     gjs_unit_test_destroy_context(fx);
-    gjs_unit_test_teardown_context_already_destroyed(fx);
+}
+
+char *
+gjs_unit_test_exception_message(GjsUnitTestFixture *fx)
+{
+    if (!JS_IsExceptionPending(fx->cx))
+        return nullptr;
+
+    JS::RootedValue v_exc(fx->cx);
+    g_assert_true(JS_GetPendingException(fx->cx, &v_exc));
+    g_assert_true(v_exc.isObject());
+
+    JS::RootedObject exc(fx->cx, &v_exc.toObject());
+    JSErrorReport *report = JS_ErrorFromException(fx->cx, exc);
+    g_assert_nonnull(report);
+
+    char *retval = g_strdup(report->message().c_str());
+    g_assert_nonnull(retval);
+    JS_ClearPendingException(fx->cx);
+    return retval;
 }
 
 /* Fork a process that waits the given time then
diff --git a/test/gjs-test-utils.h b/test/gjs-test-utils.h
index 12cd9a0..a684f03 100644
--- a/test/gjs-test-utils.h
+++ b/test/gjs-test-utils.h
@@ -28,7 +28,6 @@ struct _GjsUnitTestFixture {
     GjsContext *gjs_context;
     JSContext *cx;
     JSCompartment *compartment;
-    char *message;  /* Thrown exception message */
 };
 
 void gjs_unit_test_fixture_setup(GjsUnitTestFixture *fx,
@@ -36,11 +35,11 @@ void gjs_unit_test_fixture_setup(GjsUnitTestFixture *fx,
 
 void gjs_unit_test_destroy_context(GjsUnitTestFixture *fx);
 
-void gjs_unit_test_teardown_context_already_destroyed(GjsUnitTestFixture *fx);
-
 void gjs_unit_test_fixture_teardown(GjsUnitTestFixture *fx,
                                     gconstpointer      unused);
 
+char *gjs_unit_test_exception_message(GjsUnitTestFixture *fx);
+
 void gjs_crash_after_timeout(int seconds);
 
 void gjs_test_add_tests_for_coverage ();


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