Skip to content

Commit c0da0c0

Browse files
updateamanfgreinacher
authored andcommitted
Return original path from Mock*Info.ToString() (#406)
Contributes to #400
1 parent c34ca37 commit c0da0c0

File tree

7 files changed

+78
-36
lines changed

7 files changed

+78
-36
lines changed

System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public static IEnumerable<object[]> MockDirectoryInfo_Exists_Cases
3636
{
3737
get
3838
{
39-
yield return new object[]{ XFS.Path(@"c:\temp\folder"), true };
40-
yield return new object[]{ XFS.Path(@"c:\temp\folder\notExistant"), false };
39+
yield return new object[] { XFS.Path(@"c:\temp\folder"), true };
40+
yield return new object[] { XFS.Path(@"c:\temp\folder\notExistant"), false };
4141
}
4242
}
4343

@@ -152,8 +152,8 @@ public void MockDirectoryInfo_GetParent_ShouldReturnDirectoriesAndNamesWithSearc
152152
[Test]
153153
public void MockDirectoryInfo_EnumerateFiles_ShouldReturnAllFiles()
154154
{
155-
// Arrange
156-
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
155+
// Arrange
156+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
157157
{
158158
//Files "above" in folder we're querying
159159
{ XFS.Path(@"c:\temp\a.txt"), "" },
@@ -166,11 +166,11 @@ public void MockDirectoryInfo_EnumerateFiles_ShouldReturnAllFiles()
166166
{ XFS.Path(@"c:\temp\folder\deeper\d.txt"), "" }
167167
});
168168

169-
// Act
170-
var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\temp\folder"));
169+
// Act
170+
var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\temp\folder"));
171171

172-
// Assert
173-
Assert.AreEqual(new[]{"b.txt", "c.txt"}, directoryInfo.EnumerateFiles().ToList().Select(x => x.Name).ToArray());
172+
// Assert
173+
Assert.AreEqual(new[] { "b.txt", "c.txt" }, directoryInfo.EnumerateFiles().ToList().Select(x => x.Name).ToArray());
174174
}
175175

176176
[Test]
@@ -238,7 +238,7 @@ public void MockDirectoryInfo_Constructor_ShouldThrowArgumentNullException_IfArg
238238
var fileSystem = new MockFileSystem();
239239

240240
// Act
241-
TestDelegate action = () => new MockDirectoryInfo(fileSystem, null);
241+
TestDelegate action = () => new MockDirectoryInfo(fileSystem, null);
242242

243243
// Assert
244244
var exception = Assert.Throws<ArgumentNullException>(action);
@@ -272,20 +272,18 @@ public void MockDirectoryInfo_Constructor_ShouldThrowArgumentException_IfArgumen
272272
Assert.That(exception.Message, Does.StartWith("The path is not of a legal form."));
273273
}
274274

275-
[Test]
276-
public void MockDirectoryInfo_ToString_ShouldReturnDirectoryName()
275+
[TestCase(@"c:\temp\folder\folder")]
276+
[TestCase(@"..\..\..\Desktop")]
277+
public void MockDirectoryInfo_ToString_ShouldReturnDirectoryName(string directoryName)
277278
{
278-
var directoryPath = XFS.Path(@"c:\temp\folder\folder");
279-
280279
// Arrange
281-
var fileSystem = new MockFileSystem();
282-
var directoryInfo = new MockDirectoryInfo(fileSystem, directoryPath);
280+
var directoryPath = XFS.Path(directoryName);
283281

284282
// Act
285-
var str = directoryInfo.ToString();
283+
var mockDirectoryInfo = new MockDirectoryInfo(new MockFileSystem(), directoryPath);
286284

287285
// Assert
288-
Assert.AreEqual(directoryPath, str);
286+
Assert.AreEqual(directoryPath, mockDirectoryInfo.ToString());
289287
}
290288
}
291289
}

System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoFactoryTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void MockDriveInfoFactory_GetDrives_ShouldReturnDrives()
2626
var actualNames = actualResults.Select(d => d.Name);
2727

2828
// Assert
29-
Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"D:\" }));
29+
Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"d:\" }));
3030
}
3131

