|
1 | 1 | namespace MiniMock.Builders; |
2 | 2 |
|
3 | | -using System; |
| 3 | +using System.Collections.Generic; |
4 | 4 | using System.Linq; |
5 | 5 | using Microsoft.CodeAnalysis; |
| 6 | +using Util; |
6 | 7 |
|
7 | | -internal class ConstructorBuilder(ISymbol target) |
| 8 | +/// <summary> |
| 9 | +/// Represents a builder for constructing mock constructors. |
| 10 | +/// </summary> |
| 11 | +internal class ConstructorBuilder : ISymbolBuilder |
8 | 12 | { |
9 | | - private readonly Func<Accessibility, bool> accessibilityFilter = accessibility => accessibility == Accessibility.Public || accessibility == Accessibility.Protected; |
| 13 | + /// <summary> |
| 14 | + /// Tries to build constructors for the given symbols. |
| 15 | + /// </summary> |
| 16 | + /// <param name="builder">The code builder to add the constructors to.</param> |
| 17 | + /// <param name="symbols">The symbols to build constructors for.</param> |
| 18 | + /// <returns>True if constructors were built; otherwise, false.</returns> |
| 19 | + public bool TryBuild(CodeBuilder builder, IGrouping<string, ISymbol> symbols) |
| 20 | + { |
| 21 | + var first = symbols.First(); |
| 22 | + if (first is IMethodSymbol { MethodKind: MethodKind.Constructor }) |
| 23 | + { |
| 24 | + return BuildConstructors(builder, first.ContainingSymbol, symbols.OfType<IMethodSymbol>()); |
| 25 | + } |
10 | 26 |
|
11 | | - public void Build(CodeBuilder builder, string fullName, string name) |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Builds constructors for the specified target and adds them to the code builder. |
| 32 | + /// </summary> |
| 33 | + /// <param name="builder">The code builder to add the constructors to.</param> |
| 34 | + /// <param name="target">The target symbol for which to build constructors.</param> |
| 35 | + /// <param name="constructors">The constructors to build.</param> |
| 36 | + /// <returns>True if constructors were built; otherwise, false.</returns> |
| 37 | + private static bool BuildConstructors(CodeBuilder builder, ISymbol target, IEnumerable<IMethodSymbol> constructors) |
12 | 38 | { |
13 | | - var symbol = (INamedTypeSymbol)target; |
| 39 | + var fullName = target.ToString(); |
| 40 | + var name = "MockOf_" + target.Name; |
14 | 41 |
|
15 | | - var constructors = symbol.Constructors |
16 | | - .Where(c => this.accessibilityFilter(c.DeclaredAccessibility)) |
17 | | - .ToArray(); |
| 42 | + var typeArguments = ((INamedTypeSymbol)target).TypeArguments; |
| 43 | + if (typeArguments.Length > 0) |
| 44 | + { |
| 45 | + var types = string.Join(", ", typeArguments.Select(t => t.Name)); |
| 46 | + name = $"MockOf_{target.Name}<{types}>"; |
| 47 | + } |
18 | 48 |
|
19 | 49 | builder.Add("#region Constructors"); |
20 | 50 |
|
21 | | - if (constructors.Length == 0 || constructors.Any(t => t.Parameters.Length == 0)) |
| 51 | + foreach (var constructor in constructors) |
22 | 52 | { |
| 53 | + var parameterList = constructor.Parameters.ToString(p => $"{p.Type} {p.Name}, ", ""); |
| 54 | + var argumentList = constructor.Parameters.ToString(p => p.Name); |
| 55 | + |
| 56 | + var parameterNames = constructor.Parameters.ToString(p => p.Name + ", ", ""); |
| 57 | + |
23 | 58 | builder.Add($$""" |
24 | | - internal protected MockOf_{{target.Name}}(System.Action<Config>? config = null) { |
| 59 | + internal protected MockOf_{{target.Name}}({{parameterList}}System.Action<Config>? config = null) : base({{argumentList}}) { |
25 | 60 | var result = new Config(this); |
26 | 61 | config = config ?? new System.Action<Config>(t => { }); |
27 | 62 | config.Invoke(result); |
28 | 63 | _config = result; |
29 | 64 | } |
30 | 65 |
|
31 | | - public static {{fullName}} Create(System.Action<Config>? config = null) => new {{name}}(config); |
| 66 | + public static {{fullName}} Create({{parameterList}}System.Action<Config>? config = null) => new {{name}}({{parameterNames}}config); |
32 | 67 | """); |
33 | 68 | } |
34 | 69 |
|
35 | | - foreach (var constructor in constructors.Where(t => t.Parameters.Length > 0)) |
36 | | - { |
37 | | - var parameters = constructor.Parameters.Select(p => $"{p.Type} {p.Name}").ToArray(); |
38 | | - var parameterList = string.Join(", ", parameters); |
39 | | - var parameterNames = constructor.Parameters.Select(p => p.Name).ToArray(); |
40 | | - var parameterNamesList = string.Join(", ", parameterNames); |
| 70 | + builder.Add("#endregion"); |
41 | 71 |
|
42 | | - builder.Add($$""" |
43 | | - internal protected MockOf_{{target.Name}}({{parameterList}}, System.Action<Config>? config = null) : base({{parameterNamesList}}) { |
44 | | - var result = new Config(this); |
45 | | - config = config ?? new System.Action<Config>(t => { }); |
46 | | - config.Invoke(result); |
47 | | - _config = result; |
48 | | - } |
| 72 | + return true; |
| 73 | + } |
49 | 74 |
|
50 | | - public static {{fullName}} Create({{parameterList}}, System.Action<Config>? config = null) => new {{name}}({{parameterNamesList}}, config); |
51 | | - """); |
| 75 | + /// <summary> |
| 76 | + /// Builds an empty constructor for the specified target. |
| 77 | + /// </summary> |
| 78 | + /// <param name="target">The target symbol for which to build an empty constructor.</param> |
| 79 | + /// <returns>A code builder containing the empty constructor.</returns> |
| 80 | + public static CodeBuilder BuildEmptyConstructor(ISymbol target) |
| 81 | + { |
| 82 | + var fullName = target.ToString(); |
| 83 | + var name = "MockOf_" + target.Name; |
| 84 | + |
| 85 | + var typeArguments = ((INamedTypeSymbol)target).TypeArguments; |
| 86 | + if (typeArguments.Length > 0) |
| 87 | + { |
| 88 | + var types = string.Join(", ", typeArguments.Select(t => t.Name)); |
| 89 | + name = $"MockOf_{target.Name}<{types}>"; |
52 | 90 | } |
53 | 91 |
|
54 | | - builder.Add("#endregion"); |
| 92 | + CodeBuilder builder = new(); |
| 93 | + |
| 94 | + builder.Add($$""" |
| 95 | + #region Constructor |
| 96 | +
|
| 97 | + internal protected MockOf_{{target.Name}}(System.Action<Config>? config = null) { |
| 98 | + var result = new Config(this); |
| 99 | + config = config ?? new System.Action<Config>(t => { }); |
| 100 | + config.Invoke(result); |
| 101 | + _config = result; |
| 102 | + } |
| 103 | +
|
| 104 | + public static {{fullName}} Create(System.Action<Config>? config = null) => new {{name}}(config); |
| 105 | +
|
| 106 | + #endregion |
| 107 | + """); |
| 108 | + |
| 109 | + return builder; |
55 | 110 | } |
56 | 111 | } |
0 commit comments