|
| 1 | +using Basic.Reference.Assemblies; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.CSharp; |
| 4 | +using newtype.generator; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace newtype.tests; |
| 8 | + |
| 9 | +public class GeneratorOutputTests |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public void Generates_Output_For_Int_Alias() |
| 13 | + { |
| 14 | + const string source = """ |
| 15 | + using newtype; |
| 16 | +
|
| 17 | + [newtype<int>] |
| 18 | + public readonly partial struct TestId; |
| 19 | + """; |
| 20 | + |
| 21 | + var result = GeneratorTestHelper.RunGenerator(source); |
| 22 | + |
| 23 | + // Attribute source + alias source |
| 24 | + Assert.Equal(2, result.GeneratedTrees.Length); |
| 25 | + |
| 26 | + var generatedSources = result.Results[0].GeneratedSources; |
| 27 | + |
| 28 | + var aliasSource = generatedSources.Single(s => s.HintName.EndsWith("TestId.g.cs")); |
| 29 | + var text = aliasSource.SourceText.ToString(); |
| 30 | + |
| 31 | + Assert.Contains("private readonly int _value;", text); |
| 32 | + Assert.Contains("public TestId(int value)", text); |
| 33 | + Assert.Contains("operator +", text); |
| 34 | + Assert.Contains("operator ==", text); |
| 35 | + Assert.Contains("IComparable<TestId>", text); |
| 36 | + Assert.Contains("IEquatable<TestId>", text); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void Generates_Output_For_String_Alias() |
| 41 | + { |
| 42 | + const string source = """ |
| 43 | + using newtype; |
| 44 | +
|
| 45 | + [newtype<string>] |
| 46 | + public readonly partial struct Label; |
| 47 | + """; |
| 48 | + |
| 49 | + var result = GeneratorTestHelper.RunGenerator(source); |
| 50 | + |
| 51 | + Assert.Equal(2, result.GeneratedTrees.Length); |
| 52 | + |
| 53 | + var generatedSources = result.Results[0].GeneratedSources; |
| 54 | + |
| 55 | + var aliasSource = generatedSources.Single(s => s.HintName.EndsWith("Label.g.cs")); |
| 56 | + var text = aliasSource.SourceText.ToString(); |
| 57 | + |
| 58 | + Assert.Contains("private readonly string _value;", text); |
| 59 | + Assert.Contains(@"_value?.ToString() ?? """"", text); |
| 60 | + Assert.Contains("IComparable<Label>", text); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void Generates_Output_For_Custom_Class_Alias() |
| 65 | + { |
| 66 | + const string source = """ |
| 67 | + using newtype; |
| 68 | + using System; |
| 69 | +
|
| 70 | + public class Widget |
| 71 | + { |
| 72 | + public string Name { get; } |
| 73 | +
|
| 74 | + public Widget(string name) => Name = name; |
| 75 | +
|
| 76 | + public static Widget operator +(Widget a, Widget b) => new(a.Name + b.Name); |
| 77 | + } |
| 78 | +
|
| 79 | + [newtype<Widget>] |
| 80 | + public readonly partial struct MyWidget; |
| 81 | + """; |
| 82 | + |
| 83 | + var result = GeneratorTestHelper.RunGenerator(source); |
| 84 | + |
| 85 | + Assert.Equal(2, result.GeneratedTrees.Length); |
| 86 | + |
| 87 | + var generatedSources = result.Results[0].GeneratedSources; |
| 88 | + |
| 89 | + var aliasSource = generatedSources.Single(s => s.HintName.EndsWith("MyWidget.g.cs")); |
| 90 | + var text = aliasSource.SourceText.ToString(); |
| 91 | + |
| 92 | + // Operator forwarding |
| 93 | + Assert.Contains("operator +", text); |
| 94 | + // Property forwarding |
| 95 | + Assert.Contains("Name", text); |
| 96 | + // Constructor forwarding |
| 97 | + Assert.Contains("public MyWidget(string name)", text); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void Non_Partial_Struct_Produces_Compilation_Error() |
| 102 | + { |
| 103 | + const string source = """ |
| 104 | + using newtype; |
| 105 | +
|
| 106 | + [newtype<int>] |
| 107 | + public struct Bad; |
| 108 | + """; |
| 109 | + |
| 110 | + // Run the generator directly — we expect a compilation error (CS0260), |
| 111 | + // so we can't use RunGenerator which asserts no errors. |
| 112 | + var syntaxTree = CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(LanguageVersion.Preview)); |
| 113 | + |
| 114 | + var compilation = CSharpCompilation.Create( |
| 115 | + "Tests", |
| 116 | + [syntaxTree], |
| 117 | + Net80.References.All, |
| 118 | + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); |
| 119 | + |
| 120 | + var generator = new AliasGenerator().AsSourceGenerator(); |
| 121 | + |
| 122 | + GeneratorDriver driver = CSharpGeneratorDriver.Create( |
| 123 | + generators: [generator], |
| 124 | + parseOptions: new CSharpParseOptions(LanguageVersion.Preview), |
| 125 | + driverOptions: new GeneratorDriverOptions( |
| 126 | + disabledOutputs: IncrementalGeneratorOutputKind.None, |
| 127 | + trackIncrementalGeneratorSteps: true)); |
| 128 | + |
| 129 | + driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out _); |
| 130 | + |
| 131 | + // The generated partial declaration conflicts with the non-partial user declaration |
| 132 | + var errors = outputCompilation.GetDiagnostics() |
| 133 | + .Where(d => d.Severity == DiagnosticSeverity.Error) |
| 134 | + .ToArray(); |
| 135 | + |
| 136 | + Assert.Contains(errors, e => e.Id == "CS0260"); |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + public void Generates_Separate_Outputs_Per_Type() |
| 141 | + { |
| 142 | + const string source = """ |
| 143 | + using newtype; |
| 144 | +
|
| 145 | + [newtype<int>] |
| 146 | + public readonly partial struct IdA; |
| 147 | +
|
| 148 | + [newtype<float>] |
| 149 | + public readonly partial struct IdB; |
| 150 | + """; |
| 151 | + |
| 152 | + var result = GeneratorTestHelper.RunGenerator(source); |
| 153 | + |
| 154 | + // Attribute + 2 aliases |
| 155 | + Assert.Equal(3, result.GeneratedTrees.Length); |
| 156 | + |
| 157 | + var generatedSources = result.Results[0].GeneratedSources; |
| 158 | + |
| 159 | + Assert.Single(generatedSources.Where(s => s.HintName.EndsWith("IdA.g.cs"))); |
| 160 | + Assert.Single(generatedSources.Where(s => s.HintName.EndsWith("IdB.g.cs"))); |
| 161 | + } |
| 162 | + |
| 163 | + [Fact] |
| 164 | + public void NonGeneric_Attribute_Works() |
| 165 | + { |
| 166 | + const string source = """ |
| 167 | + using newtype; |
| 168 | +
|
| 169 | + [newtype(typeof(int))] |
| 170 | + public readonly partial struct TestId; |
| 171 | + """; |
| 172 | + |
| 173 | + var result = GeneratorTestHelper.RunGenerator(source); |
| 174 | + |
| 175 | + Assert.Equal(2, result.GeneratedTrees.Length); |
| 176 | + |
| 177 | + var generatedSources = result.Results[0].GeneratedSources; |
| 178 | + |
| 179 | + var aliasSource = generatedSources.Single(s => s.HintName.EndsWith("TestId.g.cs")); |
| 180 | + var text = aliasSource.SourceText.ToString(); |
| 181 | + |
| 182 | + Assert.Contains("private readonly int _value;", text); |
| 183 | + Assert.Contains("public TestId(int value)", text); |
| 184 | + } |
| 185 | +} |
0 commit comments