|
| 1 | +using Xunit; |
| 2 | + |
| 3 | +namespace newtype.tests; |
| 4 | + |
| 5 | +public class ReadmeExampleTests |
| 6 | +{ |
| 7 | + private const string ReadmeSource = """ |
| 8 | + using newtype; |
| 9 | +
|
| 10 | + [newtype<string>] |
| 11 | + public readonly partial struct TableId; |
| 12 | +
|
| 13 | + [newtype<int>] |
| 14 | + public readonly partial struct PizzasEaten; |
| 15 | +
|
| 16 | + [newtype<double>] |
| 17 | + public readonly partial struct Fullness; |
| 18 | +
|
| 19 | + class Guest |
| 20 | + { |
| 21 | + TableId table = "Table 1"; |
| 22 | + PizzasEaten pizzasEaten; |
| 23 | + Fullness fullness; |
| 24 | +
|
| 25 | + public void fillEmUp(Fullness threshold) |
| 26 | + { |
| 27 | + while (fullness < threshold) |
| 28 | + { |
| 29 | + pizzasEaten++; |
| 30 | + fullness += 0.1; |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + """; |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void Readme_Example_Compiles_Without_Errors() |
| 38 | + { |
| 39 | + var result = GeneratorTestHelper.RunGenerator(ReadmeSource); |
| 40 | + |
| 41 | + // Attribute + 3 aliases |
| 42 | + Assert.Equal(4, result.GeneratedTrees.Length); |
| 43 | + |
| 44 | + var sources = result.Results[0].GeneratedSources; |
| 45 | + Assert.Single(sources.Where(s => s.HintName.EndsWith("TableId.g.cs"))); |
| 46 | + Assert.Single(sources.Where(s => s.HintName.EndsWith("PizzasEaten.g.cs"))); |
| 47 | + Assert.Single(sources.Where(s => s.HintName.EndsWith("Fullness.g.cs"))); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void TableId_Has_String_Backing_And_Implicit_Conversion() |
| 52 | + { |
| 53 | + var result = GeneratorTestHelper.RunGenerator(ReadmeSource); |
| 54 | + var text = GetGeneratedText(result, "TableId.g.cs"); |
| 55 | + |
| 56 | + Assert.Contains("private readonly string _value;", text); |
| 57 | + Assert.Contains("public static implicit operator TableId(string value)", text); |
| 58 | + Assert.Contains("public static implicit operator string(TableId value)", text); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void PizzasEaten_Has_Int_Backing_And_Arithmetic() |
| 63 | + { |
| 64 | + var result = GeneratorTestHelper.RunGenerator(ReadmeSource); |
| 65 | + var text = GetGeneratedText(result, "PizzasEaten.g.cs"); |
| 66 | + |
| 67 | + Assert.Contains("private readonly int _value;", text); |
| 68 | + // ++ works via implicit conversion to int and back |
| 69 | + Assert.Contains("public static implicit operator PizzasEaten(int value)", text); |
| 70 | + Assert.Contains("public static implicit operator int(PizzasEaten value)", text); |
| 71 | + Assert.Contains("operator +", text); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void Fullness_Has_Comparison_And_Addition() |
| 76 | + { |
| 77 | + var result = GeneratorTestHelper.RunGenerator(ReadmeSource); |
| 78 | + var text = GetGeneratedText(result, "Fullness.g.cs"); |
| 79 | + |
| 80 | + Assert.Contains("private readonly double _value;", text); |
| 81 | + Assert.Contains("operator <", text); |
| 82 | + Assert.Contains("operator +", text); |
| 83 | + } |
| 84 | + |
| 85 | + private static string GetGeneratedText( |
| 86 | + Microsoft.CodeAnalysis.GeneratorDriverRunResult result, |
| 87 | + string hintNameSuffix) |
| 88 | + { |
| 89 | + return result.Results[0].GeneratedSources |
| 90 | + .Single(s => s.HintName.EndsWith(hintNameSuffix)) |
| 91 | + .SourceText.ToString(); |
| 92 | + } |
| 93 | +} |
0 commit comments