3232
[Test]
@@ -47,7 +47,7 @@ public void MockDriveInfoFactory_GetDrives_ShouldReturnDrivesWithNoDuplicates()
4747
var actualNames = actualResults.Select(d => d.Name);
4848

4949
// Assert
50-
Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"D:\" }));
50+
Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"d:\" }));
5151
}
5252

5353
[Test]
@@ -67,7 +67,7 @@ public void MockDriveInfoFactory_GetDrives_ShouldReturnOnlyLocalDrives()
6767
var actualNames = actualResults.Select(d => d.Name);
6868

6969
// Assert
70-
Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"D:\" }));
70+
Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"d:\" }));
7171
}
7272

7373
[Test]

System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void MockDriveInfo_Constructor_ShouldInitializeLocalWindowsDrives(string
2121
var driveInfo = new MockDriveInfo(fileSystem, path);
2222

2323
// Assert
24-
Assert.AreEqual(@"C:\", driveInfo.Name);
24+
Assert.AreEqual(@"c:\", driveInfo.Name);
2525
}
2626

2727
[Test]
@@ -35,7 +35,7 @@ public void MockDriveInfo_Constructor_ShouldInitializeLocalWindowsDrives_Special
3535
var driveInfo = new MockDriveInfo(fileSystem, "c");
3636

3737
// Assert
38-
Assert.AreEqual(@"C:\", driveInfo.Name);
38+
Assert.AreEqual(@"c:\", driveInfo.Name);
3939
}
4040

4141
[TestCase(@"\\unc\share")]
@@ -59,13 +59,30 @@ public void MockDriveInfo_RootDirectory_ShouldReturnTheDirectoryBase()
5959
var fileSystem = new MockFileSystem();
6060
fileSystem.AddDirectory(XFS.Path(@"c:\Test"));
6161
var driveInfo = new MockDriveInfo(fileSystem, "c:");
62-
var expectedDirectory = XFS.Path(@"C:\");
62+
var expectedDirectory = XFS.Path(@"c:\");
6363

6464
// Act
6565
var actualDirectory = driveInfo.RootDirectory;
6666

6767
// Assert
6868
Assert.AreEqual(expectedDirectory, actualDirectory.FullName);
6969
}
70+
71+
[TestCase("c:","c:\\")]
72+
[TestCase("C:","C:\\")]
73+
[TestCase("d:","d:\\")]
74+
[TestCase("e:","e:\\")]
75+
[TestCase("f:","f:\\")]
76+
public void MockDriveInfo_ToString_ShouldReturnTheDrivePath(string path, string expectedPath)
77+
{
78+
// Arrange
79+
var directoryPath = XFS.Path(path);
80+
81+
// Act
82+
var mockDriveInfo = new MockDriveInfo(new MockFileSystem(), directoryPath);
83+
84+
// Assert
85+
Assert.AreEqual(expectedPath, mockDriveInfo.ToString());
86+
}
7087
}
7188
}

System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void MockFileInfo_IsReadOnly_ShouldSetReadOnlyAttributeOfFileInMemoryFile
167167
[Test]
168168
public void MockFileInfo_IsReadOnly_ShouldSetNotReadOnlyAttributeOfFileInMemoryFileSystem()
169169
{
170-
var fileData = new MockFileData("Demo text content") {Attributes = FileAttributes.ReadOnly};
170+
var fileData = new MockFileData("Demo text content") { Attributes = FileAttributes.ReadOnly };
171171
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
172172
{
173173
{ XFS.Path(@"c:\a.txt"), fileData }
@@ -210,7 +210,7 @@ public void MockFileInfo_OpenWrite_ShouldAddDataToFileInMemoryFileSystem()
210210
{ XFS.Path(@"c:\a.txt"), fileData }
211211
});
212212
var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt"));
213-
var bytesToAdd = new byte[] {65, 66, 67, 68, 69};
213+
var bytesToAdd = new byte[] { 65, 66, 67, 68, 69 };
214214

215215

216216
using (var file = fileInfo.OpenWrite())
@@ -491,6 +491,22 @@ public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist()
491491
Assert.Throws<FileNotFoundException>(action);
492492
}
493493

494+
[TestCase(@"..\..\..\c.txt")]
495+
[TestCase(@"c:\a\b\c.txt")]
496+
[TestCase(@"c:\a\c.txt")]
497+
[TestCase(@"c:\c.txt")]
498+
public void MockFileInfo_ToString_ShouldReturnOriginalFilePath(string path)
499+
{
500+
//Arrange
501+
var filePath = XFS.Path(path);
502+
503+
//Act
504+
var mockFileInfo = new MockFileInfo(new MockFileSystem(), filePath);
505+
506+
//Assert
507+
Assert.AreEqual(filePath, mockFileInfo.ToString());
508+
}
509+
494510
#if NET40
495511
[Test]
496512
public void MockFileInfo_Replace_ShouldReplaceFileContents()

System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Globalization;
32
using System.Linq;
43
using System.Security.AccessControl;
54

@@ -10,6 +9,7 @@ public class MockDirectoryInfo : DirectoryInfoBase
109
{
1110
private readonly IMockFileDataAccessor mockFileDataAccessor;
1211
private readonly string directoryPath;
12+
private readonly string originalPath;
1313

1414
/// <summary>
1515
/// Initializes a new instance of the <see cref="MockDirectoryInfo"/> class.
@@ -21,6 +21,7 @@ public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string dire
2121
{
2222
this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
2323

24+
originalPath = directoryPath;
2425
directoryPath = mockFileDataAccessor.Path.GetFullPath(directoryPath);
2526

2627
this.directoryPath = directoryPath.TrimSlashes();
@@ -283,11 +284,6 @@ public override DirectoryInfoBase Root
283284
}
284285
}
285286

286-
public override string ToString()
287-
{
288-
return FullName;
289-
}
290-
291287
private MockFileData GetMockFileDataForRead()
292288
{
293289
return mockFileDataAccessor.GetFile(directoryPath) ?? MockFileData.NullObject;
@@ -298,5 +294,10 @@ private MockFileData GetMockFileDataForWrite()
298294
return mockFileDataAccessor.GetFile(directoryPath)
299295
?? throw new FileNotFoundException(StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), directoryPath);
300296
}
297+
298+
public override string ToString()
299+
{
300+
return originalPath;
301+
}
301302
}
302303
}

