Пример #1
0
        public void Test()
        {
            WordList wordList = new WordList();

            wordList.Add("apple", "a round fruit", null);
            wordList.Add("oRRange", "a round citrus fruit", "fruit");

            Assert.That(wordList.GetAllWords().Select(w => w.Word.Name), Is.EquivalentTo(new[] { "apple", "oRRange" }));
            Assert.That(wordList.GetAllTags(), Is.EquivalentTo(new[] { "fruit" }));
            Assert.That(wordList.GetWordsWithTag("fruit").Select(w => w.Word.Name), Is.EquivalentTo(new[] { "oRRange" }));

            wordList.Update("apple", "apple", "a round fruit", "fruit");
            wordList.Update("oRRange", "orange", "a round citrus fruit", "fruit");

            Assert.That(wordList.GetAllWords().Select(w => w.Word.Name), Is.EquivalentTo(new[] { "apple", "orange" }));
            Assert.That(wordList.GetAllTags(), Is.EquivalentTo(new[] { "fruit" }));
            Assert.That(wordList.GetWordsWithTag("fruit").Select(w => w.Word.Name), Is.EquivalentTo(new[] { "apple", "orange" }));

            wordList.MarkTranslation("a round fruit", WordEventType.Remembered);
            WordEvent translationRemenberedEvent = wordList.GetAllTranslations().Single(t => t.Translation == "a round fruit").Events.First().WordEvent;

            Assert.That(translationRemenberedEvent.EventType, Is.EqualTo(WordEventType.Remembered));
            Assert.That(translationRemenberedEvent.Translation, Is.EqualTo("a round fruit"));

            MemoryStream       mem        = new MemoryStream();
            WordListFileParser fileParser = new WordListFileParser();

            fileParser.GenerateZip(wordList, mem);

            WordList wordList2 = fileParser.ParseZip(mem);

            Assert.That(wordList2.GetAllWords().Select(w => w.Word.Name), Is.EquivalentTo(new[] { "apple", "orange" }));
            Assert.That(wordList2.GetAllTags(), Is.EquivalentTo(new[] { "fruit" }));
            Assert.That(wordList2.GetWordsWithTag("fruit").Select(w => w.Word.Name), Is.EquivalentTo(new[] { "apple", "orange" }));

            translationRemenberedEvent = wordList2.GetAllTranslations().Single(t => t.Translation == "a round fruit").Events.First().WordEvent;
            Assert.That(translationRemenberedEvent.EventType, Is.EqualTo(WordEventType.Remembered));
            Assert.That(translationRemenberedEvent.Translation, Is.EqualTo("a round fruit"));
        }
Пример #2
0
        public void SaveDictionary(string filename)
        {
            if (File.Exists(filename))
            {
                string backupFilename = filename + ".bak";
                if (File.Exists(backupFilename))
                {
                    File.Delete(backupFilename);
                }
                File.Copy(filename, filename + ".bak");
            }
            using (FileStream stream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                WordListFileParser parser = new WordListFileParser();
                parser.GenerateZip(WordList, stream);
            }
            DictPath = filename;
            WordList.ResetModified();

            Settings.Load();
            Settings.AddRecentFile(filename);
            Settings.Save();
        }
Пример #3
0
        public void OpenDictionary(string filename)
        {
            bool oldFormat = false;

            try
            {
                using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    WordListFileParser parser = new WordListFileParser();
                    WordList  = parser.ParseXml(stream);
                    oldFormat = true;
                }
            }
            catch (ParserException)
            {
                //ignore an attempt to read file in old format if it fails
            }

            if (!oldFormat)
            {
                using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    WordListFileParser parser = new WordListFileParser();
                    WordList = parser.ParseZip(stream);
                }
            }

            DictPath = oldFormat ? null : filename;

            if (!oldFormat)
            {
                Settings.Load();
                Settings.AddRecentFile(filename);
                Settings.Save();
            }
        }