TEST_FIXTURE(EnchantBrokerFreeDictTestFixture, EnchantBrokerFreeDict_HasTwoInstances_NoCrash) { EnchantDict* dictionary1 = enchant_broker_request_dict(_broker, "en-GB"); EnchantDict* dictionary2 = enchant_broker_request_dict(_broker, "en-GB"); enchant_broker_free_dict(_broker, dictionary1); enchant_broker_free_dict(_broker, dictionary2); }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_CalledTwice_CallsProviderOnceReturnsSame) { _dict = enchant_broker_request_dict(_broker, "en_GB"); requestDictionaryCalled = false; EnchantDict* dict = enchant_broker_request_dict(_broker, "en_GB"); CHECK(!requestDictionaryCalled); CHECK_EQUAL(_dict, dict); }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_ProviderDoesNotHave_CallsProvider) { _dict = enchant_broker_request_dict(_broker, "en"); CHECK_EQUAL((void*)NULL, (void*)_dict); CHECK(requestDictionaryCalled); }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_ProviderHasBase_CallsProvider) { _dict = enchant_broker_request_dict(_broker, "qaa_CA"); CHECK(_dict); CHECK(requestDictionaryCalled); }
void create_dict() { EnchantBroker * broker; broker = enchant_broker_init(); dict = enchant_broker_request_dict(broker, "en_GB"); }
static void spell_setup_languages (void) { static GSettings *gsettings = NULL; gchar *str; if (gsettings == NULL) { /* FIXME: this is never uninitialised */ gsettings = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA); g_signal_connect (gsettings, "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES, G_CALLBACK (spell_notify_languages_cb), NULL); } if (languages) { return; } languages = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) empathy_spell_free_language); str = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES); if (str != NULL) { gchar **strv; gint i; strv = g_strsplit (str, ",", -1); i = 0; while (strv && strv[i]) { SpellLanguage *lang; DEBUG ("Setting up language:'%s'", strv[i]); lang = g_slice_new0 (SpellLanguage); lang->config = enchant_broker_init (); lang->speller = enchant_broker_request_dict (lang->config, strv[i]); if (lang->speller == NULL) { DEBUG ("language '%s' has no valid dict", strv[i]); } else { g_hash_table_insert (languages, g_strdup (strv[i]), lang); } i++; } if (strv) { g_strfreev (strv); } g_free (str); } }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_InvalidTag_NULL_ErrorSet) { _dict = enchant_broker_request_dict(_broker, "en~US"); CHECK_EQUAL((void*)NULL, _dict); CHECK(NULL != enchant_broker_get_error(_broker)); }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_NullLanguageTag_NULL) { _dict = enchant_broker_request_dict(_broker, NULL); CHECK_EQUAL((void*)NULL, (void*)_dict); CHECK(!requestDictionaryCalled); }
///////////////////////////////////////////////////////////////////////////// // Test Error Conditions TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_NullBroker_NULL) { _dict = enchant_broker_request_dict(NULL, "en_GB"); CHECK_EQUAL((void*)NULL, (void*)_dict); CHECK(!requestDictionaryCalled); }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_HasPreviousError_ErrorCleared) { SetErrorOnMockProvider("something bad happened"); _dict = enchant_broker_request_dict(_broker, "en-GB"); CHECK_EQUAL((void*)NULL, (void*)enchant_broker_get_error(_broker)); }
void sc_speller_reinit_enchant_dict(void) { const gchar *lang = sc_info->default_language; /* Release a previous dict object */ if (sc_speller_dict != NULL) enchant_broker_free_dict(sc_speller_broker, sc_speller_dict); #if HAVE_ENCHANT_1_5 { const gchar *old_path; gchar *new_path; /* add custom dictionary path for myspell (primarily used on Windows) */ old_path = enchant_broker_get_param(sc_speller_broker, "enchant.myspell.dictionary.path"); if (old_path != NULL) new_path = g_strconcat( old_path, G_SEARCHPATH_SEPARATOR_S, sc_info->dictionary_dir, NULL); else new_path = sc_info->dictionary_dir; enchant_broker_set_param(sc_speller_broker, "enchant.myspell.dictionary.path", new_path); if (new_path != sc_info->dictionary_dir) g_free(new_path); } #endif create_dicts_array(); /* Check if the stored default dictionary is (still) available, fall back to the first * one in the list if not */ if (EMPTY(lang) || ! check_default_lang()) { if (sc_info->dicts->len > 0) { lang = g_ptr_array_index(sc_info->dicts, 0); g_warning("Stored language ('%s') could not be loaded. Falling back to '%s'", sc_info->default_language, lang); } else g_warning("Stored language ('%s') could not be loaded.", sc_info->default_language); } /* Request new dict object */ if (! EMPTY(lang)) sc_speller_dict = enchant_broker_request_dict(sc_speller_broker, lang); else sc_speller_dict = NULL; if (sc_speller_dict == NULL) { broker_init_failed(); gtk_widget_set_sensitive(sc_info->menu_item, FALSE); } else { gtk_widget_set_sensitive(sc_info->menu_item, TRUE); } }
TEST_FIXTURE(EnchantBrokerTestFixture, EnchantBrokerRequestDictionary_ProviderLacksListDictionaries_CallbackNeverCalled) { requestDictionaryCalled = false; EnchantDict* dict = enchant_broker_request_dict(_broker, "en_GB"); CHECK_EQUAL((void*)NULL, dict); CHECK(!requestDictionaryCalled); }
TEST_FIXTURE(EnchantBrokerFreeTestFixture, EnchantBrokerFree_DictionaryNotFreed_FreesDictionary) { EnchantDict* dict = enchant_broker_request_dict(_broker, "en_GB"); CHECK(dict); CHECK(!disposeDictionaryCalled); enchant_broker_free(_broker); _broker = NULL; CHECK(disposeDictionaryCalled); }
TEST_FIXTURE(EnchantBrokerDictExists_ProviderImplementsAll_TestFixture, EnchantBrokerDictExists_CalledWhenDictionaryIsInUse_DoesNotCallAnyMethods_GetsCachedResult) { EnchantDict* dict = enchant_broker_request_dict(_broker, "en-GB"); enchant_broker_dict_exists(_broker, "en-GB"); CHECK_EQUAL(0,listDictionariesCalled); CHECK_EQUAL(0,dictionaryExistsCalled); enchant_broker_free_dict(_broker, dict); }
static void spellchecker_reload_dicts() { while (!g_pEnchantDicts->isEmpty()) enchant_broker_free_dict(g_pEnchantBroker, g_pEnchantDicts->takeFirst()); const QStringList& wantedDictionaries = KVI_OPTION_STRINGLIST(KviOption_stringlistSpellCheckerDictionaries); foreach(QString szLang, wantedDictionaries) { EnchantDict* pDict = enchant_broker_request_dict(g_pEnchantBroker, szLang.toUtf8().data()); if (pDict) { g_pEnchantDicts->append(pDict); } else { qDebug("Can't load spellchecker dictionary %s: %s", szLang.toUtf8().data(), enchant_broker_get_error(g_pEnchantBroker)); } }
static void spell_setup_languages (void) { gchar *str; if (!empathy_conf_notify_inited) { empathy_conf_notify_add (empathy_conf_get (), EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES, spell_notify_languages_cb, NULL); empathy_conf_notify_inited = TRUE; } if (languages) { return; } if (empathy_conf_get_string (empathy_conf_get (), EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES, &str) && str) { gchar **strv; gint i; strv = g_strsplit (str, ",", -1); i = 0; while (strv && strv[i]) { SpellLanguage *lang; DEBUG ("Setting up language:'%s'", strv[i]); lang = g_slice_new0 (SpellLanguage); lang->config = enchant_broker_init (); lang->speller = enchant_broker_request_dict (lang->config, strv[i]); languages = g_list_append (languages, lang); i++; } if (strv) { g_strfreev (strv); } g_free (str); } }
int main (int argc, char **argv) { EnchantBroker *broker; EnchantDict *dict; char * lang_tag = NULL; int mode = 0, i; for (i = 1; i < argc; i++) { if (!strcmp (argv[i], "-lang")) { if (i < (argc - 1)) { lang_tag = g_strdup (argv[++i]); } else { lang_tag = enchant_get_user_language(); if (!lang_tag || !strcmp (lang_tag, "C")) { if (lang_tag) /* lang might be "C" */ g_free (lang_tag); lang_tag = g_strdup ("en"); } } mode = 1; } else if (!strcmp (argv[i], "-h") || !strcmp (argv[i], "-?") || !strcmp(argv[i], "-help")) { printf ("%s [-lang [language_tag]] [-list-dicts] [-h] [-v]\n", argv[0]); if (lang_tag) g_free (lang_tag); return 0; } else if (!strcmp (argv[i], "-v") || !strcmp (argv[i], "-version")) { printf ("%s %s\n", argv[0], VERSION); if (lang_tag) g_free (lang_tag); return 0; } else if (!strcmp (argv[i], "-list-dicts")) { mode = 2; } } broker = enchant_broker_init (); if (mode == 0) { enchant_broker_describe (broker, enumerate_providers, stdout); } else if (mode == 1) { if (!lang_tag) { printf ("Error: language tag not specified and environment variable $LANG not set.\n"); enchant_broker_free (broker); return 1; } dict = enchant_broker_request_dict (broker, lang_tag); if (!dict) { printf ("No dictionary available for '%s'.\n", lang_tag); if (lang_tag) g_free (lang_tag); enchant_broker_free (broker); return 1; } else { enchant_dict_describe (dict, describe_dict, stdout); enchant_broker_free_dict (broker, dict); } } else if (mode == 2) { enchant_broker_list_dicts (broker, enumerate_dicts, stdout); } if (lang_tag) g_free (lang_tag); enchant_broker_free (broker); return 0; }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_VerticalTabAfterLanguageTag_NotRemoved) { _dict = enchant_broker_request_dict(_broker, "en_GB\v"); CHECK_EQUAL((void*)NULL, (void*)_dict); }
//Setup EnchantBrokerFreeDictNoDisposeTestFixture():EnchantBrokerTestFixture(NoDisposeProviderConfiguration) { _dictionary = enchant_broker_request_dict(_broker, "en-GB"); dictionaryToBeFreed=NULL; }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_DifferentCase_NoRegion_Finds) { _dict = enchant_broker_request_dict(_broker, "QAA"); CHECK(_dict); }
gboolean suggestions_init() { gchar dict_tag[DICT_TAG_MAX_LENGTH] = ""; if(g_module_supported() && (mod_enchant = g_module_open(ENCHANT_FILE, G_MODULE_BIND_LAZY))) { g_module_symbol(mod_enchant, G_STRINGIFY(enchant_broker_init), (gpointer *) &enchant_broker_init); g_module_symbol(mod_enchant, G_STRINGIFY(enchant_broker_free), (gpointer *) &enchant_broker_free); g_module_symbol(mod_enchant, G_STRINGIFY(enchant_broker_list_dicts), (gpointer *) &enchant_broker_list_dicts); g_module_symbol(mod_enchant, G_STRINGIFY(enchant_broker_dict_exists), (gpointer *) &enchant_broker_dict_exists); g_module_symbol(mod_enchant, G_STRINGIFY(enchant_broker_request_dict), (gpointer *) &enchant_broker_request_dict); g_module_symbol(mod_enchant, G_STRINGIFY(enchant_broker_free_dict), (gpointer *) &enchant_broker_free_dict); g_module_symbol(mod_enchant, G_STRINGIFY(enchant_dict_check), (gpointer *) &enchant_dict_check); g_module_symbol(mod_enchant, G_STRINGIFY(enchant_dict_suggest), (gpointer *) &enchant_dict_suggest); g_module_symbol(mod_enchant, G_STRINGIFY(enchant_dict_free_string_list), (gpointer *) &enchant_dict_free_string_list); // in older version of Enchant, enchant_dict_free_string_list might be absent, they will have the // now deprecated function enchant_dict_free_suggestions if(NULL == enchant_dict_free_string_list) g_module_symbol(mod_enchant, G_STRINGIFY(enchant_dict_free_string_list), (gpointer *) &enchant_dict_free_string_list); // check if we have obtained the essential function pointers if(NULL != enchant_broker_init && NULL != enchant_broker_free && NULL != enchant_broker_dict_exists && NULL != enchant_broker_request_dict && NULL != enchant_broker_free_dict && NULL != enchant_dict_check && NULL != enchant_dict_suggest && NULL != enchant_dict_free_string_list) { enchant_broker = enchant_broker_init(); if(enchant_broker) { /* if the sys. lang is suitable, copy that else copy the default lang tag */ if(try_sys_lang(dict_tag, DICT_TAG_MAX_LENGTH)) { if(enchant_broker_dict_exists(enchant_broker, dict_tag)) { enchant_dict = enchant_broker_request_dict(enchant_broker, dict_tag); return TRUE; } G_MESSAGE("Suggestions: Couldn't get '%s' dict. Looking for alternatives...\n", dict_lang_tag); } g_strlcpy(dict_tag, dict_lang_tag, DICT_TAG_MAX_LENGTH); /* if the req. dict. doesn't exist and if list dict func. exists then try to enumerate the dictionaries and see if a compatible one can be found */ if(!enchant_broker_dict_exists(enchant_broker, dict_tag) && enchant_broker_list_dicts) { G_MESSAGE("Suggestions: Couldn't get '%s' dict. Looking for alternatives...\n", dict_lang_tag); dict_tag[0] = '\0'; enchant_broker_list_dicts(enchant_broker, find_dictionary, dict_tag); } if(dict_tag[0] != '\0') { enchant_dict = enchant_broker_request_dict(enchant_broker, dict_tag); G_MESSAGE("Suggestions module successfully loaded"); return TRUE; } } } } if(enchant_broker) { enchant_broker_free(enchant_broker); enchant_broker = NULL; } if(mod_enchant) { g_module_close(mod_enchant); mod_enchant = NULL; } G_MESSAGE("Failed to load suggestions module"); return FALSE; }
static int parse_file (FILE * in, FILE * out, IspellMode_t mode, int countLines, gchar *dictionary) { EnchantBroker * broker; EnchantDict * dict; GString * str, * word = NULL; GSList * tokens, *token_ptr; gchar * lang; size_t pos, lineCount = 0; gboolean was_last_line = FALSE, corrected_something = FALSE, terse_mode = FALSE; if (mode == MODE_A) print_version (out); if (dictionary) { lang = convert_language_code (dictionary); } else { lang = enchant_get_user_language(); if(!lang) return 1; } /* Enchant will get rid of useless trailing garbage like de_DE@euro or de_DE.ISO-8859-15 */ broker = enchant_broker_init (); dict = enchant_broker_request_dict (broker, lang); if (!dict) { fprintf (stderr, "Couldn't create a dictionary for %s\n", lang); g_free (lang); enchant_broker_free (broker); return 1; } g_free (lang); str = g_string_new (NULL); while (!was_last_line) { gboolean mode_A_no_command = FALSE; was_last_line = consume_line (in, str); if (countLines) lineCount++; if (str->len) { corrected_something = FALSE; if (mode == MODE_A) { switch (*str->str) { case '&': /* Insert uncapitalised in personal word list */ if (str->len > 1) { gunichar c = g_utf8_get_char_validated(str->str + 1, str->len); if (c > 0) { str = g_string_erase(str, 1, g_utf8_next_char(str->str + 1) - (str->str + 1)); g_string_insert_unichar(str, 1, g_unichar_tolower(c)); } } /* FALLTHROUGH */ case '*': /* Insert in personal word list */ if (str->len == 1) goto empty_word; enchant_dict_add(dict, str->str + 1, -1); break; case '@': /* Accept for this session */ if (str->len == 1) goto empty_word; enchant_dict_add_to_session(dict, str->str + 1, -1); break; case '%': /* Exit terse mode */ terse_mode = FALSE; break; case '!': /* Enter terse mode */ terse_mode = TRUE; break; /* Ignore these commands */ case '#': /* Save personal word list (enchant does this automatically) */ case '+': /* LaTeX mode */ case '-': /* nroff mode [default] */ case '~': /* change string character type (enchant is fixed to UTF-8) */ case '`': /* Enter verbose-correction mode */ break; case '$': /* Save correction for rest of session [aspell extension] */ { /* Syntax: $$ra <MISSPELLED>,<REPLACEMENT> */ gchar *prefix = "$$ra "; if (g_str_has_prefix(str->str, prefix)) { gchar *comma = g_utf8_strchr(str->str, -1, (gunichar)','); char *mis = str->str + strlen(prefix); char *cor = comma + 1; ssize_t mis_len = comma - mis; ssize_t cor_len = strlen(str->str) - (cor - str->str); enchant_dict_store_replacement(dict, mis, mis_len, cor, cor_len); } } break; case '^': /* ^ is used as prefix to prevent interpretation of original first character as a command */ /* FALLTHROUGH */ default: /* A word or words to check */ mode_A_no_command = TRUE; break; empty_word: fprintf (out, "Error: The word \"\" is invalid. Empty string.\n"); } } if (mode != MODE_A || mode_A_no_command) { token_ptr = tokens = tokenize_line (str); if (tokens == NULL) putc('\n', out); while (tokens != NULL) { corrected_something = TRUE; word = (GString *)tokens->data; tokens = tokens->next; pos = GPOINTER_TO_INT(tokens->data); tokens = tokens->next; if (mode == MODE_A) do_mode_a (out, dict, word, pos, lineCount, terse_mode); else if (mode == MODE_L) do_mode_l (out, dict, word, lineCount); g_string_free(word, TRUE); } if (token_ptr) g_slist_free (token_ptr); } } if (mode == MODE_A && corrected_something) { fwrite ("\n", 1, 1, out); } g_string_truncate (str, 0); fflush (out); } enchant_broker_free_dict (broker, dict); enchant_broker_free (broker); g_string_free (str, TRUE); return 0; }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_AtSignInLanguageTag_RemovesToTail) { _dict = enchant_broker_request_dict(_broker, "en_GB@euro"); CHECK(_dict); }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_HyphensInLanguageTag_SubstitutedWithUnderscore) { _dict = enchant_broker_request_dict(_broker, "en-GB"); CHECK(_dict); }
AspellSpeller * #endif /* USE_ENCHANT */ weechat_aspell_speller_new (const char *lang) { #ifdef USE_ENCHANT EnchantDict *new_speller; #else AspellConfig *config; AspellCanHaveError *ret; AspellSpeller *new_speller; #endif /* USE_ENCHANT */ struct t_infolist *infolist; if (!lang) return NULL; if (weechat_aspell_plugin->debug) { weechat_printf (NULL, "%s: creating new speller for lang \"%s\"", ASPELL_PLUGIN_NAME, lang); } #ifdef USE_ENCHANT new_speller = enchant_broker_request_dict (broker, lang); if (!new_speller) { weechat_printf (NULL, _("%s%s: error: unable to create speller for lang \"%s\""), weechat_prefix ("error"), ASPELL_PLUGIN_NAME, lang); return NULL; } #else /* create a speller instance for the newly created cell */ config = new_aspell_config (); aspell_config_replace (config, "lang", lang); #endif /* USE_ENCHANT */ /* apply all options */ infolist = weechat_infolist_get ("option", NULL, "aspell.option.*"); if (infolist) { while (weechat_infolist_next (infolist)) { #ifdef USE_ENCHANT /* TODO: set option with enchant */ #else aspell_config_replace (config, weechat_infolist_string (infolist, "option_name"), weechat_infolist_string (infolist, "value")); #endif /* USE_ENCHANT */ } weechat_infolist_free (infolist); } #ifndef USE_ENCHANT ret = new_aspell_speller (config); if (aspell_error (ret) != 0) { weechat_printf (NULL, "%s%s: error: %s", weechat_prefix ("error"), ASPELL_PLUGIN_NAME, aspell_error_message (ret)); delete_aspell_config (config); delete_aspell_can_have_error (ret); return NULL; } new_speller = to_aspell_speller (ret); #endif /* USE_ENCHANT */ weechat_hashtable_set (weechat_aspell_spellers, lang, new_speller); #ifndef USE_ENCHANT /* free configuration */ delete_aspell_config (config); #endif /* USE_ENCHANT */ return new_speller; }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_WhitespaceSurroundingLanguageTag_Removed) { _dict = enchant_broker_request_dict(_broker, "\n\r en_GB \t\f"); CHECK(_dict); }
TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, EnchantBrokerRequestDictionary_PeriodInLanguageTag_RemovesToTail) { _dict = enchant_broker_request_dict(_broker, "en_GB.UTF-8"); CHECK(_dict); }