[gjs/wip/ptomato/mozjs31prep: 4/7] tests: Consolidate fixture code



commit 469c7226110f1f1744154d16fe1168e6a268b0c9
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Oct 16 23:02:50 2016 -0700

    tests: Consolidate fixture code
    
    gjs-tests.cpp used a similar test fixture to gjs-test-call-args.cpp, so
    we consolidate the two into gjs-test-utils.cpp.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742249

 Makefile-test.am                                 |    3 +-
 test/gjs-test-call-args.cpp                      |   52 +---------
 test/gjs-test-utils.cpp                          |   79 +++++++++++++++
 test/{gjs-tests-add-funcs.h => gjs-test-utils.h} |   18 +++-
 test/gjs-tests.cpp                               |  111 +++++++---------------
 5 files changed, 137 insertions(+), 126 deletions(-)
---
diff --git a/Makefile-test.am b/Makefile-test.am
index 428fc8f..06e6400 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -91,7 +91,8 @@ gjs_tests_LDADD =             \
 
 gjs_tests_SOURCES =                                    \
        test/gjs-tests.cpp                              \
-       test/gjs-tests-add-funcs.h                      \
+       test/gjs-test-utils.cpp                         \
+       test/gjs-test-utils.h                           \
        test/gjs-test-call-args.cpp                     \
        test/gjs-test-coverage.cpp                      \
        mock-js-resources.c                             \
diff --git a/test/gjs-test-call-args.cpp b/test/gjs-test-call-args.cpp
index ea1ffe6..cd9dccd 100644
--- a/test/gjs-test-call-args.cpp
+++ b/test/gjs-test-call-args.cpp
@@ -5,6 +5,7 @@
 #include "gjs/compat.h"
 #include "gjs/context.h"
 #include "gjs/jsapi-util-args.h"
+#include "test/gjs-test-utils.h"
 
 #define assert_match(str, pattern)                                            \
     G_STMT_START {                                                            \
@@ -15,13 +16,6 @@
         }                                                                     \
     } G_STMT_END
 
-typedef struct {
-    GjsContext *gjs_context;
-    JSContext *cx;
-    JSCompartment *compartment;
-    char *message;  /* Thrown exception message */
-} GjsUnitTestFixture;
-
 typedef enum _test_enum {
     ZERO,
     ONE,
@@ -249,53 +243,17 @@ static JSFunctionSpec native_test_funcs[] = {
 };
 
 static void
-unit_test_error_reporter(JSContext     *cx,
-                         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);
-}
-
-static void
 setup(GjsUnitTestFixture *fx,
       gconstpointer       unused)
 {
-    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(fx->cx, unit_test_error_reporter);
-
-    JS_BeginRequest(fx->cx);
+    gjs_unit_test_fixture_setup(fx, unused);
 
     JS::RootedObject global(fx->cx, gjs_get_global_object(fx->cx));
-    fx->compartment = JS_EnterCompartment(fx->cx, global);
-
     bool success = JS_DefineFunctions(fx->cx, global, native_test_funcs);
     g_assert_true(success);
 }
 
 static void
