Пример #1
0
        public void Section2ExampleShouldWork()
        {
            using (StringWriter sw = new StringWriter())
            {
                // Save a reference to the standard output.
                TextWriter stdout = Console.Out;

                // Redirect standard output to variable.
                Console.SetOut(sw);
                SaveableDictionary dictionary = new SaveableDictionary();
                dictionary.Add("apina", "monkey");
                dictionary.Add("banaani", "banana");
                dictionary.Add("apina", "apfe");
                dictionary.Add("ohjelmointi", "programming");
                dictionary.Delete("apina");
                dictionary.Delete("banana");
                Console.WriteLine(dictionary.Translate("apina"));
                Console.WriteLine(dictionary.Translate("monkey"));
                Console.WriteLine(dictionary.Translate("banana"));
                Console.WriteLine(dictionary.Translate("banaani"));
                Console.WriteLine(dictionary.Translate("ohjelmointi"));
                Console.SetOut(stdout);
                string comparison = "\n\n\n\nprogramming\n";
                Assert.AreEqual(comparison, sw.ToString().Replace("\r\n", "\n"), "Section 2 example should work as such!");
            }
        }
Пример #2
0
        public void Section4and5ExampleShouldWork()
        {
            using (StringWriter sw = new StringWriter())
            {
                // Save a reference to the standard output.
                TextWriter stdout = Console.Out;
                // Redirect standard output to variable.
                Console.SetOut(sw);
                SaveableDictionary dictionary = new SaveableDictionary("testwords.txt");
                dictionary.Load();
                // Translate all the words in the file both ways
                Console.WriteLine(dictionary.Translate("apina"));
                Console.WriteLine(dictionary.Translate("monkey"));
                Console.WriteLine(dictionary.Translate("beer"));
                Console.WriteLine(dictionary.Translate("olut"));
                Console.WriteLine(dictionary.Translate("below"));
                Console.WriteLine(dictionary.Translate("alla oleva"));

                // Try adding, translating and removing a word, this should not affect the file
                dictionary.Add("poista", "remove");
                Console.WriteLine(dictionary.Translate("remove"));
                dictionary.Delete("remove");
                Console.SetOut(stdout);
                string comparison = "monkey\napina\nolut\nbeer\nalla oleva\nbelow\npoista\n";
                Assert.AreEqual(comparison, sw.ToString().Replace("\r\n", "\n"), "Section 4 & 5 example should work as such! Do not touch the file testwords.txt!");
            }
        }
Пример #3
0
 public void SavingToFileShouldWork()
 {
     using (StreamWriter filer = new StreamWriter("savefile.txt"))
     {
         SaveableDictionary dictionary = new SaveableDictionary("savefile.txt");
         dictionary.Load();
         // Translate all the words in the file both ways
         dictionary.Add("apina", "monkey");
         dictionary.Add("banaani", "banana");
         dictionary.Add("apina", "apfe");
         dictionary.Add("ohjelmointi", "programming");
         dictionary.Save();
         string code    = File.ReadAllText("savefile.txt");
         string content = "apina:monkey\nbanaani:banana\nohjelmointi:programming\n";
         File.Delete("savefile.txt");
         Assert.AreEqual(content, code.Replace("\r\n", "\n"), "Saving translations to a file should work!");
     }
 }
Пример #4
0
        public void SavingToFileShouldWork()
        {
            string fileName = "hiddenfile.txt";

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            File.CreateText(fileName).Close();
            SaveableDictionary dictionary = new SaveableDictionary(fileName);

            dictionary.Load();
            // Translate all the words in the file both ways
            dictionary.Add("apina", "monkey");
            dictionary.Add("banaani", "banana");
            dictionary.Add("apina", "apfe");
            dictionary.Add("ohjelmointi", "programming");
            dictionary.Save();
            string code    = File.ReadAllText(fileName);
            string content = "apina:monkey\nbanaani:banana\nohjelmointi:programming\n";

            File.Delete(fileName);
            Assert.AreEqual(content, code.Replace("\r\n", "\n"), "Saving translations to a file should work!");
        }