private async Task <IEnumerable <MappedTextReplacement> > GetReplacements(Project originalProject, IEnumerable <Document> updatedDocuments, CancellationToken token)
        {
            var replacements = new List <MappedTextReplacement>();

            foreach (var updatedDocument in updatedDocuments)
            {
                var originalDocument = originalProject.GetDocument(updatedDocument.Id);
                if (originalDocument is null)
                {
                    _logger.LogWarning("Cannot find expected generated file {FilePath}", updatedDocument.FilePath);
                    continue;
                }

                var mappedFilePath        = originalDocument.FilePath?.Replace(".cshtml.cs", ".cshtml");
                var originalSubTextGroups = (await MappedSubText.GetMappedSubTextsAsync(originalDocument, mappedFilePath, token).ConfigureAwait(false)).ToLookup(m => m.SourceLocation);
                var updatedSubTextGroups  = (await MappedSubText.GetMappedSubTextsAsync(updatedDocument, mappedFilePath, token).ConfigureAwait(false)).ToLookup(m => m.SourceLocation);

                foreach (var originalGroup in originalSubTextGroups)
                {
                    var updatedText = updatedSubTextGroups[originalGroup.Key]?.Select(t => t.Text.ToString()) ?? Enumerable.Empty <string>();
                    replacements.AddRange(_textMatcher.MatchOrderedSubTexts(originalGroup, updatedText));
                }
            }

            return(replacements.Distinct());
        }
        public async Task GetMappedSubTextsTests(string source, string?defaultMapPath, IEnumerable <MappedSubText> expected)
        {
            // Arrange
            using var workspace = new AdhocWorkspace();
            var doc = CreateDoc(workspace, source);

            // Act
            var subTexts = await MappedSubText.GetMappedSubTextsAsync(doc, defaultMapPath, CancellationToken.None).ConfigureAwait(true);

            // Assert
            Assert.Collection(subTexts, expected.Select <MappedSubText, Action <MappedSubText> >(e => a => Assert.Equal(e, a)).ToArray());
        }
        public void MatchOrderedSubTextsNegativeTests()
        {
            var matcher       = new DefaultTextMatcher(new Differ(), new CharacterChunker());
            var originalTexts = new MappedSubText[]
            {
                GetSubText("Test", "test.txt", 1)
            };
            var updatedTexts = new string[] { "Test1", "Test2" };

            Assert.Throws <ArgumentNullException>("originalTexts", () => matcher.MatchOrderedSubTexts(null !, updatedTexts));
            Assert.Throws <ArgumentNullException>("newTexts", () => matcher.MatchOrderedSubTexts(originalTexts, null !));
            Assert.Throws <ArgumentException>(() => matcher.MatchOrderedSubTexts(originalTexts, updatedTexts));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MappedTextReplacement"/> class, getting the original text, file path, and starting line from an original MappedSubText.
 /// </summary>
 /// <param name="originalText">A MappedSubText containing the original text and original text location.</param>
 /// <param name="newText">The new text to replace the original text with.</param>
 public MappedTextReplacement(MappedSubText originalText, string newText)
     : this(originalText?.Text.ToString() ?? throw new ArgumentNullException(nameof(originalText)), newText, originalText.FilePath, originalText.StartingLine)
 {
 }
示例#5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TextReplacement"/> class, getting the original text, file path, and starting line from an original MappedSubText.
 /// </summary>
 /// <param name="originalText">A MappedSubText containing the original text and original text location.</param>
 /// <param name="newText">The new text to replace the original text with.</param>
 public TextReplacement(MappedSubText originalText, SourceText newText)
     : this(originalText?.Text ?? throw new ArgumentNullException(nameof(originalText)), newText, originalText.FilePath, originalText.StartingLine)
 {
 }
 public void EqualsTests(MappedSubText a, MappedSubText b, bool expected)
 {
     Assert.Equal(expected, a.Equals(b));
     Assert.Equal(expected, b.Equals(a));
     Assert.Equal(expected, a.GetHashCode() == b.GetHashCode());
 }