diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/scanner/Identfilter-1.0-expected.gir | 46 | ||||
-rw-r--r-- | tests/scanner/Makefile.am | 16 | ||||
-rw-r--r-- | tests/scanner/identfilter.h | 11 | ||||
-rw-r--r-- | tests/scanner/identfilter.py | 50 | ||||
-rw-r--r-- | tests/scanner/test_transformer.py | 30 |
5 files changed, 152 insertions, 1 deletions
diff --git a/tests/scanner/Identfilter-1.0-expected.gir b/tests/scanner/Identfilter-1.0-expected.gir new file mode 100644 index 00000000..5c4590e4 --- /dev/null +++ b/tests/scanner/Identfilter-1.0-expected.gir @@ -0,0 +1,46 @@ +<?xml version="1.0"?> +<!-- This file was automatically generated from C sources - DO NOT EDIT! +To affect the contents of this file, edit the original C definitions, +and/or use gtk-doc annotations. --> +<repository version="1.2" + xmlns="http://www.gtk.org/introspection/core/1.0" + xmlns:c="http://www.gtk.org/introspection/c/1.0" + xmlns:glib="http://www.gtk.org/introspection/glib/1.0"> + <namespace name="Identfilter" + version="1.0" + shared-library="" + c:identifier-prefixes="Identfilter" + c:symbol-prefixes="identfilter"> + <record name="Context" c:type="identfilter_t" disguised="1"> + </record> + <record name="Object" c:type="identfilter_object_t" disguised="1"> + <method name="foo_method" c:identifier="identfilter_object_foo_method"> + <return-value transfer-ownership="none"> + <type name="none" c:type="void"/> + </return-value> + <parameters> + <instance-parameter name="self" transfer-ownership="none"> + <type name="Object" c:type="identfilter_object_t*"/> + </instance-parameter> + </parameters> + </method> + <method name="free" c:identifier="identfilter_object_free"> + <return-value transfer-ownership="none"> + <type name="none" c:type="void"/> + </return-value> + <parameters> + <instance-parameter name="self" transfer-ownership="none"> + <type name="Object" c:type="identfilter_object_t*"/> + </instance-parameter> + </parameters> + </method> + <function name="new" + c:identifier="identfilter_object_new" + introspectable="0"> + <return-value> + <type name="Object" c:type="identfilter_object_t*"/> + </return-value> + </function> + </record> + </namespace> +</repository> diff --git a/tests/scanner/Makefile.am b/tests/scanner/Makefile.am index f697b4b4..abdaed8e 100644 --- a/tests/scanner/Makefile.am +++ b/tests/scanner/Makefile.am @@ -161,10 +161,24 @@ EXTRA_DIST += \ headeronly.h \ Headeronly-1.0-expected.gir CLEANFILES += Headeronly-1.0.gir +CHECKGIRS += Headeronly-1.0.gir Headeronly-1.0.gir: headeronly.h $(AM_V_GEN) $(INTROSPECTION_SCANNER) $(INTROSPECTION_SCANNER_ARGS) --warn-all --warn-error --reparse-validate --namespace=Headeronly --nsversion=1.0 --header-only --output=$@ $< +EXTRA_DIST += \ + identfilter.h \ + Identifilter-1.0-expected.gir +CLEANFILES += Identfilter-1.0.gir +CHECKGIRS += Identfilter-1.0.gir + +Identfilter-1.0.gir: identfilter.h + $(AM_V_GEN) $(INTROSPECTION_SCANNER) $(INTROSPECTION_SCANNER_ARGS) \ + --warn-all --reparse-validate \ + --namespace=Identfilter --accept-unprefixed --nsversion=1.0 --header-only \ + --identifier-filter-cmd="$(PYTHON) $(srcdir)/identfilter.py" \ + --output=$@ $< + if BUILD_DOCTOOL DOCGIRS = Regress-1.0.gir CHECKDOCS = $(DOCGIRS:.gir=-C) $(DOCGIRS:.gir=-Python) $(DOCGIRS:.gir=-Gjs) $(DOCGIRS:.gir=-sections.txt) @@ -196,7 +210,7 @@ PYTESTS = \ test_sourcescanner.py \ test_transformer.py -TESTS = Headeronly-1.0.gir $(CHECKGIRS) $(CHECKDOCS) $(TYPELIBS) $(PYTESTS) +TESTS = $(CHECKGIRS) $(CHECKDOCS) $(TYPELIBS) $(PYTESTS) TESTS_ENVIRONMENT = srcdir=$(srcdir) top_srcdir=$(top_srcdir) builddir=$(builddir) top_builddir=$(top_builddir) \ PYTHON=$(PYTHON) UNINSTALLED_INTROSPECTION_SRCDIR=$(top_srcdir) LOG_COMPILER = $(top_srcdir)/tests/gi-tester diff --git a/tests/scanner/identfilter.h b/tests/scanner/identfilter.h new file mode 100644 index 00000000..43ad5098 --- /dev/null +++ b/tests/scanner/identfilter.h @@ -0,0 +1,11 @@ +#ifndef __IDENTFILTER_H__ +#define __IDENTFILTER_H__ + +typedef struct _identfilter identfilter_t; +typedef struct _identfilter_object identfilter_object_t; + +identfilter_object_t *identfilter_object_new (void); +void identfilter_object_foo_method (identfilter_object_t *self); +void identfilter_object_free (identfilter_object_t *self); + +#endif diff --git a/tests/scanner/identfilter.py b/tests/scanner/identfilter.py new file mode 100644 index 00000000..1138112a --- /dev/null +++ b/tests/scanner/identfilter.py @@ -0,0 +1,50 @@ +# -*- Mode: Python; py-indent-offset: 4 -*- +# vim: tabstop=4 shiftwidth=4 expandtab +# +# Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +# USA + +""" +Script which reads identifiers in the form of "foo_bar_t" from stdin and +translates them to title case like "FooBar", stripping the "_t" suffix. +This also adds a special case where a context like identifier with the same +name as the library is translated into "Context" in GI (useful in situations +like cairo_t -> Context. +""" + +import sys + + +def ensure_title_case(text): + # Special case identfilter_t which starts with the same name as the GI + # namespace we are trying to achieve. + if text == 'identfilter_t': + return 'IdentfilterContext' + + # Strip off "_t" suffix. + if text.endswith('_t'): + text = text[:-2] + + # Split text on underscores and re-join title casing each part + text = ''.join(part.title() for part in text.split('_')) + + return text + + +if __name__ == '__main__': + text = ensure_title_case(sys.stdin.read()) + sys.stdout.write(text) diff --git a/tests/scanner/test_transformer.py b/tests/scanner/test_transformer.py index 39c54a4d..37dbce91 100644 --- a/tests/scanner/test_transformer.py +++ b/tests/scanner/test_transformer.py @@ -36,6 +36,36 @@ def load_namespace_from_source_string(namespace, source): xformer.parse(ss.get_symbols()) +class TestIdentifierFilter(unittest.TestCase): + def test_underscore_t_sed_filter(self): + cmd = r"sed " \ + r"-e 's/^test_t$/TestContext/' " \ + r"-e 's/\(.*\)_t$/\1/' " \ + r"-e 's/^test_/Test_/' " \ + r"-e 's/_\([a-z]\)/\u\1/g'" + + namespace = ast.Namespace('Test', '1.0') + xformer = Transformer(namespace, identifier_filter_cmd=cmd) + + self.assertEqual(xformer.strip_identifier('test_t'), 'Context') + self.assertEqual(xformer.strip_identifier('test_foo_t'), 'Foo') + self.assertEqual(xformer.strip_identifier('test_foot'), 'Foot') + self.assertEqual(xformer.strip_identifier('test_foo_bart'), 'FooBart') + self.assertEqual(xformer.strip_identifier('test_foo_tart'), 'FooTart') + + def test_invalid_command(self): + cmd = r'this-is-not-a-real-command' + namespace = ast.Namespace('Test', '1.0') + xformer = Transformer(namespace, identifier_filter_cmd=cmd) + self.assertRaises(ValueError, xformer.strip_identifier, 'test_t') + + def test_invalid_argument(self): + cmd = r'sed --not-a-valid-argument' + namespace = ast.Namespace('Test', '1.0') + xformer = Transformer(namespace, identifier_filter_cmd=cmd) + self.assertRaises(ValueError, xformer.strip_identifier, 'test_t') + + class TestStructTypedefs(unittest.TestCase): def setUp(self): # Hack to set logging singleton |