Dreamine.FullKit.Tests 1.0.0.0
Dreamine.FullKit.Tests 기능을 검증하는 자동화 테스트 프로젝트입니다.
로딩중...
검색중...
일치하는것 없음
SourceGeneratorTests.cs
이 파일의 문서화 페이지로 가기
1using Dreamine.MVVM.Attributes;
2using Dreamine.MVVM.Generators;
3using Microsoft.CodeAnalysis;
4using Microsoft.CodeAnalysis.CSharp;
5
7
16public sealed class SourceGeneratorTests
17{
26 [Fact]
28 {
29 var source = """
30 using Dreamine.MVVM.Attributes;
31
32 namespace Sample;
33
34 public partial class MainViewModel
35 {
36 [DreamineCommand]
37 private void Save()
38 {
39 }
40 }
41 """;
42
43 var runResult = RunGenerator(source, new DreamineCommandSourceGenerator());
44
45 var generated = Assert.Single(runResult.GeneratedSources);
46 var text = generated.SourceText.ToString();
47 Assert.Contains("SaveCommand", text);
48 Assert.Contains("ICommand", text);
49 Assert.Contains("new __DreamineGeneratedCommand_Save(Save)", text);
50 }
51
60 [Fact]
62 {
63 var source = """
64 using Dreamine.MVVM.Attributes;
65
66 namespace Sample;
67
68 public partial class MainViewModel
69 {
70 private string? Result { get; set; }
71
72 private string Load() => "Loaded";
73
74 [DreamineCommand("Load", BindTo = "Result")]
75 private partial void LoadResult();
76 }
77 """;
78
79 var runResult = RunGenerator(source, new DreamineCommandSourceGenerator());
80
81 var generated = Assert.Single(runResult.GeneratedSources);
82 var text = generated.SourceText.ToString();
83 Assert.Contains("LoadResultCommand", text);
84 Assert.Contains("var __result = Load();", text);
85 Assert.Contains("Result = __result;", text);
86 }
87
96 [Fact]
98 {
99 var source = """
100 using Dreamine.MVVM.Attributes;
101
102 namespace Sample;
103
104 public class MainViewModel
105 {
106 [DreamineCommand]
107 private void Save()
108 {
109 }
110 }
111 """;
112
113 var runResult = RunGenerator(source, new DreamineCommandSourceGenerator());
114
115 Assert.Contains(runResult.Diagnostics, diagnostic => diagnostic.Id == "DMCMD002");
116 }
117
126 [Fact]
128 {
129 var source = """
130 using Dreamine.MVVM.Attributes;
131
132 namespace Sample;
133
134 public partial class MainViewModel
135 {
136 [DreamineProperty]
137 private string _title = "";
138
139 protected bool SetProperty<T>(ref T field, T value)
140 {
141 field = value;
142 return true;
143 }
144 }
145 """;
146
147 var runResult = RunGenerator(source, new DreamineAutoWiringGenerator());
148
149 var generated = Assert.Single(runResult.GeneratedSources);
150 Assert.Contains("public string Title", generated.SourceText.ToString());
151 }
152
185 private static GeneratorRunResult RunGenerator(string source, IIncrementalGenerator generator)
186 {
187 var compilation = CSharpCompilation.Create(
188 "GeneratorTests",
189 new[] { CSharpSyntaxTree.ParseText(source) },
191 new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
192
193 var driver = CSharpGeneratorDriver.Create(generator);
194 driver = (CSharpGeneratorDriver)driver.RunGeneratorsAndUpdateCompilation(compilation, out _, out _);
195
196 return driver.GetRunResult().Results.Single();
197 }
198
215 private static IEnumerable<MetadataReference> GetReferences()
216 {
217 var trustedPlatformAssemblies = ((string?)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES"))
218 ?.Split(Path.PathSeparator)
219 ?? Array.Empty<string>();
220
221 return trustedPlatformAssemblies
222 .Concat(new[] { typeof(DreamineCommandAttribute).Assembly.Location })
223 .Distinct(StringComparer.OrdinalIgnoreCase)
224 .Select(path => MetadataReference.CreateFromFile(path));
225 }
226}
static GeneratorRunResult RunGenerator(string source, IIncrementalGenerator generator)
static IEnumerable< MetadataReference > GetReferences()