[gjs/wip/ptomato/mozjs31prep: 3/3] tests: Consolidate fixture code
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/ptomato/mozjs31prep: 3/3] tests: Consolidate fixture code
- Date: Tue, 18 Oct 2016 19:28:31 +0000 (UTC)
commit 556d5c2ac5e2aaecbfadd4fe073073b30a3105f4
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.h.
https://bugzilla.gnome.org/show_bug.cgi?id=742249
Makefile-test.am | 2 +-
test/gjs-test-call-args.cpp | 52 +--------
test/{gjs-tests-add-funcs.h => gjs-test-utils.h} | 18 +++-
test/gjs-tests.cpp | 129 +++++++++++-----------
4 files changed, 88 insertions(+), 113 deletions(-)
---
diff --git a/Makefile-test.am b/Makefile-test.am
index 428fc8f..e07f4aa 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -91,7 +91,7 @@ gjs_tests_LDADD = \
gjs_tests_SOURCES = \
test/gjs-tests.cpp \
- test/gjs-tests-add-funcs.h \
+ 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-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..11c903e 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -30,37 +30,50 @@
#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;
-};
+#include "gjs-test-utils.h"
static void
test_error_reporter(JSContext *context,
const char *message,
JSErrorReport *report)
{
- g_printerr("error reported by test: %s\n", message);
+ 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
-_gjs_unit_test_fixture_begin (GjsUnitTestFixture *fixture)
+void
+gjs_unit_test_fixture_setup(GjsUnitTestFixture *fx,
+ gconstpointer unused)
{
- 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);
+ 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);
}
-static void
-_gjs_unit_test_fixture_finish (GjsUnitTestFixture *fixture)
+void
+gjs_unit_test_fixture_teardown(GjsUnitTestFixture *fx,
+ gconstpointer unused)
{
- JS_EndRequest(fixture->context);
- g_object_unref(fixture->gjs_context);
+ 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
@@ -92,90 +105,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 +268,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]