Skip to content

Correct handling of step values of zero #18532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public static bool IsValueValidForStep(decimal value, decimal min, decimal step)
return true; // Outside of the range, so we expect another validator will have picked this up.
}

if (step == 0)
{
return true; // A step of zero would trigger a divide by zero error in evaluating. So we always pass validation for zero, as effectively any step value is valid.
}

return (value - min) % step == 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;

Check notice on line 1 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/DecimalValueEditorTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ Getting better: Code Duplication

reduced similar code in: Validates_Matches_Configured_Step. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.

Check warning on line 1 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/DecimalValueEditorTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

❌ New issue: Primitive Obsession

In this module, 100.0% of all function arguments are primitive types, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
Expand Down Expand Up @@ -132,11 +132,12 @@
}
}

[TestCase(1.4, false)]
[TestCase(1.5, true)]
public void Validates_Matches_Configured_Step(object value, bool expectedSuccess)
[TestCase(0.2, 1.4, false)]
[TestCase(0.2, 1.5, true)]
[TestCase(0.0, 1.4, true)] // A step of zero would trigger a divide by zero error in evaluating. So we always pass validation for zero, as effectively any step value is valid.
public void Validates_Matches_Configured_Step(double step, object value, bool expectedSuccess)
{
var editor = CreateValueEditor();
var editor = CreateValueEditor(step: step);
var result = editor.Validate(value, false, null, PropertyValidationContext.Empty());
if (expectedSuccess)
{
Expand Down Expand Up @@ -164,7 +165,7 @@
return CreateValueEditor().ToEditor(property.Object);
}

private static DecimalPropertyEditor.DecimalPropertyValueEditor CreateValueEditor()
private static DecimalPropertyEditor.DecimalPropertyValueEditor CreateValueEditor(double step = 0.2)
{
var localizedTextServiceMock = new Mock<ILocalizedTextService>();
localizedTextServiceMock.Setup(x => x.Localize(
Expand All @@ -184,7 +185,7 @@
{
{ "min", 1.1 },
{ "max", 1.9 },
{ "step", 0.2 }
{ "step", step }
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;

Check notice on line 1 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/IntegerValueEditorTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ Getting better: Code Duplication

reduced similar code in: Validates_Matches_Configured_Step. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.

Check warning on line 1 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/IntegerValueEditorTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

❌ New issue: Primitive Obsession

In this module, 100.0% of all function arguments are primitive types, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
Expand Down Expand Up @@ -132,11 +132,12 @@
}
}

[TestCase(17, false)]
[TestCase(18, true)]
public void Validates_Matches_Configured_Step(object value, bool expectedSuccess)
[TestCase(2, 17, false)]
[TestCase(2, 18, true)]
[TestCase(0, 17, true)] // A step of zero would trigger a divide by zero error in evaluating. So we always pass validation for zero, as effectively any step value is valid.
public void Validates_Matches_Configured_Step(int step, object value, bool expectedSuccess)
{
var editor = CreateValueEditor();
var editor = CreateValueEditor(step: step);
var result = editor.Validate(value, false, null, PropertyValidationContext.Empty());
if (expectedSuccess)
{
Expand Down Expand Up @@ -164,7 +165,7 @@
return CreateValueEditor().ToEditor(property.Object);
}

private static IntegerPropertyEditor.IntegerPropertyValueEditor CreateValueEditor()
private static IntegerPropertyEditor.IntegerPropertyValueEditor CreateValueEditor(int step = 2)
{
var localizedTextServiceMock = new Mock<ILocalizedTextService>();
localizedTextServiceMock.Setup(x => x.Localize(
Expand All @@ -184,7 +185,7 @@
{
{ "min", 10 },
{ "max", 20 },
{ "step", 2 }
{ "step", step }
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Umbraco.Cms.Core.Models.Editors;
using Umbraco.Cms.Core.Models.Validation;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Infrastructure.Serialization;
Expand Down Expand Up @@ -206,17 +205,18 @@ public void Validates_Is_Less_Than_Or_Equal_To_Configured_Max(decimal from, deci
}
}

[TestCase(1.3, 1.7, true)]
[TestCase(1.4, 1.7, false)]
[TestCase(1.3, 1.6, false)]
public void Validates_Matches_Configured_Step(decimal from, decimal to, bool expectedSuccess)
[TestCase(0.2, 1.3, 1.7, true)]
[TestCase(0.2, 1.4, 1.7, false)]
[TestCase(0.2, 1.3, 1.6, false)]
[TestCase(0.0, 1.4, 1.7, true)] // A step of zero would trigger a divide by zero error in evaluating. So we always pass validation for zero, as effectively any step value is valid.
public void Validates_Matches_Configured_Step(decimal step, decimal from, decimal to, bool expectedSuccess)
{
var value = new JsonObject
{
{ "from", from },
{ "to", to },
};
var editor = CreateValueEditor();
var editor = CreateValueEditor(step: step);
var result = editor.Validate(value, false, null, PropertyValidationContext.Empty());
if (expectedSuccess)
{
Expand Down Expand Up @@ -244,7 +244,7 @@ public void Validates_Matches_Configured_Step(decimal from, decimal to, bool exp
return CreateValueEditor().ToEditor(property.Object);
}

private static SliderPropertyEditor.SliderPropertyValueEditor CreateValueEditor(bool enableRange = true)
private static SliderPropertyEditor.SliderPropertyValueEditor CreateValueEditor(bool enableRange = true, decimal step = 0.2m)
{
var localizedTextServiceMock = new Mock<ILocalizedTextService>();
localizedTextServiceMock.Setup(x => x.Localize(
Expand All @@ -265,7 +265,7 @@ private static SliderPropertyEditor.SliderPropertyValueEditor CreateValueEditor(
EnableRange = enableRange,
MinimumValue = 1.1m,
MaximumValue = 1.9m,
Step = 0.2m
Step = step
},
};
}
Expand Down