Enchant
Generic spell checking library
EnchantTestFixture.h
1 /* Copyright (c) 2007 Eric Scott Albright
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21 
22 #ifndef __ENCHANTTESTFIXTURE
23 #define __ENCHANTTESTFIXTURE
24 
25 #include <assert.h>
26 #include <glib.h>
27 #ifdef G_OS_UNIX
28 #include <fcntl.h> /* For creat; FIXME: should not be needed */
29 #endif
30 #include <glib/gstdio.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include <string>
37 #include <locale>
38 #include <codecvt>
39 
40 #include "enchant.h"
41 #include "enchant-provider.h"
42 
43 struct EnchantTestFixture
44 {
45  //Setup
47  {
48  CleanUpFiles(); //just in case we stopped the process in the middle.
49  CreateDirectory(GetTempUserEnchantDir());
50  }
51 
52  //Teardown
54  CleanUpFiles();
55  }
56  void CleanUpFiles()
57  {
58  DeleteDirAndFiles(GetTempUserEnchantDir());
59  DeleteDirAndFiles(AddToPath(LIBDIR_SUBDIR, "enchant-" ENCHANT_MAJOR_VERSION));
60  DeleteDirAndFiles("share");
61  }
62 
63  std::string GetTempUserEnchantDir()
64  {
65  return getenv("ENCHANT_CONFIG_DIR");
66  }
67 
68  std::string GetEnchantConfigDir()
69  {
70  GSList *config_dirs = enchant_get_conf_dirs();
71  const char *pkgdatadir = (char *)g_slist_nth(config_dirs, 0)->data;
72  return std::string(pkgdatadir);
73  }
74 
75  static void DeleteDirAndFiles(const std::string& dir)
76  {
77  GDir* gdir = g_dir_open(dir.c_str(), 0, NULL);
78  if(gdir != NULL)
79  {
80  const gchar* filename;
81  for(;;){
82  filename = g_dir_read_name(gdir);
83  if(filename == NULL)
84  {
85  break;
86  }
87  std::string filepath = AddToPath(dir, filename);
88  if(g_file_test(filepath.c_str(), G_FILE_TEST_IS_DIR)){
89  DeleteDirAndFiles(filepath);
90  }
91  else {
92  DeleteFile(filepath);
93  }
94  }
95  g_dir_close(gdir);
96  }
97  g_rmdir(dir.c_str());
98  }
99 
100  static std::string GetTemporaryFilename(const std::string & prefix){
101  char* tempFileName = tempnam(".", prefix.c_str());
102  std::string temp(tempFileName);
103  free(tempFileName);
104  return temp;
105  }
106 
107  static void CreateDirectory(const std::string& filepath)
108  {
109  g_mkdir_with_parents(filepath.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
110  }
111  static void CreateFile(const std::string& filepath)
112  {
113  int fh = g_creat(filepath.c_str(), S_IRUSR | S_IWUSR);
114  if(fh != -1) {
115  close(fh);
116  }
117  }
118  static void DeleteFile(const std::string& filepath)
119  {
120  if(FileExists(filepath)){
121  g_remove(filepath.c_str());
122  }
123  }
124  static bool FileExists(const std::string& filepath)
125  {
126  return(g_access(filepath.c_str(), 0)==0);
127  }
128 
129  std::string Convert(const std::wstring & ws)
130  {
131  std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
132  return convert.to_bytes(ws);
133  }
134 
135  std::wstring Convert(const std::string & s)
136  {
137  std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
138  return convert.from_bytes(s);
139  }
140 
141  static std::string AddToPath(const std::string & path, const std::string & fileOrDirName)
142  {
143  std::string result;
144  gchar* filename = g_build_filename(path.c_str(), fileOrDirName.c_str(), NULL);
145  result = std::string(filename);
146  g_free(filename);
147 
148  return result;
149  }
150 };
151 
152 #endif
Definition: EnchantTestFixture.h:44