Skip to content

Commit f67b905

Browse files
authored
Refactor (#42)
* Refactor, plus added more tests * Refactor * Refactor
1 parent 5781383 commit f67b905

18 files changed

Lines changed: 289 additions & 209 deletions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MattSourceGenHelpers.Tests")]
2+
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MattSourceGenHelpers.Generators")]
Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,10 @@
1-
namespace MattSourceGenHelpers.Abstractions;
1+
using JetBrains.Annotations;
22

3-
public static class Generate
4-
{
5-
public static IGeneratorsFactory CurrentGenerator { get; set; } = new RecordingGeneratorsFactory();
6-
7-
public static IMethodBuilder Method() => new MethodBuilder(CurrentGenerator);
8-
}
9-
10-
public interface IMethodBuilder
11-
{
12-
IMethodBuilder<TArg1> WithParameter<TArg1>();
13-
IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>();
14-
}
15-
16-
public interface IMethodBuilder<TArg1>
17-
{
18-
IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>();
19-
}
20-
21-
public class MethodBuilder : IMethodBuilder
22-
{
23-
private readonly IGeneratorsFactory _generatorsFactory;
24-
25-
public MethodBuilder(IGeneratorsFactory generatorsFactory)
26-
{
27-
_generatorsFactory = generatorsFactory;
28-
}
29-
30-
public IMethodBuilder<TArg1> WithParameter<TArg1>() => new MethodBuilder<TArg1>(_generatorsFactory);
31-
32-
public IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>() => _generatorsFactory.CreateImplementation<TReturnType>();
33-
}
3+
namespace MattSourceGenHelpers.Abstractions;
344

35-
public class MethodBuilder<TArg1> : IMethodBuilder<TArg1>
36-
{
37-
private readonly IGeneratorsFactory _generatorsFactory;
38-
39-
public MethodBuilder(IGeneratorsFactory generatorsFactory)
40-
{
41-
_generatorsFactory = generatorsFactory;
42-
}
43-
44-
public IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>() => _generatorsFactory.CreateImplementation<TArg1, TReturnType>();
45-
}
46-
47-
public class EmptyGeneratorsFactory : IGeneratorsFactory
48-
{
49-
public IMethodImplementationGenerator CreateImplementation() => new EmptyMethodImplementationGenerator();
50-
public IMethodImplementationGenerator<TReturnType> CreateImplementation<TReturnType>() => new EmptyMethodImplementationGenerator<TReturnType>();
51-
public IMethodImplementationGenerator<TArg1, TReturnType> CreateImplementation<TArg1, TReturnType>() => new EmptyMethodImplementationGenerator<TArg1, TReturnType>();
52-
}
53-
54-
public class EmptyMethodImplementationGenerator : IMethodImplementationGenerator
55-
{
56-
public IMethodImplementationGenerator WithBody(Action body) => this;
57-
public IMethodImplementationGenerator WithBody(Func<object> body) => this;
58-
}
59-
60-
public class EmptyMethodImplementationGenerator<TReturnType> : IMethodImplementationGenerator<TReturnType>
61-
{
62-
public IMethodImplementationGenerator UseBody(Func<object> body) => this;
63-
}
64-
65-
public class EmptyMethodImplementationGenerator<TArg1, TReturnType> : IMethodImplementationGenerator<TArg1, TReturnType>
5+
public static class Generate
666
{
67-
public IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> WithSwitchBody() =>
68-
new RecordingMethodImplementationGeneratorSwitchBody<TArg1, TReturnType>(new SwitchBodyRecord());
69-
public IMethodImplementationGenerator WithBody(Action body) => this;
70-
public IMethodImplementationGenerator WithBody(Func<object> body) => this;
71-
}
7+
internal static IGeneratorsFactory CurrentGenerator { get; [UsedImplicitly] set; } = new MockGeneratorsFactory();
728

73-
public interface IGeneratorsFactory
74-
{
75-
IMethodImplementationGenerator CreateImplementation();
76-
IMethodImplementationGenerator<TReturnType> CreateImplementation<TReturnType>();
77-
IMethodImplementationGenerator<TArg1, TReturnType> CreateImplementation<TArg1, TReturnType>();
78-
}
9+
public static IMethodBuilder Method() => CurrentGenerator.ForMethod();
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace MattSourceGenHelpers.Abstractions;
2+
3+
public interface IGeneratorsFactory
4+
{
5+
IMethodBuilder ForMethod();
6+
7+
IMethodImplementationGenerator<TReturnType> CreateImplementation<TReturnType>();
8+
IMethodImplementationGenerator<TArg1, TReturnType> CreateImplementation<TArg1, TReturnType>();
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace MattSourceGenHelpers.Abstractions;
2+
3+
public interface IMethodBuilder
4+
{
5+
IMethodBuilder<TArg1> WithParameter<TArg1>();
6+
IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>();
7+
}
8+
9+
public interface IMethodBuilder<TArg1>
10+
{
11+
IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>();
12+
}

MattSourceGenHelpers.Abstractions/IMethodGenerator.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
namespace MattSourceGenHelpers.Abstractions;
1+
using JetBrains.Annotations;
22

3-
public interface IMethodImplementationGenerator<TReturnType> : IMethodImplementationGenerator
3+
namespace MattSourceGenHelpers.Abstractions;
4+
5+
public interface IMethodImplementationGenerator<[UsedImplicitly] TReturnType> : IMethodImplementationGenerator
46
{
5-
IMethodImplementationGenerator UseBody(Func<object> body);
7+
IMethodImplementationGenerator UseBody([UsedImplicitly] Func<object> body);
68
}
79

810
public interface IMethodImplementationGenerator<TArg1, TReturnType> : IMethodImplementationGenerator
911
{
1012
IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> WithSwitchBody();
1113
}
1214

13-
public interface IMethodImplementationGeneratorVoid
14-
{
15-
IMethodImplementationGenerator CompileTimeBody(Action func);
16-
IMethodImplementationGenerator RuntimeBody(Action func);
17-
}
18-
1915
public interface IMethodImplementationGenerator;

MattSourceGenHelpers.Abstractions/Integer.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace MattSourceGenHelpers.Abstractions;
2+
3+
public class MethodBuilder(IGeneratorsFactory generatorsFactory) : IMethodBuilder
4+
{
5+
public IMethodBuilder<TArg1> WithParameter<TArg1>() => new MethodBuilder<TArg1>(generatorsFactory);
6+
7+
public IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>() => generatorsFactory.CreateImplementation<TReturnType>();
8+
}
9+
10+
public class MethodBuilder<TArg1>(IGeneratorsFactory generatorsFactory) : IMethodBuilder<TArg1>
11+
{
12+
public IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>() => generatorsFactory.CreateImplementation<TArg1, TReturnType>();
13+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
namespace MattSourceGenHelpers.Abstractions;
2+
3+
public class MockGeneratorsFactory : IGeneratorsFactory
4+
{
5+
public IMethodBuilder ForMethod() => new MockMethodBuilder();
6+
7+
public IMethodImplementationGenerator<TReturnType> CreateImplementation<TReturnType>() => new MockMethodImplementationGenerator<TReturnType>();
8+
9+
public IMethodImplementationGenerator<TArg1, TReturnType> CreateImplementation<TArg1, TReturnType>() =>
10+
new MockMethodImplementationGenerator<TArg1, TReturnType>();
11+
}
12+
13+
public class MockMethodImplementationGenerator<TReturnType> : IMethodImplementationGenerator<TReturnType>
14+
{
15+
public IMethodImplementationGenerator UseBody(Func<object> body) => this;
16+
}
17+
18+
public class MockMethodImplementationGenerator<TArg1, TReturnType> : IMethodImplementationGenerator<TArg1, TReturnType>
19+
{
20+
public IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> WithSwitchBody() =>
21+
new MockMethodImplementationGeneratorSwitchBody<TArg1, TReturnType>();
22+
}
23+
24+
public class MockMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> : IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType>
25+
{
26+
public IMethodImplementationGeneratorSwitchBodyCase<TArg1, TReturnType> ForCases(params object[] cases)
27+
=> new MockMethodImplementationGeneratorSwitchBodyCase<TArg1, TReturnType>();
28+
29+
public IMethodImplementationGeneratorSwitchBodyDefaultCase<TArg1, TReturnType> ForDefaultCase()
30+
=> new MockMethodImplementationGeneratorSwitchBodyDefaultCase<TArg1, TReturnType>();
31+
}
32+
33+
public class MockMethodImplementationGeneratorSwitchBodyDefaultCase<TArg1, TReturnType> : IMethodImplementationGeneratorSwitchBodyDefaultCase<TArg1,
34+
TReturnType>
35+
{
36+
public IMethodImplementationGenerator<TArg1, TReturnType> ReturnConstantValue(Func<TArg1, TReturnType> func)
37+
=> new MockMethodImplementationGenerator<TArg1, TReturnType>();
38+
39+
public IMethodImplementationGenerator<TArg1, TReturnType> UseBody(Func<TArg1, Func<TReturnType>> func)
40+
=> new MockMethodImplementationGenerator<TArg1, TReturnType>();
41+
}
42+
43+
public class MockMethodImplementationGeneratorSwitchBodyCase<TArg1, TReturnType> : IMethodImplementationGeneratorSwitchBodyCase<TArg1, TReturnType>
44+
{
45+
public IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> ReturnConstantValue(Func<TArg1, TReturnType> constantValueFactory)
46+
=> new MockMethodImplementationGeneratorSwitchBody<TArg1, TReturnType>();
47+
48+
public IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> UseBody(Func<TArg1, Action<TReturnType>> body)
49+
=> new MockMethodImplementationGeneratorSwitchBody<TArg1, TReturnType>();
50+
}
51+
52+
public class MockMethodBuilder : IMethodBuilder
53+
{
54+
public IMethodBuilder<TArg1> WithParameter<TArg1>() => new MockMethodBuilder<TArg1>();
55+
56+
public IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>() => new MockImplementationGenerator<TReturnType>();
57+
}
58+
59+
public class MockImplementationGenerator<TReturnType> : IMethodImplementationGenerator<TReturnType>
60+
{
61+
public IMethodImplementationGenerator UseBody(Func<object> body) => this;
62+
}
63+
64+
public class MockMethodBuilder<TArg1Input> : IMethodBuilder<TArg1Input>
65+
{
66+
public IMethodImplementationGenerator<TArg1Input, TReturnType> WithReturnType<TReturnType>()
67+
=> new MockMethodImplementationGenerator<TArg1Input, TReturnType>();
68+
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1+
using JetBrains.Annotations;
2+
13
namespace MattSourceGenHelpers.Abstractions;
24

35
/// <summary>
46
/// Thrown when a partial method is called during source generation.
57
/// Partial methods cannot be invoked inside generator methods because they are the
68
/// methods being generated — they do not have an implementation yet at generation time.
79
/// </summary>
8-
public class PartialMethodCalledDuringGenerationException : InvalidOperationException
9-
{
10-
public PartialMethodCalledDuringGenerationException(string methodName, string typeName)
11-
: base(
12-
$"Partial method '{typeName}.{methodName}' was called during source generation. " +
13-
$"Partial methods cannot be invoked inside generator methods because their implementations " +
14-
$"are what is being generated. Remove the call to '{methodName}' from your generator method.")
15-
{
16-
}
17-
}
10+
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedWithFixedConstructorSignature)]
11+
public class PartialMethodCalledDuringGenerationException(string methodName, string typeName) : InvalidOperationException(
12+
$"Partial method '{typeName}.{methodName}' was called during source generation. " +
13+
$"Partial methods cannot be invoked inside generator methods because their implementations " +
14+
$"are what is being generated. Remove the call to '{methodName}' from your generator method.");

0 commit comments

Comments
 (0)