Skip to content

Commit

Permalink
changed exception messages and types to match exceptions thrown by .net
Browse files Browse the repository at this point in the history
  • Loading branch information
srasch committed Feb 20, 2015
1 parent 5fb1722 commit 192fbec
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 27 deletions.
99 changes: 77 additions & 22 deletions TestHelpers.Tests/MockFileTests.cs
Expand Up @@ -775,7 +775,7 @@ public void MockFile_Move_ShouldThrowArgumentNullExceptionWhenSourceIsNull_Messa

var exception = Assert.Throws<ArgumentNullException>(()=>fileSystem.File.Move(null, destFilePath));

Assert.That(exception.Message, Is.StringStarting("Value can not be null."));
Assert.That(exception.Message, Is.StringStarting("File name cannot be null."));
}

[Test]
Expand All @@ -788,44 +788,99 @@ public void MockFile_Move_ShouldThrowArgumentNullExceptionWhenSourceIsNull_Messa
Assert.That(exception.ParamName, Is.EqualTo("sourceFileName"));
}

[TestCase('>')]
[TestCase('<')]
[TestCase('"')]
public void MockFile_Move_ShouldThrowArgumentExceptionWhenSourceContainsInvalidChars_Message(char invalidChar)
[Test]
public void MockFile_Move_ShouldThrowNotSupportedExceptionWhenSourceFileNameContainsInvalidChars_Message()
{
if (XFS.IsUnixPlatform())
{
Assert.Pass("Path.GetInvalidChars() does not return anything on Mono");
return;
}

var sourceFilePath = XFS.Path(@"c:\something\demo.txt") + invalidChar;
string destFilePath = XFS.Path(@"c:\something\demo.txt");
var destFilePath = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem();

var exception = Assert.Throws<ArgumentException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));
foreach (var invalidChar in fileSystem.Path.GetInvalidFileNameChars().Where(x => x != fileSystem.Path.DirectorySeparatorChar))
{
var sourceFilePath = XFS.Path(@"c:\something\demo.txt") + invalidChar;

var exception =
Assert.Throws<NotSupportedException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));

Assert.That(exception.Message, Is.StringStarting("Illegal characters in path."));
Assert.That(exception.Message, Is.EqualTo("The given path's format is not supported."),
string.Format("Testing char: [{0:c}] \\{1:X4}", invalidChar, (int)invalidChar));
}
}

[TestCase('>')]
[TestCase('<')]
[TestCase('"')]
public void MockFile_Move_ShouldThrowArgumentExceptionWhenTargetContainsInvalidChars_Message(char invalidChar)
[Test]
public void MockFile_Move_ShouldThrowNotSupportedExceptionWhenSourcePathContainsInvalidChars_Message()
{
if (XFS.IsUnixPlatform())
{
Assert.Pass("Path.GetInvalidChars() does not return anything on Mono");
return;
}

string sourceFilePath = XFS.Path(@"c:\something\demo.txt");
var destFilePath = XFS.Path(@"c:\something\demo.txt") + invalidChar;
var destFilePath = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem();

var exception = Assert.Throws<ArgumentException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));
foreach (var invalidChar in fileSystem.Path.GetInvalidPathChars())
{
var sourceFilePath = XFS.Path(@"c:\some" + invalidChar + @"thing\demo.txt");

var exception =
Assert.Throws<ArgumentException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));

Assert.That(exception.Message, Is.EqualTo("Illegal characters in path."),
string.Format("Testing char: [{0:c}] \\{1:X4}", invalidChar, (int)invalidChar));
}
}

[Test]
public void MockFile_Move_ShouldThrowNotSupportedExceptionWhenTargetPathContainsInvalidChars_Message()
{
if (XFS.IsUnixPlatform())
{
Assert.Pass("Path.GetInvalidChars() does not return anything on Mono");
return;
}

Assert.That(exception.Message, Is.StringStarting("Illegal characters in path."));
var sourceFilePath = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem();

foreach (var invalidChar in fileSystem.Path.GetInvalidPathChars())
{
var destFilePath = XFS.Path(@"c:\some" + invalidChar + @"thing\demo.txt");

var exception =
Assert.Throws<ArgumentException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));

