test for libenchant introducing some echant bindings test enchant bindings
authorMathias Hasselmann <mathias.hasselmann@gmx.de>
Sat, 18 Aug 2007 09:26:55 +0000 (09:26 +0000)
committerMathias Hasselmann <hasselmm@src.gnome.org>
Sat, 18 Aug 2007 09:26:55 +0000 (09:26 +0000)
2007-08-18  Mathias Hasselmann  <mathias.hasselmann@gmx.de>

* configure.ac: test for libenchant
* vapi/enchant.vala: introducing some echant bindings
* tests/Makefile.am, tests/testenchant.vala: test enchant bindings

svn path=/trunk/; revision=478

ChangeLog
configure.ac
tests/Makefile.am
tests/testenchant.vala [new file with mode: 0644]
vapi/enchant.vala [new file with mode: 0644]

index 22f6711..d532c50 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-08-18  Mathias Hasselmann  <mathias.hasselmann@gmx.de>
+
+       * configure.ac: test for libenchant
+       * vapi/enchant.vala: introducing some echant bindings
+       * tests/Makefile.am, tests/testenchant.vala: test enchant bindings
+
 2007-08-17  Mathias Hasselmann  <mathias.hasselmann@gmx.de>
 
        * vapi/libglade-2.0.vala: revert last change: the field
