Skip to content

Commit dc9a36c

Browse files
committed
feat: pizza example, maybe needs work
1 parent a82a697 commit dc9a36c

File tree

3 files changed

+117
-5
lines changed

3 files changed

+117
-5
lines changed

NewType.Generator/NUGET.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,37 @@ dotnet add package newtype
1212

1313
## Usage
1414

15-
#### Basic: strongly typed `string`-names, `int`-IDs, and `int`-counts
15+
#### Basic: typed IDs and counts
1616
```csharp
1717
using newtype;
1818
19+
[newtype<string>]
20+
public readonly partial struct TableId;
21+
1922
[newtype<int>]
20-
public readonly partial struct Pizzas;
23+
public readonly partial struct PizzasEaten;
2124
2225
[newtype<double>]
2326
public readonly partial struct Fullness;
27+
28+
class Guest
29+
{
30+
TableId table = "Table 1";
31+
PizzasEaten pizzasEaten;
32+
Fullness fullness;
33+
34+
public void fillEmUp(Fullness threshold)
35+
{
36+
while (fullness < threshold)
37+
{
38+
pizzasEaten++;
39+
fullness += 0.1;
40+
}
41+
}
42+
}
2443
```
2544
26-
#### Typical: quantities backed by the same data type.
45+
#### Typical: quantities backed by the same data type but distinct domain semantics
2746
*For example, forces, velocities, positions, etc. all lose their semantics when expressed as `Vector3`*
2847
```csharp
2948
using System.Numerics;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dotnet add package newtype
1818

1919
## Usage
2020

21-
#### Basic: strongly typed `string`-names, `int`-IDs, and `int`-counts
21+
#### Basic: typed IDs and counts
2222
```csharp
2323
using newtype;
2424

@@ -48,7 +48,7 @@ class Guest
4848
}
4949
```
5050

51-
#### Typical: quantities backed by the same data type.
51+
#### Typical: quantities backed by the same data type but distinct domain semantics
5252
*For example, forces, velocities, positions, etc. all lose their semantics when expressed as `Vector3`*
5353
```csharp
5454
using System.Numerics;

0 commit comments

Comments
 (0)