public void ToReplacementModel_RelativeUri_BinaryReplacement()
        {
            var mock = new Mock <IFileSystem>();

            mock.Setup(fs => fs.FileExists(It.IsAny <string>())).Returns(false);
            mock.Setup(fs => fs.FileReadAllText(It.IsAny <string>())).Returns(CodeSample);
            var regionCache = new FileRegionsCache(fileSystem: mock.Object);

            Uri uri = new Uri(RelativeFilePath, UriKind.Relative);

            byte[] bytes = Encoding.UTF8.GetBytes(ReplacementText);

            Replacement replacement = new Replacement
            {
                DeletedRegion = new Region
                {
                    ByteOffset = 210,
                },
                InsertedContent = new ArtifactContent
                {
                    Binary = Convert.ToBase64String(bytes),
                },
            };
            ReplacementModel model = replacement.ToReplacementModel(regionCache, uri);

            model.Should().NotBeNull();
            model.InsertedString.Should().BeNull();
            model.InsertedBytes.Should().BeEquivalentTo(bytes);
            model.Region.Should().NotBeNull();
            model.Region.IsBinaryRegion.Should().BeTrue();
            model.Region.CharOffset.Should().Be(-1);
            model.Region.ByteOffset.Should().Be(210);
        }
        public void ToReplacementModel_RelativeUri_TextReplacement()
        {
            var mock = new Mock <IFileSystem>();

            mock.Setup(fs => fs.FileExists(It.IsAny <string>())).Returns(false);
            mock.Setup(fs => fs.FileReadAllText(It.IsAny <string>())).Returns(CodeSample);
            var regionCache = new FileRegionsCache(fileSystem: mock.Object);

            Uri uri = new Uri(RelativeFilePath, UriKind.Relative);

            Replacement replacement = new Replacement
            {
                DeletedRegion = new Region
                {
                    CharOffset = 196,
                    CharLength = 14,
                },
                InsertedContent = new ArtifactContent
                {
                    Text = ReplacementText,
                },
            };
            ReplacementModel model = replacement.ToReplacementModel(regionCache, uri);

            model.Should().NotBeNull();
            model.InsertedBytes.Should().BeNull();
            model.InsertedString.Should().BeEquivalentTo(ReplacementText);
            model.Region.Should().NotBeNull();
            model.Region.CharLength.Should().Be(14);
            model.Region.CharOffset.Should().Be(196);
            model.Region.IsBinaryRegion.Should().BeFalse();
            model.Region.ByteOffset.Should().Be(-1);
        }
        public void ToReplacementModel_NullReplacement_ReturnNull()
        {
            var mock = new Mock <IFileSystem>();

            mock.Setup(fs => fs.FileExists(It.IsAny <string>())).Returns(false);
            mock.Setup(fs => fs.FileReadAllText(It.IsAny <string>())).Returns(CodeSample);
            var regionCache = new FileRegionsCache(fileSystem: mock.Object);

            Uri uri = new Uri(FilePath);

            Replacement      replacement = null;
            ReplacementModel model       = replacement.ToReplacementModel(regionCache, uri);

            model.Should().BeNull();
        }
        public void ToReplacementModel_WhenUriIsRelativeAndReplacementRegionIsCharOffsetLength_ProducesExpectedModel()
        {
            const string FilePath = "test.txt";
            var          fileUri  = new Uri(FilePath, UriKind.Relative);

            const int    DeletedCharOffset = 12;
            const int    DeletedCharLength = 2;
            const string ReplacementString = "FortyTwo";

            var replacement = new Replacement
            {
                InsertedContent = new ArtifactContent
                {
                    Text = ReplacementString,
                },
                DeletedRegion = new Region
                {
                    CharOffset = DeletedCharOffset,
                    CharLength = DeletedCharLength,
                },
            };

            var run = new Run();

            var mockFileSystem = new Mock <IFileSystem>();

            var fileRegionsCache = new FileRegionsCache(FileRegionsCacheCapacity, mockFileSystem.Object);

            var actualModel = replacement.ToReplacementModel(fileRegionsCache, fileUri);

            actualModel.Region.CharOffset.Should().Be(DeletedCharOffset);
            actualModel.Region.CharLength.Should().Be(DeletedCharLength);
            actualModel.IsTextReplacement.Should().BeTrue();
            actualModel.IsBinaryReplacement.Should().BeFalse();
            actualModel.InsertedString.Should().Be(ReplacementString);

            // The run does not contain any path information, so when the FileRegionsCache is
            // constructed, it does not consult the file system.
            mockFileSystem.VerifyNoOtherCalls();
        }