Assert.That(exception.Message, Is.EqualTo("Illegal characters in path."),
string.Format("Testing char: [{0:c}] \\{1:X4}", invalidChar, (int)invalidChar));
}
}
[Test]
public void MockFile_Move_ShouldThrowNotSupportedExceptionWhenTargetFileNameContainsInvalidChars_Message()
{
if (XFS.IsUnixPlatform())
{
Assert.Pass("Path.GetInvalidChars() does not return anything on Mono");
return;
}

var sourceFilePath = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem();

foreach (var invalidChar in fileSystem.Path.GetInvalidFileNameChars().Where(x => x != fileSystem.Path.DirectorySeparatorChar))
{
var destFilePath = XFS.Path(@"c:\something\demo.txt") + invalidChar;

var exception =
Assert.Throws<NotSupportedException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));

Assert.That(exception.Message, Is.EqualTo("The given path's format is not supported."),
string.Format("Testing char: [{0:c}] \\{1:X4}", invalidChar, (int)invalidChar));
}
}

[Test]
Expand All @@ -836,7 +891,7 @@ public void MockFile_Move_ShouldThrowArgumentExceptionWhenSourceIsEmpty_Message(

var exception = Assert.Throws<ArgumentException>(() => fileSystem.File.Move(string.Empty, destFilePath));

Assert.That(exception.Message, Is.StringStarting("An empty file name is invalid."));
Assert.That(exception.Message, Is.StringStarting("Empty file name is not legal."));
}

[Test]
Expand All @@ -857,7 +912,7 @@ public void MockFile_Move_ShouldThrowArgumentExceptionWhenSourceIsStringOfBlanks

var exception = Assert.Throws<ArgumentException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));

Assert.That(exception.Message, Is.EqualTo("The path has an invalid format."));
Assert.That(exception.Message, Is.EqualTo("The path is not of a legal form."));
}

[Test]
Expand All @@ -868,7 +923,7 @@ public void MockFile_Move_ShouldThrowArgumentNullExceptionWhenTargetIsNull_Messa

var exception = Assert.Throws<ArgumentNullException>(() => fileSystem.File.Move(sourceFilePath, null));

Assert.That(exception.Message, Is.StringStarting("Value can not be null."));
Assert.That(exception.Message, Is.StringStarting("File name cannot be null."));
}

[Test]
Expand All @@ -890,7 +945,7 @@ public void MockFile_Move_ShouldThrowArgumentExceptionWhenTargetIsStringOfBlanks

var exception = Assert.Throws<ArgumentException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));

Assert.That(exception.Message, Is.EqualTo("The path has an invalid format."));
Assert.That(exception.Message, Is.EqualTo("The path is not of a legal form."));
}

[Test]
Expand All @@ -901,7 +956,7 @@ public void MockFile_Move_ShouldThrowArgumentExceptionWhenTargetIsEmpty_Message(

var exception = Assert.Throws<ArgumentException>(() => fileSystem.File.Move(sourceFilePath, string.Empty));

Assert.That(exception.Message, Is.StringStarting("An empty file name is invalid."));
Assert.That(exception.Message, Is.StringStarting("Empty file name is not legal."));
}

[Test]
Expand Down
23 changes: 18 additions & 5 deletions TestingHelpers/MockFile.cs
Expand Up @@ -217,13 +217,26 @@ public override DateTime GetLastWriteTimeUtc(string path)
[DebuggerNonUserCode]
private void ValidateParameter(string value, string paramName) {
if (value == null)
throw new ArgumentNullException(paramName, "Value can not be null.");
throw new ArgumentNullException(paramName, "File name cannot be null.");
if (value == string.Empty)
throw new ArgumentException("An empty file name is invalid.", paramName);
throw new ArgumentException("Empty file name is not legal.", paramName);
if (value.Trim() == "")
throw new ArgumentException("The path has an invalid format.");
if (value.IndexOfAny(mockPath.GetInvalidPathChars()) > -1)
throw new ArgumentException("Illegal characters in path.", paramName);
throw new ArgumentException("The path is not of a legal form.");
if (ExtractFileName(value).IndexOfAny(mockPath.GetInvalidFileNameChars()) > -1)
throw new NotSupportedException("The given path's format is not supported.");
if (ExtractFilePath(value).IndexOfAny(mockPath.GetInvalidPathChars()) > -1)
throw new ArgumentException("Illegal characters in path.");
}

private string ExtractFilePath(string fullFileName)
{
var extractFilePath = fullFileName.Split(mockPath.DirectorySeparatorChar);
return string.Join(mockPath.DirectorySeparatorChar.ToString(), extractFilePath.Take(extractFilePath.Length - 1));
}

private string ExtractFileName(string fullFileName)
{
return fullFileName.Split(mockPath.DirectorySeparatorChar).Last();
}

public override Stream Open(string path, FileMode mode)
Expand Down

0 comments on commit 192fbec

Please sign in to comment.