コード例 #1
0
        public void it_can_open_any_file_for_read_and_write_and_seek()
        {
            var fileSystem = new DefaultFileSystem();

            var anyFile = fileSystem.New(NewRandomTempFile());

            using (var theStream = fileSystem.Open(anyFile)) {
                Assert.IsTrue(theStream.CanRead	, "Expected the resultant stream to be readable");
                Assert.IsTrue(theStream.CanWrite, "Expected the resultant stream to be writable");
                Assert.IsTrue(theStream.CanSeek	, "Expected the resultant stream to be seekable");
            }
        }
コード例 #2
0
        public void it_does_not_create_the_file_if_it_does_not_exist()
        {
            var fileSystem = new DefaultFileSystem();

            var aFileThatDoesNotExist = new FileInfo("xxx.xxx");

            File.Delete(aFileThatDoesNotExist.FullName);

            Assert.IsFalse(aFileThatDoesNotExist.Exists);

            var theError = Assert.Throws<FileNotFoundException>(() => fileSystem.Open(aFileThatDoesNotExist));

            Assert.That(theError.Message, Is.StringMatching("^Could not find file"));
        }
コード例 #3
0
        public void it_deletes_files()
        {
            var theFilePath = new FileInfo(NewRandomTempFile());

            using (File.Create(theFilePath.FullName)) {;}

            Assert.IsTrue(theFilePath.Exists, "Invalid test data. Expected the file to exist so it can be deleted.");

            var fileSystem = new DefaultFileSystem();

            fileSystem.Delete(theFilePath);

            Assert.IsTrue(theFilePath.Exists, "Expected the file to have been deleted");
        }
コード例 #4
0
        public void it_fails_to_create_a_new_file_if_the_file_already_exists()
        {
            var theFilePath = NewRandomTempFile();

            var fileSystem = new DefaultFileSystem();

            fileSystem.New(theFilePath);

            Assert.IsTrue(File.Exists(theFilePath),
                "Invalid test data, The file <{0}> does not exist. " +
                "The file must exist before a create attempt is tried.",
                theFilePath
            );

            Assert.Throws<IOException>(() => fileSystem.New(theFilePath),
                "Expected trying to create a file that already exists to fail with error"
            );
        }
コード例 #5
0
        private void AssertFlushesAutomatically(Byte[] bytes)
        {
            var fileSystem = new DefaultFileSystem();
            var theFilePath = fileSystem.New(NewRandomTempFile());

            Assert.That(theFilePath.Length, Is.EqualTo(0),
                "Expected the file to be empty to start with"
            );

            using (var _out = fileSystem.Open(theFilePath)) {
                _out.Write(bytes, 0, bytes.Length);

                theFilePath.Refresh();

                Assert.AreEqual(bytes.Length, theFilePath.Length,
                    "Expected the bytes to have been automatically flushed because the buffer was filled"
                );
            }
        }
コード例 #6
0
        private void AssertDoesNotFlushAutomatically(Byte[] bytes)
        {
            var fileSystem = new DefaultFileSystem();
            var theFilePath = fileSystem.New(NewRandomTempFile());

            Assert.That(theFilePath.Length, Is.EqualTo(0),
                "Expected the file to be empty to start with"
            );

            using (var _out = fileSystem.Open(theFilePath)) {
                _out.Write(bytes, 0, bytes.Length);

                theFilePath.Refresh();

                Assert.AreEqual(0, theFilePath.Length,
                    "Expected that the bytes would not be flushed because the buffer is not yet full"
                );
            }
        }
コード例 #7
0
        public void you_need_to_flush_a_file_stream_to_propagate_bytes_to_disk_unless_buffer_is_full()
        {
            var fileSystem = new DefaultFileSystem();
            var theFilePath = fileSystem.New(NewRandomTempFile());

            using (var _out = fileSystem.Open(theFilePath)) {
                var bytes = Encoding.UTF8.GetBytes("An example string");
                _out.Write(bytes, 0, bytes.Length);

                Assert.That(theFilePath.Length, Is.EqualTo(0),
                    "Expected the bytes to have been buffered and not yet written to disk"
                );

                _out.Flush();

                theFilePath.Refresh();

                Assert.That(theFilePath.Length, Is.GreaterThan(0),
                    "Expected the bytes to have been written to disk now that the stream has been flushed"
                );
            }
        }
コード例 #8
0
        public void it_opens_the_file_supplied()
        {
            var fileSystem = new DefaultFileSystem();

            var anyFile = fileSystem.New(NewRandomTempFile());

            var theExpectedText = "Let’s make toast the American way, you burn it and I’ll scrape it.";

            using (var theStream = fileSystem.Open(anyFile)) {
                using (var writer = new StreamWriter(theStream)) {
                    writer.Write(theExpectedText);
                }
            }

            var theActualTextInTheFile = File.ReadAllText(anyFile.FullName);

            Assert.AreEqual(theExpectedText, theActualTextInTheFile,
                "Expected the text in the file <{0}> to match because we asked DefaultFileSystem to " +
                "open it and then we wrote that text to it. " +
                "This means the file we asked to be opened was not opened (because the write was successful).",
                anyFile
            );
        }