public void SetUp() { var mockFileData = new MockFileData(() => { throw new ContentCalledException(); }); fileSystem = new MockFileSystem(); ((MockFileSystem)fileSystem).AddFile(@"b:\thefile.useless", mockFileData); }
public void MockFileData_LazyLoadFuncIsCalledOnDemand() { var contents = Enumerable.Range(19, 42).Select(_ => (byte)_).ToArray(); var lazyLoadFuncHasBeenCalled = false; var x = new MockFileData(() => { lazyLoadFuncHasBeenCalled = true; return contents; }); Assert.IsFalse(lazyLoadFuncHasBeenCalled); Assert.AreEqual(x.Contents, contents); Assert.IsTrue(lazyLoadFuncHasBeenCalled); }
public void MockFile_Copy_ShouldThrowExceptionWhenFileExistsAtDestination() { string sourceFileName = XFS.Path(@"c:\source\demo.txt"); var sourceContents = new MockFileData("Source content"); string destFileName = XFS.Path(@"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), XFS.Path(@"The file c:\destination\demo.txt already exists.")); }
public void ReadFile_IncorrectlyFormattedPeriod_FailsAndOutputsToConsoleError() { var mockInputFile = new MockFileData("Vitor,Peru,75000,7%,01 March"); this.Fixture.MockFS.AddFile(Globals.InputFilePath, mockInputFile); using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); object objs = new FileContext(Globals.InputFilePath, Globals.OutputFilePath, this.Fixture.MockFS, Log.Logger).ReadFile(); string output = string.Format("Error in line {0} : {1}. || Line: {2}", 1, "Date Interval in an incorrect format", "Vitor,Peru,75000,7%,01 March"); Assert.Contains(output, sw.ToString()); Console.SetOut(new StreamWriter(Console.OpenStandardOutput())); } }
public void ReadFile_MockSuperOutBounds_FailsAndOutputsToConsoleError() { var mockInputFile = new MockFileData("Carlos,Salvador,81000,51%,01 March - 31 March"); this.Fixture.MockFS.AddFile(Globals.InputFilePath, mockInputFile); using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); object objs = new FileContext(Globals.InputFilePath, Globals.OutputFilePath, this.Fixture.MockFS, Log.Logger).ReadFile(); string output = string.Format("Error in line {0} : {1}. || Line: {2}", 1, "Super out of bounds [0-50]%", "Carlos,Salvador,81000,51%,01 March - 31 March"); Assert.Contains(output, sw.ToString()); Console.SetOut(new StreamWriter(Console.OpenStandardOutput())); } }
public void ReadFile_MockWrongAnnualIncome_FailsAndOutputsToConsoleError() { var mockInputFile = new MockFileData("Mauro,Coutinho,70000.5,9%,01 March - 31 March"); this.Fixture.MockFS.AddFile(Globals.InputFilePath, mockInputFile); using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); object objs = new FileContext(Globals.InputFilePath, Globals.OutputFilePath, this.Fixture.MockFS, Log.Logger).ReadFile(); string output = string.Format("Error in line {0} : {1}. || Line: {2}", 1, "Annual income in an incorrect format", "Mauro,Coutinho,70000.5,9%,01 March - 31 March"); Assert.Contains(output, sw.ToString()); Console.SetOut(new StreamWriter(Console.OpenStandardOutput())); } }
public void ReadFile_MockWrongParameterNumber_FailsAndOutputsToConsoleError() { var mockInputFile = new MockFileData("Willy,60000,8%,01 March - 31 March"); this.Fixture.MockFS.AddFile(Globals.InputFilePath, mockInputFile); using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); object objs = new FileContext(Globals.InputFilePath, Globals.OutputFilePath, this.Fixture.MockFS, Log.Logger).ReadFile(); string output = string.Format("Error in line {0} : {1}. || Line: {2}", 1, "Wrong parameter number", "Willy,60000,8%,01 March - 31 March"); Assert.Contains(output, sw.ToString()); Console.SetOut(new StreamWriter(Console.OpenStandardOutput())); } }
public void MockFile_GetAttributeOfExistingFile_ShouldReturnCorrectValue() { var filedata = new MockFileData("test") { Attributes = FileAttributes.Hidden }; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\something\demo.txt"), filedata } }); var attributes = fileSystem.File.GetAttributes(XFS.Path(@"c:\something\demo.txt")); Assert.That(attributes, Is.EqualTo(FileAttributes.Hidden)); }
public void MockFile_Copy_ShouldThrowExceptionWhenFileExistsAtDestination() { string sourceFileName = XFS.Path(@"c:\source\demo.txt"); var sourceContents = new MockFileData("Source content"); string destFileName = XFS.Path(@"c:\destination\demo.txt"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { sourceFileName, sourceContents }, { destFileName, new MockFileData("Destination content") } }); Action action = () => fileSystem.File.Copy(sourceFileName, destFileName); action.ShouldThrow <IOException>(XFS.Path(@"The file c:\destination\demo.txt already exists.")); }
public void MockFileInfo_Decrypt_ShouldUnsetEncryptedAttributeOfFileInMemoryFileSystem() { var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); fileInfo.Encrypt(); fileInfo.Decrypt(); Assert.AreNotEqual(FileAttributes.Encrypted, fileData.Attributes & FileAttributes.Encrypted); }
public void FileIsNotEmpty() { var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData("data1,data2,data3\ndata11,data22,data33\ndata111,data222,data333"); mockFileSystem.AddFile(@"C:\temp\in.txt", mockInputFile); Library.FileReader sut = new FileReader(new DataReader()); var records = sut.ReadFile(mockFileSystem.Path.ToString(), 1); var fakeReader = new data(); var path = "pathToSomeFile" var data = new[] { "Line 1", "Line 2", Line 3" };
public void Is_Serializable() { var file1 = new MockFileData("Demo\r\ntext\ncontent\rvalue"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { @"c:\something\demo.txt", file1 }, { @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) } }); var memoryStream = new MemoryStream(); var serializer = new Runtime.Serialization.Formatters.Binary.BinaryFormatter(); serializer.Serialize(memoryStream, fileSystem); Assert.That(memoryStream.Length > 0, "Length didn't increase after serialization task."); }
public void MockFile_Copy_ShouldCreateFileAtNewDestination() { string sourceFileName = XFS.Path(@"c:\source\demo.txt"); var sourceContents = new MockFileData("Source content"); string destFileName = XFS.Path(@"c:\source\demo_copy.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); }
public void GetReviewShouldThrowExceptionWhenNoMatch() { // Arrange string fileName = "oneReview"; var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData(FileStrings.GetFile(fileName)); string path = $@"C:\tmp\{fileName}.json"; mockFileSystem.AddFile(path, mockInputFile); IAsyncContentManager contentManager = new FileContentManager(path, mockFileSystem); Guid guid = new Guid("7176da9a-3670-4fe3-8d11-cb19d697620e"); // Act // Assert Assert.ThrowsAsync <InvalidOperationException>(async() => await contentManager.GetReviewAsync(guid)); }
public async Task GetReviewsShouldReturnEmptyListIfFileIsEmpty() { // Arrange var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData(FileStrings.GetFile("empty")); string path = @"C:\tmp\empty.json"; mockFileSystem.AddFile(path, mockInputFile); IAsyncContentManager contentManager = new FileContentManager(path, mockFileSystem); // Act List <Review> reviews = await contentManager.GetReviewsAsync(); // Assert Assert.IsFalse(reviews.Any()); }
public async Task GetReviewsShouldReturnStoredListIfFileIsCorrect(string fileName, int expectedCount) { // Arrange var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData(FileStrings.GetFile(fileName)); string path = $@"C:\tmp\{fileName}.json"; mockFileSystem.AddFile(path, mockInputFile); IAsyncContentManager contentManager = new FileContentManager(path, mockFileSystem); // Act List <Review> reviews = await contentManager.GetReviewsAsync(); // Assert Assert.AreEqual(expectedCount, reviews.Count()); }
public async Task GetReviewShouldReturnNullIfFileIsEmpty() { // Arrange var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData(FileStrings.GetFile("empty")); string path = @"C:\tmp\empty.json"; mockFileSystem.AddFile(path, mockInputFile); IAsyncContentManager contentManager = new FileContentManager(path, mockFileSystem); // Act Review review = await contentManager.GetReviewAsync(It.IsAny <Guid>()); // Assert Assert.IsNull(review); }
public void MockFile_Copy_ShouldCreateFileAtNewDestination() { string sourceFileName = XFS.Path(@"c:\source\demo.txt"); var sourceContents = new MockFileData("Source content"); string destFileName = XFS.Path(@"c:\source\demo_copy.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); }
public void MockFileInfo_IsReadOnly_ShouldSetReadOnlyAttributeOfFileInMemoryFileSystem() { // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); // Act fileInfo.IsReadOnly = true; // Assert Assert.AreEqual(FileAttributes.ReadOnly, fileData.Attributes & FileAttributes.ReadOnly); }
public void MockFileSystem_GetFile_ShouldReturnFileRegisteredInConstructorWhenPathsDifferByCase() { // Arrange var file1 = new MockFileData("Demo\r\ntext\ncontent\rvalue"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { @"c:\something\demo.txt", file1 }, { @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) } }); // Act var result = fileSystem.GetFile(@"c:\SomeThing\DEMO.txt"); // Assert Assert.AreEqual(file1, result); }
public void MockFileInfo_IsReadOnly_ShouldSetNotReadOnlyAttributeOfFileInMemoryFileSystem() { var fileData = new MockFileData("Demo text content") { Attributes = FileAttributes.ReadOnly }; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); fileInfo.IsReadOnly = false; Assert.AreNotEqual(FileAttributes.ReadOnly, fileData.Attributes & FileAttributes.ReadOnly); }
public void SetUp() { fileSystem = new MockFileSystem(); var rnd = new Random(); var rndBytes = new byte[2048]; rnd.NextBytes(rndBytes); var rndBytesFile = new MockFileData(rndBytes); fileSystem.AddDirectory(@"C:\Archive"); fileSystem.AddFile(@"C:\source1\file.bin", rndBytesFile); fileSystem.AddFile(@"C:\source1\1\file2.bin", rndBytesFile); fileSystem.AddFile(@"C:\source1\2\file3.bin", rndBytesFile); fileSystem.AddFile(@"C:\source2\1\1\file4.bin", rndBytesFile); fileSystem.AddDirectory(@"C:\source3"); }
public void MockFile_WriteAllTextAsync_ShouldThrowAnUnauthorizedAccessExceptionIfTheFileIsReadOnly() { // Arrange var fileSystem = new MockFileSystem(); string filePath = XFS.Path(@"c:\something\demo.txt"); var mockFileData = new MockFileData(new byte[0]); mockFileData.Attributes = FileAttributes.ReadOnly; fileSystem.AddFile(filePath, mockFileData); // Act AsyncTestDelegate action = () => fileSystem.File.WriteAllTextAsync(filePath, null); // Assert Assert.ThrowsAsync <UnauthorizedAccessException>(action); }
public void Is_Serializable() { MockFileData file1 = new MockFileData("Demo\r\ntext\ncontent\rvalue"); MockFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { @"c:\something\demo.txt", file1 }, { @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) } }); MemoryStream memoryStream = new MemoryStream(); var serializer = new XmlSerializer(fileSystem.GetType()); serializer.Serialize(memoryStream, fileSystem); Assert.True(memoryStream.Length > 0, "Length didn't increase after serialization task."); }
public void MockFileSystem_GetFile_ShouldReturnFileRegisteredInConstructor() { // Arrange MockFileData file1 = new MockFileData("Demo\r\ntext\ncontent\rvalue"); MockFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { @"c:\something\demo.txt", file1 }, { @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) } }); // Act MockFileData result = fileSystem.GetFile(@"c:\something\demo.txt"); // Assert Assert.Equal(file1, result); }
//[TestMethod()] public void SetGamePathTest() { //arrange string testPath = "C:\\Program Files\\Test\\Guild Wars 2"; var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData("game_path : C:\\Program Files\\Failed\\Guild Wars 2"); Configuration.AttachMockFileSystem(mockFileSystem); mockFileSystem.AddFile($"{Directory.GetCurrentDirectory()}\\config.yaml", mockInputFile); //act Configuration.SetGamePath(testPath); //assert Assert.AreEqual(testPath, Configuration.getConfigAsYAML().game_path); }
public void MockFileSystem_ByDefault_IsSerializable() { var file1 = new MockFileData("Demo\r\ntext\ncontent\rvalue"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { @"c:\something\demo.txt", file1 }, { @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) } }); var memoryStream = new MemoryStream(); var serializer = new Runtime.Serialization.Formatters.Binary.BinaryFormatter(); serializer.Serialize(memoryStream, fileSystem); Assert.That(memoryStream.Length > 0, "Length didn't increase after serialization task."); }
public void MockFile_Copy_ShouldOverwriteFileWhenOverwriteFlagIsTrue() { string sourceFileName = XFS.Path(@"c:\source\demo.txt"); var sourceContents = new MockFileData("Source content"); string destFileName = XFS.Path(@"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); }
public void MockFileSystem_GetFile_ShouldReturnFileRegisteredInConstructorWhenPathsDifferByCase() { // Arrange var file1 = new MockFileData("Demo\r\ntext\ncontent\rvalue"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { @"c:\something\demo.txt", file1 }, { @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) } }); // Act var result = fileSystem.GetFile(@"c:\SomeThing\DEMO.txt"); // Assert Assert.AreEqual(file1, result); }
public IArchiveFactory AddDocCollection(int numberOfFiles, int fileSize = (int)ByteHelper.MegaByte) { var collectionName = $"doc{_docCollectionCount + 1}"; _fileSystem.AddDirectory($"{_basePath}\\{_name}\\Documents\\{collectionName}"); var data = new MockFileData(_byteGenerator.GenerateBufferFromSeed(fileSize)); var dataChecksum = GetChecksum(data); for (int i = 0; i <= numberOfFiles; i++) { _fileSystem.AddFile($"{_basePath}\\{_name}\\Documents\\{collectionName}\\document-{i}.tif", data); AddToFileindex($"{_basePath}\\{_name}\\Documents\\{collectionName}\\document-{i}.tif", dataChecksum); } _docCollectionCount++; return(this); }
public void MockFileSystem_RemoveFile_ThrowsUnauthorizedAccessExceptionIfFileIsReadOnly() { var path = XFS.Path(@"C:\file.txt"); var readOnlyFile = new MockFileData("") { Attributes = FileAttributes.ReadOnly }; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { path, readOnlyFile }, }); TestDelegate action = () => fileSystem.RemoveFile(path); Assert.Throws <UnauthorizedAccessException>(action); }
public void MockFile_AppendText_CreatesNewFileForAppendToNonExistingFile() { string filepath = XFS.Path(@"c:\something\doesnt\exist.txt"); MockFileSystem filesystem = new MockFileSystem(new Dictionary <string, MockFileData>()); StreamWriter stream = filesystem.File.AppendText(filepath); stream.Write("New too!"); stream.Flush(); stream.Dispose(); MockFileData file = filesystem.GetFile(filepath); Assert.Equal(file.TextContents, "New too!"); Assert.True(filesystem.FileExists(filepath)); }
public void GetGameFiles_ReturnsListOfFiles_Ok() { var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData("line1\nline2\nline3"); mockFileSystem.AddFile(Path.Combine(GameFileManagerStatic.GameFolder, "in1.xml"), mockInputFile); mockFileSystem.AddFile(Path.Combine(GameFileManagerStatic.GameFolder, "in2.xml"), mockInputFile); GameFileManagerStatic.FileSystemDI = mockFileSystem; var gameDetails = GameFileManagerStatic.GetGameFiles().ToList(); Assert.IsNotNull(gameDetails); Assert.AreEqual(2, gameDetails.Count); }
public void OutputProcessedOrderCsvData() { const string inputDir = @"c:\root\in"; const string inputFileName = "myfile.csv"; var inputFilePath = Path.Combine(inputDir, inputFileName); const string outputDir = @"c:\root\out"; const string outputFileName = "myfileout.csv"; var outputFilePath = Path.Combine(outputDir, outputFileName); var csvLines = new StringBuilder(); csvLines.AppendLine("OrderNumber,CustomerNumber,Description,Quantity"); csvLines.AppendLine("42, 100001, Shirt, II"); csvLines.AppendLine("43, 200002, Shorts, I"); csvLines.AppendLine("@ This is a comment"); csvLines.AppendLine(""); csvLines.Append("44, 300003, Cap, V"); var mockInputFile = new MockFileData(csvLines.ToString()); var mockFileSystem = new MockFileSystem(); mockFileSystem.AddFile(inputFilePath, mockInputFile); mockFileSystem.AddDirectory(outputDir); var sut = new CsvFileProcessor(inputFilePath, outputFilePath, mockFileSystem); sut.Process(); Assert.True(mockFileSystem.FileExists(outputFilePath)); var processedFile = mockFileSystem.GetFile(outputFilePath); var lines = processedFile.TextContents.SplitLines(); //Assert.Equal("OrderNumber,Customer,Amount", lines[0]); //Assert.Equal("42,100001,2", lines[1]); //Assert.Equal("43,200002,1", lines[2]); //Assert.Equal("44,300003,5", lines[3]); Approvals.Verify(processedFile.TextContents); }
public void MockFileInfo_LastAccessTimeUtc_ShouldReturnLastAccessTimeUtcOfFileInMemoryFileSystem() { // Arrange var lastAccessTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { LastAccessTime = lastAccessTime }; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { @"c:\a.txt", fileData } }); var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt"); // Act var result = fileInfo.LastAccessTimeUtc; // Assert Assert.AreEqual(lastAccessTime.ToUniversalTime(), result); }
public void posts_without_front_matter_uses_convention_to_render_folder() { var file = new MockFileData("# Title"); var lastmod = new DateTime(2000, 1, 1); file.LastWriteTime = lastmod; fileSystem.AddFile(@"C:\TestSite\_posts\SomeFile.md", file); var outputPath = string.Format("/{0}/{1}", lastmod.ToString("yyyy'/'MM'/'dd"), "SomeFile.html"); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); var firstPost = siteContext.Posts.First(); Assert.Equal(outputPath, firstPost.Url); }
public void CheckFirstStringUpperTest() { var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData("line1\nline2\nline3"); mockFileSystem.AddFile(@"C:\temp\in.txt", mockInputFile); var sut = new CopyClassStream(mockFileSystem); sut.ConvertFirstLineToUpper(@"C:\temp\in.txt"); MockFileData mockOutputFile = mockFileSystem.GetFile(@"C:\temp\in.out.txt"); var bytes = mockOutputFile.Contents; string[] outputLines = mockOutputFile.TextContents.SplitLines(); Assert.AreEqual("LINE1", outputLines[0]); Assert.AreEqual("line2", outputLines[1]); Assert.AreEqual("line3", outputLines[2]); }
public void MockFileInfo_CreationTimeUtc_ShouldReturnCreationTimeUtcOfFileInMemoryFileSystem() { var creationTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { CreationTime = creationTime }; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); var result = fileInfo.CreationTimeUtc; Assert.AreEqual(creationTime.ToUniversalTime(), result); }
public void MockFileInfo_CreationTime_ShouldSetCreationTimeOfFileInMemoryFileSystem() { // Arrange var creationTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { CreationTime = creationTime }; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); // Act var newTime = DateTime.Now; fileInfo.CreationTime = newTime; // Assert Assert.AreEqual(newTime, fileInfo.CreationTime); }
public void MockFileInfo_IsReadOnly_ShouldSetNotReadOnlyAttributeOfFileInMemoryFileSystem() { // Arrange var fileData = new MockFileData("Demo text content") {Attributes = FileAttributes.ReadOnly}; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); // Act fileInfo.IsReadOnly = false; // Assert Assert.AreNotEqual(FileAttributes.ReadOnly, fileData.Attributes & FileAttributes.ReadOnly); }
public void MockFileData_AcceptsFuncAndReturnsContents() { var contents = Enumerable.Range(0, 100).Select(_ => (byte)_).ToArray(); var x = new MockFileData(() => contents); Assert.AreEqual(x.Contents, contents); }
public void MockFile_GetAttributeOfExistingUncDirectory_ShouldReturnCorrectValue() { var filedata = new MockFileData("test"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { XFS.Path(@"\\share\folder\demo.txt"), filedata } }); var attributes = fileSystem.File.GetAttributes(XFS.Path(@"\\share\folder")); Assert.That(attributes, Is.EqualTo(FileAttributes.Directory)); }
public void MockFileInfo_LastWriteTimeUtc_ShouldSetLastWriteTimeUtcOfFileInMemoryFileSystem() { // Arrange var lastWriteTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { LastWriteTime = lastWriteTime }; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); // Act var newUtcTime = DateTime.UtcNow; fileInfo.LastWriteTime = newUtcTime; // Assert Assert.AreEqual(newUtcTime, fileInfo.LastWriteTime); }
public void MockFileInfo_Decrypt_ShouldReturnCorrectContentsFileInMemoryFileSystem() { // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); fileInfo.Encrypt(); // Act fileInfo.Decrypt(); string newcontents; using (var newfile = fileInfo.OpenText()) { newcontents = newfile.ReadToEnd(); } // Assert Assert.AreEqual("Demo text content", newcontents); }
public void CreateText_DataSentIn_ShouldWriteToFile() { //Arrange var fileData = new MockFileData(""); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { @"z:\a.txt", fileData } }); var fileInfo = new MockFileInfo(fileSystem, @"z:\a.txt"); //Act var writer = fileInfo.CreateText(); writer.WriteLine("test"); writer.Close(); var reader = fileInfo.OpenText(); var result = reader.ReadToEnd(); //Assert Assert.AreEqual("test",result); }
public void MockFileInfo_CreationTimeUtc_ShouldReturnCreationTimeUtcOfFileInMemoryFileSystem() { // Arrange var creationTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { CreationTime = creationTime }; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); // Act var result = fileInfo.CreationTimeUtc; // Assert Assert.AreEqual(creationTime.ToUniversalTime(), result); }
public void MockFile_Decrypt_ShouldDecryptTheFile() { // Arrange const string Content = "Demo text content"; var fileData = new MockFileData(Content); var filePath = XFS.Path(@"c:\a.txt"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { {filePath, fileData } }); // Act fileSystem.File.Decrypt(filePath); string newcontents; using (var newfile = fileSystem.File.OpenText(filePath)) { newcontents = newfile.ReadToEnd(); } // Assert Assert.AreNotEqual(Content, newcontents); }
public void Serializable_can_deserialize() { //Arrange string textContentStr = "Text Contents"; //Act MockFileData data = new MockFileData(textContentStr); IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); formatter.Serialize(stream, data); stream.Seek(0, SeekOrigin.Begin); MockFileData deserialized = (MockFileData)formatter.Deserialize(stream); //Assert Assert.That(deserialized.TextContents, Is.EqualTo(textContentStr)); }
public void Serializable_works() { //Arrange MockFileData data = new MockFileData("Text Contents"); //Act IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); formatter.Serialize(stream, data); //Assert Assert.Pass(); }
public void MockFile_OpenText_ShouldRetainCreationTime() { // Arrange var fs = new MockFileSystem(); string filepath = XFS.Path(@"C:\TestData\test.txt"); var file = new MockFileData(@"I'm here"); var creationTime = new DateTime(2012, 03, 21); file.CreationTime = creationTime; fs.AddFile(filepath, file); // Act using (var reader = fs.File.OpenText(filepath)) { reader.ReadLine(); } // Assert Assert.AreEqual(creationTime, fs.FileInfo.FromFileName(filepath).CreationTime); }
public void MockFileInfo_OpenWrite_ShouldAddDataToFileInMemoryFileSystem() { // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); var bytesToAdd = new byte[] {65, 66, 67, 68, 69}; // Act using (var file = fileInfo.OpenWrite()) file.Write(bytesToAdd, 0, bytesToAdd.Length); string newcontents; using (var newfile = fileInfo.OpenText()) newcontents = newfile.ReadToEnd(); // Assert Assert.AreEqual("ABCDEtext content", newcontents); }
public void MockFile_WriteAllText_ShouldThrowAnUnauthorizedAccessExceptionIfTheFileIsReadOnly() { // Arrange var fileSystem = new MockFileSystem(); string filePath = XFS.Path(@"c:\something\demo.txt"); var mockFileData = new MockFileData(new byte[0]); mockFileData.Attributes = FileAttributes.ReadOnly; fileSystem.AddFile(filePath, mockFileData); // Act TestDelegate action = () => fileSystem.File.WriteAllText(filePath, null); // Assert Assert.Throws<UnauthorizedAccessException>(action); }
public void MockFileInfo_AppendText_ShouldAddTextToFileInMemoryFileSystem() { // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); // Act using (var file = fileInfo.AppendText()) file.WriteLine("This should be at the end"); string newcontents; using (var newfile = fileInfo.OpenText()) newcontents = newfile.ReadToEnd(); // Assert Assert.AreEqual("Demo text contentThis should be at the end\r\n", newcontents); }
public void OpenText_MockFileDataopulated_ReturnsData() { //Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { @"z:\a.txt", fileData } }); var fileInfo = new MockFileInfo(fileSystem, @"z:\a.txt"); //Act var reader = fileInfo.OpenText(); var result = reader.ReadToEnd(); //Assert Assert.AreEqual("Demo text content", result); }
public void MockFile_GetAttributeOfExistingFile_ShouldReturnCorrectValue() { var filedata = new MockFileData("test") { Attributes = FileAttributes.Hidden }; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { XFS.Path(@"c:\something\demo.txt"), filedata } }); var attributes = fileSystem.File.GetAttributes(XFS.Path(@"c:\something\demo.txt")); Assert.That(attributes, Is.EqualTo(FileAttributes.Hidden)); }