Skip to content

Commit 40c82ce

Browse files
committed
Add one more generator test for enum values
1 parent 3742195 commit 40c82ce

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsCodegen.cs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,161 @@ public object? A
14001400
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
14011401
}
14021402

1403+
[TestMethod]
1404+
public void ObservablePropertyWithForwardedAttributesWithEnumWithCombinedValues_WorksCorrectly()
1405+
{
1406+
string source = """
1407+
using System.ComponentModel;
1408+
using CommunityToolkit.Mvvm.ComponentModel;
1409+
1410+
#nullable enable
1411+
1412+
namespace MyApp;
1413+
1414+
partial class MyViewModel : ObservableObject
1415+
{
1416+
[ObservableProperty]
1417+
[property: DefaultValue(NegativeEnum1.A)]
1418+
[property: DefaultValue(NegativeEnum1.B)]
1419+
[property: DefaultValue((NegativeEnum1)42)]
1420+
[property: DefaultValue((NegativeEnum1)OtherClass.MyNumber)]
1421+
[property: DefaultValue(OtherClass.MyEnumValue)]
1422+
[property: DefaultValue(NegativeEnum2.A)]
1423+
[property: DefaultValue(NegativeEnum2.B)]
1424+
[property: DefaultValue((NegativeEnum2)42)]
1425+
[property: DefaultValue((NegativeEnum2)OtherClass.MyNumber)]
1426+
[property: DefaultValue((NegativeEnum2)(int)OtherClass.MyEnumValue)]
1427+
[property: DefaultValue(NegativeEnum3.A)]
1428+
[property: DefaultValue(NegativeEnum3.B)]
1429+
[property: DefaultValue((NegativeEnum3)42)]
1430+
[property: DefaultValue((NegativeEnum3)OtherClass.MyNumber)]
1431+
[property: DefaultValue((NegativeEnum3)(int)OtherClass.MyEnumValue)]
1432+
[property: DefaultValue(NegativeEnum4.A)]
1433+
[property: DefaultValue(NegativeEnum4.B)]
1434+
[property: DefaultValue((NegativeEnum4)42)]
1435+
[property: DefaultValue((NegativeEnum4)unchecked((sbyte)OtherClass.MyNumber))]
1436+
[property: DefaultValue((NegativeEnum4)(int)OtherClass.MyEnumValue)]
1437+
private object? a;
1438+
}
1439+
1440+
public static class OtherClass
1441+
{
1442+
public const int MyNumber = 456;
1443+
public const NegativeEnum1 MyEnumValue = NegativeEnum1.C;
1444+
}
1445+
1446+
public class DefaultValueAttribute : Attribute
1447+
{
1448+
public DefaultValueAttribute(object value)
1449+
{
1450+
}
1451+
}
1452+
1453+
public enum NegativeEnum1
1454+
{
1455+
A = 0,
1456+
B = -1073741824,
1457+
C = 123
1458+
}
1459+
1460+
public enum NegativeEnum2 : long
1461+
{
1462+
A = 0,
1463+
B = long.MinValue
1464+
}
1465+
1466+
public enum NegativeEnum3 : short
1467+
{
1468+
A = 1,
1469+
B = -1234
1470+
}
1471+
1472+
public enum NegativeEnum4 : sbyte
1473+
{
1474+
A = 1,
1475+
B = -1
1476+
}
1477+
""";
1478+
1479+
string result = """
1480+
// <auto-generated/>
1481+
#pragma warning disable
1482+
#nullable enable
1483+
namespace MyApp
1484+
{
1485+
/// <inheritdoc/>
1486+
partial class MyViewModel
1487+
{
1488+
/// <inheritdoc cref="a"/>
1489+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1490+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
1491+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum1)0)]
1492+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum1)-1073741824)]
1493+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum1)42)]
1494+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum1)456)]
1495+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum1)123)]
1496+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum2)0)]
1497+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum2)-9223372036854775808)]
1498+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum2)42)]
1499+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum2)456)]
1500+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum2)123)]
1501+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum3)1)]
1502+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum3)-1234)]
1503+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum3)42)]
1504+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum3)456)]
1505+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum3)123)]
1506+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum4)1)]
1507+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum4)-1)]
1508+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum4)42)]
1509+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum4)-56)]
1510+
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum4)123)]
1511+
public object? A
1512+
{
1513+
get => a;
1514+
set
1515+
{
1516+
if (!global::System.Collections.Generic.EqualityComparer<object?>.Default.Equals(a, value))
1517+
{
1518+
OnAChanging(value);
1519+
OnAChanging(default, value);
1520+
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.A);
1521+
a = value;
1522+
OnAChanged(value);
1523+
OnAChanged(default, value);
1524+
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.A);
1525+
}
1526+
}
1527+
}
1528+
1529+
/// <summary>Executes the logic for when <see cref="A"/> is changing.</summary>
1530+
/// <param name="value">The new property value being set.</param>
1531+
/// <remarks>This method is invoked right before the value of <see cref="A"/> is changed.</remarks>
1532+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1533+
partial void OnAChanging(object? value);
1534+
/// <summary>Executes the logic for when <see cref="A"/> is changing.</summary>
1535+
/// <param name="oldValue">The previous property value that is being replaced.</param>
1536+
/// <param name="newValue">The new property value being set.</param>
1537+
/// <remarks>This method is invoked right before the value of <see cref="A"/> is changed.</remarks>
1538+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1539+
partial void OnAChanging(object? oldValue, object? newValue);
1540+
/// <summary>Executes the logic for when <see cref="A"/> just changed.</summary>
1541+
/// <param name="value">The new property value that was set.</param>
1542+
/// <remarks>This method is invoked right after the value of <see cref="A"/> is changed.</remarks>
1543+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1544+
partial void OnAChanged(object? value);
1545+
/// <summary>Executes the logic for when <see cref="A"/> just changed.</summary>
1546+
/// <param name="oldValue">The previous property value that was replaced.</param>
1547+
/// <param name="newValue">The new property value that was set.</param>
1548+
/// <remarks>This method is invoked right after the value of <see cref="A"/> is changed.</remarks>
1549+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.2.0.0")]
1550+
partial void OnAChanged(object? oldValue, object? newValue);
1551+
}
1552+
}
1553+
""";
1554+
1555+
VerifyGenerateSources(source, new[] { new ObservablePropertyGenerator() }, ("MyApp.MyViewModel.g.cs", result));
1556+
}
1557+
14031558
// See https://github.com/CommunityToolkit/dotnet/issues/632
14041559
[TestMethod]
14051560
public void RelayCommandMethodWithPartialDeclarations_TriggersCorrectly()

0 commit comments

Comments
 (0)