[Telepathy-commits] [telepathy-glib/master] Add a test that asserts that TP_ERRORS contains every error defined by the spec
Simon McVittie
simon.mcvittie at collabora.co.uk
Fri Jan 30 04:01:46 PST 2009
Now that we don't auto-generate TP_ERRORS, we need to verify that
errors added in the spec get added to the TpError enum by the
maintainer when a new spec is imported. This is done by generating
a test that will be run during `make check`.
Sample output:
/* org.freedesktop.Telepathy.Error.Busy */
value_by_name = g_enum_get_value_by_name (klass, "TP_ERROR_BUSY");
value_by_nick = g_enum_get_value_by_nick (klass, "Busy");
g_assert (value_by_name != NULL);
g_assert (value_by_nick != NULL);
g_assert_cmpint (value_by_name->value, ==, TP_ERROR_BUSY);
g_assert_cmpint (value_by_nick->value, ==, TP_ERROR_BUSY);
g_assert_cmpstr (value_by_name->value_name, ==, "TP_ERROR_BUSY");
g_assert_cmpstr (value_by_nick->value_name, ==, "TP_ERROR_BUSY");
g_assert_cmpstr (value_by_name->value_nick, ==, "Busy");
g_assert_cmpstr (value_by_nick->value_nick, ==, "Busy");
---
.gitignore | 1 +
tests/dbus/Makefile.am | 9 ++++++
tests/dbus/error-enum.c | 18 +++++++++++++
tools/Makefile.am | 1 +
tools/glib-errors-check-gen.py | 54 ++++++++++++++++++++++++++++++++++++++++
5 files changed, 83 insertions(+), 0 deletions(-)
create mode 100644 tests/dbus/error-enum.c
create mode 100644 tools/glib-errors-check-gen.py
diff --git a/.gitignore b/.gitignore
index 54aeb88..d7f8bfd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -78,6 +78,7 @@ tests/dbus/test-contacts-bug-19101
tests/dbus/test-contacts-mixin
tests/dbus/test-dbus
tests/dbus/test-disconnection
+tests/dbus/test-error-enum
tests/dbus/test-example-no-protocols
tests/dbus/test-finalized-in-invalidated-handler
tests/dbus/test-group-mixin
diff --git a/tests/dbus/Makefile.am b/tests/dbus/Makefile.am
index 7a1c22c..f97dc8b 100644
--- a/tests/dbus/Makefile.am
+++ b/tests/dbus/Makefile.am
@@ -13,6 +13,7 @@ noinst_PROGRAMS = \
test-contacts-mixin \
test-dbus \
test-disconnection \
+ test-error-enum \
test-example-no-protocols \
test-finalized-in-invalidated-handler \
test-group-mixin \
@@ -54,6 +55,9 @@ test_contacts_bug_19101_SOURCES = contacts-bug-19101.c
test_contacts_mixin_SOURCES = contacts-mixin.c
+test_error_enum_SOURCES = error-enum.c
+nodist_test_error_enum_SOURCES = _gen/errors-check.h
+
test_finalized_in_invalidated_handler_SOURCES = \
finalized-in-invalidated-handler.c
@@ -126,6 +130,7 @@ VALGRIND_TESTS_ENVIRONMENT = \
$(VALGRIND_FLAGS)
BUILT_SOURCES = \
+ _gen/errors-check.h \
_gen/svc.h \
_gen/svc.c
@@ -145,3 +150,7 @@ _gen/svc.c _gen/svc.h: with-properties.xml \
--filename=_gen/svc \
--signal-marshal-prefix=NOT_NEEDED \
$< Test_Svc_
+
+_gen/errors-check.h: $(top_srcdir)/spec/errors.xml \
+ $(top_srcdir)/tools/glib-errors-check-gen.py
+ $(PYTHON) $(top_srcdir)/tools/glib-errors-check-gen.py $< > $@
diff --git a/tests/dbus/error-enum.c b/tests/dbus/error-enum.c
new file mode 100644
index 0000000..4eb318e
--- /dev/null
+++ b/tests/dbus/error-enum.c
@@ -0,0 +1,18 @@
+#include <telepathy-glib/errors.h>
+
+static void
+test_tp_errors (void)
+{
+#include "tests/dbus/_gen/errors-check.h"
+}
+
+int
+main (int argc,
+ char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/test-error-enum/TP_ERRORS", test_tp_errors);
+
+ return g_test_run ();
+}
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 4af909a..4d44471 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -16,6 +16,7 @@ EXTRA_DIST = \
git-which-branch.sh \
glib-client-gen.py \
glib-client-marshaller-gen.py \
+ glib-errors-check-gen.py \
glib-errors-enum-body-gen.py \
glib-errors-enum-header-gen.py \
glib-ginterface-gen.py \
diff --git a/tools/glib-errors-check-gen.py b/tools/glib-errors-check-gen.py
new file mode 100644
index 0000000..3d16b8e
--- /dev/null
+++ b/tools/glib-errors-check-gen.py
@@ -0,0 +1,54 @@
+#!/usr/bin/python
+
+import sys
+import xml.dom.minidom
+
+from libglibcodegen import NS_TP, camelcase_to_upper, get_docstring, \
+ get_descendant_text
+
+class Generator(object):
+ def __init__(self, dom):
+ self.dom = dom
+ self.errors = self.dom.getElementsByTagNameNS(NS_TP, 'errors')[0]
+
+ def __call__(self):
+
+ print '{'
+ print ' GEnumClass *klass;'
+ print ' GEnumValue *value_by_name;'
+ print ' GEnumValue *value_by_nick;'
+ print ''
+ print ' g_type_init ();'
+ print ' klass = g_type_class_ref (TP_TYPE_ERROR);'
+
+ for error in self.errors.getElementsByTagNameNS(NS_TP, 'error'):
+ ns = error.parentNode.getAttribute('namespace')
+ nick = error.getAttribute('name').replace(' ', '')
+ enum = 'TP_ERROR_' + camelcase_to_upper(nick.replace('.', ''))
+
+ print ''
+ print ' /* %s.%s */' % (ns, nick)
+ print (' value_by_name = g_enum_get_value_by_name (klass, "%s");'
+ % enum)
+ print (' value_by_nick = g_enum_get_value_by_nick (klass, "%s");'
+ % nick)
+ print (' g_assert (value_by_name != NULL);')
+ print (' g_assert (value_by_nick != NULL);')
+ print (' g_assert_cmpint (value_by_name->value, ==, %s);'
+ % enum)
+ print (' g_assert_cmpint (value_by_nick->value, ==, %s);'
+ % enum)
+ print (' g_assert_cmpstr (value_by_name->value_name, ==, "%s");'
+ % enum)
+ print (' g_assert_cmpstr (value_by_nick->value_name, ==, "%s");'
+ % enum)
+ print (' g_assert_cmpstr (value_by_name->value_nick, ==, "%s");'
+ % nick)
+ print (' g_assert_cmpstr (value_by_nick->value_nick, ==, "%s");'
+ % nick)
+
+ print '}'
+
+if __name__ == '__main__':
+ argv = sys.argv[1:]
+ Generator(xml.dom.minidom.parse(argv[0]))()
--
1.5.6.5
More information about the telepathy-commits
mailing list