System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public MockDriveInfo(IMockFileDataAccessor mockFileDataAccessor, string name) :
2020
const string DRIVE_SEPARATOR = @":\";
2121
if (name.Length == 1)
2222
{
23-
name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR;
23+
name = name[0] + DRIVE_SEPARATOR;
2424
}
2525
else if (name.Length == 2 && name[1] == ':')
2626
{
27-
name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR;
27+
name = name[0] + DRIVE_SEPARATOR;
2828
}
2929
else if (name.Length == 3 && name.EndsWith(DRIVE_SEPARATOR, StringComparison.Ordinal))
3030
{
31-
name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR;
31+
name = name[0] + DRIVE_SEPARATOR;
3232
}
3333
else
3434
{
@@ -63,6 +63,11 @@ public override DirectoryInfoBase RootDirectory
6363
}
6464
}
6565

66+
public override string ToString()
67+
{
68+
return Name;
69+
}
70+
6671
public new long TotalFreeSpace { get; protected set; }
6772
public new long TotalSize { get; protected set; }
6873
public override string VolumeLabel { get; set; }

System.IO.Abstractions.TestingHelpers/MockFileInfo.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public override Stream OpenRead()
242242

243243
public override StreamReader OpenText()
244244
{
245-
return new StreamReader(OpenRead());
245+
return new StreamReader(OpenRead());
246246
}
247247

248248
public override Stream OpenWrite()
@@ -296,7 +296,7 @@ public override bool IsReadOnly
296296
set
297297
{
298298
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
299-
if(value)
299+
if (value)
300300
MockFileData.Attributes |= FileAttributes.ReadOnly;
301301
else
302302
MockFileData.Attributes &= ~FileAttributes.ReadOnly;
@@ -311,5 +311,10 @@ public override long Length
311311
return MockFileData.Contents.Length;
312312
}
313313
}
314+
315+
public override string ToString()
316+
{
317+
return path;
318+
}
314319
}
315320
}

0 commit comments

Comments
 (0)