Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Source/Mockolate.SourceGenerators/Entities/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public Method(IMethodSymbol methodSymbol, List<Method>? alreadyDefinedMethods, I
UseOverride = methodSymbol.IsVirtual || methodSymbol.IsAbstract;
IsAbstract = methodSymbol.IsAbstract;
IsStatic = methodSymbol.IsStatic;
IsInitOnly = methodSymbol.IsInitOnly;
ReturnType = methodSymbol.ReturnsVoid ? Type.Void : Type.From(methodSymbol.ReturnType);
Name = Helpers.EscapeIfKeyword(methodSymbol.ExplicitInterfaceImplementations.Length > 0 ? methodSymbol.ExplicitInterfaceImplementations[0].Name : methodSymbol.Name);
ContainingType = methodSymbol.ContainingType.ToDisplayString(Helpers.TypeDisplayFormat);
Expand Down Expand Up @@ -51,6 +52,7 @@ public Method(IMethodSymbol methodSymbol, List<Method>? alreadyDefinedMethods, I
public bool UseOverride { get; }
public bool IsAbstract { get; }
public bool IsStatic { get; }
public bool IsInitOnly { get; }
public bool IsProtected => Accessibility is Accessibility.Protected or Accessibility.ProtectedOrInternal;

public MemberType MemberType => (IsStatic, IsProtected) switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2646,7 +2646,7 @@ property.IndexerParameters is not null
sb.Append(property.Setter.Accessibility.ToVisibilityString()).Append(' ');
}

sb.AppendLine("set");
sb.AppendLine(property.Setter.IsInitOnly ? "init" : "set");
sb.AppendLine("\t\t\t{");

// Ref-struct-keyed indexer setter: dispatches through
Expand Down Expand Up @@ -2702,7 +2702,7 @@ property.IndexerParameters is not null
.Append(", value);").AppendLine();
}

if (!property.IsStatic)
if (!property.IsStatic && !property.Setter.IsInitOnly)
{
sb.Append("\t\t\t\tif (").Append(mockRegistry).Append(".Wraps is ").Append(className)
.Append(" wraps)").AppendLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,45 @@ public sealed partial class ClassTests
{
public sealed class PropertiesTests
{
[Fact]
public async Task InitOnlyProperty_ShouldEmitInitAccessorAndCompile()
{
GeneratorResult result = Generator
.Run("""
using Mockolate;

namespace MyCode;
public class Program
{
public static void Main(string[] args)
{
_ = IMyService.CreateMock();
}
}

public interface IMyService
{
string Name { get; init; }
}
""");

await That(result.Diagnostics).IsEmpty();
await That(result.Sources["Mock.IMyService.g.cs"])
.Contains("""
public string Name
{
get
{
return this.MockRegistry.GetPropertyFast<string>(global::Mockolate.Mock.IMyService.MemberId_Name_Get, global::Mockolate.Mock.IMyService.PropertyAccess_Name_Get, static b => b.DefaultValue.Generate(default(string)!), this.MockRegistry.Wraps is not global::MyCode.IMyService wraps ? null : () => wraps.Name);
}
init
{
this.MockRegistry.SetPropertyFast<string>(global::Mockolate.Mock.IMyService.MemberId_Name_Get, global::Mockolate.Mock.IMyService.MemberId_Name_Set, "global::MyCode.IMyService.Name", value);
}
}
""").IgnoringNewlineStyle();
}

[Fact]
public async Task MultipleImplementations_ShouldOnlyHaveOneExplicitImplementation()
{
Expand Down