summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Chimento <philip@endlessm.com>2013-04-09 14:03:53 +0100
committerP. F. Chimento <philip.chimento@gmail.com>2013-04-17 17:15:52 +0200
commit5049b8938f407629690c6682263187986fb5f2ba (patch)
treeaaed4d880af24856a37c8e48918b20f3d2819fb0
parent89671a4914caf1e20ea20a19c6bff7a058ebb3c4 (diff)
Add test framework
There are automated tests which run when you do 'make check', and also smoke tests which demonstrate one piece of functionality, which can be used as a sort of sanity check or demo. [#1]
-rw-r--r--.gitignore4
-rw-r--r--Makefile.am12
-rw-r--r--endless/Makefile.am2
-rw-r--r--endless/init-private.h14
-rw-r--r--endless/init.c13
-rw-r--r--test/Makefile.am19
-rw-r--r--test/run-tests.c18
-rw-r--r--test/run-tests.h9
-rw-r--r--test/smoke-tests/hello.c11
-rw-r--r--test/test-hello.c59
-rw-r--r--test/test-init.c19
11 files changed, 179 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index aff4368..cd82144 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
# Build products
endless-0.pc
+test/run-tests
+test/smoke-tests/hello
*.py[cod]
@@ -29,6 +31,7 @@ stamp*
/m4/ltoptions.m4
/m4/ltsugar.m4
/m4/ltversion.m4
+.dirstamp
# Gettext droppings
/ABOUT-NLS
@@ -97,6 +100,7 @@ pip-log.txt
.coverage
.tox
nosetests.xml
+test-results.xml
# Translations
*.mo
diff --git a/Makefile.am b/Makefile.am
index d4a27a0..11a922e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,6 +20,8 @@ EXTRA_DIST = @EOS_SDK_API_NAME@.pc.in
DEFS = -DLOCALEDIR=\"$(localedir)\" @DEFS@
AM_CFLAGS = @STRICT_CFLAGS@
+# Generated files that 'make clean' should erase
+CLEANFILES =
DISTCLEANFILES =
# # # LIBRARY # # #
@@ -37,3 +39,13 @@ nobase_endlessinclude_HEADERS = \
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = @EOS_SDK_API_NAME@.pc
DISTCLEANFILES += @EOS_SDK_API_NAME@.pc
+
+# # # TESTS # # #
+
+include $(top_srcdir)/test/Makefile.am
+
+# Run tests when running 'make check'
+TESTS_ENVIRONMENT = gtester -k -o test-results.xml
+TESTS = test/run-tests
+
+CLEANFILES += test-results.xml
diff --git a/endless/Makefile.am b/endless/Makefile.am
index 89af544..e653097 100644
--- a/endless/Makefile.am
+++ b/endless/Makefile.am
@@ -10,7 +10,7 @@ endless_private_installed_headers = \
endless_library_sources = \
endless/hello.c \
- endless/init.c
+ endless/init.c endless/init-private.h
# Endless GUI library
lib_LTLIBRARIES = libendless-@EOS_SDK_API_VERSION@.la
diff --git a/endless/init-private.h b/endless/init-private.h
new file mode 100644
index 0000000..d242d5e
--- /dev/null
+++ b/endless/init-private.h
@@ -0,0 +1,14 @@
+/* Copyright 2013 Endless Mobile, Inc. */
+
+#ifndef EOS_INIT_PRIVATE_H
+#define EOS_INIT_PRIVATE_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+gboolean eos_is_inited (void);
+
+G_END_DECLS
+
+#endif /* EOS_INIT_PRIVATE_H */
diff --git a/endless/init.c b/endless/init.c
index 69ce87b..cf1cdeb 100644
--- a/endless/init.c
+++ b/endless/init.c
@@ -4,6 +4,8 @@
#include <glib.h>
#include <glib/gi18n-lib.h>
+#include "init-private.h"
+
/* Constructors supported since GCC 2.7; I have this on GLib's authority. This
should also work on Clang. */
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
@@ -38,3 +40,14 @@ _eos_init (void)
_eos_initialized = TRUE;
}
}
+
+/*
+ * eos_is_inited:
+ *
+ * For testing purposes.
+ */
+gboolean
+eos_is_inited (void)
+{
+ return _eos_initialized;
+}
diff --git a/test/Makefile.am b/test/Makefile.am
new file mode 100644
index 0000000..85c0fe7
--- /dev/null
+++ b/test/Makefile.am
@@ -0,0 +1,19 @@
+# Copyright 2013 Endless Mobile, Inc.
+
+noinst_PROGRAMS = \
+ test/run-tests \
+ test/smoke-tests/hello
+
+TEST_FLAGS = @EOS_SDK_CFLAGS@ -I$(top_srcdir)
+TEST_LIBS = @EOS_SDK_LIBS@ $(top_builddir)/libendless-@EOS_SDK_API_VERSION@.la
+
+test_run_tests_SOURCES = \
+ test/run-tests.c \
+ test/test-init.c \
+ test/test-hello.c
+test_run_tests_CPPFLAGS = $(TEST_FLAGS)
+test_run_tests_LDADD = $(TEST_LIBS)
+
+test_smoke_tests_hello_SOURCES = test/smoke-tests/hello.c
+test_smoke_tests_hello_CPPFLAGS = $(TEST_FLAGS)
+test_smoke_tests_hello_LDADD = $(TEST_LIBS)
diff --git a/test/run-tests.c b/test/run-tests.c
new file mode 100644
index 0000000..ba016bc
--- /dev/null
+++ b/test/run-tests.c
@@ -0,0 +1,18 @@
+/* Copyright 2013 Endless Mobile, Inc. */
+
+#include <glib-object.h>
+#include <glib.h>
+
+#include "run-tests.h"
+
+int
+main (int argc,
+ char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+
+ add_init_tests ();
+ add_hello_tests ();
+
+ return g_test_run ();
+}
diff --git a/test/run-tests.h b/test/run-tests.h
new file mode 100644
index 0000000..3efac9e
--- /dev/null
+++ b/test/run-tests.h
@@ -0,0 +1,9 @@
+/* Copyright 2013 Endless Mobile, Inc. */
+
+#ifndef RUN_TESTS_H
+#define RUN_TESTS_H
+
+void add_init_tests (void);
+void add_hello_tests (void);
+
+#endif /* RUN_TESTS_H */
diff --git a/test/smoke-tests/hello.c b/test/smoke-tests/hello.c
new file mode 100644
index 0000000..4538835
--- /dev/null
+++ b/test/smoke-tests/hello.c
@@ -0,0 +1,11 @@
+/* Copyright 2013 Endless Mobile, Inc. */
+
+#include <endless/endless.h>
+
+int
+main (int argc,
+ char **argv)
+{
+ eos_hello_sample_function (NULL, NULL);
+ return 0;
+}
diff --git a/test/test-hello.c b/test/test-hello.c
new file mode 100644
index 0000000..687f14d
--- /dev/null
+++ b/test/test-hello.c
@@ -0,0 +1,59 @@
+/* Copyright 2013 Endless Mobile, Inc. */
+
+#include <stdlib.h>
+#include <glib.h>
+#include <endless/endless.h>
+
+#include "run-tests.h"
+
+#define EXPECTED_HELLO_STRING "Hello, world!\n"
+
+static void
+test_hello_stdout (void)
+{
+ GError *error = NULL;
+
+ /* Unix-only test */
+ if (g_test_trap_fork(0 /* timeout */, G_TEST_TRAP_SILENCE_STDOUT))
+ {
+ gboolean success = eos_hello_sample_function (NULL, &error);
+ g_assert (success);
+ g_assert_no_error (error);
+ exit (0);
+ }
+
+ g_test_trap_assert_passed ();
+ g_test_trap_assert_stdout (EXPECTED_HELLO_STRING);
+}
+
+static void
+test_hello_gfile (void)
+{
+ GError *error = NULL;
+ GFileIOStream *stream;
+ GFile *file = g_file_new_tmp ("sdktestXXXXXX", &stream, &error);
+ gboolean success;
+ char *file_contents;
+
+ g_assert_no_error (error);
+ g_io_stream_close (G_IO_STREAM (stream), NULL, &error);
+ g_assert_no_error (error);
+
+ success = eos_hello_sample_function (file, &error);
+ g_assert (success);
+ g_assert_no_error (error);
+
+ g_file_load_contents (file, NULL, &file_contents, NULL, NULL, &error);
+ g_assert_no_error (error);
+ g_assert_cmpstr (file_contents, ==, EXPECTED_HELLO_STRING);
+
+ g_free (file_contents);
+ g_object_unref (file);
+}
+
+void
+add_hello_tests (void)
+{
+ g_test_add_func ("/hello/stdout", test_hello_stdout);
+ g_test_add_func ("/hello/gfile", test_hello_gfile);
+}
diff --git a/test/test-init.c b/test/test-init.c
new file mode 100644
index 0000000..1fd856e
--- /dev/null
+++ b/test/test-init.c
@@ -0,0 +1,19 @@
+/* Copyright 2013 Endless Mobile, Inc. */
+
+#include <glib.h>
+#include <endless/endless.h>
+#include "endless/init-private.h"
+
+#include "run-tests.h"
+
+static void
+test_constructor_called (void)
+{
+ g_assert (eos_is_inited ());
+}
+
+void
+add_init_tests (void)
+{
+ g_test_add_func ("/init/constructor-called", test_constructor_called);
+}