Skip to content

Commit

Permalink
capabilities: fix assignment to default value of non-standard DotvvmP…
Browse files Browse the repository at this point in the history
…roperty

There was an inverted condition when compared to SetValueDirect.
In most cases, SetValueDirect is used, except when the property
is a DotvvmProperty with overriden GetValue/SetValue or is inherited
  • Loading branch information
exyi committed Oct 27, 2024
1 parent 1031039 commit 583bf7d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void SetOptionalValueOrBindingSlow<T>(DotvvmBindableObject c, Dotv
public static void SetValueOrBindingSlow<T>(DotvvmBindableObject c, DotvvmProperty p, ValueOrBinding<T> val)
{
var boxedVal = val.UnwrapToObject();
if (Object.Equals(boxedVal, p.DefaultValue) && c.IsPropertySet(p))
if (Object.Equals(boxedVal, p.DefaultValue) && !c.IsPropertySet(p))
{
// setting to default value and the property is not set -> do nothing
}
Expand Down
52 changes: 52 additions & 0 deletions src/Tests/Runtime/CapabilityPropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,58 @@ public void BitMoreComplexCapability_InheritedProperties()
Assert.AreEqual(null, control2.Nullable);
}

[DataTestMethod]
[DataRow(typeof(TestControl6))]
[DataRow(typeof(TestControlFallbackProps))]
[DataRow(typeof(TestControlInheritedProps))]
public void BitMoreComplexCapability_SetDefaultValue(Type controlType)
{
var control1 = (DotvvmBindableObject)Activator.CreateInstance(controlType);
var capProp = DotvvmCapabilityProperty.Find(controlType, typeof(BitMoreComplexCapability));
control1.SetValue(capProp, new BitMoreComplexCapability { NotNullable = 30, ValueOrBinding = new((int?)null) }); // default
// XAssert.Empty(control1.Properties);
Assert.AreEqual(30, control1.GetCapability<BitMoreComplexCapability>().NotNullable);
Assert.AreEqual(null, control1.GetCapability<BitMoreComplexCapability>().ValueOrBinding.GetValue());

control1.SetProperty("NotNullable", 1);
control1.SetProperty("ValueOrBinding", 2);
Assert.AreEqual(1, control1.GetCapability<BitMoreComplexCapability>().NotNullable);
Assert.AreEqual(2, control1.GetCapability<BitMoreComplexCapability>().ValueOrBinding.GetValue());

control1.SetValue(capProp, new BitMoreComplexCapability { NotNullable = 30, ValueOrBinding = new((int?)null) }); // reset defaults
Assert.AreEqual(30, control1.GetCapability<BitMoreComplexCapability>().NotNullable);
Assert.AreEqual(null, control1.GetCapability<BitMoreComplexCapability>().ValueOrBinding.GetValue());
}

[DataTestMethod]
[DataRow(typeof(TestControl6))]
[DataRow(typeof(TestControlFallbackProps))]
[DataRow(typeof(TestControlInheritedProps))]
public void BitMoreComplexCapability_SetEmptyValue(Type controlType)
{
var control1 = (DotvvmBindableObject)Activator.CreateInstance(controlType);
var capProp = DotvvmCapabilityProperty.Find(controlType, typeof(BitMoreComplexCapability));

Assert.IsFalse(control1.GetCapability<BitMoreComplexCapability>().ValueOrBindingNullable.HasValue);

var prop = controlType == typeof(TestControlFallbackProps) ? "ValueOrBindingNullable2" : "ValueOrBindingNullable";

control1.SetProperty(prop, null);
XAssert.Single(control1.Properties);
Assert.IsFalse(control1.GetCapability<BitMoreComplexCapability>().ValueOrBindingNullable.HasValue);

control1.SetProperty(prop, 1);
Assert.AreEqual(1, control1.GetCapability<BitMoreComplexCapability>().ValueOrBindingNullable?.GetValue());


control1.SetValue(capProp, new BitMoreComplexCapability { ValueOrBindingNullable = null }); // removes property
Assert.AreEqual(null, control1.GetCapability<BitMoreComplexCapability>().ValueOrBindingNullable);
Assert.IsFalse(control1.IsPropertySet(control1.GetDotvvmProperty(prop)));

control1.SetValue(capProp, new BitMoreComplexCapability { ValueOrBindingNullable = new(2) }); // sets new value
Assert.AreEqual(2, control1.GetCapability<BitMoreComplexCapability>().ValueOrBindingNullable?.GetValue());
}

[DataTestMethod]
[DataRow(typeof(TestControl6))]
[DataRow(typeof(TestControlFallbackProps))]
Expand Down

0 comments on commit 583bf7d

Please sign in to comment.