Skip to content

Commit

Permalink
beautifies code
Browse files Browse the repository at this point in the history
adds more null argument checks
  • Loading branch information
manne committed Aug 7, 2016
1 parent c851dc2 commit 0a9d860
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 33 deletions.
2 changes: 1 addition & 1 deletion TestHelpers.Tests/MockDirectoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemCon

[Test]
public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet() {
string directory = System.IO.Path.GetTempPath();
string directory = Path.GetTempPath();
var fileSystem = new MockFileSystem();

var actual = fileSystem.Directory.GetCurrentDirectory();
Expand Down
6 changes: 4 additions & 2 deletions TestHelpers.Tests/MockFileTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using NUnit.Framework;

Expand Down Expand Up @@ -554,7 +556,7 @@ public void Serializable_works()
MockFileData data = new MockFileData("Text Contents");

//Act
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
formatter.Serialize(stream, data);

Expand All @@ -571,7 +573,7 @@ public void Serializable_can_deserialize()
//Act
MockFileData data = new MockFileData(textContentStr);

System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
formatter.Serialize(stream, data);

Expand Down
15 changes: 10 additions & 5 deletions TestingHelpers/MockDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class MockDirectory : DirectoryBase

public MockDirectory(IMockFileDataAccessor mockFileDataAccessor, FileBase fileBase, string currentDirectory)
{
if (mockFileDataAccessor == null)
{
throw new ArgumentNullException("mockFileDataAccessor");
}

this.currentDirectory = currentDirectory;
this.mockFileDataAccessor = mockFileDataAccessor;
this.fileBase = fileBase;
Expand All @@ -37,7 +42,7 @@ public override DirectoryInfoBase CreateDirectory(string path, DirectorySecurity

if (path.Length == 0)
{
throw new ArgumentException("Path cannot be the empty string or all whitespace.", "path");
throw new ArgumentException(Properties.Resources.PATH_CANNOT_BE_THE_EMPTY_STRING_OR_ALL_WHITESPACE, "path");
}

if (mockFileDataAccessor.FileExists(path))
Expand Down Expand Up @@ -100,12 +105,12 @@ public override bool Exists(string path)

public override DirectorySecurity GetAccessControl(string path)
{
throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}

public override DirectorySecurity GetAccessControl(string path, AccessControlSections includeSections)
{
throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}

public override DateTime GetCreationTime(string path)
Expand Down Expand Up @@ -284,7 +289,7 @@ public override DirectoryInfoBase GetParent(string path)

if (path.Length == 0)
{
throw new ArgumentException("Path cannot be the empty string or all whitespace.", "path");
throw new ArgumentException(Properties.Resources.PATH_CANNOT_BE_THE_EMPTY_STRING_OR_ALL_WHITESPACE, "path");
}

if (MockPath.HasIllegalCharacters(path, false))
Expand Down Expand Up @@ -358,7 +363,7 @@ public override void Move(string sourceDirName, string destDirName)

public override void SetAccessControl(string path, DirectorySecurity directorySecurity)
{
throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}

public override void SetCreationTime(string path, DateTime creationTime)
Expand Down
3 changes: 2 additions & 1 deletion TestingHelpers/MockDirectoryData.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace System.IO.Abstractions.TestingHelpers
{
[Serializable]
public class MockDirectoryData : MockFileData {
public class MockDirectoryData : MockFileData
{
public override bool IsDirectory { get { return true; } }

public MockDirectoryData() : base(string.Empty)
Expand Down
2 changes: 1 addition & 1 deletion TestingHelpers/MockDirectoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public override FileSystemInfoBase[] GetFileSystemInfos(string searchPattern)

public override FileSystemInfoBase[] GetFileSystemInfos(string searchPattern, SearchOption searchOption)
{
return GetDirectories(searchPattern, searchOption).OfType<FileSystemInfoBase>().Concat(this.GetFiles(searchPattern, searchOption)).ToArray();
return GetDirectories(searchPattern, searchOption).OfType<FileSystemInfoBase>().Concat(GetFiles(searchPattern, searchOption)).ToArray();
}

public override void MoveTo(string destDirName)
Expand Down
2 changes: 1 addition & 1 deletion TestingHelpers/MockDriveInfoFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public bool Equals(string x, string y)

if (x[1] == ':' && y[1] == ':')
{
return Char.ToUpperInvariant(x[0]) == Char.ToUpperInvariant(y[0]);
return char.ToUpperInvariant(x[0]) == char.ToUpperInvariant(y[0]);
}

return false;
Expand Down
3 changes: 2 additions & 1 deletion TestingHelpers/MockFileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class MockFileData
/// <summary>
/// The null object.
/// </summary>
public static readonly MockFileData NullObject = new MockFileData(string.Empty) {
public static readonly MockFileData NullObject = new MockFileData(string.Empty)
{
LastWriteTime = new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc),
LastAccessTime = new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc),
CreationTime = new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc),
Expand Down
27 changes: 16 additions & 11 deletions TestingHelpers/MockFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ internal class MockFileInfo : FileInfoBase

public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path)
{
if (mockFileSystem == null)
{
throw new ArgumentNullException("mockFileSystem");
}

this.mockFileSystem = mockFileSystem;
this.path = path;
}
Expand Down Expand Up @@ -78,7 +83,7 @@ public override string Extension
{
// System.IO.Path.GetExtension does only string manipulation,
// so it's safe to delegate.
return Path.GetExtension(this.path);
return Path.GetExtension(path);
}
}

Expand Down Expand Up @@ -134,7 +139,7 @@ public override DateTime LastWriteTimeUtc
get
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
return MockFileData.LastWriteTime.UtcDateTime;
return MockFileData.LastWriteTime.UtcDateTime;
}
set
{
Expand All @@ -159,7 +164,7 @@ public override FileInfoBase CopyTo(string destFileName)
new MockFile(mockFileSystem).Copy(FullName, destFileName);
return mockFileSystem.FileInfo.FromFileName(destFileName);
}

public override FileInfoBase CopyTo(string destFileName, bool overwrite)
{
new MockFile(mockFileSystem).Copy(FullName, destFileName, overwrite);
Expand Down Expand Up @@ -194,12 +199,12 @@ public override void Encrypt()

public override FileSecurity GetAccessControl()
{
throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}

public override FileSecurity GetAccessControl(AccessControlSections includeSections)
{
throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}

public override void MoveTo(string destFileName)
Expand Down Expand Up @@ -240,24 +245,24 @@ public override Stream OpenWrite()

public override FileInfoBase Replace(string destinationFileName, string destinationBackupFileName)
{
throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}

public override FileInfoBase Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
{
throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}

public override void SetAccessControl(FileSecurity fileSecurity)
{
throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}

public override DirectoryInfoBase Directory
{
get
{
return mockFileSystem.DirectoryInfo.FromDirectoryName(this.DirectoryName);
return mockFileSystem.DirectoryInfo.FromDirectoryName(DirectoryName);
}
}

Expand All @@ -267,7 +272,7 @@ public override string DirectoryName
{
// System.IO.Path.GetDirectoryName does only string manipulation,
// so it's safe to delegate.
return Path.GetDirectoryName(this.path);
return Path.GetDirectoryName(path);
}
}

Expand All @@ -283,7 +288,7 @@ public override bool IsReadOnly
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
if(value)
MockFileData.Attributes |= FileAttributes.ReadOnly;
else
else
MockFileData.Attributes &= ~FileAttributes.ReadOnly;
}
}
Expand Down
7 changes: 6 additions & 1 deletion TestingHelpers/MockFileInfoFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
[Serializable]
public class MockFileInfoFactory : IFileInfoFactory
{
readonly IMockFileDataAccessor mockFileSystem;
private readonly IMockFileDataAccessor mockFileSystem;

public MockFileInfoFactory(IMockFileDataAccessor mockFileSystem)
{
if (mockFileSystem == null)
{
throw new ArgumentNullException("mockFileSystem");
}

this.mockFileSystem = mockFileSystem;
}

Expand Down
4 changes: 2 additions & 2 deletions TestingHelpers/MockFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
[Serializable]
public class MockFileStream : MemoryStream
{
readonly IMockFileDataAccessor mockFileDataAccessor;
readonly string path;
private readonly IMockFileDataAccessor mockFileDataAccessor;
private readonly string path;

public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false)
{
Expand Down
12 changes: 6 additions & 6 deletions TestingHelpers/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace System.IO.Abstractions.TestingHelpers
[Serializable]
public class MockFileSystem : IFileSystem, IMockFileDataAccessor
{
readonly IDictionary<string, MockFileData> files;
readonly FileBase file;
readonly DirectoryBase directory;
readonly IFileInfoFactory fileInfoFactory;
readonly PathBase pathField;
readonly IDirectoryInfoFactory directoryInfoFactory;
private readonly IDictionary<string, MockFileData> files;
private readonly FileBase file;
private readonly DirectoryBase directory;
private readonly IFileInfoFactory fileInfoFactory;
private readonly PathBase pathField;
private readonly IDirectoryInfoFactory directoryInfoFactory;
private readonly IDriveInfoFactory driveInfoFactory;

[NonSerialized]
Expand Down
7 changes: 6 additions & 1 deletion TestingHelpers/MockPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ namespace System.IO.Abstractions.TestingHelpers
[Serializable]
public class MockPath : PathWrapper
{
readonly IMockFileDataAccessor mockFileDataAccessor;
private readonly IMockFileDataAccessor mockFileDataAccessor;

private static readonly char[] InvalidAdditionalPathChars = { '*', '?' };

public MockPath(IMockFileDataAccessor mockFileDataAccessor)
{
if (mockFileDataAccessor == null)
{
throw new ArgumentNullException("mockFileDataAccessor");
}

this.mockFileDataAccessor = mockFileDataAccessor;
}

Expand Down
9 changes: 9 additions & 0 deletions TestingHelpers/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions TestingHelpers/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,7 @@
<data name="VALUE_CANNOT_BE_NULL" xml:space="preserve">
<value>Value cannot be null.</value>
</data>
<data name="PATH_CANNOT_BE_THE_EMPTY_STRING_OR_ALL_WHITESPACE" xml:space="preserve">
<value>Path cannot be the empty string or all whitespace.</value>
</data>
</root>
1 change: 1 addition & 0 deletions TestingHelpers/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace System.IO.Abstractions.TestingHelpers
{
internal static class StringExtensions
{
[Pure]
internal static string[] SplitLines(this string input)
{
var list = new List<string>();
Expand Down

0 comments on commit 0a9d860

Please sign in to comment.