Skip to content

Commit 822c8bd

Browse files
committed
Atualização do SPPreview e melhorias na classe Problem
* Atualização de dependências - Atualizado o `SPPreview` para a versão `-preview-4.2`. * Refatoração da classe Problem - Introduzido o campo privado `_property` para gerenciar `Property`. - Alterado `Property` para usar o novo campo, permitindo lógica personalizada. * Novas funcionalidades na classe Problem - Adicionadas as funções `ChainProperty` e `ChainProperty` com índice para encadeamento de propriedades. * Testes unitários - Adicionados testes para verificar o comportamento das novas funções de encadeamento. - Introduzido o enum `MyEnum` para uso nos testes, demonstrando extensões com valores de enum.
1 parent 21b822c commit 822c8bd

3 files changed

Lines changed: 151 additions & 2 deletions

File tree

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111
<PropertyGroup>
1212
<SPVer>1.0.0</SPVer>
13-
<SPPreview>-preview-4.1</SPPreview>
13+
<SPPreview>-preview-4.2</SPPreview>
1414
</PropertyGroup>
1515
<PropertyGroup>
1616
<FluentValidationVer>12.0.0</FluentValidationVer>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
namespace RoyalCode.SmartProblems.Tests.Basics;
2+
3+
public class ProblemTests
4+
{
5+
[Fact]
6+
public void Problem_ChainProperty_MustConcatProperties()
7+
{
8+
// Arrange
9+
var problem = Problems.InvalidParameter("Invalid parameter", "my-property");
10+
11+
// Act
12+
problem.ChainProperty("my-other-property");
13+
14+
// Assert
15+
Assert.Equal("my-other-property.my-property", problem.Property);
16+
}
17+
18+
[Fact]
19+
public void Problem_ChainProperty_WithNullProperty_MustDoNothing()
20+
{
21+
// Arrange
22+
var problem = Problems.InvalidParameter("Invalid parameter");
23+
24+
// Act
25+
problem.ChainProperty("my-property");
26+
27+
// Assert
28+
Assert.Null(problem.Property);
29+
}
30+
31+
[Fact]
32+
public void Problem_ChainProperty_WithEmptyProperty_MustDoNothing()
33+
{
34+
// Arrange
35+
var problem = Problems.InvalidParameter("Invalid parameter", string.Empty);
36+
37+
// Act
38+
problem.ChainProperty("my-property");
39+
40+
// Assert
41+
Assert.Equal(string.Empty, problem.Property);
42+
}
43+
44+
[Fact]
45+
public void Problem_ChainProperty_WithIndex_MustConcatProperties()
46+
{
47+
// Arrange
48+
var problem = Problems.InvalidParameter("Invalid parameter", "my-property");
49+
50+
// Act
51+
problem.ChainProperty("my-other-property", 1);
52+
53+
// Assert
54+
Assert.Equal("my-other-property[1].my-property", problem.Property);
55+
}
56+
57+
[Fact]
58+
public void Problem_ChainProperty_WithIndexAndNullProperty_MustDoNothing()
59+
{
60+
// Arrange
61+
var problem = Problems.InvalidParameter("Invalid parameter");
62+
63+
// Act
64+
problem.ChainProperty("my-property", 1);
65+
66+
// Assert
67+
Assert.Null(problem.Property);
68+
}
69+
70+
[Fact]
71+
public void Problem_ChainProperty_WithIndexAndEmptyProperty_MustDoNothing()
72+
{
73+
// Arrange
74+
var problem = Problems.InvalidParameter("Invalid parameter", string.Empty);
75+
76+
// Act
77+
problem.ChainProperty("my-property", 1);
78+
79+
// Assert
80+
Assert.Equal(string.Empty, problem.Property);
81+
}
82+
83+
[Fact]
84+
public void Problem_With_MustAddExtension()
85+
{
86+
// Arrange
87+
var problem = Problems.InvalidParameter("Invalid parameter");
88+
89+
// Act
90+
problem.With("my-key", "my-value");
91+
92+
// Assert
93+
Assert.NotNull(problem.Extensions);
94+
Assert.Single(problem.Extensions);
95+
Assert.Equal("my-value", problem.Extensions["my-key"]);
96+
}
97+
98+
[Fact]
99+
public void Problem_WithEnum_MustAddExtension()
100+
{
101+
// Arrange
102+
var problem = Problems.InvalidParameter("Invalid parameter");
103+
104+
// Act
105+
problem.With("my-key", MyEnum.Value1);
106+
107+
// Assert
108+
Assert.NotNull(problem.Extensions);
109+
Assert.Single(problem.Extensions);
110+
Assert.Equal("Value1", problem.Extensions["my-key"]);
111+
}
112+
}
113+
114+
file enum MyEnum
115+
{
116+
Value1,
117+
Value2
118+
}

src/RoyalCode.SmartProblems/Problem.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public sealed class Problem
3636

3737
#endregion
3838

39+
private string? _property;
40+
3941
/// <summary>
4042
/// <para>
4143
/// Description of the problem, like a message to the user.
@@ -60,7 +62,7 @@ public sealed class Problem
6062
/// <summary>
6163
/// Optional, the property that the problem is related to.
6264
/// </summary>
63-
public string? Property { get; init; }
65+
public string? Property { get => _property; init => _property = value; }
6466

6567
/// <summary>
6668
/// Optional, extra information about the problem.
@@ -96,6 +98,35 @@ public Problem With<TEnum>(string key, TEnum value)
9698
return With(key, value.ToString());
9799
}
98100

101+
/// <summary>
102+
/// Encadeia uma propriedade ao início da propriedade atual, formando uma propriedade aninhada (ex: Endereco.Rua).
103+
/// </summary>
104+
/// <param name="parentProperty">Nome da propriedade a ser encadeada.</param>
105+
/// <returns>Esta instância de Problem com a propriedade modificada.</returns>
106+
public Problem ChainProperty(string parentProperty)
107+
{
108+
if (string.IsNullOrEmpty(parentProperty) || string.IsNullOrEmpty(_property))
109+
return this;
110+
111+
_property = $"{parentProperty}.{Property}";
112+
return this;
113+
}
114+
115+
/// <summary>
116+
/// Encadeia uma propriedade de coleção com índice ao início da propriedade atual (ex: Endereco[0].Rua).
117+
/// </summary>
118+
/// <param name="parentProperty">Nome da propriedade de coleção.</param>
119+
/// <param name="index">Índice da coleção.</param>
120+
/// <returns>Esta instância de Problem com a propriedade modificada.</returns>
121+
public Problem ChainProperty(string parentProperty, int index)
122+
{
123+
if (string.IsNullOrEmpty(parentProperty) || string.IsNullOrEmpty(_property))
124+
return this;
125+
126+
_property = $"{parentProperty}[{index}].{Property}";
127+
return this;
128+
}
129+
99130
/// <inheritdoc />
100131
public override string ToString() => ToStringFactory(this);
101132

0 commit comments

Comments
 (0)