1 /* Copyright (c) 2007 Eric Scott Albright
\r
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
4 * of this software and associated documentation files (the "Software"), to deal
\r
5 * in the Software without restriction, including without limitation the rights
\r
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
7 * copies of the Software, and to permit persons to whom the Software is
\r
8 * furnished to do so, subject to the following conditions:
\r
10 * The above copyright notice and this permission notice shall be included in
\r
11 * all copies or substantial portions of the Software.
\r
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
23 using System.Collections.Generic;
\r
25 using System.Threading;
\r
26 using Microsoft.Win32;
\r
27 using NUnit.Framework;
\r
29 namespace Enchant.Tests
\r
32 public class DictionaryTests
\r
34 #region Setup/Teardown
\r
39 oldRegistryValue = (string) Registry.GetValue(@"HKEY_CURRENT_USER\Software\Enchant\Config", "Data_Dir", null);
\r
40 tempdir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
\r
42 Registry.SetValue(@"HKEY_CURRENT_USER\Software\Enchant\Config", "Data_Dir", tempdir, RegistryValueKind.String);
\r
43 broker = new Broker();
\r
44 dictionary = broker.RequestDictionary("en_US");
\r
48 public void Teardown()
\r
50 if (oldRegistryValue == null)
\r
52 Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Enchant").OpenSubKey("Config", true).DeleteValue(
\r
57 Registry.SetValue(@"HKEY_CURRENT_USER\Software\Enchant\Config",
\r
60 RegistryValueKind.String);
\r
63 dictionary.Dispose();
\r
65 while (Directory.Exists(tempdir))
\r
67 Directory.Delete(tempdir, true);
\r
73 private Broker broker;
\r
74 private Dictionary dictionary;
\r
75 private string tempdir;
\r
76 private string oldRegistryValue;
\r
79 public void FixtureSetup()
\r
81 string providerDir = Path.Combine(Path.Combine(
\r
82 Directory.GetCurrentDirectory(), "lib"), "enchant");
\r
83 if (!Directory.Exists(providerDir))
\r
85 Directory.CreateDirectory(providerDir);
\r
87 File.Copy("libenchant_ispell.dll",
\r
88 Path.Combine(providerDir, "libenchant_ispell.dll"), true);
\r
89 File.Copy("libenchant_myspell.dll",
\r
90 Path.Combine(providerDir, "libenchant_myspell.dll"), true);
\r
91 InstallDictionary("myspell", new string[]{"en_US.aff", "en_US.dic"});
\r
94 static private void InstallDictionary(string provider, IEnumerable<string> files)
\r
96 string dictionarySourceDir =
\r
97 Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(
\r
98 Directory.GetCurrentDirectory(), ".."), ".."),
\r
100 "enchant"), provider);
\r
102 string dictionaryDestDir = Path.Combine(Path.Combine(Path.Combine(
\r
103 Directory.GetCurrentDirectory(), "share"), "enchant"),
\r
106 if (!Directory.Exists(dictionaryDestDir))
\r
108 Directory.CreateDirectory(dictionaryDestDir);
\r
111 foreach (string file in files)
\r
113 File.Copy(Path.Combine(dictionarySourceDir, file),
\r
114 Path.Combine(dictionaryDestDir, file), true);
\r
119 [TestFixtureTearDown]
\r
120 public void FixtureTearDown()
\r
122 Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), "lib"), true);
\r
123 Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), "share"), true);
\r
129 Assert.IsFalse(dictionary.Check("Googled"));
\r
130 dictionary.Add("Googled");
\r
131 Assert.IsTrue(dictionary.Check("Googled"));
\r
135 public void AddToSession(string word)
\r
137 Assert.IsFalse(dictionary.Check("list"));
\r
138 dictionary.AddToSession("list");
\r
139 Assert.IsTrue(dictionary.Check("list"));
\r
143 public void Check()
\r
145 Assert.IsTrue(dictionary.Check("hello"));
\r
146 Assert.IsFalse(dictionary.Check("helo"));
\r
150 public void Information()
\r
152 DictionaryInfo info = dictionary.Information;
\r
153 Console.WriteLine("Language:{0}\tName:{1}\tDescription:{2}\tFile:{3}",
\r
155 info.Provider.Name,
\r
156 info.Provider.Description,
\r
157 info.Provider.File);
\r
158 Assert.AreEqual("en_US", info.Language);
\r
159 Assert.IsNotEmpty(info.Provider.Name);
\r
160 Assert.IsNotEmpty(info.Provider.Description);
\r
161 Assert.IsNotEmpty(info.Provider.File);
\r
165 public void IsAdded()
\r
167 Assert.IsFalse(dictionary.IsAdded("list"));
\r
168 dictionary.AddToSession("list");
\r
169 Assert.IsTrue(dictionary.IsAdded("list"));
\r
173 public void IsRemoved()
\r
175 Assert.IsFalse(dictionary.IsRemoved("list"));
\r
176 dictionary.RemoveFromSession("list");
\r
177 Assert.IsTrue(dictionary.IsRemoved("list"));
\r
181 public void Remove()
\r
183 Assert.IsTrue(dictionary.Check("world"));
\r
184 dictionary.Remove("world");
\r
185 Assert.IsFalse(dictionary.Check("world"));
\r
189 public void RemoveFromSession()
\r
191 Assert.IsTrue(dictionary.Check("hello"));
\r
192 dictionary.RemoveFromSession("hello");
\r
193 Assert.IsFalse(dictionary.Check("hello"));
\r
197 public void StoreReplacement()
\r
199 dictionary.StoreReplacement("theirs", "their's");
\r
203 public void Suggest()
\r
205 List<string> suggestions = new List<string>(dictionary.Suggest("helo"));
\r
206 Assert.Contains("hello", suggestions);
\r
210 public void Dispose_Called_SendsDisposedEvent()
\r
212 bool disposedEventCalled = false;
\r
213 dictionary.Disposed += delegate
\r
214 { disposedEventCalled = true; };
\r
215 dictionary.Dispose();
\r
216 Assert.IsTrue(disposedEventCalled);
\r