Skip to content
Open
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
4 changes: 4 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
<NuGetAuditMode>all</NuGetAuditMode>
<NuGetAuditLevel>low</NuGetAuditLevel>
</PropertyGroup>

<PropertyGroup>
<!--<DefineConstants>$(DefineConstants);TEMP_REQUIRE_IMPLEMENTATION_SETTER_NON_INTERFERENCE</DefineConstants>-->
</PropertyGroup>
</Project>
19 changes: 19 additions & 0 deletions PropertyChanged.Fody/Config/CheckForSetterNonInterferenceConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Linq;
using System.Xml;

public partial class ModuleWeaver
{
// default: false to preserve behavior from PropertyChanged.Fody 2.x
public bool EnsureNonInterferenceWithCustomSetterBehaviors = false;

public void ResolveCheckForSetterNonInterferenceConfig()
{
var value = Config?.Attributes("EnsureNonInterferenceWithCustomSetterBehaviors")
.Select(a => a.Value)
.SingleOrDefault();
if (value != null)
{
EnsureNonInterferenceWithCustomSetterBehaviors = XmlConvert.ToBoolean(value.ToLowerInvariant());
}
}
}
15 changes: 15 additions & 0 deletions PropertyChanged.sln
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PropertyChanged.Fody.Analyz
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PropertyChanged.Fody.Analyzer.Tests", "PropertyChanged.Fody.Analyzer.Tests\PropertyChanged.Fody.Analyzer.Tests.csproj", "{35A8A625-B010-4F9D-A901-9B07D9E13A16}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyWithSetterSideEffects", "TestAssemblies\AssemblyWithSetterSideEffects\AssemblyWithSetterSideEffects.csproj", "{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -338,6 +340,18 @@ Global
{35A8A625-B010-4F9D-A901-9B07D9E13A16}.Release|ARM.Build.0 = Release|Any CPU
{35A8A625-B010-4F9D-A901-9B07D9E13A16}.Release|x86.ActiveCfg = Release|Any CPU
{35A8A625-B010-4F9D-A901-9B07D9E13A16}.Release|x86.Build.0 = Release|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Debug|ARM.ActiveCfg = Debug|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Debug|ARM.Build.0 = Debug|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Debug|x86.ActiveCfg = Debug|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Debug|x86.Build.0 = Debug|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Release|Any CPU.Build.0 = Release|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Release|ARM.ActiveCfg = Release|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Release|ARM.Build.0 = Release|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Release|x86.ActiveCfg = Release|Any CPU
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -363,6 +377,7 @@ Global
{89CC72A8-43A9-4593-9FA5-61091C989B40} = {546A6338-E25E-4796-AF08-A4742E3BDF7B}
{9D3B5C35-6523-45D9-B11D-0FBB67C0866A} = {546A6338-E25E-4796-AF08-A4742E3BDF7B}
{10BDE166-0BFC-41D9-9326-805717A054B3} = {546A6338-E25E-4796-AF08-A4742E3BDF7B}
{E4E54CE2-DD54-41B9-BDC0-DF45FA575535} = {546A6338-E25E-4796-AF08-A4742E3BDF7B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ABD5CA78-3690-4D62-8642-3463D9FAE144}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<TargetFrameworks>net48;net6.0</TargetFrameworks>
<DisableFody>true</DisableFody>
</PropertyGroup>
</Project>
52 changes: 52 additions & 0 deletions TestAssemblies/AssemblyWithSetterSideEffects/Classes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.ComponentModel;

public class WithSideEffectBeforeValueAssignment :
INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new(propertyName));
}

string _property1;
public string Property1
{
get => _property1;
set
{
CallSideEffectBefore();
_property1 = value;
}
}


public int SideEffectBeforeCallCount { get; set; }
void CallSideEffectBefore() => SideEffectBeforeCallCount++;
}

public class WithSideEffectAfterValueAssignment :
INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new(propertyName));
}



string _property1;
public string Property1
{
get => _property1;
set
{
_property1 = value;
CallSideEffectAfter();
}
}

