[gjs/wip/xulrunner-1.9.3-rebase4: 3/16] tests: Refactor duplicate code for creating and destroying context



commit d502d48e0161fec4306dc951ea7e64e8ff0b326c
Author: Colin Walters <walters verbum org>
Date:   Sat Sep 18 17:18:01 2010 -0400

    tests: Refactor duplicate code for creating and destroying context
    
    For XULRunner 1.9.3 we'll need to fix this code, so ensure we only
    need to fix it in one place.

 Makefile.am             |    2 +
 gjs/jsapi-util-array.c  |   29 ++++------------------
 gjs/jsapi-util-error.c  |   32 ++++--------------------
 gjs/jsapi-util-string.c |   62 ++++++++++------------------------------------
 gjs/unit-test-utils.c   |   54 ++++++++++++++++++++++++++++++++++++++++
 gjs/unit-test-utils.h   |   38 ++++++++++++++++++++++++++++
 6 files changed, 118 insertions(+), 99 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 9b52b1c..48e6dc6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -32,6 +32,7 @@ noinst_HEADERS +=		\
 	gjs/context-jsapi.h	\
 	gjs/jsapi-private.h	\
 	gjs/profiler.h		\
+	gjs/unit-test-utils.h	\
 	util/crash.h		\
 	util/error.h		\
 	util/glib.h		\
@@ -81,6 +82,7 @@ libgjs_la_SOURCES =		\
 	gjs/native.c		\
 	gjs/profiler.c		\
 	gjs/stack.c		\
+	gjs/unit-test-utils.c	\
 	util/error.c		\
 	util/glib.c		\
 	util/crash.c		\
diff --git a/gjs/jsapi-util-array.c b/gjs/jsapi-util-array.c
index b4c7360..6b77c44 100644
--- a/gjs/jsapi-util-array.c
+++ b/gjs/jsapi-util-array.c
@@ -286,38 +286,23 @@ gjs_rooted_array_free(JSContext        *context,
 }
 
 #if GJS_BUILD_TESTS
+#include "unit-test-utils.h"
 #include <string.h>
 
-static void
-test_error_reporter(JSContext     *context,
-                    const char    *message,
-                    JSErrorReport *report)
-{
-    g_printerr("error reported by test: %s\n", message);
-}
-
 #define N_ELEMS 15
 
 void
 gjstest_test_func_gjs_jsapi_util_array(void)
 {
-    JSRuntime *runtime;
+    GjsUnitTestFixture fixture;
     JSContext *context;
     JSObject *global;
     GjsRootedArray *array;
     int i;
     jsval value;
 
-    runtime = JS_NewRuntime(1024*1024 /* max bytes */);
-    context = JS_NewContext(runtime, 8192);
-
-    JS_BeginRequest(context);
-
-    global = JS_NewObject(context, NULL, NULL, NULL);
-    JS_SetGlobalObject(context, global);
-    JS_InitStandardClasses(context, global);
-
-    JS_SetErrorReporter(context, test_error_reporter);
+    _gjs_unit_test_fixture_begin(&fixture);
+    context = fixture.context;
 
     array = gjs_rooted_array_new();
 
@@ -343,11 +328,7 @@ gjstest_test_func_gjs_jsapi_util_array(void)
 
     gjs_rooted_array_free(context, array, TRUE);
 
-    JS_EndRequest(context);
-
-    JS_DestroyContext(context);
-    JS_DestroyRuntime(runtime);
-    JS_ShutDown();
+    _gjs_unit_test_fixture_finish(&fixture);
 }
 
 #endif /* GJS_BUILD_TESTS */
diff --git a/gjs/jsapi-util-error.c b/gjs/jsapi-util-error.c
index 50d4449..92a7751 100644
--- a/gjs/jsapi-util-error.c
+++ b/gjs/jsapi-util-error.c
@@ -194,36 +194,18 @@ gjs_throw_g_error (JSContext       *context,
 }
 
 #if GJS_BUILD_TESTS
