diff options
author | Eric Albright <eric_albright@sil.org> | 2007-11-20 15:19:46 +0000 |
---|---|---|
committer | Eric Albright <eric_albright@sil.org> | 2007-11-20 15:19:46 +0000 |
commit | 0be330764f92699101f0cf648e068a82e27e2f15 (patch) | |
tree | f39a2c1f9475184315f3175d1842087151156d6f | |
parent | 013af6814443e957c3a8bd428697a0d36a4fc0ed (diff) | |
download | enchant-0be330764f92699101f0cf648e068a82e27e2f15.tar.gz |
add exclude functionality (enchant_dict_remove, enchant_dict_remove_from_session, enchant_dict_is_removed) and mirrored api for pwl (enchant_dict_add for enchant_dict_add_to_pwl and enchant_dict_is_added for enchant_dict_is_in_session)
git-svn-id: svn+ssh://svn.abisource.com/svnroot/enchant/trunk@22321 bcba8976-2d24-0410-9c9c-aab3bd5fdfd6
20 files changed, 1709 insertions, 232 deletions
diff --git a/msvc/unittest-enchant.vcproj b/msvc/unittest-enchant.vcproj index f3b49d8..d053e81 100644 --- a/msvc/unittest-enchant.vcproj +++ b/msvc/unittest-enchant.vcproj @@ -242,7 +242,7 @@ Name="dictionary"
>
<File
- RelativePath="..\unittests\dictionary\enchant_dict_add_to_pwl_tests.cpp"
+ RelativePath="..\unittests\dictionary\enchant_dict_add_tests.cpp"
>
</File>
<File
@@ -266,7 +266,19 @@ >
</File>
<File
- RelativePath="..\unittests\dictionary\enchant_dict_is_in_session_tests.cpp"
+ RelativePath="..\unittests\dictionary\enchant_dict_is_added_tests.cpp"
+ >
+ </File>
+ <File
+ RelativePath="..\unittests\dictionary\enchant_dict_is_removed_tests.cpp"
+ >
+ </File>
+ <File
+ RelativePath="..\unittests\dictionary\enchant_dict_remove_from_session_tests.cpp"
+ >
+ </File>
+ <File
+ RelativePath="..\unittests\dictionary\enchant_dict_remove_tests.cpp"
>
</File>
<File
diff --git a/src/enchant++.h b/src/enchant++.h index 50c8067..554d91b 100644 --- a/src/enchant++.h +++ b/src/enchant++.h @@ -113,8 +113,8 @@ namespace enchant return result; } - void add_to_pwl (const std::string & utf8word) { - enchant_dict_add_to_pwl (m_dict, utf8word.c_str(), + void add (const std::string & utf8word) { + enchant_dict_add (m_dict, utf8word.c_str(), utf8word.size()); } @@ -123,6 +123,26 @@ namespace enchant utf8word.size()); } + void is_added (const std::string & utf8word) { + enchant_dict_is_added (m_dict, utf8word.c_str(), + utf8word.size()); + } + + void remove (const std::string & utf8word) { + enchant_dict_remove (m_dict, utf8word.c_str(), + utf8word.size()); + } + + void remove_from_session (const std::string & utf8word) { + enchant_dict_remove_from_session (m_dict, utf8word.c_str(), + utf8word.size()); + } + + void is_removed (const std::string & utf8word) { + enchant_dict_is_removed (m_dict, utf8word.c_str(), + utf8word.size()); + } + void store_replacement (const std::string & utf8bad, const std::string & utf8good) { enchant_dict_store_replacement (m_dict, @@ -148,9 +168,13 @@ namespace enchant /* deprecated */ void add_to_personal (const std::string & utf8word) { - return add_to_pwl (utf8word); + return add (utf8word); } + /* deprecated */ + void add_to_pwl (const std::string & utf8word) { + return add (utf8word); + } private: // space reserved for API/ABI expansion diff --git a/src/enchant-provider.h b/src/enchant-provider.h index d3f3759..339f963 100644 --- a/src/enchant-provider.h +++ b/src/enchant-provider.h @@ -91,6 +91,9 @@ struct str_enchant_dict const char *const mis, size_t mis_len, const char *const cor, size_t cor_len); + void (*add_to_exclude) (struct str_enchant_dict * me, + const char *const word, size_t len); + void * _reserved[5]; }; diff --git a/src/enchant.c b/src/enchant.c index 828a3e2..1c93f13 100644 --- a/src/enchant.c +++ b/src/enchant.c @@ -70,10 +70,13 @@ struct str_enchant_broker typedef struct str_enchant_session { - GHashTable *session; + GHashTable *session_include; + GHashTable *session_exclude; EnchantPWL *personal; + EnchantPWL *exclude; char * personal_filename; + char * exclude_filename; char * language_tag; char * error; @@ -355,9 +358,12 @@ enchant_iso_639_from_tag (const char * const dict_tag) static void enchant_session_destroy (EnchantSession * session) { - g_hash_table_destroy (session->session); + g_hash_table_destroy (session->session_include); + g_hash_table_destroy (session->session_exclude); enchant_pwl_free (session->personal); + enchant_pwl_free (session->exclude); g_free (session->personal_filename); + g_free (session->exclude_filename); g_free (session->language_tag); if (session->error) @@ -367,11 +373,15 @@ enchant_session_destroy (EnchantSession * session) } static EnchantSession * -enchant_session_new_with_pwl (EnchantProvider * provider, const char * const pwl, const char * const lang, - gboolean fail_if_no_pwl) +enchant_session_new_with_pwl (EnchantProvider * provider, + const char * const pwl, + const char * const excl, + const char * const lang, + gboolean fail_if_no_pwl) { EnchantSession * session; EnchantPWL *personal = NULL; + EnchantPWL *exclude = NULL; if (pwl) personal = enchant_pwl_init_with_file (pwl); @@ -383,13 +393,20 @@ enchant_session_new_with_pwl (EnchantProvider * provider, const char * const pwl personal = enchant_pwl_init (); } + if (excl) + exclude = enchant_pwl_init_with_file (excl); + if (exclude == NULL) + exclude = enchant_pwl_init (); session = g_new0 (EnchantSession, 1); - session->session = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); + session->session_include = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); + session->session_exclude = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); session->personal = personal; + session->exclude = exclude; session->provider = provider; session->language_tag = g_strdup (lang); session->personal_filename = g_strdup (pwl); + session->exclude_filename = g_strdup (excl); return session; } @@ -398,7 +415,7 @@ static EnchantSession * enchant_session_new (EnchantProvider *provider, const char * const lang) { EnchantSession * session; - char * home_dir, * dic = NULL, * filename; + char * home_dir, * dic = NULL, *excl = NULL, * filename; home_dir = enchant_get_user_home_dir (); if (home_dir) @@ -410,10 +427,18 @@ enchant_session_new (EnchantProvider *provider, const char * const lang) filename, NULL); g_free (filename); + + filename = g_strdup_printf ("%s.exc", lang); + excl = g_build_filename (home_dir, + ENCHANT_USER_PATH_EXTENSION, + filename, + NULL); + g_free (filename); + g_free (home_dir); } - session = enchant_session_new_with_pwl (provider, dic, lang, FALSE); + session = enchant_session_new_with_pwl (provider, dic, excl, lang, FALSE); if (dic) g_free (dic); @@ -424,7 +449,17 @@ enchant_session_new (EnchantProvider *provider, const char * const lang) static void enchant_session_add (EnchantSession * session, const char * const word, size_t len) { - g_hash_table_insert (session->session, g_strndup (word, len), GINT_TO_POINTER(TRUE)); + char* key = g_strndup (word, len); + g_hash_table_remove (session->session_exclude, key); + g_hash_table_insert (session->session_include, key, GINT_TO_POINTER(TRUE)); +} + +static void +enchant_session_remove (EnchantSession * session, const char * const word, size_t len) +{ + char* key = g_strndup (word, len); + g_hash_table_remove (session->session_include, key); + g_hash_table_insert (session->session_exclude, key, GINT_TO_POINTER(TRUE)); } static void @@ -433,6 +468,43 @@ enchant_session_add_personal (EnchantSession * session, const char * const word, enchant_pwl_add(session->personal, word, len); } +static void +enchant_session_remove_personal (EnchantSession * session, const char * const word, size_t len) +{ + enchant_pwl_remove(session->personal, word, len); +} + +static void +enchant_session_add_exclude (EnchantSession * session, const char * const word, size_t len) +{ + enchant_pwl_add(session->exclude, word, len); +} + +static void +enchant_session_remove_exclude (EnchantSession * session, const char * const word, size_t len) +{ + enchant_pwl_remove(session->exclude, word, len); +} + +/* a word is excluded if it is in the exclude dictionary or in the session exclude list + * AND the word has not been added to the session include list + */ +static gboolean +enchant_session_exclude (EnchantSession * session, const char * const word, size_t len) +{ + gboolean result = FALSE; + + char * utf = g_strndup (word, len); + + if (!g_hash_table_lookup (session->session_include, utf) && + (g_hash_table_lookup (session->session_exclude, utf)|| + enchant_pwl_check (session->exclude, word, len) == 0 )) + result = TRUE; + g_free (utf); + + return result; +} + static gboolean enchant_session_contains (EnchantSession * session, const char * const word, size_t len) { @@ -440,8 +512,9 @@ enchant_session_contains (EnchantSession * session, const char * const word, siz char * utf = g_strndup (word, len); - if (g_hash_table_lookup (session->session, utf) || - enchant_pwl_check (session->personal, utf, len) == 0) + if (g_hash_table_lookup (session->session_include, utf) || + (enchant_pwl_check (session->personal, word, len) == 0 && + !enchant_pwl_check (session->exclude, word, len) == 0)) result = TRUE; g_free (utf); @@ -542,10 +615,14 @@ enchant_dict_check (EnchantDict * dict, const char *const word, ssize_t len) session = (EnchantSession*)dict->enchant_private_data; enchant_session_clear_error (session); - /* first, see if it's in our session */ - if (enchant_session_contains (session, word, len)) - return 0; + /* first, see if it's to be excluded*/ + if (enchant_session_exclude (session, word, len)) + return 1; + /* then, see if it's in our pwl or session*/ + if (enchant_session_contains(session, word, len)) + return 0; + if (dict->check) return (*dict->check) (dict, word, len); else if (session->is_pwl) @@ -578,6 +655,8 @@ enchant_dict_merge_suggestions(EnchantDict * dict, if (!g_utf8_validate(new_suggs[i], sugg_len, NULL)) copy = 0; + else if (enchant_session_exclude(session, new_suggs[i], sugg_len)) + copy = 0; else { char * normalized_new_sugg; @@ -686,14 +765,16 @@ enchant_dict_suggest (EnchantDict * dict, const char *const word, } /** - * enchant_dict_add_to_pwl + * enchant_dict_add * @dict: A non-null #EnchantDict * @word: The non-null word you wish to add to your personal dictionary, in UTF-8 encoding * @len: The byte length of @word, or -1 for strlen (@word) * + * Remarks: if the word exists in the exclude dictionary, it will be removed from the + * exclude dictionary */ ENCHANT_MODULE_EXPORT (void) -enchant_dict_add_to_pwl (EnchantDict * dict, const char *const word, +enchant_dict_add (EnchantDict * dict, const char *const word, ssize_t len) { EnchantSession * session; @@ -710,24 +791,40 @@ enchant_dict_add_to_pwl (EnchantDict * dict, const char *const word, session = (EnchantSession*)dict->enchant_private_data; enchant_session_clear_error (session); enchant_session_add_personal (session, word, len); + enchant_session_remove_exclude (session, word, len); if (dict->add_to_personal) (*dict->add_to_personal) (dict, word, len); } /** + * enchant_dict_add_to_pwl + * @dict: A non-null #EnchantDict + * @word: The non-null word you wish to add to your personal dictionary, in UTF-8 encoding + * @len: The byte length of @word, or -1 for strlen (@word) + * + * DEPRECATED. Please use enchant_dict_add() instead. + */ +ENCHANT_MODULE_EXPORT (void) +enchant_dict_add_to_pwl (EnchantDict * dict, const char *const word, + ssize_t len) +{ + enchant_dict_add(dict,word,len); +} + +/** * enchant_dict_add_to_personal * @dict: A non-null #EnchantDict * @word: The non-null word you wish to add to your personal dictionary, in UTF-8 encoding * @len: The byte length of @word, or -1 for strlen (@word) * - * DEPRECATED. Please use enchant_dict_add_to_pwl() instead. + * DEPRECATED. Please use enchant_dict_add() instead. */ ENCHANT_MODULE_EXPORT (void) enchant_dict_add_to_personal (EnchantDict * dict, const char *const word, ssize_t len) { - enchant_dict_add_to_pwl (dict, word, len); + enchant_dict_add(dict, word, len); } /** @@ -761,14 +858,14 @@ enchant_dict_add_to_session (EnchantDict * dict, const char *const word, } /** - * enchant_dict_is_in_session + * enchant_dict_is_added * @dict: A non-null #EnchantDict - * @word: The word you wish to see if it's in your session in UTF8 encoding + * @word: The word you wish to see if it has been added (to your session or dict) in UTF8 encoding * @len: the byte length of @word, or -1 for strlen (@word) */ ENCHANT_MODULE_EXPORT (int) -enchant_dict_is_in_session (EnchantDict * dict, const char *const word, - ssize_t len) +enchant_dict_is_added (EnchantDict * dict, const char *const word, + ssize_t len) { EnchantSession * session; @@ -788,6 +885,109 @@ enchant_dict_is_in_session (EnchantDict * dict, const char *const word, } /** + * enchant_dict_is_in_session + * @dict: A non-null #EnchantDict + * @word: The word you wish to see if it's in your session in UTF8 encoding + * @len: the byte length of @word, or -1 for strlen (@word) + * + * DEPRECATED. Please use enchant_dict_is_added() instead. +*/ +ENCHANT_MODULE_EXPORT (int) +enchant_dict_is_in_session (EnchantDict * dict, const char *const word, + ssize_t len) +{ + return enchant_dict_is_added(dict, word, len); +} + +/** + * enchant_dict_remove + * @dict: A non-null #EnchantDict + * @word: The non-null word you wish to add to your exclude dictionary and + * remove from the personal dictionary, in UTF-8 encoding + * @len: The byte length of @word, or -1 for strlen (@word) + * + */ +ENCHANT_MODULE_EXPORT (void) +enchant_dict_remove (EnchantDict * dict, const char *const word, + ssize_t len) +{ + EnchantSession * session; + + g_return_if_fail (dict); + g_return_if_fail (word); + + if (len < 0) + len = strlen (word); + + g_return_if_fail (len); + g_return_if_fail (g_utf8_validate(word, len, NULL)); + + session = (EnchantSession*)dict->enchant_private_data; + enchant_session_clear_error (session); + + enchant_session_remove_personal (session, word, len); + enchant_session_add_exclude(session, word, len); + + if (dict->add_to_exclude) + (*dict->add_to_exclude) (dict, word, len); +} + +/** + * enchant_dict_remove_from_session + * @dict: A non-null #EnchantDict + * @word: The non-null word you wish to exclude from this spell-checking session, in UTF-8 encoding + * @len: The byte length of @word, or -1 for strlen (@word) + * + */ +ENCHANT_MODULE_EXPORT (void) +enchant_dict_remove_from_session (EnchantDict * dict, const char *const word, + ssize_t len) +{ + EnchantSession * session; + + g_return_if_fail (dict); + g_return_if_fail (word); + + if (len < 0) + len = strlen (word); + + g_return_if_fail (len); + g_return_if_fail (g_utf8_validate(word, len, NULL)); + + session = (EnchantSession*)dict->enchant_private_data; + enchant_session_clear_error (session); + + enchant_session_remove (session, word, len); +} + +/** + * enchant_dict_is_removed + * @dict: A non-null #EnchantDict + * @word: The word you wish to see if it has been removed (from your session or dict) in UTF8 encoding + * @len: the byte length of @word, or -1 for strlen (@word) + */ +ENCHANT_MODULE_EXPORT (int) +enchant_dict_is_removed (EnchantDict * dict, const char *const word, + ssize_t len) +{ + EnchantSession * session; + + g_return_val_if_fail (dict, 0); + g_return_val_if_fail (word, 0); + + if (len < 0) + len = strlen (word); + + g_return_val_if_fail (len, 0); + g_return_val_if_fail (g_utf8_validate(word, len, NULL), 0); + + session = (EnchantSession*)dict->enchant_private_data; + enchant_session_clear_error (session); + + return enchant_session_exclude (session, word, len); +} + +/** * enchant_dict_store_replacement * @dict: A non-null #EnchantDict * @mis: The non-null word you wish to add a correction for, in UTF-8 encoding @@ -1318,8 +1518,12 @@ enchant_broker_request_pwl_dict (EnchantBroker * broker, const char *const pwl) return dict; } - session = enchant_session_new_with_pwl (NULL, pwl, "Personal Wordlist", TRUE); - if (!session) + /* since the broker pwl file is a read/write file (there is no readonly dictionary associated) + * there is no need for complementary exclude file to add a word to. The word just needs to be + * removed from the broker pwl file + */ + session = enchant_session_new_with_pwl (NULL, pwl, NULL, "Personal Wordlist", TRUE); + if (!session) { broker->error = g_strdup_printf ("Couldn't open personal wordlist '%s'", pwl); return NULL; diff --git a/src/enchant.h b/src/enchant.h index 32c4d0e..fb87f64 100644 --- a/src/enchant.h +++ b/src/enchant.h @@ -98,19 +98,28 @@ ENCHANT_MODULE_EXPORT (char **) enchant_dict_suggest (EnchantDict * dict, const char *const word, ssize_t len, size_t * out_n_suggs); ENCHANT_MODULE_EXPORT (void) + enchant_dict_add (EnchantDict * dict, const char *const word, + ssize_t len); +ENCHANT_MODULE_EXPORT (void) enchant_dict_add_to_session (EnchantDict * dict, const char *const word, ssize_t len); +ENCHANT_MODULE_EXPORT (void) + enchant_dict_remove (EnchantDict * dict, const char *const word, + ssize_t len); +ENCHANT_MODULE_EXPORT (void) + enchant_dict_remove_from_session (EnchantDict * dict, const char *const word, + ssize_t len); ENCHANT_MODULE_EXPORT (int) - enchant_dict_is_in_session (EnchantDict * dict, const char *const word, + enchant_dict_is_added (EnchantDict * dict, const char *const word, + ssize_t len); +ENCHANT_MODULE_EXPORT (int) + enchant_dict_is_removed (EnchantDict * dict, const char *const word, ssize_t len); ENCHANT_MODULE_EXPORT (void) enchant_dict_store_replacement (EnchantDict * dict, const char *const mis, ssize_t mis_len, const char *const cor, ssize_t cor_len); ENCHANT_MODULE_EXPORT (void) - enchant_dict_add_to_pwl (EnchantDict * dict, const char *const word, - ssize_t len); -ENCHANT_MODULE_EXPORT (void) enchant_dict_free_string_list (EnchantDict * dict, char **string_list); #ifndef ENCHANT_DISABLE_DEPRECATED @@ -119,6 +128,12 @@ ENCHANT_MODULE_EXPORT (void) ENCHANT_MODULE_EXPORT (void) enchant_dict_add_to_personal (EnchantDict * dict, const char *const word, ssize_t len); +ENCHANT_MODULE_EXPORT (void) + enchant_dict_add_to_pwl (EnchantDict * dict, const char *const word, + ssize_t len); +ENCHANT_MODULE_EXPORT (int) + enchant_dict_is_in_session (EnchantDict * dict, const char *const word, + ssize_t len); #endif /* ENCHANT_DISABLE_DEPRECATED */ /* const */ @@ -174,6 +174,7 @@ static EnchantTrie* enchant_trie_init(void); static void enchant_trie_free(EnchantTrie* trie); static void enchant_trie_free_cb(void*,void*,void*); static EnchantTrie* enchant_trie_insert(EnchantTrie* trie,const char *const word); +static void enchant_trie_remove(EnchantTrie* trie,const char *const word); static void enchant_trie_find_matches(EnchantTrie* trie,EnchantTrieMatcher *matcher); static void enchant_trie_find_matches_cb(void* keyV,void* subtrieV,void* matcherV); static EnchantTrieMatcher* enchant_trie_matcher_init(const char* const word, size_t len, @@ -339,12 +340,100 @@ static void enchant_pwl_add_to_trie(EnchantPWL *pwl, } } +static void enchant_pwl_remove_from_trie(EnchantPWL *pwl, + const char *const word, size_t len) +{ + char * normalized_word = g_utf8_normalize (word, len, G_NORMALIZE_NFD); + + if( g_hash_table_remove (pwl->words_in_trie, normalized_word) ) + { + enchant_trie_remove(pwl->trie, normalized_word); + if(pwl->trie && pwl->trie->subtries == NULL && pwl->trie->value == NULL) + pwl->trie = NULL; // make trie empty if has no content + } + + g_free(normalized_word); +} + void enchant_pwl_add(EnchantPWL *pwl, const char *const word, size_t len) { enchant_pwl_add_to_trie(pwl, word, len, TRUE); } +void enchant_pwl_remove(EnchantPWL *pwl, + const char *const word, size_t len) +{ + if(enchant_pwl_check(pwl, word, len) == 1) + return; + + enchant_pwl_remove_from_trie(pwl, word, len); + + if (pwl->filename) + { + char * contents; + size_t length; + + FILE *f; + + if(!g_file_get_contents(pwl->filename, &contents, &length, NULL)) + return; + + f = g_fopen(pwl->filename, "wb"); /*binary because g_file_get_contents reads binary*/ + if (f) + { + const gunichar BOM = 0xfeff; + char * filestart, *searchstart, *needle; + char * key; + + enchant_lock_file (f); + key = g_strndup(word, len); + + if(BOM == g_utf8_get_char(contents)) + { + filestart = g_utf8_next_char(contents); + fwrite (contents, sizeof(char), filestart-contents, f); + } + else + filestart = contents; + + searchstart = filestart; + for(;;) + { + /*find word*/ + needle = strstr(searchstart, key); + if(needle == NULL) + { + fwrite (searchstart, sizeof(char), length - (searchstart - contents), f); + break; + } + else + { + char* foundend = needle+len; + if((needle == filestart || contents[needle-contents-1] == '\n' || contents[needle-contents-1] == '\r') && + (foundend == contents + length || *foundend == '\n' || *foundend == '\r')) + { + fwrite (searchstart, sizeof(char), needle - searchstart, f); + searchstart = foundend; + while (*searchstart == '\n' || *searchstart == '\r') + ++searchstart; + } + else { + fwrite (searchstart, sizeof(char), needle - searchstart+1, f); + searchstart = needle+1; + } + } + } + g_free(key); + + enchant_unlock_file (f); + + fclose (f); + } + g_free(contents); + } +} + int enchant_pwl_contains(EnchantPWL *pwl, const char *const word, size_t len) { EnchantTrieMatcher* matcher; @@ -517,7 +606,6 @@ char** enchant_pwl_suggest(EnchantPWL *pwl,const char *const word, { EnchantTrieMatcher* matcher; EnchantSuggList sugg_list; - size_t i; sugg_list.suggs = g_new0(char*,ENCHANT_PWL_MAX_SUGGS+1); sugg_list.sugg_errs = g_new0(int,ENCHANT_PWL_MAX_SUGGS); @@ -689,6 +777,63 @@ static EnchantTrie* enchant_trie_insert(EnchantTrie* trie,const char *const word return trie; } +static void enchant_trie_remove(EnchantTrie* trie,const char *const word) +{ + char *tmpWord; + ssize_t nxtCh = 0; + EnchantTrie* subtrie; + + if (trie == NULL) + return; + + if (trie->value == NULL) { + if (trie->subtries != NULL) { + /* Store multiple words in subtries */ + if (word[0] == '\0') { + /* Mark end-of-string with special node */ + g_hash_table_remove(trie->subtries, ""); + } else { + nxtCh = (ssize_t)(g_utf8_next_char(word)-word); + tmpWord = g_strndup(word,nxtCh); + subtrie = g_hash_table_lookup(trie->subtries, + tmpWord); + enchant_trie_remove(subtrie, + (word+nxtCh)); + + if(subtrie->subtries == NULL && subtrie->value == NULL) + g_hash_table_remove(trie->subtries, tmpWord); + + g_free(tmpWord); + } + + if(g_hash_table_size(trie->subtries) == 1) + { + char* key; + GList* keys = g_hash_table_get_keys(trie->subtries); + key = (char*) keys->data; + subtrie = g_hash_table_lookup(trie->subtries, key); + + /* only remove trie nodes that have values by propogating these up */ + if(subtrie->value) + { + trie->value = g_strconcat(key, subtrie->value, NULL); + enchant_trie_free(subtrie); + g_hash_table_destroy(trie->subtries); + trie->subtries = NULL; + } + + g_list_free(keys); + } + } + } else { + if(strcmp(trie->value, word) == 0) + { + g_free(trie->value); + trie->value = NULL; + } + } +} + static void enchant_trie_find_matches(EnchantTrie* trie,EnchantTrieMatcher *matcher) { int errs = 0; @@ -44,6 +44,7 @@ EnchantPWL* enchant_pwl_init(void); EnchantPWL* enchant_pwl_init_with_file(const char * file); void enchant_pwl_add(EnchantPWL * me, const char *const word, size_t len); +void enchant_pwl_remove(EnchantPWL * me, const char *const word, size_t len); int enchant_pwl_check(EnchantPWL * me,const char *const word, size_t len); char** enchant_pwl_suggest(EnchantPWL *me,const char *const word, size_t len, size_t* out_n_suggs); diff --git a/tests/test-enchant.c b/tests/test-enchant.c index b463449..e4c5a54 100644 --- a/tests/test-enchant.c +++ b/tests/test-enchant.c @@ -100,7 +100,7 @@ run_dict_tests (EnchantDict * dict) } printf ("Adding 'helllo' to personal\n"); - enchant_dict_add_to_pwl (dict, "helllo", 6); + enchant_dict_add (dict, "helllo", 6); for (i = 0; i < (sizeof (check_checks) / sizeof (check_checks[0])); i++) { printf ("enchant_dict_check (%s): %s\n", check_checks[i], diff --git a/unittests/EnchantBrokerTestFixture.h b/unittests/EnchantBrokerTestFixture.h index f91d681..ebe0326 100644 --- a/unittests/EnchantBrokerTestFixture.h +++ b/unittests/EnchantBrokerTestFixture.h @@ -311,13 +311,20 @@ struct EnchantBrokerTestFixture : EnchantTestFixture g_rmdir(dir.c_str());
}
- EnchantDict* RequestPersonalDictionary(){
+ EnchantDict* RequestPersonalDictionary()
+ {
std::string pwlFileName = GetTemporaryFilename("epwl");
CreateFile(pwlFileName);
pwlFilenames.push(pwlFileName);
return enchant_broker_request_pwl_dict(_broker, pwlFileName.c_str());
}
+ std::string GetLastPersonalDictionaryFileName()
+ {
+ return pwlFilenames.top();
+ }
+
+
void FreeDictionary(EnchantDict* dictionary){
if(dictionary != NULL){
enchant_broker_free_dict(_broker, dictionary);
diff --git a/unittests/EnchantDictionaryTestFixture.h b/unittests/EnchantDictionaryTestFixture.h index c7d8838..a2cddd7 100644 --- a/unittests/EnchantDictionaryTestFixture.h +++ b/unittests/EnchantDictionaryTestFixture.h @@ -100,6 +100,7 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture {
EnchantDict* _dict;
EnchantDict* _pwl;
+ std::string _pwlFileName;
std::string origLangEnv;
bool hasLangEnv;
std::string languageTag;
@@ -111,6 +112,7 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture {
InitializeTestDictionary();
_pwl = RequestPersonalDictionary();
+ _pwlFileName = GetLastPersonalDictionaryFileName();
bool hasLangEnv = (g_getenv("LANG") != NULL);
if(hasLangEnv)
@@ -160,8 +162,23 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture bool PersonalWordListFileHasContents()
{
+ return FileHasContents(GetPersonalDictFileName());
+ }
+
+ bool ExcludeFileHasContents()
+ {
+ return FileHasContents(GetExcludeDictFileName());
+ }
+
+ bool BrokerPWLFileHasContents()
+ {
+ return FileHasContents(_pwlFileName);
+ }
+
+ bool FileHasContents(const std::string & filename)
+ {
bool hasContents = false;
- int fd = g_open(GetPersonalDictFileName().c_str(), O_RDONLY, S_IREAD );
+ int fd = g_open(filename.c_str(), O_RDONLY, S_IREAD );
if(fd == -1){
return false;
}
@@ -179,6 +196,10 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture return AddToPath(GetEnchantPersonalDir(), "qaa.dic");
}
+ std::string GetExcludeDictFileName(){
+ return AddToPath(GetEnchantPersonalDir(), "qaa.exc");
+ }
+
void SetErrorOnMockDictionary(const std::string& error)
{
enchant_dict_set_error(_dict, error.c_str());
@@ -200,9 +221,14 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture return enchant_dict_check(_dict, word.c_str(), word.size())==0;
}
+ void RemoveWordFromDictionary(const std::string& word)
+ {
+ enchant_dict_remove(_dict, word.c_str(), word.size());
+ }
+
void AddWordToDictionary(const std::string& word)
{
- enchant_dict_add_to_pwl(_dict, word.c_str(), word.size());
+ enchant_dict_add(_dict, word.c_str(), word.size());
}
void AddWordsToDictionary(const std::vector<const std::string>& sWords)
@@ -216,7 +242,17 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture void ExternalAddWordToDictionary(const std::string& word)
{
- FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "at");
+ ExternalAddWordToFile(word, GetPersonalDictFileName());
+ }
+
+ void ExternalAddWordToExclude(const std::string& word)
+ {
+ ExternalAddWordToFile(word, GetExcludeDictFileName());
+ }
+
+ static void ExternalAddWordToFile(const std::string& word, const std::string& filename)
+ {
+ FILE * f = g_fopen(filename.c_str(), "at");
if(f)
{
fputs(word.c_str(), f);
@@ -239,6 +275,24 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture }
}
+ std::vector<std::string> GetExpectedSuggestions(const std::string& s, size_t begin = 0)
+ {
+ size_t cSuggestions;
+ char** expectedSuggestions = MockDictionarySuggest (_dict,
+ s.c_str(),
+ s.size(),
+ &cSuggestions);
+
+ std::vector<std::string> result;
+ if(expectedSuggestions != NULL && begin < cSuggestions){
+ result.insert(result.begin(), expectedSuggestions+begin, expectedSuggestions+cSuggestions);
+ FreeStringList(expectedSuggestions);
+ }
+
+ return result;
+ }
+
+
std::vector<const std::string> GetSuggestionsFromWord(const std::string& word)
{
std::vector<const std::string> result;
@@ -255,6 +309,10 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture return result;
}
+ std::vector<const std::string> GetSuggestions(const std::string& s)
+ {
+ return GetSuggestionsFromWord("helo");
+ }
};
#if defined(_MSC_VER)
#pragma warning(pop)
diff --git a/unittests/dictionary/enchant_dict_add_to_pwl_tests.cpp b/unittests/dictionary/enchant_dict_add_tests.cpp index 816c3d4..aab3c6a 100644 --- a/unittests/dictionary/enchant_dict_add_to_pwl_tests.cpp +++ b/unittests/dictionary/enchant_dict_add_tests.cpp @@ -48,10 +48,10 @@ static void DictionaryAddToPersonal_ProviderConfiguration (EnchantProvider * me, me->dispose_dict = MockProviderDisposeDictionary;
}
-struct EnchantDictionaryAddToPersonal_TestFixture : EnchantDictionaryTestFixture
+struct EnchantDictionaryAdd_TestFixture : EnchantDictionaryTestFixture
{
//Setup
- EnchantDictionaryAddToPersonal_TestFixture():
+ EnchantDictionaryAdd_TestFixture():
EnchantDictionaryTestFixture(DictionaryAddToPersonal_ProviderConfiguration)
{
addToPersonalCalled = false;
@@ -67,128 +67,154 @@ struct EnchantDictionaryAddToPersonalNotImplemented_TestFixture : EnchantDiction addToPersonalCalled = false;
}
};
+
/**
- * enchant_dict_add_to_pwl
+ * enchant_dict_add
* @dict: A non-null #EnchantDict
* @word: The non-null word you wish to add to your personal dictionary, in UTF-8 encoding
* @len: The byte length of @word, or -1 for strlen (@word)
*
+ * Remarks: if the word exists in the exclude dictionary, it will be removed from the
+ * exclude dictionary
*/
-
/////////////////////////////////////////////////////////////////////////////
// Test Normal Operation
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_WordAddedToEnchantPwlFile)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_WordExistsInDictionary)
+{
+ enchant_dict_add(_dict, "hello", -1);
+ CHECK(IsWordInDictionary("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_WordExistsInSession)
+{
+ enchant_dict_add(_dict, "hello", -1);
+ CHECK(IsWordInSession("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_WordAddedToEnchantPwlFile)
{
CHECK(!PersonalWordListFileHasContents());
- enchant_dict_add_to_pwl(_dict, "hello", -1);
+ enchant_dict_add(_dict, "hello", -1);
CHECK(PersonalWordListFileHasContents());
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_PassedOnToProvider_LenComputed)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_PassedOnToProvider_LenComputed)
{
- enchant_dict_add_to_pwl(_dict, "hello", -1);
+ enchant_dict_add(_dict, "hello", -1);
CHECK(addToPersonalCalled);
CHECK_EQUAL(std::string("hello"), wordToAdd);
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_PassedOnToProvider_LenSpecified)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_PassedOnToProvider_LenSpecified)
{
- enchant_dict_add_to_pwl(_dict, "hellodisregard me", 5);
+ enchant_dict_add(_dict, "hellodisregard me", 5);
CHECK(addToPersonalCalled);
CHECK_EQUAL(std::string("hello"), wordToAdd);
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_WordExistsInSession_StillCallsProvider)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_WordExistsInSession_StillCallsProvider)
{
enchant_dict_add_to_session(_dict, "session", -1);
CHECK(!addToPersonalCalled);
- enchant_dict_add_to_pwl(_dict, "session", -1);
+ enchant_dict_add(_dict, "session", -1);
CHECK(addToPersonalCalled);
CHECK_EQUAL(std::string("session"), wordToAdd);
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_WordExistsInPersonal_StillCallsProvider)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_WordExistsInPersonal_StillCallsProvider)
{
- enchant_dict_add_to_pwl(_dict, "personal", -1);
+ enchant_dict_add(_dict, "personal", -1);
addToPersonalCalled=false;
wordToAdd = std::string();
- enchant_dict_add_to_pwl(_dict, "personal", -1);
+ enchant_dict_add(_dict, "personal", -1);
CHECK(addToPersonalCalled);
CHECK_EQUAL(std::string("personal"), wordToAdd);
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_InBrokerPwl)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_WordExistsInExclude_RemovedFromExcludeAddedToPersonal)
{
- enchant_dict_add_to_pwl(_pwl, "personal", -1);
+ enchant_dict_remove(_dict, "personal", -1);
+ CHECK(ExcludeFileHasContents());
+ enchant_dict_add(_dict, "personal", -1);
+ CHECK(!ExcludeFileHasContents());
+ CHECK(PersonalWordListFileHasContents());
+}
+
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_InBrokerPwl)
+{
+ enchant_dict_add(_pwl, "personal", -1);
CHECK(!addToPersonalCalled);
CHECK(!PersonalWordListFileHasContents());
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_IsPermanent)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_IsPermanent)
{
- enchant_dict_add_to_pwl(_dict, "hello", -1);
- CHECK(IsWordInSession("hello"));
+ enchant_dict_add(_dict, "hello", -1);
+ CHECK(IsWordInDictionary("hello"));
ReloadTestDictionary();
- CHECK(IsWordInSession("hello"));
+ CHECK(IsWordInDictionary("hello"));
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_HasPreviousError_ErrorCleared)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_HasPreviousError_ErrorCleared)
{
SetErrorOnMockDictionary("something bad happened");
- enchant_dict_add_to_pwl(_dict, "hello", -1);
+ enchant_dict_add(_dict, "hello", -1);
CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));
}
/////////////////////////////////////////////////////////////////////////////
// Test Error Conditions
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_NullDictionary_NotAdded)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_NullDictionary_NotAdded)
{
- enchant_dict_add_to_pwl(NULL, "hello", -1);
+ enchant_dict_add(NULL, "hello", -1);
CHECK(!addToPersonalCalled);
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_NullWord_NotAdded)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_NullWord_NotAdded)
{
- enchant_dict_add_to_pwl(_dict, NULL, -1);
+ enchant_dict_add(_dict, NULL, -1);
CHECK(!addToPersonalCalled);
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_EmptyWord_NotAdded)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_EmptyWord_NotAdded)
{
- enchant_dict_add_to_pwl(_dict, "", -1);
+ enchant_dict_add(_dict, "", -1);
CHECK(!addToPersonalCalled);
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_WordSize0_NotAdded)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_WordSize0_NotAdded)
{
- enchant_dict_add_to_pwl(_dict, "hello", 0);
+ enchant_dict_add(_dict, "hello", 0);
CHECK(!addToPersonalCalled);
}
-TEST_FIXTURE(EnchantDictionaryAddToPersonal_TestFixture,
- EnchantDictionaryAddToPersonal_InvalidUtf8Word_NotAdded)
+TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,
+ EnchantDictionaryAdd_InvalidUtf8Word_NotAdded)
{
- enchant_dict_add_to_pwl(_dict, "\xa5\xf1\x08", -1);
+ enchant_dict_add(_dict, "\xa5\xf1\x08", -1);
CHECK(!addToPersonalCalled);
}
@@ -196,13 +222,13 @@ TEST_FIXTURE(EnchantDictionaryAddToPersonalNotImplemented_TestFixture, EnchantDictionaryAddToPersonalNotImplemented_WordAddedToEnchantPwlFile)
{
CHECK(!PersonalWordListFileHasContents());
- enchant_dict_add_to_pwl(_dict, "hello", -1);
+ enchant_dict_add(_dict, "hello", -1);
CHECK(PersonalWordListFileHasContents());
}
TEST_FIXTURE(EnchantDictionaryAddToPersonalNotImplemented_TestFixture,
EnchantDictionaryAddToPersonalNotImplemented_NotPassedOnToProvider)
{
- enchant_dict_add_to_pwl(_dict, "hello", -1);
+ enchant_dict_add(_dict, "hello", -1);
CHECK(!addToPersonalCalled);
}
diff --git a/unittests/dictionary/enchant_dict_add_to_session_tests.cpp b/unittests/dictionary/enchant_dict_add_to_session_tests.cpp index e3c2b29..a62aad8 100644 --- a/unittests/dictionary/enchant_dict_add_to_session_tests.cpp +++ b/unittests/dictionary/enchant_dict_add_to_session_tests.cpp @@ -115,7 +115,7 @@ TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture, TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,
EnchantDictionaryAddToSession_WordExistsInPersonal_StillCallsProvider)
{
- enchant_dict_add_to_pwl(_dict, "personal", -1);
+ enchant_dict_add(_dict, "personal", -1);
CHECK(!addToSessionCalled);
enchant_dict_add_to_session(_dict, "personal", -1);
@@ -124,6 +124,17 @@ TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture, }
TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,
+ EnchantDictionaryAddToSession_WordExistsInExclude_AddedToSessionNotRemovedFromExcludeFile)
+{
+ enchant_dict_remove(_dict, "personal", -1);
+
+ enchant_dict_add_to_session(_dict, "personal", -1);
+ CHECK(IsWordInDictionary("personal"));
+ CHECK(ExcludeFileHasContents());
+}
+
+
+TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,
EnchantDictionaryAddToSession_WordAddedToSession)
{
enchant_dict_add_to_session(_dict, "hello", -1);
diff --git a/unittests/dictionary/enchant_dict_check_tests.cpp b/unittests/dictionary/enchant_dict_check_tests.cpp index acf8b59..3091d34 100644 --- a/unittests/dictionary/enchant_dict_check_tests.cpp +++ b/unittests/dictionary/enchant_dict_check_tests.cpp @@ -127,7 +127,7 @@ TEST_FIXTURE(EnchantDictionaryCheck_TestFixture, TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
EnchantDictionaryCheck_WordExistsInPersonal_0_DoesNotCallProvider)
{
- enchant_dict_add_to_pwl(_dict, "personal", -1);
+ enchant_dict_add(_dict, "personal", -1);
CHECK_EQUAL(0, enchant_dict_check(_dict, "personal", -1));
CHECK(!dictCheckCalled);
@@ -143,7 +143,7 @@ TEST_FIXTURE(EnchantDictionaryCheck_TestFixture, TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
EnchantDictionaryCheck_WordDoesExists_InBrokerPwl_0)
{
- enchant_dict_add_to_pwl(_pwl, "personal", -1);
+ enchant_dict_add(_pwl, "personal", -1);
CHECK_EQUAL(0, enchant_dict_check(_pwl, "personal", -1));
}
@@ -211,4 +211,14 @@ TEST_FIXTURE(EnchantDictionaryCheckNotImplemented_TestFixture, {
CHECK_EQUAL(1, enchant_dict_check(_pwl, "hello", -1));
CHECK(!dictCheckCalled);
-}
\ No newline at end of file +}
+
+TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
+ EnchantDictionaryCheck_WordInDictionaryAndExclude_1)
+{
+ ExternalAddWordToExclude("hello");
+ ExternalAddWordToDictionary("hello");
+
+ ReloadTestDictionary();
+ CHECK_EQUAL(1, enchant_dict_check(_dict, "hello", -1));
+}
diff --git a/unittests/dictionary/enchant_dict_is_added_tests.cpp b/unittests/dictionary/enchant_dict_is_added_tests.cpp new file mode 100644 index 0000000..4005667 --- /dev/null +++ b/unittests/dictionary/enchant_dict_is_added_tests.cpp @@ -0,0 +1,132 @@ +/* Copyright (c) 2007 Eric Scott Albright
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <UnitTest++.h>
+#include <enchant.h>
+#include "../EnchantDictionaryTestFixture.h"
+
+struct EnchantDictionaryIsAdded_TestFixture : EnchantDictionaryTestFixture
+{};
+
+/**
+ * enchant_dict_is_added
+ * @dict: A non-null #EnchantDict
+ * @word: The word you wish to see if it's in your session
+ * @len: the byte length of @word, or -1 for strlen (@word)
+ */
+
+/////////////////////////////////////////////////////////////////////////////
+// Test Normal Operation
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_AddedToSession_1)
+{
+ enchant_dict_add_to_session(_dict, "hello", -1);
+
+ CHECK_EQUAL(1, enchant_dict_is_added(_dict, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_Added_1)
+{
+ enchant_dict_add(_dict, "hello", -1);
+
+ CHECK_EQUAL(1, enchant_dict_is_added(_dict, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_NotAdded_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_added(_dict, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_OnBrokerPwl_AddedToSession_1)
+{
+ enchant_dict_add_to_session(_pwl, "hello", -1);
+
+ CHECK_EQUAL(1, enchant_dict_is_added(_pwl, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_OnBrokerPwl_Added_1)
+{
+ enchant_dict_add(_pwl, "hello", -1);
+
+ CHECK_EQUAL(1, enchant_dict_is_added(_pwl, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_OnBrokerPwl_NotAdded_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_added(_pwl, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_HasPreviousError_ErrorCleared)
+{
+ SetErrorOnMockDictionary("something bad happened");
+
+ enchant_dict_is_added(_dict, "hello", -1);
+ CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));
+}
+
+/////////////////////////////////////////////////////////////////////////////
+// Test Error Conditions
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_NullDictionary_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_added(NULL, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_NullWord_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_added(_dict, NULL, -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_EmptyWord_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_added(_dict, "", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_WordSize0_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_added(_dict, "hello", 0));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_InvalidUtf8Word_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_added(_dict, "\xa5\xf1\x08", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsAdded_TestFixture,
+ EnchantDictionaryIsAdded_WordExistsInPwlAndExclude_0)
+{
+ ExternalAddWordToDictionary("hello");
+ ExternalAddWordToExclude("hello");
+
+ ReloadTestDictionary();
+ CHECK_EQUAL(0, enchant_dict_is_added(_dict, "hello", -1));
+}
+
diff --git a/unittests/dictionary/enchant_dict_is_in_session_tests.cpp b/unittests/dictionary/enchant_dict_is_in_session_tests.cpp deleted file mode 100644 index e08a5fa..0000000 --- a/unittests/dictionary/enchant_dict_is_in_session_tests.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/* Copyright (c) 2007 Eric Scott Albright
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include <UnitTest++.h>
-#include <enchant.h>
-#include "../EnchantDictionaryTestFixture.h"
-
-struct EnchantDictionaryIsInSession_TestFixture : EnchantDictionaryTestFixture
-{};
-
-/**
- * enchant_dict_is_in_session
- * @dict: A non-null #EnchantDict
- * @word: The word you wish to see if it's in your session
- * @len: the byte length of @word, or -1 for strlen (@word)
- */
-
-/////////////////////////////////////////////////////////////////////////////
-// Test Normal Operation
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_InSession_1)
-{
- enchant_dict_add_to_session(_dict, "hello", -1);
-
- CHECK_EQUAL(1, enchant_dict_is_in_session(_dict, "hello", -1));
-}
-
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_InPersonal_1)
-{
- enchant_dict_add_to_pwl(_dict, "hello", -1);
-
- CHECK_EQUAL(1, enchant_dict_is_in_session(_dict, "hello", -1));
-}
-
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_NotInSession_0)
-{
- CHECK_EQUAL(0, enchant_dict_is_in_session(_dict, "hello", -1));
-}
-
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_OnBrokerPwl_InSession_1)
-{
- enchant_dict_add_to_session(_pwl, "hello", -1);
-
- CHECK_EQUAL(1, enchant_dict_is_in_session(_pwl, "hello", -1));
-}
-
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_OnBrokerPwl_InPersonal_1)
-{
- enchant_dict_add_to_pwl(_pwl, "hello", -1);
-
- CHECK_EQUAL(1, enchant_dict_is_in_session(_pwl, "hello", -1));
-}
-
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_OnBrokerPwl_NotInSession_0)
-{
- CHECK_EQUAL(0, enchant_dict_is_in_session(_pwl, "hello", -1));
-}
-
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_HasPreviousError_ErrorCleared)
-{
- SetErrorOnMockDictionary("something bad happened");
-
- enchant_dict_is_in_session(_dict, "hello", -1);
- CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));
-}
-
-/////////////////////////////////////////////////////////////////////////////
-// Test Error Conditions
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_NullDictionary_0)
-{
- CHECK_EQUAL(0, enchant_dict_is_in_session(NULL, "hello", -1));
-}
-
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_NullWord_0)
-{
- CHECK_EQUAL(0, enchant_dict_is_in_session(_dict, NULL, -1));
-}
-
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_EmptyWord_0)
-{
- CHECK_EQUAL(0, enchant_dict_is_in_session(_dict, "", -1));
-}
-
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_WordSize0_0)
-{
- CHECK_EQUAL(0, enchant_dict_is_in_session(_dict, "hello", 0));
-}
-
-TEST_FIXTURE(EnchantDictionaryIsInSession_TestFixture,
- EnchantDictionaryIsInSession_InvalidUtf8Word_0)
-{
- CHECK_EQUAL(0, enchant_dict_is_in_session(_dict, "\xa5\xf1\x08", -1));
-}
diff --git a/unittests/dictionary/enchant_dict_is_removed_tests.cpp b/unittests/dictionary/enchant_dict_is_removed_tests.cpp new file mode 100644 index 0000000..8efdd2c --- /dev/null +++ b/unittests/dictionary/enchant_dict_is_removed_tests.cpp @@ -0,0 +1,134 @@ +/* Copyright (c) 2007 Eric Scott Albright
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <UnitTest++.h>
+#include <enchant.h>
+#include "../EnchantDictionaryTestFixture.h"
+
+struct EnchantDictionaryIsRemoved_TestFixture : EnchantDictionaryTestFixture
+{};
+
+/**
+ * enchant_dict_is_removed
+ * @dict: A non-null #EnchantDict
+ * @word: The word you wish to query
+ * @len: the byte length of @word, or -1 for strlen (@word)
+ *
+ * returns: 1 if word has been removed (from session or permanently)
+ */
+
+/////////////////////////////////////////////////////////////////////////////
+// Test Normal Operation
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_RemovedFromSession_1)
+{
+ enchant_dict_remove_from_session(_dict, "hello", -1);
+
+ CHECK_EQUAL(1, enchant_dict_is_removed(_dict, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_Removed_1)
+{
+ enchant_dict_remove(_dict, "hello", -1);
+
+ CHECK_EQUAL(1, enchant_dict_is_removed(_dict, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_NotRemoved_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_removed(_dict, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_OnBrokerPwl_RemovedFromSession_1)
+{
+ enchant_dict_remove_from_session(_pwl, "hello", -1);
+
+ CHECK_EQUAL(1, enchant_dict_is_removed(_pwl, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_OnBrokerPwl_Removed_1)
+{
+ enchant_dict_remove(_pwl, "hello", -1);
+
+ CHECK_EQUAL(1, enchant_dict_is_removed(_pwl, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_OnBrokerPwl_NotRemoved_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_removed(_pwl, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_HasPreviousError_ErrorCleared)
+{
+ SetErrorOnMockDictionary("something bad happened");
+
+ enchant_dict_is_removed(_dict, "hello", -1);
+ CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));
+}
+
+/////////////////////////////////////////////////////////////////////////////
+// Test Error Conditions
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_NullDictionary_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_removed(NULL, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_NullWord_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_removed(_dict, NULL, -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_EmptyWord_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_removed(_dict, "", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_WordSize0_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_removed(_dict, "hello", 0));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_InvalidUtf8Word_0)
+{
+ CHECK_EQUAL(0, enchant_dict_is_removed(_dict, "\xa5\xf1\x08", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryIsRemoved_TestFixture,
+ EnchantDictionaryIsRemoved_WordExistsInPwlAndExclude_1)
+{
+ ExternalAddWordToExclude("hello");
+ ExternalAddWordToDictionary("hello");
+
+ ReloadTestDictionary();
+ CHECK_EQUAL(1, enchant_dict_is_removed(_dict, "hello", -1));
+}
+
diff --git a/unittests/dictionary/enchant_dict_remove_from_session_tests.cpp b/unittests/dictionary/enchant_dict_remove_from_session_tests.cpp new file mode 100644 index 0000000..eeb2053 --- /dev/null +++ b/unittests/dictionary/enchant_dict_remove_from_session_tests.cpp @@ -0,0 +1,194 @@ +/* Copyright (c) 2007 Eric Scott Albright
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#define NOMINMAX //don't want windows to collide with std::min
+
+#include <UnitTest++.h>
+#include <enchant.h>
+#include "../EnchantDictionaryTestFixture.h"
+
+static bool dictCheckCalled;
+
+static int
+MockDictionaryCheck (EnchantDict * dict, const char *const word, size_t len)
+{
+ dict;
+ dictCheckCalled = true;
+ if(strncmp("hello", word, len)==0)
+ {
+ return 0; //good word
+ }
+ return 1; // bad word
+}
+
+
+static EnchantDict* MockProviderRequestCheckMockDictionary(EnchantProvider * me, const char *tag)
+{
+
+ EnchantDict* dict = MockProviderRequestBasicMockDictionary(me, tag);
+ dict->check = MockDictionaryCheck;
+ return dict;
+}
+
+static void DictionaryCheck_ProviderConfiguration (EnchantProvider * me, const char *)
+{
+ me->request_dict = MockProviderRequestCheckMockDictionary;
+ me->dispose_dict = MockProviderDisposeDictionary;
+}
+
+struct EnchantDictionaryRemoveFromSession_TestFixture : EnchantDictionaryTestFixture
+{
+ //Setup
+ EnchantDictionaryRemoveFromSession_TestFixture():
+ EnchantDictionaryTestFixture(DictionaryCheck_ProviderConfiguration)
+ {
+ dictCheckCalled = false;
+ }
+};
+
+/**
+ * enchant_dict_remove_from_session
+ * @dict: A non-null #EnchantDict
+ * @word: The non-null word you wish to exclude from this spell-checking session, in UTF-8 encoding
+ * @len: The byte length of @word, or -1 for strlen (@word)
+ *
+ */
+
+/////////////////////////////////////////////////////////////////////////////
+// Test Normal Operation
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_WordNotAddedToEnchantExcludeFile)
+{
+ enchant_dict_remove_from_session(_dict, "hello", -1);
+ CHECK(!ExcludeFileHasContents());
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_WordRemovedFromSession)
+{
+ enchant_dict_add_to_session(_dict, "hello", -1);
+ CHECK(IsWordInSession("hello"));
+ enchant_dict_remove_from_session(_dict, "hello", -1);
+ CHECK(!IsWordInSession("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_CalledTwice)
+{
+ enchant_dict_add_to_session(_dict, "hello", -1);
+ enchant_dict_remove_from_session(_dict, "hello", -1);
+ enchant_dict_remove_from_session(_dict, "hello", -1);
+ CHECK(!IsWordInSession("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_WordExcludedFromDictionary)
+{
+ enchant_dict_remove_from_session(_dict, "hello", -1);
+ CHECK(!IsWordInDictionary("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_InBrokerSession_WordExcludedFromBrokerSession)
+{
+ enchant_dict_add_to_session(_pwl, "hello", -1);
+ CHECK(enchant_dict_is_in_session(_pwl, "hello", -1));
+ enchant_dict_remove_from_session(_pwl, "hello", -1);
+ CHECK(!enchant_dict_is_in_session(_pwl, "hello", -1));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_IsNotPermanent)
+{
+ enchant_dict_remove_from_session(_dict, "hello", -1);
+ CHECK(!IsWordInDictionary("hello"));
+
+ ReloadTestDictionary();
+
+ CHECK(IsWordInDictionary("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_NotGivenAsSuggestion)
+{
+ enchant_dict_remove_from_session(_dict, "aelo", -1);
+
+ std::vector<const std::string> suggestions = GetSuggestions("helo");
+ CHECK_EQUAL(3, suggestions.size());
+ CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo",1), suggestions, std::min(3u,suggestions.size()) );
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_ThenAddedToSession_GivenAsSuggestion)
+{
+ enchant_dict_remove_from_session(_dict, "aelo", -1);
+ enchant_dict_add_to_session(_dict, "aelo", -1);
+
+ std::vector<const std::string> suggestions = GetSuggestions("helo");
+ CHECK_EQUAL(4, suggestions.size());
+ CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo"), suggestions, suggestions.size());
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_HasPreviousError_ErrorCleared)
+{
+ SetErrorOnMockDictionary("something bad happened");
+
+ enchant_dict_remove_from_session(_dict, "hello", -1);
+ CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));
+}
+
+/////////////////////////////////////////////////////////////////////////////
+// Test Error Conditions
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_NullDictionary_NotRemoved)
+{
+ enchant_dict_remove_from_session(NULL, "hello", -1);
+ CHECK(IsWordInDictionary("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_NullWord_NotRemoved)
+{
+ enchant_dict_remove_from_session(_dict, NULL, -1);
+ CHECK(IsWordInDictionary("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_EmptyWord_NotRemoved)
+{
+ enchant_dict_remove_from_session(_dict, "", -1);
+ CHECK(IsWordInDictionary("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_WordSize0_NotRemoved)
+{
+ enchant_dict_remove_from_session(_dict, "hello", 0);
+ CHECK(IsWordInDictionary("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,
+ EnchantDictionaryRemoveFromSession_InvalidUtf8Word_NotRemoved)
+{
+ enchant_dict_remove_from_session(_dict, "\xa5\xf1\x08", -1);
+ CHECK(IsWordInDictionary("hello"));
+}
diff --git a/unittests/dictionary/enchant_dict_remove_tests.cpp b/unittests/dictionary/enchant_dict_remove_tests.cpp new file mode 100644 index 0000000..5d2f730 --- /dev/null +++ b/unittests/dictionary/enchant_dict_remove_tests.cpp @@ -0,0 +1,336 @@ +/* Copyright (c) 2007 Eric Scott Albright
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#define NOMINMAX //don't want windows to collide with std::min
+
+#include <UnitTest++.h>
+#include <enchant.h>
+#include "../EnchantDictionaryTestFixture.h"
+
+static bool addToExcludeCalled;
+static std::string wordToAdd;
+
+static void
+MockDictionaryAddToExclude (EnchantDict * dict, const char *const word, size_t len)
+{
+ dict;
+ addToExcludeCalled = true;
+ wordToAdd = std::string(word, len);
+}
+
+static bool dictCheckCalled;
+
+static int
+MockDictionaryCheck (EnchantDict * dict, const char *const word, size_t len)
+{
+ dict;
+ dictCheckCalled = true;
+ if(strncmp("hello", word, len)==0)
+ {
+ return 0; //good word
+ }
+ return 1; // bad word
+}
+
+
+static EnchantDict* MockProviderRequestAddToExcludeMockDictionary(EnchantProvider * me, const char *tag)
+{
+
+ EnchantDict* dict = MockProviderRequestBasicMockDictionary(me, tag);
+ dict->add_to_exclude = MockDictionaryAddToExclude;
+ dict->check = MockDictionaryCheck;
+ return dict;
+}
+
+static void DictionaryAddToExclude_ProviderConfiguration (EnchantProvider * me, const char *)
+{
+ me->request_dict = MockProviderRequestAddToExcludeMockDictionary;
+ me->dispose_dict = MockProviderDisposeDictionary;
+}
+
+struct EnchantDictionaryRemove_TestFixture : EnchantDictionaryTestFixture
+{
+ //Setup
+ EnchantDictionaryRemove_TestFixture():
+ EnchantDictionaryTestFixture(DictionaryAddToExclude_ProviderConfiguration)
+ {
+ addToExcludeCalled = false;
+ dictCheckCalled = false;
+ }
+};
+
+static EnchantDict* MockProviderRequestCheckMockDictionary(EnchantProvider * me, const char *tag)
+{
+
+ EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);
+ dict->check = MockDictionaryCheck;
+ return dict;
+}
+
+static void DictionaryCheck_ProviderConfiguration (EnchantProvider * me, const char *)
+{
+ me->request_dict = MockProviderRequestCheckMockDictionary;
+ me->dispose_dict = MockProviderDisposeDictionary;
+}
+
+struct EnchantDictionaryRemoveNotImplemented_TestFixture : EnchantDictionaryTestFixture
+{
+ //Setup
+ EnchantDictionaryRemoveNotImplemented_TestFixture():
+ EnchantDictionaryTestFixture(DictionaryCheck_ProviderConfiguration)
+ {
+ addToExcludeCalled = false;
+ dictCheckCalled = false;
+ }
+};
+/**
+ * enchant_dict_remove
+ * @dict: A non-null #EnchantDict
+ * @word: The non-null word you wish to add to your exclude dictionary and
+ * remove from the personal dictionary, in UTF-8 encoding
+ * @len: The byte length of @word, or -1 for strlen (@word)
+ *
+ */
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Test Normal Operation
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_WordNoLongerExistsInDictionary)
+{
+ CHECK(IsWordInDictionary("hello"));
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(!IsWordInDictionary("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_WordNoLongerExistsInSession)
+{
+ enchant_dict_add(_dict, "hello", -1);
+ CHECK(IsWordInSession("hello"));
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(!IsWordInSession("hello"));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_WordAddedToEnchantExcludeFile)
+{
+ CHECK(!ExcludeFileHasContents());
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(ExcludeFileHasContents());
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_WordRemovedFromEnchantPwlFile)
+{
+ enchant_dict_add(_dict, "hello", -1);
+ CHECK(PersonalWordListFileHasContents());
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(!PersonalWordListFileHasContents());
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_PassedOnToProvider_LenComputed)
+{
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(addToExcludeCalled);
+ CHECK_EQUAL(std::string("hello"), wordToAdd);
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_PassedOnToProvider_LenSpecified)
+{
+ enchant_dict_remove(_dict, "hellodisregard me", 5);
+ CHECK(addToExcludeCalled);
+ CHECK_EQUAL(std::string("hello"), wordToAdd);
+}
+
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_WordRemovedFromSession_StillCallsProvider)
+{
+ enchant_dict_remove_from_session(_dict, "hello", -1);
+ CHECK(!addToExcludeCalled);
+
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(addToExcludeCalled);
+ CHECK_EQUAL(std::string("hello"), wordToAdd);
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_WordExistsInExclude_StillCallsProvider)
+{
+ enchant_dict_remove(_dict, "hello", -1);
+ addToExcludeCalled=false;
+ wordToAdd = std::string();
+
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(addToExcludeCalled);
+ CHECK_EQUAL(std::string("hello"), wordToAdd);
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_ProviderNotAskedIfWordExistsInDictionary)
+{
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(!IsWordInDictionary("hello"));
+ CHECK(!dictCheckCalled);
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_NotGivenAsSuggestion)
+{
+ enchant_dict_remove(_dict, "aelo", -1);
+
+ std::vector<const std::string> suggestions = GetSuggestions("helo");
+ CHECK_EQUAL(3, suggestions.size());
+ CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo",1), suggestions, std::min(3u,suggestions.size()));
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_ThenAddedToSession_GivenAsSuggestion)
+{
+ enchant_dict_remove(_dict, "aelo", -1);
+ enchant_dict_add_to_session(_dict, "aelo", -1);
+
+ std::vector<const std::string> suggestions = GetSuggestions("helo");
+ CHECK_EQUAL(4, suggestions.size());
+ CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo"), suggestions, suggestions.size());
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_ThenAddedToSession_WhenSessionOverStillExcluded)
+{
+ enchant_dict_remove(_dict, "aelo", -1);
+ enchant_dict_add_to_session(_dict, "aelo", -1);
+
+ ReloadTestDictionary(); // to see what actually persisted
+
+ std::vector<const std::string> suggestions = GetSuggestions("helo");
+ CHECK_EQUAL(3, suggestions.size());
+ CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo",1), suggestions, std::min(3u,suggestions.size()));
+}
+
+/* since the broker pwl file is a read/write file (there is no readonly dictionary in addition)
+ * there is no need for complementary exclude file to add a word to. The word just needs to be
+ * removed from the broker pwl file
+ */
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_OnBrokerPwl_RemovedFromBrokerPwlFile)
+{
+ CHECK(!BrokerPWLFileHasContents());
+ enchant_dict_add(_pwl, "personal", -1);
+ CHECK(BrokerPWLFileHasContents());
+
+ enchant_dict_remove(_pwl, "personal", -1);
+ CHECK(!BrokerPWLFileHasContents());
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_OnBrokerPwl_NotAddedToOtherPwlFile)
+{
+ CHECK(!BrokerPWLFileHasContents());
+
+ enchant_dict_remove(_pwl, "personal", -1);
+ CHECK(!addToExcludeCalled);
+ CHECK(!ExcludeFileHasContents());
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_IsPermanent)
+{
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(!IsWordInDictionary("hello"));
+
+ ReloadTestDictionary();
+
+ CHECK(!IsWordInDictionary("hello"));
+}
+
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_HasPreviousError_ErrorCleared)
+{
+ SetErrorOnMockDictionary("something bad happened");
+
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));
+}
+
+/////////////////////////////////////////////////////////////////////////////
+// Test Error Conditions
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_NullDictionary_NotRemoved)
+{
+ enchant_dict_remove(NULL, "hello", -1);
+ CHECK(!addToExcludeCalled);
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_NullWord_NotRemoved)
+{
+ enchant_dict_remove(_dict, NULL, -1);
+ CHECK(!addToExcludeCalled);
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_EmptyWord_NotRemoved)
+{
+ enchant_dict_remove(_dict, "", -1);
+ CHECK(!addToExcludeCalled);
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_WordSize0_NotRemoved)
+{
+ enchant_dict_remove(_dict, "hello", 0);
+ CHECK(!addToExcludeCalled);
+}
+
+TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
+ EnchantDictionaryRemove_InvalidUtf8Word_NotRemoved)
+{
+ enchant_dict_remove(_dict, "\xa5\xf1\x08", -1);
+ CHECK(!addToExcludeCalled);
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveNotImplemented_TestFixture,
+ EnchantDictionaryRemoveNotImplemented_WordAddedToExcludeFile)
+{
+ CHECK(!ExcludeFileHasContents());
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(ExcludeFileHasContents());
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveNotImplemented_TestFixture,
+ EnchantDictionaryRemoveNotImplemented_NotPassedOnToProvider)
+{
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(!addToExcludeCalled);
+}
+
+TEST_FIXTURE(EnchantDictionaryRemoveNotImplemented_TestFixture,
+ EnchantDictionaryRemoveNotImplemented_StillExcluded)
+{
+ CHECK(IsWordInDictionary("hello"));
+ enchant_dict_remove(_dict, "hello", -1);
+ CHECK(!IsWordInDictionary("hello"));
+}
diff --git a/unittests/dictionary/enchant_dict_suggest_tests.cpp b/unittests/dictionary/enchant_dict_suggest_tests.cpp index cc2f1e0..4480339 100644 --- a/unittests/dictionary/enchant_dict_suggest_tests.cpp +++ b/unittests/dictionary/enchant_dict_suggest_tests.cpp @@ -56,25 +56,7 @@ struct EnchantDictionarySuggestTestFixtureBase : EnchantDictionaryTestFixture FreeStringList(_suggestions);
}
- std::vector<std::string> GetExpectedSuggestions(const std::string& s, size_t begin = 0)
- {
- size_t cSuggestions;
- char** expectedSuggestions = MockDictionarySuggest (_dict,
- s.c_str(),
- s.size(),
- &cSuggestions);
-
- std::vector<std::string> result;
- if(expectedSuggestions != NULL && begin < cSuggestions){
- result.insert(result.begin(), expectedSuggestions+begin, expectedSuggestions+cSuggestions);
- FreeStringList(expectedSuggestions);
- }
-
- return result;
- }
-
char** _suggestions;
-
};
static char **
@@ -265,7 +247,7 @@ TEST_FIXTURE(EnchantDictionarySuggest_TestFixture, TEST_FIXTURE(EnchantDictionarySuggest_TestFixture,
EnchantDictionarySuggest_InBrokerPwlSession)
{
- enchant_dict_add_to_pwl(_pwl, "hello", -1);
+ enchant_dict_add(_pwl, "hello", -1);
_suggestions = enchant_dict_suggest(_pwl, "helo", -1, NULL);
CHECK(_suggestions);
CHECK(!dictSuggestCalled);
@@ -275,7 +257,7 @@ TEST_FIXTURE(EnchantDictionarySuggest_TestFixture, EnchantDictionarySuggest_SuggestionsFromPersonal_addedToEnd)
{
size_t cSuggestions;
- enchant_dict_add_to_pwl(_dict, "hello", -1);
+ enchant_dict_add(_dict, "hello", -1);
_suggestions = enchant_dict_suggest(_dict, "helo", -1, &cSuggestions);
CHECK(_suggestions);
CHECK_EQUAL(5, cSuggestions);
@@ -297,7 +279,7 @@ TEST_FIXTURE(EnchantDictionarySuggest_TestFixture, {
size_t cSuggestions;
- enchant_dict_add_to_pwl(_dict, "aelo", -1);
+ enchant_dict_add(_dict, "aelo", -1);
_suggestions = enchant_dict_suggest(_dict, "helo", -1, &cSuggestions);
CHECK(_suggestions);
CHECK_EQUAL(4, cSuggestions);
@@ -453,3 +435,17 @@ TEST_FIXTURE(EnchantDictionarySuggest_TestFixture, CHECK_EQUAL(1, cSuggestions);
CHECK_EQUAL(Convert(L"fianc\xe9"), _suggestions[0]);
}
+
+TEST_FIXTURE(EnchantDictionarySuggestNotImplemented_TestFixture,
+ EnchantDictionarySuggest_WordInDictionaryAndExclude_NotInSuggestions)
+{
+ ExternalAddWordToExclude("hello");
+ ExternalAddWordToDictionary("hello");
+
+ size_t cSuggestions;
+ _suggestions = enchant_dict_suggest(_dict, "helo", -1, &cSuggestions);
+ CHECK(!_suggestions);
+
+ CHECK_EQUAL(0, cSuggestions);
+}
+
diff --git a/unittests/pwl/enchant_pwl_tests.cpp b/unittests/pwl/enchant_pwl_tests.cpp index ab1958b..101466a 100644 --- a/unittests/pwl/enchant_pwl_tests.cpp +++ b/unittests/pwl/enchant_pwl_tests.cpp @@ -622,3 +622,293 @@ TEST_FIXTURE(EnchantPwl_TestFixture, CHECK_ARRAY_EQUAL(expected, suggestions, expected.size());
}
}
+
+/////////////////////////////////////////////////////////////////////////////
+// Remove from PWL
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_SharedPrefix1)
+{
+ AddWordToDictionary("help");
+ AddWordToDictionary("hello");
+
+ CHECK( IsWordInDictionary("help") );
+ CHECK( IsWordInDictionary("hello") );
+
+ RemoveWordFromDictionary("help");
+
+ CHECK(!IsWordInDictionary("help") );
+ CHECK( IsWordInDictionary("hello") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_SharedPrefix2)
+{
+ AddWordToDictionary("help");
+ AddWordToDictionary("hello");
+
+ CHECK( IsWordInDictionary("help") );
+ CHECK( IsWordInDictionary("hello") );
+
+ RemoveWordFromDictionary("hello");
+
+ CHECK( IsWordInDictionary("help") );
+ CHECK(!IsWordInDictionary("hello") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_SharedPrefix3)
+{
+ AddWordToDictionary("help");
+ AddWordToDictionary("hello");
+ AddWordToDictionary("helm");
+
+ CHECK( IsWordInDictionary("help") );
+ CHECK( IsWordInDictionary("hello") );
+ CHECK( IsWordInDictionary("helm") );
+
+ RemoveWordFromDictionary("hello");
+
+ CHECK( IsWordInDictionary("help") );
+ CHECK(!IsWordInDictionary("hello") );
+ CHECK( IsWordInDictionary("helm") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_SharedPrefix4)
+{
+ AddWordToDictionary("help");
+ AddWordToDictionary("hello");
+ AddWordToDictionary("helm");
+
+ CHECK( IsWordInDictionary("help") );
+ CHECK( IsWordInDictionary("hello") );
+ CHECK( IsWordInDictionary("helm") );
+
+ RemoveWordFromDictionary("help");
+
+ CHECK(!IsWordInDictionary("help") );
+ CHECK( IsWordInDictionary("hello") );
+ CHECK( IsWordInDictionary("helm") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_SingleWord)
+{
+ AddWordToDictionary("hello");
+
+ CHECK( IsWordInDictionary("hello") );
+
+ RemoveWordFromDictionary("hello");
+
+ CHECK(!IsWordInDictionary("hello") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_MultipleWords1)
+{
+ AddWordToDictionary("special");
+ AddWordToDictionary("hello");
+
+ CHECK( IsWordInDictionary("special") );
+ CHECK( IsWordInDictionary("hello") );
+
+ RemoveWordFromDictionary("hello");
+
+ CHECK( IsWordInDictionary("special") );
+ CHECK(!IsWordInDictionary("hello") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_MultipleWords2)
+{
+ AddWordToDictionary("special");
+ AddWordToDictionary("hello");
+
+ CHECK( IsWordInDictionary("special") );
+ CHECK( IsWordInDictionary("hello") );
+
+ RemoveWordFromDictionary("special");
+
+ CHECK(!IsWordInDictionary("special") );
+ CHECK( IsWordInDictionary("hello") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_ProperPrefix1)
+{
+ AddWordToDictionary("ant");
+ AddWordToDictionary("anteater");
+
+ CHECK( IsWordInDictionary("ant") );
+ CHECK( IsWordInDictionary("anteater") );
+
+ RemoveWordFromDictionary("ant");
+
+ CHECK(!IsWordInDictionary("ant") );
+ CHECK( IsWordInDictionary("anteater") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_ProperPrefix2)
+{
+ AddWordToDictionary("anteater");
+ AddWordToDictionary("ant");
+
+ CHECK( IsWordInDictionary("ant") );
+ CHECK( IsWordInDictionary("anteater") );
+
+ RemoveWordFromDictionary("ant");
+
+ CHECK(!IsWordInDictionary("ant") );
+ CHECK( IsWordInDictionary("anteater") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_ProperPrefix3)
+{
+ AddWordToDictionary("ant");
+ AddWordToDictionary("anteater");
+
+ CHECK( IsWordInDictionary("ant") );
+ CHECK( IsWordInDictionary("anteater") );
+
+ RemoveWordFromDictionary("anteater");
+
+ CHECK( IsWordInDictionary("ant") );
+ CHECK(!IsWordInDictionary("anteater") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_ProperPrefix4)
+{
+ AddWordToDictionary("anteater");
+ AddWordToDictionary("ant");
+
+ CHECK( IsWordInDictionary("ant") );
+ CHECK( IsWordInDictionary("anteater") );
+
+ RemoveWordFromDictionary("anteater");
+
+ CHECK( IsWordInDictionary("ant") );
+ CHECK(!IsWordInDictionary("anteater") );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_ItemRemovedFromFile)
+{
+ std::vector<const std::string> sWords;
+ sWords.push_back("cat");
+ sWords.push_back("hat");
+ sWords.push_back("that");
+ sWords.push_back("bat");
+ sWords.push_back("tot");
+
+ std::vector<const std::string>::const_iterator removed = sWords.insert(sWords.begin()+2, "hello");
+ AddWordsToDictionary(sWords);
+
+ RemoveWordFromDictionary("hello");
+
+ ReloadTestDictionary(); // to see what actually persisted
+
+ for(std::vector<const std::string>::const_iterator itWord = sWords.begin(); itWord != removed; ++itWord){
+ CHECK( IsWordInDictionary(*itWord) );
+ }
+ CHECK(!IsWordInDictionary(*removed) );
+
+ for(std::vector<const std::string>::const_iterator itWord = removed+1; itWord != sWords.end(); ++itWord){
+ CHECK(IsWordInDictionary(*itWord) );
+ }
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_ItemRemovedFromBeginningOfFile)
+{
+ std::vector<const std::string> sWords;
+ sWords.push_back("cat");
+ sWords.push_back("hat");
+ sWords.push_back("that");
+ sWords.push_back("bat");
+ sWords.push_back("tot");
+
+ std::vector<const std::string>::const_iterator removed = sWords.insert(sWords.begin(), "hello");
+ AddWordsToDictionary(sWords);
+
+ RemoveWordFromDictionary("hello");
+
+ ReloadTestDictionary(); // to see what actually persisted
+
+ CHECK(!IsWordInDictionary(*removed) );
+
+ for(std::vector<const std::string>::const_iterator itWord = removed+1; itWord != sWords.end(); ++itWord){
+ CHECK(IsWordInDictionary(*itWord) );
+ }
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_ItemRemovedFromBeginningOfFileWithBOM)
+{
+ std::string Utf8Bom ("\xef\xbb\xbf");
+
+ std::vector<const std::string> sWords;
+ sWords.push_back(Utf8Bom + "hello");
+ sWords.push_back("cat");
+ sWords.push_back("hat");
+
+ ExternalAddWordsToDictionary(sWords);
+
+ RemoveWordFromDictionary("hello");
+
+ ReloadTestDictionary(); // to see what actually persisted
+
+ CHECK(!IsWordInDictionary("hello") );
+
+ for(std::vector<const std::string>::const_iterator itWord = sWords.begin()+1; itWord != sWords.end(); ++itWord){
+ CHECK(IsWordInDictionary(*itWord) );
+ }
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_ItemRemovedFromEndOfFile)
+{
+ std::vector<const std::string> sWords;
+ sWords.push_back("cat");
+ sWords.push_back("hat");
+ sWords.push_back("that");
+ sWords.push_back("bat");
+ sWords.push_back("tot");
+
+ std::vector<const std::string>::const_iterator removed = sWords.insert(sWords.end(), "hello");
+ AddWordsToDictionary(sWords);
+
+ RemoveWordFromDictionary("hello");
+
+ ReloadTestDictionary(); // to see what actually persisted
+
+ for(std::vector<const std::string>::const_iterator itWord = sWords.begin(); itWord != removed; ++itWord){
+ CHECK( IsWordInDictionary(*itWord) );
+ }
+ CHECK(!IsWordInDictionary(*removed) );
+}
+
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ PwlRemove_FileHasProperSubset_ItemRemovedFromFile)
+{
+ std::vector<const std::string> sWords;
+ sWords.push_back("cat");
+ sWords.push_back("hat");
+ sWords.push_back("that");
+ sWords.push_back("bat");
+ sWords.push_back("tot");
+ sWords.push_back("anteater");
+
+ std::vector<const std::string>::const_iterator removed = sWords.insert(sWords.end(), "ant");
+ AddWordsToDictionary(sWords);
+ RemoveWordFromDictionary("ant");
+
+ ReloadTestDictionary(); // to see what actually persisted
+
+ for(std::vector<const std::string>::const_iterator itWord = sWords.begin(); itWord != removed; ++itWord){
+ CHECK( IsWordInDictionary(*itWord) );
+ }
+ CHECK(!IsWordInDictionary(*removed) );
+}
|