/
MockFileTests.cs
643 lines (530 loc) · 23.2 KB
/
MockFileTests.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Linq;
namespace System.IO.Abstractions.TestingHelpers.Tests
{
[TestFixture]
public class MockFileTests
{
[Test]
public void MockFile_AppendAllText_ShouldPersistNewText()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
file.AppendAllText(path, "+ some text");
// Assert
Assert.AreEqual(
"Demo text content+ some text",
file.ReadAllText(path));
}
[Test]
public void MockFile_AppendAllText_ShouldPersistNewTextWithCustomEncoding()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
file.AppendAllText(path, "+ some text", Encoding.BigEndianUnicode);
// Assert
var expected = new byte[]
{
68, 101, 109, 111, 32, 116, 101, 120, 116, 32, 99, 111, 110, 116,
101, 110, 255, 253, 0, 43, 0, 32, 0, 115, 0, 111, 0, 109, 0, 101,
0, 32, 0, 116, 0, 101, 0, 120, 0, 116
};
CollectionAssert.AreEqual(
expected,
file.ReadAllBytes(path));
}
[Test]
public void MockFile_GetSetCreationTime_ShouldPersist()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var creationTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetCreationTime(path, creationTime);
var result = file.GetCreationTime(path);
// Assert
Assert.AreEqual(creationTime, result);
}
[Test]
public void MockFile_SetCreationTimeUtc_ShouldAffectCreationTime()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var creationTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetCreationTimeUtc(path, creationTime.ToUniversalTime());
var result = file.GetCreationTime(path);
// Assert
Assert.AreEqual(creationTime, result);
}
[Test]
public void MockFile_SetCreationTime_ShouldAffectCreationTimeUtc()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var creationTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetCreationTime(path, creationTime);
var result = file.GetCreationTimeUtc(path);
// Assert
Assert.AreEqual(creationTime.ToUniversalTime(), result);
}
[Test]
public void MockFile_GetSetLastAccessTime_ShouldPersist()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastAccessTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastAccessTime(path, lastAccessTime);
var result = file.GetLastAccessTime(path);
// Assert
Assert.AreEqual(lastAccessTime, result);
}
[Test]
public void MockFile_SetLastAccessTimeUtc_ShouldAffectLastAccessTime()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastAccessTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastAccessTimeUtc(path, lastAccessTime.ToUniversalTime());
var result = file.GetLastAccessTime(path);
// Assert
Assert.AreEqual(lastAccessTime, result);
}
[Test]
public void MockFile_SetLastAccessTime_ShouldAffectLastAccessTimeUtc()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastAccessTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastAccessTime(path, lastAccessTime);
var result = file.GetLastAccessTimeUtc(path);
// Assert
Assert.AreEqual(lastAccessTime.ToUniversalTime(), result);
}
[Test]
public void MockFile_GetSetLastWriteTime_ShouldPersist()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastWriteTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastWriteTime(path, lastWriteTime);
var result = file.GetLastWriteTime(path);
// Assert
Assert.AreEqual(lastWriteTime, result);
}
[Test]
public void MockFile_SetLastWriteTimeUtc_ShouldAffectLastWriteTime()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastWriteTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastWriteTimeUtc(path, lastWriteTime.ToUniversalTime());
var result = file.GetLastWriteTime(path);
// Assert
Assert.AreEqual(lastWriteTime, result);
}
[Test]
public void MockFile_SetLastWriteTime_ShouldAffectLastWriteTimeUtc()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastWriteTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastWriteTime(path, lastWriteTime);
var result = file.GetLastWriteTimeUtc(path);
// Assert
Assert.AreEqual(lastWriteTime.ToUniversalTime(), result);
}
[Test]
public void MockFile_Exists_ShouldReturnTrueForSamePath()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", new MockFileData("Demo text content") },
{ @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.Exists(@"c:\something\other.gif");
// Assert
Assert.IsTrue(result);
}
[Test]
public void MockFile_Exists_ShouldReturnTrueForPathVaryingByCase()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", new MockFileData("Demo text content") },
{ @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.Exists(@"c:\SomeThing\Other.gif");
// Assert
Assert.IsTrue(result);
}
[Test]
public void MockFile_Exists_ShouldReturnFalseForEntirelyDifferentPath()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", new MockFileData("Demo text content") },
{ @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.Exists(@"c:\SomeThing\DoesNotExist.gif");
// Assert
Assert.IsFalse(result);
}
[Test]
public void MockFile_ReadAllBytes_ShouldReturnOriginalByteData()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", new MockFileData("Demo text content") },
{ @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.ReadAllBytes(@"c:\something\other.gif");
// Assert
CollectionAssert.AreEqual(
new byte[] { 0x21, 0x58, 0x3f, 0xa9 },
result);
}
[Test]
public void MockFile_ReadAllLines_ShouldReturnOriginalTextData()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", new MockFileData("Demo\r\ntext\ncontent\rvalue") },
{ @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.ReadAllLines(@"c:\something\demo.txt");
// Assert
CollectionAssert.AreEqual(
new[] { "Demo", "text", "content", "value" },
result);
}
[Test]
public void MockFile_ReadAllLines_ShouldReturnOriginalDataWithCustomEncoding()
{
// Arrange
const string text = "Hello\r\nthere\rBob\nBob!";
var encodedText = Encoding.BigEndianUnicode.GetBytes(text);
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", new MockFileData(encodedText) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.ReadAllLines(@"c:\something\demo.txt", Encoding.BigEndianUnicode);
// Assert
CollectionAssert.AreEqual(
new [] { "Hello", "there", "Bob", "Bob!" },
result);
}
[Test]
public void MockFile_ReadAllText_ShouldReturnOriginalTextData()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", new MockFileData("Demo text content") },
{ @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.ReadAllText(@"c:\something\demo.txt");
// Assert
Assert.AreEqual(
"Demo text content",
result);
}
[Test]
public void MockFile_ReadAllText_ShouldReturnOriginalDataWithCustomEncoding()
{
// Arrange
const string text = "Hello there!";
var encodedText = Encoding.BigEndianUnicode.GetBytes(text);
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", new MockFileData(encodedText) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.ReadAllText(@"c:\something\demo.txt", Encoding.BigEndianUnicode);
// Assert
Assert.AreEqual(text, result);
}
[Test]
public void MockFile_ReadAllBytes_ShouldReturnDataSavedByWriteAllBytes()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
var fileContent = new byte[] { 1, 2, 3, 4 };
// Act
fileSystem.File.WriteAllBytes(path, fileContent);
// Assert
Assert.AreEqual(
fileContent,
fileSystem.File.ReadAllBytes(path));
}
[Test]
public void MockFile_WriteAllBytes_ShouldWriteDataToMemoryFileSystem()
{
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
var fileContent = new byte[] { 1, 2, 3, 4 };
// Act
fileSystem.File.WriteAllBytes(path, fileContent);
// Assert
Assert.AreEqual(
fileContent,
fileSystem.GetFile(path).Contents);
}
[Test]
public void MockFile_WriteAllText_ShouldWriteTextFileToMemoryFileSystem()
{
// Arrange
const string path = @"c:\something\demo.txt";
const string fileContent = "Hello there!";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
// Act
fileSystem.File.WriteAllText(path, fileContent);
// Assert
Assert.AreEqual(
fileContent,
fileSystem.GetFile(path).TextContents);
}
[Test]
public void MockFile_WriteAllText_ShouldOverriteAnExistingFile()
{
// http://msdn.microsoft.com/en-us/library/ms143375.aspx
// Arrange
const string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
// Act
fileSystem.File.WriteAllText(path, "foo");
fileSystem.File.WriteAllText(path, "bar");
// Assert
Assert.AreEqual("bar", fileSystem.GetFile(path).TextContents);
}
[Test]
public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem()
{
const string sourceFilePath = @"c:\something\demo.txt";
const string sourceFileContent = "this is some content";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
{sourceFilePath, new MockFileData(sourceFileContent)}
});
const string destFilePath = @"c:\somethingelse\demo1.txt";
fileSystem.File.Move(sourceFilePath, destFilePath);
Assert.That(fileSystem.FileExists(destFilePath), Is.True);
Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent));
Assert.That(fileSystem.FileExists(sourceFilePath), Is.False);
}
[Test]
public void MockFile_OpenWrite_ShouldCreateNewFiles() {
const string filePath = @"c:\something\demo.txt";
const string fileContent = "this is some content";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
var bytes = new UTF8Encoding(true).GetBytes(fileContent);
var stream = fileSystem.File.OpenWrite(filePath);
stream.Write(bytes, 0, bytes.Length);
stream.Close();
Assert.That(fileSystem.FileExists(filePath), Is.True);
Assert.That(fileSystem.GetFile(filePath).TextContents, Is.EqualTo(fileContent));
}
[Test]
public void MockFile_OpenWrite_ShouldOverwriteExistingFiles()
{
const string filePath = @"c:\something\demo.txt";
const string startFileContent = "this is some content";
const string endFileContent = "this is some other content";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
{filePath, new MockFileData(startFileContent)}
});
var bytes = new UTF8Encoding(true).GetBytes(endFileContent);
var stream = fileSystem.File.OpenWrite(filePath);
stream.Write(bytes, 0, bytes.Length);
stream.Close();
Assert.That(fileSystem.FileExists(filePath), Is.True);
Assert.That(fileSystem.GetFile(filePath).TextContents, Is.EqualTo(endFileContent));
}
[Test]
public void MockFile_Copy_ShouldOverwriteFileWhenOverwriteFlagIsTrue()
{
string sourceFileName = @"c:\source\demo.txt";
MockFileData sourceContents = new MockFileData("Source content");
string destFileName = @"c:\destination\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{sourceFileName, sourceContents},
{destFileName, new MockFileData("Destination content")}
});
fileSystem.File.Copy(sourceFileName, destFileName, true);
var copyResult = fileSystem.GetFile(destFileName);
Assert.AreEqual(copyResult.Contents, sourceContents.Contents);
}
[Test]
public void MockFile_Copy_ShouldCreateFileAtNewDestination()
{
string sourceFileName = @"c:\source\demo.txt";
MockFileData sourceContents = new MockFileData("Source content");
string destFileName = @"c:\destination\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{sourceFileName, sourceContents}
});
fileSystem.File.Copy(sourceFileName, destFileName, false);
var copyResult = fileSystem.GetFile(destFileName);
Assert.AreEqual(copyResult.Contents, sourceContents.Contents);
}
[Test]
public void MockFile_Copy_ShouldThrowExceptionWhenFileExistsAtDestination()
{
string sourceFileName = @"c:\source\demo.txt";
MockFileData sourceContents = new MockFileData("Source content");
string destFileName = @"c:\destination\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{sourceFileName, sourceContents},
{destFileName, new MockFileData("Destination content")}
});
Assert.Throws<IOException>(() => fileSystem.File.Copy(sourceFileName, destFileName), @"The file c:\destination\demo.txt already exists.");
}
[Test]
public void MockFile_Delete_ShouldRemoveFileFromFileSystem()
{
const string fullPath = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ fullPath, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
file.Delete(fullPath);
Assert.That(fileSystem.FileExists(fullPath), Is.False);
}
[Test]
public void Mockfile_Create_ShouldCreateNewStream()
{
const string fullPath = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
var sut = new MockFile(fileSystem);
Assert.That(fileSystem.FileExists(fullPath), Is.False);
sut.Create(fullPath).Close();
Assert.That(fileSystem.FileExists(fullPath), Is.True);
}
[Test]
public void Mockfile_Create_CanWriteToNewStream()
{
const string fullPath = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
var data = new UTF8Encoding(false).GetBytes("Test string");
var sut = new MockFile(fileSystem);
using (var stream = sut.Create(fullPath))
{
stream.Write(data, 0, data.Length);
}
var mockFileData = fileSystem.GetFile(fullPath);
var fileData = mockFileData.Contents;
Assert.That(fileData, Is.EqualTo(data));
}
[Test]
public void MockFile_Delete_Should_RemoveFiles()
{
const string filePath = @"c:\something\demo.txt";
const string fileContent = "this is some content";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { filePath, new MockFileData(fileContent) } });
Assert.AreEqual(1, fileSystem.AllFiles.Count());
fileSystem.File.Delete(filePath);
Assert.AreEqual(0, fileSystem.AllFiles.Count());
}
[Test]
public void MockFile_Delete_No_File_Does_Nothing()
{
const string filePath = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
fileSystem.File.Delete(filePath);
}
}
}