diff options
author | Colin Walters <walters@verbum.org> | 2010-09-18 17:18:01 -0400 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2010-09-23 13:36:50 -0400 |
commit | 5468db7b20274b3d139e69464bcdfa50c6eb4ffd (patch) | |
tree | 46651fbd2694bc3935ded2275dc3a75d30d6bfa4 /gjs | |
parent | 58ecae847b2c4066a5c1b7ddd8e5d0d53c78970e (diff) | |
download | gjs-5468db7b20274b3d139e69464bcdfa50c6eb4ffd.tar.gz |
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.
https://bugzilla.gnome.org/show_bug.cgi?id=630413
Diffstat (limited to 'gjs')
-rw-r--r-- | gjs/jsapi-util-array.c | 29 | ||||
-rw-r--r-- | gjs/jsapi-util-error.c | 32 | ||||
-rw-r--r-- | gjs/jsapi-util-string.c | 62 | ||||
-rw-r--r-- | gjs/unit-test-utils.c | 54 | ||||
-rw-r--r-- | gjs/unit-test-utils.h | 38 |
5 files changed, 116 insertions, 99 deletions
diff --git a/gjs/jsapi-util-array.c b/gjs/jsapi-util-array.c index b4c7360e..6b77c44b 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 50d4449b..92a77519 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 4a571761..d3ae73bf 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 00000000..2c3f5fb3 --- /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 00000000..be8a205b --- /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 |