public void MetadataIsWritten()
        {
            // Arrange

            var metadata = new[]
            {
                new DocumentMetadata(
                    new DocumentMetadataInfo
                {
                    FullName           = "ItemFullName",
                    ProjectDisplayName = "ItemProjectDisplayName",
                    ProjectFullName    = "ItemProjectFullName"
                }, null, null)
            };

            using (var writer = new TestingTextWriter())
            {
                var ioService = Mock.Of <IIOService>(s =>
                                                     s.GetWriter(It.IsAny <string>()) == writer);

                var service = new PinnedItemStorageService(ioService);

                // Act

                service.Write(metadata, "FullName");

                // Assert

                Assert.AreEqual(
                    "[{\"FullName\":\"ItemFullName\",\"ProjectDisplayName\":\"ItemProjectDisplayName\",\"ProjectFullName\":\"ItemProjectFullName\"}]\r\n",
                    writer.WrittenData);
            }
        }
        public void ReadReturnsExistingMetadataInfo()
        {
            // Arrange

            const string savedData =
                "[{\"FullName\":\"ItemFullName\",\"ProjectDisplayName\":\"ItemProjectDisplayName\",\"ProjectFullName\":\"ItemProjectFullName\"}]\r\n";

            var ioService = Mock.Of <IIOService>(s =>
                                                 s.GetReader(It.IsAny <string>()) == Mock.Of <TextReader>(r =>
                                                                                                          r.ReadToEnd() == savedData));

            var service = new PinnedItemStorageService(ioService);

            // Act

            var info = service.Read("FullName");

            // Assert

            var item = info.Single();

            Assert.AreEqual("ItemFullName", item.FullName);
            Assert.AreEqual("ItemProjectDisplayName", item.ProjectDisplayName);
            Assert.AreEqual("ItemProjectFullName", item.ProjectFullName);
        }
        public void OutputDirectoryIsCreatedBeforeDataIsWritten()
        {
            // Arrange

            var directoryCreated            = false;
            var directoryCreatedBeforeWrite = false;

            using (var writer = Mock.Of <TextWriter>())
            {
                Mock.Get(writer)
                .Setup(w => w.WriteLine(It.IsAny <string>()))
                .Callback(() => directoryCreatedBeforeWrite = directoryCreated);

                var ioService = Mock.Of <IIOService>(s =>
                                                     s.GetWriter(It.IsAny <string>()) == writer);

                Mock.Get(ioService)
                .Setup(s => s.CreateDirectory(It.IsAny <string>()))
                .Callback(() => directoryCreated = true);

                var service = new PinnedItemStorageService(ioService);

                // Act

                service.Write(new DocumentMetadata[0], "FullName");

                // Assert

                Assert.IsTrue(directoryCreatedBeforeWrite);
            }
        }
        public void OutputDirectoryIsCreated()
        {
            // Arrange

            using (var writer = new TestingTextWriter())
            {
                var ioService = Mock.Of <IIOService>(s =>
                                                     s.GetWriter(It.IsAny <string>()) == writer);

                var service = new PinnedItemStorageService(ioService);

                // Act

                service.Write(new DocumentMetadata[0], "FullName");

                // Assert

                var expectedPath = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                    "WorkingFilesList");

                Mock.Get(ioService).Verify(s =>
                                           s.CreateDirectory(expectedPath));
            }
        }
        public void ExceptionIsThrownWhenFullNameIsEmptyWhenGettingReader(string fullName)
        {
            // Arrange

            var service = new PinnedItemStorageService(
                Mock.Of <IIOService>());

            // Act, Assert

            Assert.Throws <ArgumentException>(() =>
                                              service.Read(fullName));
        }
        public void ExceptionIsThrownWhenFullNameIsEmptyWhenRemovingSavedMetadata(
            string fullName)
        {
            // Arrange

            var service = new PinnedItemStorageService(
                Mock.Of <IIOService>());

            // Act, Assert

            Assert.Throws <ArgumentException>(() =>
                                              service.Remove(fullName));
        }
        public void ReadReturnsEmptyMetadataInfoListIfFileDoesNotExist()
        {
            // Arrange

            var service = new PinnedItemStorageService(
                Mock.Of <IIOService>());

            // Act

            var info = service.Read("FullName");

            // Assert

            Assert.IsEmpty(info);
        }
        public void FullNameIsHashedWhenRemovingSavedMetadata()
        {
            // Arrange

            var ioService = Mock.Of <IIOService>(s =>
                                                 s.FileExists(_hashedPath) == true);

            var service = new PinnedItemStorageService(ioService);

            // Act

            service.Remove(FullName);

            // Assert

            Mock.Get(ioService).Verify(s =>
                                       s.Delete(_hashedPath));
        }
        public void SavedMetadataIsNotRemovedIfFullNamePathDoesNotExist()
        {
            // Arrange

            var ioService = Mock.Of <IIOService>(s =>
                                                 s.FileExists(It.IsAny <string>()) == false);

            var service = new PinnedItemStorageService(ioService);

            // Act

            service.Remove(FullName);

            // Assert

            Mock.Get(ioService).Verify(s =>
                                       s.Delete(It.IsAny <string>()),
                                       Times.Never);
        }
        public void ReaderIsDisposed()
        {
            // Arrange

            using (var reader = new TestingTextReader())
            {
                var ioService = Mock.Of <IIOService>(s =>
                                                     s.GetReader(It.IsAny <string>()) == reader);

                var service = new PinnedItemStorageService(ioService);

                // Act

                service.Read("FullName");

                // Assert

                Assert.IsTrue(reader.DisposeInvoked);
            }
        }
        public void WriterIsDisposed()
        {
            // Arrange

            using (var writer = new TestingTextWriter())
            {
                var ioService = Mock.Of <IIOService>(s =>
                                                     s.GetWriter(It.IsAny <string>()) == writer);

                var service = new PinnedItemStorageService(ioService);

                // Act

                service.Write(new DocumentMetadata[0], "FullName");

                // Assert

                Assert.IsTrue(writer.DisposeInvoked);
            }
        }
        public void FullNameIsHashedWhenRequestingWriter()
        {
            // Arrange

            using (var writer = new TestingTextWriter())
            {
                var ioService = Mock.Of <IIOService>(s =>
                                                     s.GetWriter(It.IsAny <string>()) == writer);

                var service = new PinnedItemStorageService(ioService);

                // Act

                service.Write(new DocumentMetadata[0], FullName);

                // Assert

                Mock.Get(ioService).Verify(s =>
                                           s.GetWriter(_hashedPath));
            }
        }
        public void FullNameIsHashedWhenRequestingReader()
        {
            // Arrange

            using (var reader = new TestingTextReader())
            {
                var ioService = Mock.Of <IIOService>(s =>
                                                     s.GetReader(It.IsAny <string>()) == reader);

                var service = new PinnedItemStorageService(ioService);

                // Act

                service.Read(FullName);

                // Assert

                Mock.Get(ioService).Verify(s =>
                                           s.GetReader(_hashedPath));
            }
        }