public int SideEffectAfterCallCount { get; set; }
void CallSideEffectAfter() => SideEffectAfterCallCount++;
}
99 changes: 99 additions & 0 deletions Tests/AssemblyWithSetterSideEffectsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
public class AssemblyWithSetterSideEffectsTests
{
static readonly string[] peVerifyIgnoreCodes =
{
#if NETCOREAPP
"0x80131869"
#endif
};

const string assemblyName = "AssemblyWithSetterSideEffects.dll";

[Theory]
#if TEMP_REQUIRE_IMPLEMENTATION_SETTER_NON_INTERFERENCE
[InlineData([true, "same", "same", 2])]
#endif
[InlineData([true, "different1", "different2", 2])]
[InlineData([false, "same", "same", 2])]
[InlineData([false, "different1", "different2", 2])]
public void CallsPreAssignmentSideEffect(bool checkForEquality, string firstAssignment, string secondAssignment, int expectedCallCount)
{
const string className = "WithSideEffectBeforeValueAssignment";

var weaver = new ModuleWeaver(){CheckForEquality = checkForEquality, EnsureNonInterferenceWithCustomSetterBehaviors = true };
var testResult = weaver.ExecuteTestRun(assemblyName, ignoreCodes: peVerifyIgnoreCodes);

var instance = testResult.GetInstance(className);

instance.Property1 = firstAssignment;
instance.Property1 = secondAssignment;

var callCount = (int)instance.SideEffectBeforeCallCount;
Assert.Equal(expectedCallCount, callCount);
}

[Theory]
#if TEMP_REQUIRE_IMPLEMENTATION_SETTER_NON_INTERFERENCE
[InlineData([true, "same", "same", 2])]
#endif
[InlineData([true, "different1", "different2", 2])]
[InlineData([false, "same", "same", 2])]
[InlineData([false, "different1", "different2", 2])]
public void CallsPostAssignmentSideEffect(bool checkForEquality, string firstAssignment, string secondAssignment, int expectedCallCount)
{
const string className = "WithSideEffectAfterValueAssignment";

var weaver = new ModuleWeaver() { CheckForEquality = checkForEquality, EnsureNonInterferenceWithCustomSetterBehaviors = true };
var testResult = weaver.ExecuteTestRun(assemblyName, ignoreCodes: peVerifyIgnoreCodes);

var instance = testResult.GetInstance(className);

instance.Property1 = firstAssignment;
instance.Property1 = secondAssignment;

var callCount = (int)instance.SideEffectAfterCallCount;
Assert.Equal(expectedCallCount, callCount);
}

[Theory]
[InlineData([true, "same", "same", 1])]
[InlineData([true, "different1", "different2", 2])]
[InlineData([false, "same", "same", 2])]
[InlineData([false, "different1", "different2", 2])]
public void CallsPreAssignmentSideEffectLegacy(bool checkForEquality, string firstAssignment, string secondAssignment, int expectedCallCount)
{
const string className = "WithSideEffectBeforeValueAssignment";

var weaver = new ModuleWeaver() { CheckForEquality = checkForEquality, EnsureNonInterferenceWithCustomSetterBehaviors = false };
var testResult = weaver.ExecuteTestRun(assemblyName, ignoreCodes: peVerifyIgnoreCodes);

var instance = testResult.GetInstance(className);

instance.Property1 = firstAssignment;
instance.Property1 = secondAssignment;

var callCount = (int)instance.SideEffectBeforeCallCount;
Assert.Equal(expectedCallCount, callCount);
}

[Theory]
[InlineData([true, "same", "same", 1])]
[InlineData([true, "different1", "different2", 2])]
[InlineData([false, "same", "same", 2])]
[InlineData([false, "different1", "different2", 2])]
public void CallsPostAssignmentSideEffectLegacy(bool checkForEquality, string firstAssignment, string secondAssignment, int expectedCallCount)
{
const string className = "WithSideEffectAfterValueAssignment";

var weaver = new ModuleWeaver() { CheckForEquality = checkForEquality, EnsureNonInterferenceWithCustomSetterBehaviors = false };
var testResult = weaver.ExecuteTestRun(assemblyName, ignoreCodes: peVerifyIgnoreCodes);

var instance = testResult.GetInstance(className);

instance.Property1 = firstAssignment;
instance.Property1 = secondAssignment;

var callCount = (int)instance.SideEffectAfterCallCount;
Assert.Equal(expectedCallCount, callCount);
}
}
48 changes: 48 additions & 0 deletions Tests/CheckForSetterNonInterferenceConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Xml.Linq;

public class CheckForSetterNonInterferenceConfigTests
{
[Fact]
public void False()
{
var xElement = XElement.Parse("<PropertyChanged EnsureNonInterferenceWithCustomSetterBehaviors='false'/>");
var weaver = new ModuleWeaver { Config = xElement };
weaver.ResolveCheckForSetterNonInterferenceConfig();
Assert.False(weaver.EnsureNonInterferenceWithCustomSetterBehaviors);
}

[Fact]
public void False0()
{
var xElement = XElement.Parse("<PropertyChanged EnsureNonInterferenceWithCustomSetterBehaviors='0'/>");
var weaver = new ModuleWeaver { Config = xElement };
weaver.ResolveCheckForSetterNonInterferenceConfig();
Assert.False(weaver.EnsureNonInterferenceWithCustomSetterBehaviors);
}

[Fact]
public void True()
{
var xElement = XElement.Parse("<PropertyChanged EnsureNonInterferenceWithCustomSetterBehaviors='True'/>");
var weaver = new ModuleWeaver { Config = xElement };
weaver.ResolveCheckForSetterNonInterferenceConfig();
Assert.True(weaver.EnsureNonInterferenceWithCustomSetterBehaviors);
}

[Fact]
public void True1()
{
var xElement = XElement.Parse("<PropertyChanged EnsureNonInterferenceWithCustomSetterBehaviors='1'/>");
var weaver = new ModuleWeaver { Config = xElement };
weaver.ResolveCheckForSetterNonInterferenceConfig();
Assert.True(weaver.EnsureNonInterferenceWithCustomSetterBehaviors);
}

[Fact]
public void Default()
{
var weaver = new ModuleWeaver();
weaver.ResolveCheckForSetterNonInterferenceConfig();
Assert.False(weaver.EnsureNonInterferenceWithCustomSetterBehaviors);
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
<ProjectReference Include="..\TestAssemblies\AssemblyWithStaticOnPropertyNameChanged\AssemblyWithStaticOnPropertyNameChanged.csproj" />
<ProjectReference Include="..\TestAssemblies\AssemblyWithTypeFilter\AssemblyWithTypeFilter.csproj" />
<ProjectReference Include="..\TestAssemblies\AssemblyWithInheritance\AssemblyWithInheritance.csproj" />
<ProjectReference Include="..\TestAssemblies\AssemblyWithSetterSideEffects\AssemblyWithSetterSideEffects.csproj" />
</ItemGroup>
</Project>