-static void
-test_error_reporter(JSContext     *context,
-                    const char    *message,
-                    JSErrorReport *report)
-{
-    g_printerr("error reported by test: %s\n", message);
-}
+#include "unit-test-utils.h"
 
 void
 gjstest_test_func_gjs_jsapi_util_error_throw(void)
 {
-    JSRuntime *runtime;
+    GjsUnitTestFixture fixture;
     JSContext *context;
-    JSObject *global;
     jsval exc, value, previous;
     const char *s;
 
-    /* create a runtime just to avoid tangling this test with all the
-     * code surrounding how we create one normally in context.c
-     */
-    runtime = JS_NewRuntime(1024*1024 /* max bytes */);
-    context = JS_NewContext(runtime, 8192);
-
-    JS_BeginRequest(context);
-
-    global = JS_NewObject(context, NULL, NULL, NULL);
-    JS_SetGlobalObject(context, global);
-    JS_InitStandardClasses(context, global);
-
-    JS_SetErrorReporter(context, test_error_reporter);
+    _gjs_unit_test_fixture_begin(&fixture);
+    context = fixture.context;
 
     /* Test that we can throw */
 
@@ -273,11 +255,7 @@ gjstest_test_func_gjs_jsapi_util_error_throw(void)
 
     JS_RemoveRoot(context, &previous);
 
-    JS_EndRequest(context);
-
-    JS_DestroyContext(context);
-    JS_DestroyRuntime(runtime);
-    JS_ShutDown();
+    _gjs_unit_test_fixture_finish(&fixture);
 }
 
 #endif /* GJS_BUILD_TESTS */
diff --git a/gjs/jsapi-util-string.c b/gjs/jsapi-util-string.c
index 4a57176..d3ae73b 100644
--- a/gjs/jsapi-util-string.c
+++ b/gjs/jsapi-util-string.c
@@ -443,44 +443,28 @@ gjs_get_string_id (jsval            id_val,
 
 
 #if GJS_BUILD_TESTS
+#include "unit-test-utils.h"
 #include <string.h>
 
 
-static void
-test_error_reporter(JSContext     *context,
-                    const char    *message,
-                    JSErrorReport *report)
-{
-    g_printerr("error reported by test: %s\n", message);
-}
-
 void
 gjstest_test_func_gjs_jsapi_util_string_js_string_utf8(void)
 {
+    GjsUnitTestFixture fixture;
+    JSContext *context;
     const char *utf8_string = "\303\211\303\226 foobar \343\203\237";
     char *utf8_result;
-    JSRuntime *runtime;
-    JSContext *context;
-    JSObject *global;
     jsval js_string;
 
-    runtime = JS_NewRuntime(1024*1024 /* max bytes */);
-    context = JS_NewContext(runtime, 8192);
-    JS_BeginRequest(context);
-    global = JS_NewObject(context, NULL, NULL, NULL);
-    JS_SetGlobalObject(context, global);
-    JS_InitStandardClasses(context, global);
-
-    JS_SetErrorReporter(context, test_error_reporter);
+    _gjs_unit_test_fixture_begin(&fixture);
+    context = fixture.context;
 
     g_assert(gjs_string_from_utf8(context, utf8_string, -1, &js_string) == JS_TRUE);
     g_assert(js_string);
     g_assert(JSVAL_IS_STRING(js_string));
     g_assert(gjs_string_to_utf8(context, js_string, &utf8_result) == JS_TRUE);
 
-    JS_EndRequest(context);
-    JS_DestroyContext(context);
-    JS_DestroyRuntime(runtime);
+    _gjs_unit_test_fixture_finish(&fixture);
 
     g_assert(g_str_equal(utf8_string, utf8_result));
 
@@ -490,21 +474,14 @@ gjstest_test_func_gjs_jsapi_util_string_js_string_utf8(void)
 void
 gjstest_test_func_gjs_jsapi_util_string_get_ascii(void)
 {
-    JSRuntime *runtime;
+    GjsUnitTestFixture fixture;
     JSContext *context;
-    JSObject *global;
     const char *ascii_string = "Hello, world";
     JSString  *js_string;
     jsval      void_value;
 
-    runtime = JS_NewRuntime(1024*1024 /* max bytes */);
-    context = JS_NewContext(runtime, 8192);
-    JS_BeginRequest(context);
-    global = JS_NewObject(context, NULL, NULL, NULL);
-    JS_SetGlobalObject(context, global);
-    JS_InitStandardClasses(context, global);
-
-    JS_SetErrorReporter(context, test_error_reporter);
+    _gjs_unit_test_fixture_begin(&fixture);
+    context = fixture.context;
 
     js_string = JS_NewStringCopyZ(context, ascii_string);
     g_assert(g_str_equal(gjs_string_get_ascii(STRING_TO_JSVAL(js_string)), ascii_string));
@@ -512,17 +489,14 @@ gjstest_test_func_gjs_jsapi_util_string_get_ascii(void)
     g_assert(gjs_string_get_ascii_checked(context, void_value) == NULL);
     g_assert(JS_IsExceptionPending(context));
 
-    JS_EndRequest(context);
-    JS_DestroyContext(context);
-    JS_DestroyRuntime(runtime);
+    _gjs_unit_test_fixture_finish(&fixture);
 }
 
 void
 gjstest_test_func_gjs_jsapi_util_string_get_binary(void)
 {
-    JSRuntime *runtime;
+    GjsUnitTestFixture fixture;
     JSContext *context;
-    JSObject *global;
     const char binary_string[] = "foo\0bar\0baz";
     const char binary_string_odd[] = "foo\0bar\0baz123";
     jsval js_string;
@@ -533,14 +507,8 @@ gjstest_test_func_gjs_jsapi_util_string_get_binary(void)
     g_assert_cmpuint(sizeof(binary_string), ==, 12);
     g_assert_cmpuint(sizeof(binary_string_odd), ==, 15);
 
-    runtime = JS_NewRuntime(1024*1024 /* max bytes */);
-    context = JS_NewContext(runtime, 8192);
-    JS_BeginRequest(context);
-    global = JS_NewObject(context, NULL, NULL, NULL);
-    JS_SetGlobalObject(context, global);
-    JS_InitStandardClasses(context, global);
-
-    JS_SetErrorReporter(context, test_error_reporter);
+    _gjs_unit_test_fixture_begin(&fixture);
+    context = fixture.context;
 
     js_string = JSVAL_VOID;
     JS_AddRoot(context, &js_string);
@@ -582,9 +550,7 @@ gjstest_test_func_gjs_jsapi_util_string_get_binary(void)
                                         &data, &len));
     g_assert(JS_IsExceptionPending(context));
 
-    JS_EndRequest(context);
-    JS_DestroyContext(context);
-    JS_DestroyRuntime(runtime);
+    _gjs_unit_test_fixture_finish(&fixture);
 }
 
 #endif /* GJS_BUILD_TESTS */
