예제 #1
0
        public void WritingToFile()
        {
            // Make a new log file and write to it
            TransLog log = new TransLog();

            if (File.Exists(log.LogPath))
            {
                File.Delete(log.LogPath);
            }
            Assert.IsFalse(File.Exists(log.LogPath), "No log file should exist yet at the target location.");
            string sampleTextA = "Hello, world!";

            log.Writer(sampleTextA, log.LogPath);

            // Does it have the expected text?
            using (StreamReader sr = new StreamReader(log.LogPath))
            {
                while (!sr.EndOfStream)
                {
                    Assert.AreEqual(sampleTextA, sr.ReadLine(), "File should contain: Hello, world!");
                }
            }

            // Can new text append?
            string sampleTextB = "My name is Log.txt, nice to meet you.";

            log.Writer(sampleTextB, log.LogPath);
            using (StreamReader sr = new StreamReader(log.LogPath))
            {
                Assert.AreEqual(sampleTextA, sr.ReadLine(), "File should have sampleTextA on first line");
                Assert.AreEqual(sampleTextB, sr.ReadLine(), "File should have sampleTextB on second line");
            }

            File.Delete(log.LogPath);
        }
예제 #2
0
        public void CreateLogFile()
        {
            // Instantiate new log and clear any existing Log.txt file
            TransLog log = new TransLog();

            if (File.Exists(log.LogPath))
            {
                File.Delete(log.LogPath);
            }
            Assert.IsFalse(File.Exists(log.LogPath), "No log file should exist yet at the target location.");

            // Create a new file
            string sampleTextA = "Hello, world!";

            log.Writer(sampleTextA, log.LogPath);

            // Was the file created?
            Assert.IsTrue(File.Exists(log.LogPath), "Log file should have been created.");

            // Cleanup
            File.Delete(log.LogPath);
        }