| 1 | using CascadeIDE.Models.Intercom; |
| 2 | using CascadeIDE.Services.Intercom; |
| 3 | using Xunit; |
| 4 | |
| 5 | namespace CascadeIDE.Tests; |
| 6 | |
| 7 | public sealed class AttachmentAnchorRoslynResolverTests |
| 8 | { |
| 9 | private const string SampleSource = |
| 10 | """ |
| 11 | namespace Sample; |
| 12 | |
| 13 | public class Worker |
| 14 | { |
| 15 | public void Run() |
| 16 | { |
| 17 | for (var i = 0; i < 1; i++) { } |
| 18 | for (var j = 0; j < 2; j++) { } |
| 19 | } |
| 20 | } |
| 21 | """; |
| 22 | |
| 23 | [Fact] |
| 24 | public void TryResolveLineRange_FindsMethodBySimpleName() |
| 25 | { |
| 26 | var path = writeTempCs(); |
| 27 | Assert.True( |
| 28 | AttachmentAnchorRoslynResolver.TryResolveLineRange(path, "Run", null, out var lines, out var detail), |
| 29 | detail); |
| 30 | Assert.True(lines.Start.Value >= 5); |
| 31 | Assert.True(lines.End.Value >= lines.Start.Value); |
| 32 | } |
| 33 | |
| 34 | [Fact] |
| 35 | public void TryResolveLineRange_FindsSecondForLoop() |
| 36 | { |
| 37 | var path = writeTempCs(); |
| 38 | Assert.True( |
| 39 | AttachmentAnchorRoslynResolver.TryResolveLineRange( |
| 40 | path, null, new AttachmentSyntaxScope("for", 1, "Run"), out var first, out _)); |
| 41 | Assert.True( |
| 42 | AttachmentAnchorRoslynResolver.TryResolveLineRange( |
| 43 | path, null, new AttachmentSyntaxScope("for", 2, "Run"), out var second, out var detail), |
| 44 | detail); |
| 45 | Assert.True(second.Start.Value > first.Start.Value); |
| 46 | } |
| 47 | |
| 48 | private static string writeTempCs() |
| 49 | { |
| 50 | var dir = Path.Combine(Path.GetTempPath(), nameof(AttachmentAnchorRoslynResolverTests) + "_" + Guid.NewGuid().ToString("N")); |
| 51 | Directory.CreateDirectory(dir); |
| 52 | var path = Path.Combine(dir, "Worker.cs"); |
| 53 | File.WriteAllText(path, SampleSource); |
| 54 | return path; |
| 55 | } |
| 56 | } |
| 57 | |