-teardown(GjsUnitTestFixture *fx,
-         gconstpointer       unused)
-{
-    JS_LeaveCompartment(fx->cx, fx->compartment);
-    JS_EndRequest(fx->cx);
-
-    g_object_unref(fx->gjs_context);
-
-    if (fx->message != NULL)
-        g_printerr("**\n%s\n", fx->message);
-    g_free(fx->message);
-}
-
-static void
 run_code(GjsUnitTestFixture *fx,
          gconstpointer       code)
 {
@@ -334,13 +292,15 @@ run_code_expect_exception(GjsUnitTestFixture *fx,
         expected_msg += 2;
         assert_match(fx->message, expected_msg);
     }
+    g_clear_pointer(&fx->message, g_free);
 }
 
 void
 gjs_test_add_tests_for_parse_call_args(void)
 {
-#define ADD_CALL_ARGS_TEST_BASE(path, code, f) \
-    g_test_add("/callargs/" path, GjsUnitTestFixture, code, setup, f, teardown)
+#define ADD_CALL_ARGS_TEST_BASE(path, code, f)                         \
+    g_test_add("/callargs/" path, GjsUnitTestFixture, code, setup, f,  \
+               gjs_unit_test_fixture_teardown)
 #define ADD_CALL_ARGS_TEST(path, code) \
     ADD_CALL_ARGS_TEST_BASE(path, code, run_code)
 #define ADD_CALL_ARGS_TEST_XFAIL(path, code) \
diff --git a/test/gjs-test-utils.cpp b/test/gjs-test-utils.cpp
new file mode 100644
index 0000000..1fea5e5
--- /dev/null
+++ b/test/gjs-test-utils.cpp
@@ -0,0 +1,79 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2016 Endless Mobile, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <config.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include <glib.h>
+
+#include "gjs/compat.h"
+#include "gjs/context.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)
+{
+    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(fx->cx, test_error_reporter);
+
+    JS_BeginRequest(fx->cx);
+
+    JS::RootedObject global(fx->cx, gjs_get_global_object(fx->cx));
+    fx->compartment = JS_EnterCompartment(fx->cx, global);
+}
+
+void
+gjs_unit_test_fixture_teardown(GjsUnitTestFixture *fx,
+                               gconstpointer      unused)
+{
+    JS_LeaveCompartment(fx->cx, fx->compartment);
+    JS_EndRequest(fx->cx);
+
+    g_object_unref(fx->gjs_context);
+
+    if (fx->message != NULL)
+        g_printerr("**\n%s\n", fx->message);
+    g_free(fx->message);
+}
diff --git a/test/gjs-tests-add-funcs.h b/test/gjs-test-utils.h
similarity index 64%
rename from test/gjs-tests-add-funcs.h
rename to test/gjs-test-utils.h
index 23ae846..92e2d9f 100644
--- a/test/gjs-tests-add-funcs.h
+++ b/test/gjs-test-utils.h
@@ -17,8 +17,22 @@
  *
  * Authored By: Sam Spilsbury <sam endlessm com>
  */
-#ifndef GJS_TESTS_ADD_FUNCS_H
-#define GJS_TESTS_ADD_FUNCS_H
+#ifndef GJS_TEST_UTILS_H
+#define GJS_TEST_UTILS_H
+
+typedef struct _GjsUnitTestFixture GjsUnitTestFixture;
+struct _GjsUnitTestFixture {
+    GjsContext *gjs_context;
+    JSContext *cx;
+    JSCompartment *compartment;
+    char *message;  /* Thrown exception message */
+};
+
+void gjs_unit_test_fixture_setup(GjsUnitTestFixture *fx,
+                                 gconstpointer       unused);
+
+void gjs_unit_test_fixture_teardown(GjsUnitTestFixture *fx,
+                                    gconstpointer      unused);
 
 void gjs_test_add_tests_for_coverage ();
 
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index be5bd81..9979413 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -30,38 +30,7 @@
 #include <gjs/context.h>
 
 #include "gjs/compat.h"
-#include "gjs-tests-add-funcs.h"
-
-typedef struct _GjsUnitTestFixture GjsUnitTestFixture;
-
-struct _GjsUnitTestFixture {
-    JSContext *context;
-    GjsContext *gjs_context;
-};
-
-static void
-test_error_reporter(JSContext     *context,
-                    const char    *message,
-                    JSErrorReport *report)
-{
-    g_printerr("error reported by test: %s\n", message);
-}
-
-static void
-_gjs_unit_test_fixture_begin (GjsUnitTestFixture *fixture)
-{
-    fixture->gjs_context = gjs_context_new ();
-    fixture->context = (JSContext *) gjs_context_get_native_context (fixture->gjs_context);
-    JS_BeginRequest(fixture->context);
-    JS_SetErrorReporter(fixture->context, test_error_reporter);
-}
-
-static void
-_gjs_unit_test_fixture_finish (GjsUnitTestFixture *fixture)
-{
-    JS_EndRequest(fixture->context);
-    g_object_unref(fixture->gjs_context);
-}
+#include "gjs-test-utils.h"
 
 static void
 gjstest_test_func_gjs_context_construct_destroy(void)
@@ -92,90 +61,68 @@ gjstest_test_func_gjs_context_construct_eval(void)
 }
 
 static void
-gjstest_test_func_gjs_jsapi_util_string_js_string_utf8(void)
+gjstest_test_func_gjs_jsapi_util_string_js_string_utf8(GjsUnitTestFixture *fx,
+                                                       gconstpointer       unused)
 {
-    GjsUnitTestFixture fixture;
-    JSContext *context;
-    JSObject *global;
-
     const char *utf8_string = "\303\211\303\226 foobar \343\203\237";
     char *utf8_result;
+    JS::RootedValue js_string(fx->cx);
 
-    _gjs_unit_test_fixture_begin(&fixture);
-    context = fixture.context;
-    JS::RootedValue js_string(context);
-    global = gjs_get_global_object(context);
-    JSCompartment *oldCompartment = JS_EnterCompartment(context, global);
-
-    g_assert(gjs_string_from_utf8(context, utf8_string, -1, &js_string));
+    g_assert(gjs_string_from_utf8(fx->cx, utf8_string, -1, &js_string));
     g_assert(js_string.isString());
-    g_assert(gjs_string_to_utf8(context, js_string, &utf8_result));
-
-    JS_LeaveCompartment(context, oldCompartment);
-    _gjs_unit_test_fixture_finish(&fixture);
-
+    g_assert(gjs_string_to_utf8(fx->cx, js_string, &utf8_result));
     g_assert(g_str_equal(utf8_string, utf8_result));
-
     g_free(utf8_result);
 }
 
 static void
