@@ -37,6 +37,39 @@ public readonly partial record struct Glyph(char Value)
3737 ScenarioExpect . True ( emit . Success , string . Join ( "\n " , emit . Diagnostics ) ) ;
3838 }
3939
40+ [ Scenario ( "GeneratesFlyweightCacheWithoutTryGet" ) ]
41+ [ Fact ]
42+ public void GeneratesFlyweightCacheWithoutTryGet ( )
43+ {
44+ const string source = """
45+ using PatternKit.Generators.Flyweight;
46+
47+ namespace TestNamespace;
48+
49+ [Flyweight(typeof(string), CacheTypeName = "TokenCache", GenerateTryGet = false)]
50+ public sealed partial record class Token(string Value)
51+ {
52+ [FlyweightFactory]
53+ private static Token Create(string key) => new(key);
54+ }
55+ """ ;
56+
57+ var comp = RoslynTestHelpers . CreateCompilation ( source , nameof ( GeneratesFlyweightCacheWithoutTryGet ) ) ;
58+ var gen = new FlyweightGenerator ( ) ;
59+ _ = RoslynTestHelpers . Run ( comp , gen , out var result , out var updated ) ;
60+
61+ ScenarioExpect . All ( result . Results , r => ScenarioExpect . Empty ( r . Diagnostics ) ) ;
62+
63+ var generated = result . Results . SelectMany ( r => r . GeneratedSources ) . Single ( s => s . HintName == "Token.Flyweight.g.cs" ) . SourceText . ToString ( ) ;
64+ ScenarioExpect . Contains ( "partial record class Token" , generated ) ;
65+ ScenarioExpect . Contains ( "public sealed partial class TokenCache" , generated ) ;
66+ ScenarioExpect . Contains ( "public global::TestNamespace.Token Get(string key)" , generated ) ;
67+ ScenarioExpect . DoesNotContain ( "public bool TryGet(" , generated ) ;
68+
69+ var emit = updated . Emit ( Stream . Null ) ;
70+ ScenarioExpect . True ( emit . Success , string . Join ( "\n " , emit . Diagnostics ) ) ;
71+ }
72+
4073 [ Scenario ( "ReportsMissingFactory" ) ]
4174 [ Fact ]
4275 public void ReportsMissingFactory ( )
@@ -83,6 +116,61 @@ public readonly partial record struct Glyph(char Value)
83116 ScenarioExpect . Contains ( diags , d => d . Id == "PKFLY006" ) ;
84117 }
85118
119+ [ Scenario ( "ReportsMultipleFlyweightFactories" ) ]
120+ [ Fact ]
121+ public void ReportsMultipleFlyweightFactories ( )
122+ {
123+ const string source = """
124+ using PatternKit.Generators.Flyweight;
125+
126+ namespace TestNamespace;
127+
128+ [Flyweight(typeof(string))]
129+ public readonly partial record struct Glyph(char Value)
130+ {
131+ [FlyweightFactory]
132+ private static Glyph Create(string key) => new(key[0]);
133+
134+ [FlyweightFactory]
135+ private static Glyph CreateAlternate(string key) => new(key[^1]);
136+ }
137+ """ ;
138+
139+ var comp = RoslynTestHelpers . CreateCompilation ( source , nameof ( ReportsMultipleFlyweightFactories ) ) ;
140+ var gen = new FlyweightGenerator ( ) ;
141+ _ = RoslynTestHelpers . Run ( comp , gen , out var result , out _ ) ;
142+
143+ var diags = result . Results . SelectMany ( r => r . Diagnostics ) ;
144+ ScenarioExpect . Contains ( diags , d => d . Id == "PKFLY003" ) ;
145+ }
146+
147+ [ Scenario ( "ReportsFlyweightCacheNameConflict" ) ]
148+ [ Fact ]
149+ public void ReportsFlyweightCacheNameConflict ( )
150+ {
151+ const string source = """
152+ using PatternKit.Generators.Flyweight;
153+
154+ namespace TestNamespace;
155+
156+ public sealed class GlyphFlyweightCache;
157+
158+ [Flyweight(typeof(string))]
159+ public readonly partial record struct Glyph(char Value)
160+ {
161+ [FlyweightFactory]
162+ private static Glyph Create(string key) => new(key[0]);
163+ }
164+ """ ;
165+
166+ var comp = RoslynTestHelpers . CreateCompilation ( source , nameof ( ReportsFlyweightCacheNameConflict ) ) ;
167+ var gen = new FlyweightGenerator ( ) ;
168+ _ = RoslynTestHelpers . Run ( comp , gen , out var result , out _ ) ;
169+
170+ var diags = result . Results . SelectMany ( r => r . Diagnostics ) ;
171+ ScenarioExpect . Contains ( diags , d => d . Id == "PKFLY005" ) ;
172+ }
173+
86174 [ Scenario ( "ReportsNonPartialAndNonStaticFactory" ) ]
87175 [ Fact ]
88176 public void ReportsNonPartialAndNonStaticFactory ( )
@@ -115,4 +203,36 @@ public readonly partial record struct NonStaticFactoryGlyph(char Value)
115203 ScenarioExpect . Contains ( diags , d => d . Id == "PKFLY001" ) ;
116204 ScenarioExpect . Contains ( diags , d => d . Id == "PKFLY004" ) ;
117205 }
206+
207+ [ Scenario ( "ReportsInvalidFlyweightFactoryForWrongKeyAndReturnTypes" ) ]
208+ [ Fact ]
209+ public void ReportsInvalidFlyweightFactoryForWrongKeyAndReturnTypes ( )
210+ {
211+ const string source = """
212+ using PatternKit.Generators.Flyweight;
213+
214+ namespace TestNamespace;
215+
216+ [Flyweight(typeof(string))]
217+ public readonly partial record struct WrongKeyGlyph(char Value)
218+ {
219+ [FlyweightFactory]
220+ private static WrongKeyGlyph Create(int key) => new((char)key);
221+ }
222+
223+ [Flyweight(typeof(string))]
224+ public readonly partial record struct WrongReturnGlyph(char Value)
225+ {
226+ [FlyweightFactory]
227+ private static string Create(string key) => key;
228+ }
229+ """ ;
230+
231+ var comp = RoslynTestHelpers . CreateCompilation ( source , nameof ( ReportsInvalidFlyweightFactoryForWrongKeyAndReturnTypes ) ) ;
232+ var gen = new FlyweightGenerator ( ) ;
233+ _ = RoslynTestHelpers . Run ( comp , gen , out var result , out _ ) ;
234+
235+ var diags = result . Results . SelectMany ( r => r . Diagnostics ) . ToArray ( ) ;
236+ ScenarioExpect . Equal ( 2 , diags . Count ( d => d . Id == "PKFLY004" ) ) ;
237+ }
118238}
0 commit comments