diff --git a/gjs/unit-test-utils.c b/gjs/unit-test-utils.c
new file mode 100644
index 0000000..20fde06
--- /dev/null
+++ b/gjs/unit-test-utils.c
@@ -0,0 +1,54 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2010 Red Hat, 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 <gjs/gjs.h>
+#include "unit-test-utils.h"
+
+static void
+test_error_reporter(JSContext     *context,
+                    const char    *message,
+                    JSErrorReport *report)
+{
+    g_printerr("error reported by test: %s\n", message);
+}
+
+void
+_gjs_unit_test_fixture_begin (GjsUnitTestFixture *fixture)
+{
+  JSObject *global;
+  fixture->runtime = JS_NewRuntime(1024*1024 /* max bytes */);
+  fixture->context = JS_NewContext(fixture->runtime, 8192);
+  JS_BeginRequest(fixture->context);
+  global = JS_NewObject(fixture->context, NULL, NULL, NULL);
+  JS_SetGlobalObject(fixture->context, global);
+  JS_InitStandardClasses(fixture->context, global);
+  JS_SetErrorReporter(fixture->context, test_error_reporter);
+}
+
+void
+_gjs_unit_test_fixture_finish (GjsUnitTestFixture *fixture)
+{
+  JS_EndRequest(fixture->context);
+  JS_DestroyContext(fixture->context);
+  JS_DestroyRuntime(fixture->runtime);
+}
diff --git a/gjs/unit-test-utils.h b/gjs/unit-test-utils.h
new file mode 100644
index 0000000..dfc51e8
--- /dev/null
+++ b/gjs/unit-test-utils.h
@@ -0,0 +1,38 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2010 Red Hat, 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.
+ */
+
+#ifndef _GJS_UNIT_TEST_UTILS_H
+#define _GJS_UNIT_TEST_UTILS_H
+
+typedef struct _GjsUnitTestFixture GjsUnitTestFixture;
+
+struct _GjsUnitTestFixture {
+  JSRuntime *runtime;
+  JSContext *context;
+};
+
+void _gjs_unit_test_fixture_begin (GjsUnitTestFixture *fixture);
+
+void _gjs_unit_test_fixture_finish (GjsUnitTestFixture *fixture);
+
+#endif



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