-gjstest_test_func_gjs_jsapi_util_error_throw(void)
+gjstest_test_func_gjs_jsapi_util_error_throw(GjsUnitTestFixture *fx,
+                                             gconstpointer       unused)
 {
-    GjsUnitTestFixture fixture;
-    JSContext *context;
-    JSObject *global;
     JS::Value exc, value;
     char *s = NULL;
 
-    _gjs_unit_test_fixture_begin(&fixture);
-    context = fixture.context;
-    global = gjs_get_global_object(context);
-    JSCompartment *oldCompartment = JS_EnterCompartment(context, global);
     /* Test that we can throw */
 
-    gjs_throw(context, "This is an exception %d", 42);
+    gjs_throw(fx->cx, "This is an exception %d", 42);
 
-    g_assert(JS_IsExceptionPending(context));
+    g_assert(JS_IsExceptionPending(fx->cx));
 
     exc = JS::UndefinedValue();
-    JS_GetPendingException(context, &exc);
+    JS_GetPendingException(fx->cx, &exc);
     g_assert(!exc.isUndefined());
 
     value = JS::UndefinedValue();
-    JS_GetProperty(context, &exc.toObject(), "message",
+    JS_GetProperty(fx->cx, &exc.toObject(), "message",
                    &value);
 
     g_assert(value.isString());
 
-    gjs_string_to_utf8(context, value, &s);
+    gjs_string_to_utf8(fx->cx, value, &s);
     g_assert_nonnull(s);
     g_assert_cmpstr(s, ==, "This is an exception 42");
-    JS_free(context, s);
+    JS_free(fx->cx, s);
 
     /* keep this around before we clear it */
-    JS::RootedValue previous(context, exc);
+    JS::RootedValue previous(fx->cx, exc);
 
-    JS_ClearPendingException(context);
+    JS_ClearPendingException(fx->cx);
 
-    g_assert(!JS_IsExceptionPending(context));
+    g_assert(!JS_IsExceptionPending(fx->cx));
 
     /* Check that we don't overwrite a pending exception */
-    JS_SetPendingException(context, previous);
+    JS_SetPendingException(fx->cx, previous);
 
-    g_assert(JS_IsExceptionPending(context));
+    g_assert(JS_IsExceptionPending(fx->cx));
 
-    gjs_throw(context, "Second different exception %s", "foo");
+    gjs_throw(fx->cx, "Second different exception %s", "foo");
 
-    g_assert(JS_IsExceptionPending(context));
+    g_assert(JS_IsExceptionPending(fx->cx));
 
     exc = JS::UndefinedValue();
-    JS_GetPendingException(context, &exc);
+    JS_GetPendingException(fx->cx, &exc);
     g_assert(!exc.isUndefined());
     g_assert(&exc.toObject() == &previous.toObject());
-
-    JS_LeaveCompartment(context, oldCompartment);
-    _gjs_unit_test_fixture_finish(&fixture);
 }
 
 static void
@@ -277,14 +224,24 @@ main(int    argc,
 
     g_test_add_func("/gjs/context/construct/destroy", gjstest_test_func_gjs_context_construct_destroy);
     g_test_add_func("/gjs/context/construct/eval", gjstest_test_func_gjs_context_construct_eval);
-    g_test_add_func("/gjs/jsapi/util/error/throw", gjstest_test_func_gjs_jsapi_util_error_throw);
-    g_test_add_func("/gjs/jsapi/util/string/js/string/utf8", 
gjstest_test_func_gjs_jsapi_util_string_js_string_utf8);
     g_test_add_func("/gjs/jsutil/strip_shebang/no_shebang", 
gjstest_test_strip_shebang_no_advance_for_no_shebang);
     g_test_add_func("/gjs/jsutil/strip_shebang/have_shebang", 
gjstest_test_strip_shebang_advance_for_shebang);
     g_test_add_func("/gjs/jsutil/strip_shebang/only_shebang", 
gjstest_test_strip_shebang_return_null_for_just_shebang);
     g_test_add_func("/util/glib/strv/concat/null", gjstest_test_func_util_glib_strv_concat_null);
     g_test_add_func("/util/glib/strv/concat/pointers", gjstest_test_func_util_glib_strv_concat_pointers);
 
+#define ADD_JSAPI_UTIL_TEST(path, func)                            \
+    g_test_add("/gjs/jsapi/util/" path, GjsUnitTestFixture, NULL,  \
+               gjs_unit_test_fixture_setup, func,                  \
+               gjs_unit_test_fixture_teardown)
+
+    ADD_JSAPI_UTIL_TEST("error/throw",
+                        gjstest_test_func_gjs_jsapi_util_error_throw);
+    ADD_JSAPI_UTIL_TEST("string/js/string/utf8",
+                        gjstest_test_func_gjs_jsapi_util_string_js_string_utf8);
+
+#undef ADD_JSAPI_UTIL_TEST
+
     gjs_test_add_tests_for_coverage ();
     gjs_test_add_tests_for_parse_call_args();
 


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