/
MockFileReadAllLinesTests.cs
161 lines (130 loc) · 5.79 KB
/
MockFileReadAllLinesTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
namespace System.IO.Abstractions.TestingHelpers.Tests
{
using Collections.Generic;
using NUnit.Framework;
using Text;
using XFS = MockUnixSupport;
using System.Threading.Tasks;
using System.Threading;
public class MockFileReadAllLinesTests
{
[Test]
public void MockFile_ReadAllLines_ShouldReturnOriginalTextData()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\something\demo.txt"), new MockFileData("Demo\r\ntext\ncontent\rvalue") },
{ XFS.Path(@"c:\something\other.gif"), new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.ReadAllLines(XFS.Path(@"c:\something\demo.txt"));
// Assert
CollectionAssert.AreEqual(
new[] { "Demo", "text", "content", "value" },
result);
}
[Test]
public void MockFile_ReadAllLines_ShouldReturnOriginalDataWithCustomEncoding()
{
// Arrange
string text = "Hello\r\nthere\rBob\nBob!";
var encodedText = Encoding.BigEndianUnicode.GetBytes(text);
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\something\demo.txt"), new MockFileData(encodedText) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.ReadAllLines(XFS.Path(@"c:\something\demo.txt"), Encoding.BigEndianUnicode);
// Assert
CollectionAssert.AreEqual(
new[] { "Hello", "there", "Bob", "Bob!" },
result);
}
[Test]
public void MockFile_ReadAllLines_NotExistingFile_ThrowsCorrectFileNotFoundException()
{
var absentFileNameFullPath = XFS.Path(@"c:\you surely don't have such file.hope-so");
var mockFileSystem = new MockFileSystem();
var act = new TestDelegate(() =>
mockFileSystem.File.ReadAllText(absentFileNameFullPath)
);
var exception = Assert.Catch<FileNotFoundException>(act);
Assert.That(exception.FileName, Is.EqualTo(absentFileNameFullPath));
Assert.That(exception.Message, Is.EqualTo("Could not find file '" + absentFileNameFullPath + "'."));
}
[Test]
public void MockFile_ReadAllLines_ShouldNotReturnBom()
{
// Arrange
var testFilePath = XFS.Path(@"c:\a test file.txt");
const string testText = "Hello World";
var fileSystem = new MockFileSystem();
fileSystem.File.WriteAllLines(testFilePath, new[] { testText }, Encoding.UTF8);
// Act
var result = fileSystem.File.ReadAllLines(testFilePath, Encoding.UTF8);
// Assert
Assert.That(result.Length, Is.EqualTo(1));
Assert.That(result[0], Is.EqualTo(testText));
}
#if FEATURE_ASYNC_FILE
[Test]
public async Task MockFile_ReadAllLinesAsync_ShouldReturnOriginalTextData()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\something\demo.txt"), new MockFileData("Demo\r\ntext\ncontent\rvalue") },
{ XFS.Path(@"c:\something\other.gif"), new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
});
var file = new MockFile(fileSystem);
// Act
var result = await file.ReadAllLinesAsync(XFS.Path(@"c:\something\demo.txt"));
// Assert
CollectionAssert.AreEqual(
new[] { "Demo", "text", "content", "value" },
result);
}
[Test]
public async Task MockFile_ReadAllLinesAsync_ShouldReturnOriginalDataWithCustomEncoding()
{
// Arrange
string text = "Hello\r\nthere\rBob\nBob!";
var encodedText = Encoding.BigEndianUnicode.GetBytes(text);
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\something\demo.txt"), new MockFileData(encodedText) }
});
var file = new MockFile(fileSystem);
// Act
var result = await file.ReadAllLinesAsync(XFS.Path(@"c:\something\demo.txt"), Encoding.BigEndianUnicode);
// Assert
CollectionAssert.AreEqual(
new[] { "Hello", "there", "Bob", "Bob!" },
result);
}
[Test]
public void MockFile_ReadAllLinesAsync_ShouldThrowOperationCanceledExceptionIfCanceled()
{
var fileSystem = new MockFileSystem();
AsyncTestDelegate action = async () =>
await fileSystem.File.ReadAllLinesAsync(@"C:\a.txt", new CancellationToken(canceled: true));
Assert.ThrowsAsync<OperationCanceledException>(action);
}
[Test]
public void MockFile_ReadAllLinesAsync_NotExistingFile_ThrowsCorrectFileNotFoundException()
{
var absentFileNameFullPath = XFS.Path(@"c:\you surely don't have such file.hope-so");
var mockFileSystem = new MockFileSystem();
var act = new AsyncTestDelegate(async() =>
await mockFileSystem.File.ReadAllTextAsync(absentFileNameFullPath)
);
var exception = Assert.CatchAsync<FileNotFoundException>(act);
Assert.That(exception.FileName, Is.EqualTo(absentFileNameFullPath));
Assert.That(exception.Message, Is.EqualTo("Could not find file '" + absentFileNameFullPath + "'."));
}
#endif
}
}