Skip to content

Commit 4b8ff76

Browse files
m-ringlerfgreinacher
authored andcommitted
Implicit conversion from FileInfo to FileInfoBase (#348)
* Implicit conversion from FileInfo to FileInfoBase * Added unit test for conversion of null FileInfo to FileInfoBase. Fixes #348
1 parent e8d8e72 commit 4b8ff76

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace System.IO.Abstractions.TestingHelpers.Tests
2+
{
3+
using NUnit.Framework;
4+
5+
/// <summary>
6+
/// Unit tests for the conversion operators of the <see cref="FileInfoBase"/> class.
7+
/// </summary>
8+
public class FileInfoBaseConversionTests
9+
{
10+
/// <summary>
11+
/// Tests that a <c>null</c> <see cref="FileInfo"/> is correctly converted to a <c>null</c> <see cref="FileInfoBase"/> without exception.
12+
/// </summary>
13+
[Test]
14+
public void FileInfoBase_FromFileInfo_ShouldReturnNullIfFileInfoIsNull()
15+
{
16+
// Arrange
17+
FileInfo fileInfo = null;
18+
19+
// Act
20+
FileInfoBase actual = fileInfo;
21+
22+
// Assert
23+
Assert.IsNull(actual);
24+
}
25+
}
26+
}

System.IO.Abstractions/FileInfoBase.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ public abstract class FileInfoBase : FileSystemInfoBase
8181

8282
public static implicit operator FileInfoBase(FileInfo fileInfo)
8383
{
84+
if (fileInfo == null)
85+
{
86+
return null;
87+
}
88+
8489
return new FileInfoWrapper(fileInfo);
8590
}
8691
}
87-
}
92+
}

0 commit comments

Comments
 (0)