Skip to content

Commit

Permalink
don't generate setter for non-byRef parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
connorivy committed Dec 3, 2024
1 parent bd80d64 commit 2829461
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ internal class {this.GetArgCollectionName()} : OriginalArgBag<{this.paramTypes}>

for (int i = 0; i < this.methodSymbol.Parameters.Length; i++)
{
var param = this.methodSymbol.Parameters[i];
sb.Append(
$@"
public {this .methodSymbol.Parameters[i] .Type.ToFullReturnTypeString()} {this.methodSymbol.Parameters[i].Name} {{ get => this.Arg{i + 1}; set => this.Arg{i + 1} = value; }}"
public {this .methodSymbol.Parameters[i] .Type.ToFullReturnTypeString()} {param.Name} {{ get => this.Arg{i + 1};{(param.RefKind == RefKind.None ? "" : $" set => this.Arg{i + 1} = value;")} }}"
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,6 @@ Dictionary<string, AssertPropertyMetadata> assertMeta
}

public virtual string? GetIndexerType() => null;

public override StringBuilder AddOriginalCollectionType(StringBuilder sb) => sb;
}
20 changes: 20 additions & 0 deletions tests/MockMe.Tests.Overloads/ArgumentModifierTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using MockMe.Generated.MockMe.Tests.Overloads;
using Xunit;

namespace MockMe.Tests.Overloads;
Expand Down Expand Up @@ -38,4 +39,23 @@ public void OutKeyword_WhenConfiguredInCallbackCall_ShouldSetTheCorrectValue()
Assert.Equal(99, result);
mock.Assert.OutArgument(out _).WasCalled();
}

[Fact]
public void ParametersNotPassedByReference_ShouldNotHaveASetter()
{
var mock = Mock.Me<AllOverloads>(default(AllOverloads));

var outParamType = typeof(AllOverloadsMockSetup.OutArgument_OutInt32Collection);
var regularParamType = typeof(AllOverloadsMockSetup.OutArgument_Int32Collection);

var outParamGetter = outParamType.GetMethod("get_arg");
var outParamSetter = outParamType.GetMethod("set_arg");
Assert.NotNull(outParamGetter);
Assert.NotNull(outParamSetter);

var regularParamGetter = regularParamType.GetMethod("get_arg");
var regularParamSetter = regularParamType.GetMethod("set_arg");
Assert.NotNull(regularParamGetter);
Assert.Null(regularParamSetter); // this type should NOT have a setter because that wouldn't make sense
}
}
2 changes: 2 additions & 0 deletions wiki/AdvancedUsage.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ MyCoolClass myClass = mock;
myClass.TryGetValue(99, out var outValue); // outValue is "my out value"
```

**_NOTE:_** The `arg.val` will only be settable if the method parameter is passed by reference, such as with ref and out parameters because assigning to this parameter is meaningless for a normal method parameter.

0 comments on commit 2829461

Please sign in to comment.