File IO Tests
For this, we need to determine about the minimum characteristics of file IO capabilities. Whenever you open a streamreader or streamwriter object, you must ensure the object is disposed, and so using is a nice keyword to use for this. An example of using is defined below:
using (StreamWriter writer = new StreamWriter(this.GetPath(), false))
{
writer.Write("Test");
}
The using keyword is useful when the object you are instantiating implements IDisposable. This is because it will automatically handle disposing of the object for you; this way, we can ensure that the writer is closed upon completion, and the file will not be locked for the next reader/writer access.
From this, I create the following tests:
[TestFixture()]
public class FileIOTest
{
private string GetPath()
{
return AppDomain.CurrentDomain.BaseDirectory + "\\test.tl";
}
[Test()]
public void TestWriteToFile()
{
using (StreamWriter writer = new StreamWriter(this.GetPath(), false))
{
writer.Write("Test");
}
using (StreamReader reader = new StreamReader(this.GetPath()))
{
Assert.AreEqual("Test", reader.ReadToEnd());
}
}
[Test()]
public void TestWriteLineToFile()
{
using (StreamWriter writer = new StreamWriter(this.GetPath(), false))
{
writer.WriteLine("Test");
}
using (StreamReader reader = new StreamReader(this.GetPath()))
{
Assert.AreEqual("Test\r\n", reader.ReadToEnd());
}
}
[Test()]
public void TestWriteLinesToFile()
{
using (StreamWriter writer = new StreamWriter(this.GetPath(), false))
{
writer.WriteLine("Test");
}
using (StreamReader reader = new StreamReader(this.GetPath()))
{
Assert.AreEqual("Test\r\n", reader.ReadToEnd());
}
using (StreamWriter writer = new StreamWriter(this.GetPath(), true))
{
writer.WriteLine("Test");
}
using (StreamReader reader = new StreamReader(this.GetPath()))
{
Assert.AreEqual("Test\r\nTest\r\n", reader.ReadToEnd());
}
}
}
All of these tests worked successfully. But, I had been thinking about using XML, and so the next test we will have to perform will use the XML objects to access it.