public void UnknownFileType_ReturnsAFileTypeCalledUnknown()
        {
            var handles = CreateRecognizerHandles();
            var fileTypeManager = new FileTypeManager(handles);

            FileType fileType = fileTypeManager.UnknownFileType;

            Assert.Multiple(() =>
            {
                Assert.AreEqual("Unknown", fileType.Id);
                Assert.AreEqual("File of unknown type.", fileType.Description);
                Assert.IsNull(fileType.SuperType);
            });
        }
        public void GetFileTypeById_WhenIdIsNull_Throws()
        {
            var handles = CreateRecognizerHandles();
            var fileTypeManager = new FileTypeManager(handles);

            Assert.Throws<ArgumentNullException>(() => fileTypeManager.GetFileTypeById(null));
        }
        public void IdentifyFileType_FileInspector_WhenContentsNotAvailable_ReturnsTypeOnlyIfNoContentCriteriaAndRecognizerMatches(
            bool contentCriteria, bool recognizerResult, bool expectedMatch)
        {
            var inspector = new FakeFileInspector();
            var recognizer = MockRepository.GenerateStub<IFileTypeRecognizer>();
            recognizer.Stub(x => x.IsRecognizedFile(inspector)).Return(recognizerResult);
            var handles = CreateRecognizerHandles(
                new RecognizerInfo
                {
                    Traits = new FileTypeRecognizerTraits("TypeA", "A")
                    {
                        ContentsRegex = contentCriteria ? ".*" : null
                    },
                    Recognizer = recognizer
                });
            var fileTypeManager = new FileTypeManager(handles);

            FileType fileType = fileTypeManager.IdentifyFileType(inspector);

            Assert.AreEqual(expectedMatch ? "TypeA" : "Unknown", fileType.Id);
        }
        public void IdentifyFileType_FileInspectorWithCandidates_WhenMatchCriteriaAndCandidatesAreVaried_ReturnsTypeIfMatchedOrUnknownOtherwise(
            bool includeSupertypeAsCandidate, bool includeSubtypeAsCandidate,
            bool superRecognizerResult, bool subRecognizerResult,
            string expectedMatchType)
        {
            var inspector = new FakeFileInspector()
            {
                FileInfo = new FileInfo(@"C:\file.txt"),
                Contents = "contents"
            };
            var superRecognizer = MockRepository.GenerateStub<IFileTypeRecognizer>();
            superRecognizer.Stub(x => x.IsRecognizedFile(inspector)).Return(superRecognizerResult);
            var subRecognizer = MockRepository.GenerateStub<IFileTypeRecognizer>();
            subRecognizer.Stub(x => x.IsRecognizedFile(inspector)).Return(subRecognizerResult);
            var handles = CreateRecognizerHandles(
                new RecognizerInfo
                {
                    Traits = new FileTypeRecognizerTraits("SuperType", "Super"),
                    Recognizer = superRecognizer
                },
                new RecognizerInfo
                {
                    Traits = new FileTypeRecognizerTraits("SubType", "Sub") { SuperTypeId = "SuperType" },
                    Recognizer = subRecognizer
                });
            var fileTypeManager = new FileTypeManager(handles);
            var candidates = new List<FileType>();
            if (includeSupertypeAsCandidate)
                candidates.Add(fileTypeManager.GetFileTypeById("SuperType"));
            if (includeSubtypeAsCandidate)
                candidates.Add(fileTypeManager.GetFileTypeById("SubType"));

            FileType fileType = fileTypeManager.IdentifyFileType(inspector, candidates);

            Assert.AreEqual(expectedMatchType, fileType.Id);
        }
        public void IdentifyFileType_FileInspectorWithCandidates_WhenCandidatesIsNull_Throws()
        {
            var handles = CreateRecognizerHandles();
            var fileTypeManager = new FileTypeManager(handles);

            Assert.Throws<ArgumentNullException>(() => fileTypeManager.IdentifyFileType(MockRepository.GenerateStub<IFileInspector>(), null));
        }
        public void IdentifyFileType_FileInspector_WhenMatchCriteriaAreVaried_ReturnsTypeIfMatchedOrUnknownOtherwise(string fileNameRegex, string contentsRegex, bool recognizerResult,
            bool expectedMatch)
        {
            var inspector = new FakeFileInspector()
            {
                FileInfo = new FileInfo(@"C:\file.txt"),
                Contents = "contents"
            };                
            var recognizer = MockRepository.GenerateStub<IFileTypeRecognizer>();
            recognizer.Stub(x => x.IsRecognizedFile(inspector)).Return(recognizerResult);
            var handles = CreateRecognizerHandles(
                new RecognizerInfo
                {
                    Traits = new FileTypeRecognizerTraits("TypeA", "A")
                        {
                            FileNameRegex = fileNameRegex,
                            ContentsRegex = contentsRegex
                        },
                    Recognizer = recognizer
                });
            var fileTypeManager = new FileTypeManager(handles);

            FileType fileType = fileTypeManager.IdentifyFileType(inspector);

            Assert.AreEqual(expectedMatch ? "TypeA" : "Unknown", fileType.Id);
        }
        public void IdentifyFileType_FileInspectorWithCandidates_WhenFileInspectorIsNull_Throws()
        {
            var handles = CreateRecognizerHandles();
            var fileTypeManager = new FileTypeManager(handles);

            Assert.Throws<ArgumentNullException>(() => fileTypeManager.IdentifyFileType((IFileInspector)null, new FileType[0]));
        }
        public void IdentifyFileType_FileInfoWithCandidates_WhenCandidatesIsNull_Throws()
        {
            var handles = CreateRecognizerHandles();
            var fileTypeManager = new FileTypeManager(handles);

            Assert.Throws<ArgumentNullException>(() => fileTypeManager.IdentifyFileType(new FileInfo(@"C:\"), null));
        }
        public void IdentifyFileType_FileInfo_WhenFileInfoIsNull_Throws()
        {
            var handles = CreateRecognizerHandles();
            var fileTypeManager = new FileTypeManager(handles);

            Assert.Throws<ArgumentNullException>(() => fileTypeManager.IdentifyFileType((FileInfo)null));
        }
        public void GetFileTypes_ReturnsListOfAllFileTypesIncludingUnknown()
        {
            var handles = CreateRecognizerHandles(
                new RecognizerInfo { Traits = new FileTypeRecognizerTraits("TypeA", "A") },
                new RecognizerInfo { Traits = new FileTypeRecognizerTraits("TypeB", "B") { SuperTypeId = "TypeC" } },
                new RecognizerInfo { Traits = new FileTypeRecognizerTraits("TypeC", "C") { SuperTypeId = "TypeD" } },
                new RecognizerInfo { Traits = new FileTypeRecognizerTraits("TypeD", "D") });
            var fileTypeManager = new FileTypeManager(handles);

            var expectedFileTypes = new FileType[5];
            expectedFileTypes[0] = new FileType("TypeA", "A", null);
            expectedFileTypes[3] = new FileType("TypeD", "D", null);
            expectedFileTypes[2] = new FileType("TypeC", "C", expectedFileTypes[3]);
            expectedFileTypes[1] = new FileType("TypeB", "B", expectedFileTypes[2]);
            expectedFileTypes[4] = fileTypeManager.UnknownFileType;

            IList<FileType> actualFileTypes = fileTypeManager.GetFileTypes();
            Assert.Over.KeyedPairs(
                expectedFileTypes.ToDictionary(x => x.Id),
                actualFileTypes.ToDictionary(x => x.Id),
                (x, y) => AssertEx.That(() => AreEquivalent(x, y)));
        }
        public void GetFileTypeById_WhenIdIsNotKnown_ReturnsNull()
        {
            var handles = CreateRecognizerHandles();
            var fileTypeManager = new FileTypeManager(handles);

            Assert.IsNull(fileTypeManager.GetFileTypeById("Type"));
        }
        public void GetFileTypeById_WhenIdIsKnown_ReturnsFileType()
        {
            var handles = CreateRecognizerHandles(
                new RecognizerInfo { Traits = new FileTypeRecognizerTraits("Type", "A file type.") });
            var fileTypeManager = new FileTypeManager(handles);

            FileType fileType = fileTypeManager.GetFileTypeById("Type");
            Assert.AreEqual("Type", fileType.Id);
            Assert.AreEqual("A file type.", fileType.Description);
        }
        public void GetFileTypeById_WhenIdIsTheUnknownFileTypeId_ReturnsUnknownFileType()
        {
            var handles = CreateRecognizerHandles();
            var fileTypeManager = new FileTypeManager(handles);

            FileType fileType = fileTypeManager.GetFileTypeById("Unknown");
            Assert.AreSame(fileTypeManager.UnknownFileType, fileType);
        }