index 2c746cb..18fe023 100644 (file)
@@ -30,11 +30,19 @@ AC_ARG_ENABLE(vapigen, AS_HELP_STRING([--enable-vapigen], [Enable VAPI generator
 AM_CONDITIONAL(ENABLE_VAPIGEN, test x$enable_vapigen = xyes)
 
 GLIB_REQUIRED=2.10.0
+ENCHANT_REQUIRED=1.3.0
 
 PKG_CHECK_MODULES(GLIB, glib-2.0 >= $GLIB_REQUIRED gobject-2.0 >= $GLIB_REQUIRED)
+
 AC_SUBST(GLIB_CFLAGS)
 AC_SUBST(GLIB_LIBS)
 
+PKG_CHECK_MODULES(ENCHANT, enchant >= $ENCHANT_REQUIRED, have_enchant=yes, have_enchant=no)
+AM_CONDITIONAL(HAVE_ENCHANT, test x$have_enchant = xyes)
+
+AC_SUBST(ENCHANT_CFLAGS)
+AC_SUBST(ENCHANT_LIBS)
+
 AC_CONFIG_FILES([Makefile
            vala-1.0.pc
            gee/Makefile
index 9f915a4..cffd56a 100644 (file)
@@ -1,5 +1,35 @@
 NULL =
 
+INCLUDES = \
+       $(GLIB_CFLAGS) \
+       $(ENCHANT_CFLAGS) \
+       $(NULL)
+AM_LDFLAGS = \
+       $(GLIB_LIBS) \
+       $(ENCHANT_LIBS) \
+       $(NULL)
+
+BUILT_SOURCES = \
+       $(NULL)
+noinst_PROGRAMS = \
+       $(NULL)
+
+if HAVE_ENCHANT
+BUILT_SOURCES += testenchant.stamp
+noinst_PROGRAMS += testenchant
+endif
+
+testenchant_SOURCES = \
+       testenchant.stamp \
+       testenchant.c \
+       testenchant.h \
+       testenchant.vala \
+       $(NULL)
+
+testenchant.stamp: $(filter %.vala,$(testenchant_SOURCES))
+       $(VALAC) --vapidir $(srcdir)/../vapi --pkg enchant $^
+       touch $@
+
 TESTS_ENVIRONMENT = $(srcdir)/testrunner.sh
 
 TESTS = \
@@ -106,4 +136,8 @@ EXTRA_DIST = \
        test-031.out \
        test-032.out \
        test-033.out \
+       \
+       testenchant.stamp \
+       testenchant.vala \
        $(NULL)
+
diff --git a/tests/testenchant.vala b/tests/testenchant.vala
new file mode 100644 (file)
index 0000000..61c904c
--- /dev/null
@@ -0,0 +1,95 @@
+using Enchant;
+using GLib;
+
+class TestEnchant {
+       static void info (string message) {
+               stdout.printf ("INFO: %s\n", message);
+       }
+
+       static void test (string message, bool result) {
+               stdout.printf ("TEST: %s: %s\n", message, result ? "SUCCESS" : "FAILURE");
+       }
+
+       static void main (string[] args) {
+               Broker broker = new Broker ();
+               weak Dict dict;
+
+               info ("providers for broker %p".printf (broker));
+               broker.describe (broker_describe_cb);
+
+               info ("dictionaries for broker %p".printf (broker));
+               broker.list_dicts (dict_describe_cb);
+
+               dict = broker.request_dict ("invalid-tag");
+
+               test ("requesting invalid dictionary", null == dict);
+               info ("broker error message".printf (broker.get_error ()));
+
+               dict = broker.request_dict ("en");
+
+               test ("requesting english dictionary", null != dict);
+               test ("broker error is null", null == broker.get_error ());
+
+               info ("description of dictionary %p".printf (dict));
+               dict.describe (dict_describe_cb);
+
+               var text = "The quick prown fox jummps over the lasy dok".split (" ");
+
+               foreach (string word in text) {
+                       weak string[] suggestions;
+                       string result;
+
+do { // FIXME: Bug 467896
+                       switch (dict.check (word)) {
+                               case 0:
+                                       result = "good";
+                                       break;
+
+                               case 1:
+                                       suggestions = dict.suggest (word);
+                                       result = "bad (%d suggestions: %s)".printf (suggestions.length, string.joinv (", ", suggestions));
+                                       break;
+
+                               case -1:
+                                       result = "error: %s".printf (dict.get_error ());
+                                       break;
+
+                               default:
+                                       assert_not_reached ();
+                                       break;
+                       }
+} while (false);
+
+                       info ("%s: %s".printf (word, result));
+               }
+
+               var bad_word = "the:colons:make:this:a:bad:word";
+               int result;
+
+               result = dict.is_in_session (bad_word);
+               test ("bad word is not in session", 0 == result);
+
+               result = dict.check (bad_word);
+               test ("bad word is rejected", 1 == result);
+
+               info ("adding bad word to session");
+               dict.add_to_session (bad_word);
+
+               result = dict.is_in_session (bad_word);
+               test ("bad word is in session now", 1 == result);
+
+               result = dict.check (bad_word);
+               test ("bad word is accepted now", 0 == result);
+
+               broker.free_dict (dict);
+               dict = null;
+       }
+
+       static void broker_describe_cb (string name, string desc, string libname) {
+               info ("- %s (%s) - %s".printf (name, desc, libname));
+       }
+
+       static void dict_describe_cb (string language, string provider_name, string provider_desc, string provider_libname) {
+               info ("- %s (%s) - %s".printf (language, provider_desc, provider_libname));
+       }
+}
diff --git a/vapi/enchant.vala b/vapi/enchant.vala
new file mode 100644 (file)
index 0000000..3aac5b0
--- /dev/null
@@ -0,0 +1,34 @@
+[CCode (cname_prefix = "enchant_", cheader_filename = "enchant.h")]
+namespace Enchant {
+       public static delegate void BrokerDescribeFn (string provider_name, string provider_desc, string provider_dll_file, pointer user_data);
+       public static delegate void DictDescribeFn (string lang_tag, string provider_name, string provider_desc, string provider_file, pointer user_data);
+
+       [ReferenceType (unref_function = "enchant_broker_free")]
+       public struct Broker {
+               [CCode (cname = "enchant_broker_init")]
+               public Broker ();
+
+               public weak Dict request_dict (weak string! tag);       // FIXME integrate with memory manager
+               public weak Dict request_pwl_dict (weak string! pwl);   // FIXME integrate with memory manager
+               public void free_dict (Dict! dict);                     // FIXME integrate with memory manager
+               public int dict_exists (weak string! tag);
+               public void set_ordering (weak string! tag, weak string! ordering);
+               public void describe (BrokerDescribeFn fn, pointer user_data = null);
+               public void list_dicts (DictDescribeFn fn, pointer user_data = null);
+               public weak string! get_error ();
+       }
+
+       [ReferenceType ()]
+       public struct Dict {
+               public int check (weak string! word, int len = -1);
+               public weak string[] suggest (weak string! word, int len = -1); // FIXME integrate with memory manager
+               public void free_string_list (weak string[] string_list);       // FIXME integrate with memory manager
+               public void add_to_session (weak string! word, int len = -1);
+               public int is_in_session (weak string! word, int len = -1);
+               public void store_replacement (weak string! mis, int mis_len, weak string! cor, int cor_len);
+               public void add_to_pwl (weak string! word, int len = -1);
+               public void describe (DictDescribeFn fn, pointer user_data = null);
+               [NoArrayLength ()]
+               public weak string! get_error ();
+       }
+}