Skip to content

Commit b9450d6

Browse files
Copilotmrdevrobot
andcommitted
Complete unit test suite with 99 tests covering core business logic
Co-authored-by: lucafabbri <12503462+lucafabbri@users.noreply.github.com>
1 parent d1f372e commit b9450d6

3 files changed

Lines changed: 359 additions & 0 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using FluentAssertions;
2+
using Microsoft.CodeAnalysis;
3+
using Moq;
4+
using Xunit;
5+
6+
namespace ProjectR.Tests.Mapping;
7+
8+
public class LegacyMappingClassesTests
9+
{
10+
[Fact]
11+
public void MappingInstruction_IsAbstractBaseClass()
12+
{
13+
// Assert
14+
typeof(MappingInstruction).Should().BeAbstract();
15+
}
16+
17+
[Fact]
18+
public void NestedObjectMapperMapping_InheritsFromMappingInstruction()
19+
{
20+
// Assert
21+
typeof(NestedObjectMapperMapping).Should().BeDerivedFrom<MappingInstruction>();
22+
}
23+
24+
[Fact]
25+
public void NestedObjectMapperMapping_Properties_CanBeSetAndRetrieved()
26+
{
27+
// Arrange
28+
var mockSourceProperty = new Mock<IPropertySymbol>();
29+
var mockDestinationProperty = new Mock<IPropertySymbol>();
30+
var mockMapperType = new Mock<INamedTypeSymbol>();
31+
var mapping = new NestedObjectMapperMapping();
32+
33+
// Act
34+
mapping.SourceProperty = mockSourceProperty.Object;
35+
mapping.DestinationProperty = mockDestinationProperty.Object;
36+
mapping.MapperType = mockMapperType.Object;
37+
38+
// Assert
39+
mapping.SourceProperty.Should().Be(mockSourceProperty.Object);
40+
mapping.DestinationProperty.Should().Be(mockDestinationProperty.Object);
41+
mapping.MapperType.Should().Be(mockMapperType.Object);
42+
}
43+
44+
[Fact]
45+
public void ParameterMapping_Properties_CanBeSetAndRetrieved()
46+
{
47+
// Arrange
48+
var mapping = new ParameterMapping();
49+
var sourcePropertyName = "SourceProperty";
50+
var destinationParameterName = "destinationParam";
51+
52+
// Act
53+
mapping.SourcePropertyName = sourcePropertyName;
54+
mapping.DestinationParameterName = destinationParameterName;
55+
56+
// Assert
57+
mapping.SourcePropertyName.Should().Be(sourcePropertyName);
58+
mapping.DestinationParameterName.Should().Be(destinationParameterName);
59+
}
60+
61+
[Fact]
62+
public void PropertyMapping_InheritsFromMappingInstruction()
63+
{
64+
// Assert
65+
typeof(PropertyMapping).Should().BeDerivedFrom<MappingInstruction>();
66+
}
67+
68+
[Fact]
69+
public void PropertyMapping_Properties_CanBeSetAndRetrieved()
70+
{
71+
// Arrange
72+
var mapping = new PropertyMapping();
73+
var sourcePropertyName = "SourceProperty";
74+
var destinationPropertyName = "DestinationProperty";
75+
76+
// Act
77+
mapping.SourcePropertyName = sourcePropertyName;
78+
mapping.DestinationPropertyName = destinationPropertyName;
79+
80+
// Assert
81+
mapping.SourcePropertyName.Should().Be(sourcePropertyName);
82+
mapping.DestinationPropertyName.Should().Be(destinationPropertyName);
83+
}
84+
85+
[Fact]
86+
public void PropertyMappingInstruction_IsAbstractBaseClass()
87+
{
88+
// Assert
89+
typeof(PropertyMappingInstruction).Should().BeAbstract();
90+
}
91+
92+
[Fact]
93+
public void PropertyMappingInstruction_InheritsFromMappingInstruction()
94+
{
95+
// Assert
96+
typeof(PropertyMappingInstruction).Should().BeDerivedFrom<MappingInstruction>();
97+
}
98+
99+
[Fact]
100+
public void PropertyMappingInstruction_Constructor_SetsDestinationProperty()
101+
{
102+
// Arrange
103+
var mockDestinationProperty = new Mock<IPropertySymbol>();
104+
105+
// Act
106+
var instruction = new TestPropertyMappingInstruction(mockDestinationProperty.Object);
107+
108+
// Assert
109+
instruction.DestinationProperty.Should().Be(mockDestinationProperty.Object);
110+
}
111+
112+
[Fact]
113+
public void PropertyMappingInstruction_DestinationProperty_IsReadOnly()
114+
{
115+
// Arrange
116+
var mockDestinationProperty = new Mock<IPropertySymbol>();
117+
var instruction = new TestPropertyMappingInstruction(mockDestinationProperty.Object);
118+
119+
// Assert
120+
var property = typeof(PropertyMappingInstruction).GetProperty(nameof(PropertyMappingInstruction.DestinationProperty));
121+
property!.CanWrite.Should().BeFalse();
122+
property.CanRead.Should().BeTrue();
123+
}
124+
125+
// Test implementation of abstract PropertyMappingInstruction
126+
private class TestPropertyMappingInstruction : PropertyMappingInstruction
127+
{
128+
public TestPropertyMappingInstruction(IPropertySymbol destinationProperty) : base(destinationProperty) { }
129+
}
130+
}

ProjectR.Tests/PoliciesApiTests.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using FluentAssertions;
2+
using Xunit;
3+
4+
namespace ProjectR.Tests.Policies;
5+
6+
public class PoliciesApiTests
7+
{
8+
[Fact]
9+
public void MappingStrategy_Enum_HasExpectedValues()
10+
{
11+
// Assert
12+
Enum.GetValues<MappingStrategy>().Should().Contain(new[]
13+
{
14+
MappingStrategy.UsePublicConstructors,
15+
MappingStrategy.UseStaticFactories,
16+
MappingStrategy.UsePublicSetters
17+
});
18+
}
19+
20+
[Fact]
21+
public void MappingStrategy_HasCorrectValues()
22+
{
23+
// Assert
24+
((int)MappingStrategy.UsePublicConstructors).Should().Be(1);
25+
((int)MappingStrategy.UseStaticFactories).Should().Be(2);
26+
((int)MappingStrategy.UsePublicSetters).Should().Be(3);
27+
}
28+
29+
[Fact]
30+
public void IPolicyConfiguration_IsInterface()
31+
{
32+
// Assert
33+
typeof(IPolicyConfiguration).IsInterface.Should().BeTrue();
34+
}
35+
36+
[Fact]
37+
public void IMemberConfiguration_IsInterface()
38+
{
39+
// Assert
40+
typeof(IMemberConfiguration<,>).IsInterface.Should().BeTrue();
41+
}
42+
43+
[Fact]
44+
public void IPolicyBuilder_IsInterface()
45+
{
46+
// Assert
47+
typeof(IPolicyBuilder<,>).IsInterface.Should().BeTrue();
48+
}
49+
50+
[Fact]
51+
public void IProjectAsPolicyBuilder_InheritsFromIPolicyBuilder()
52+
{
53+
// Assert
54+
typeof(IProjectAsPolicyBuilder<,>).Should().BeAssignableTo(typeof(IPolicyBuilder<,>));
55+
}
56+
57+
[Fact]
58+
public void IBuildPolicyBuilder_InheritsFromIPolicyBuilder()
59+
{
60+
// Assert
61+
typeof(IBuildPolicyBuilder<,>).Should().BeAssignableTo(typeof(IPolicyBuilder<,>));
62+
}
63+
64+
[Fact]
65+
public void IApplyToPolicyBuilder_InheritsFromIPolicyBuilder()
66+
{
67+
// Assert
68+
typeof(IApplyToPolicyBuilder<,>).Should().BeAssignableTo(typeof(IPolicyBuilder<,>));
69+
}
70+
71+
[Fact]
72+
public void ICreationPolicy_IsInterface()
73+
{
74+
// Assert
75+
typeof(ICreationPolicy).IsInterface.Should().BeTrue();
76+
}
77+
78+
[Fact]
79+
public void IModificationPolicy_IsInterface()
80+
{
81+
// Assert
82+
typeof(IModificationPolicy).IsInterface.Should().BeTrue();
83+
}
84+
85+
[Fact]
86+
public void IMappingPolicy_IsInterface()
87+
{
88+
// Assert
89+
typeof(IMappingPolicy).IsInterface.Should().BeTrue();
90+
}
91+
92+
[Fact]
93+
public void IMappingPolicy_IsMarkerInterface()
94+
{
95+
// Assert
96+
typeof(IMappingPolicy).GetMethods().Should().BeEmpty();
97+
typeof(IMappingPolicy).GetProperties().Should().BeEmpty();
98+
}
99+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using FluentAssertions;
2+
using ProjectR.Policies;
3+
using Xunit;
4+
5+
namespace ProjectR.Tests.Policies;
6+
7+
public class PolicyImplementationTests
8+
{
9+
[Fact]
10+
public void CustomCreationPolicy_ImplementsICreationPolicy()
11+
{
12+
// Arrange
13+
var strategies = new List<MappingStrategy> { MappingStrategy.UsePublicConstructors };
14+
15+
// Act
16+
var policy = new CustomCreationPolicy(strategies);
17+
18+
// Assert
19+
policy.Should().BeAssignableTo<ICreationPolicy>();
20+
}
21+
22+
[Fact]
23+
public void CustomCreationPolicy_Constructor_SetsStrategiesAsReadOnly()
24+
{
25+
// Arrange
26+
var strategies = new List<MappingStrategy>
27+
{
28+
MappingStrategy.UsePublicConstructors,
29+
MappingStrategy.UseStaticFactories
30+
};
31+
32+
// Act
33+
var policy = new CustomCreationPolicy(strategies);
34+
35+
// Assert
36+
policy.Strategies.Should().BeAssignableTo<IReadOnlyList<MappingStrategy>>();
37+
policy.Strategies.Should().Equal(MappingStrategy.UsePublicConstructors, MappingStrategy.UseStaticFactories);
38+
policy.Strategies.Should().HaveCount(2);
39+
}
40+
41+
[Fact]
42+
public void CustomCreationPolicy_Constructor_WithEmptyList_CreatesEmptyReadOnlyList()
43+
{
44+
// Arrange
45+
var strategies = new List<MappingStrategy>();
46+
47+
// Act
48+
var policy = new CustomCreationPolicy(strategies);
49+
50+
// Assert
51+
policy.Strategies.Should().BeEmpty();
52+
policy.Strategies.Should().BeAssignableTo<IReadOnlyList<MappingStrategy>>();
53+
}
54+
55+
[Fact]
56+
public void CustomCreationPolicy_StrategiesIsReadOnly_WrapAroundOriginalList()
57+
{
58+
// Arrange
59+
var strategies = new List<MappingStrategy> { MappingStrategy.UsePublicConstructors };
60+
var policy = new CustomCreationPolicy(strategies);
61+
62+
// Act
63+
strategies.Add(MappingStrategy.UseStaticFactories);
64+
65+
// Assert - The policy's strategies reflects changes to the original list (wrapped reference)
66+
policy.Strategies.Should().HaveCount(2);
67+
policy.Strategies.Should().Equal(MappingStrategy.UsePublicConstructors, MappingStrategy.UseStaticFactories);
68+
}
69+
70+
[Fact]
71+
public void CustomModificationPolicy_ImplementsIModificationPolicy()
72+
{
73+
// Arrange
74+
var strategies = new List<MappingStrategy> { MappingStrategy.UsePublicSetters };
75+
76+
// Act
77+
var policy = new CustomModificationPolicy(strategies);
78+
79+
// Assert
80+
policy.Should().BeAssignableTo<IModificationPolicy>();
81+
}
82+
83+
[Fact]
84+
public void CustomModificationPolicy_Constructor_SetsStrategiesAsReadOnly()
85+
{
86+
// Arrange
87+
var strategies = new List<MappingStrategy>
88+
{
89+
MappingStrategy.UsePublicSetters,
90+
MappingStrategy.UseStaticFactories
91+
};
92+
93+
// Act
94+
var policy = new CustomModificationPolicy(strategies);
95+
96+
// Assert
97+
policy.Strategies.Should().BeAssignableTo<IReadOnlyList<MappingStrategy>>();
98+
policy.Strategies.Should().Equal(MappingStrategy.UsePublicSetters, MappingStrategy.UseStaticFactories);
99+
policy.Strategies.Should().HaveCount(2);
100+
}
101+
102+
[Fact]
103+
public void CustomModificationPolicy_Constructor_WithEmptyList_CreatesEmptyReadOnlyList()
104+
{
105+
// Arrange
106+
var strategies = new List<MappingStrategy>();
107+
108+
// Act
109+
var policy = new CustomModificationPolicy(strategies);
110+
111+
// Assert
112+
policy.Strategies.Should().BeEmpty();
113+
policy.Strategies.Should().BeAssignableTo<IReadOnlyList<MappingStrategy>>();
114+
}
115+
116+
[Fact]
117+
public void CustomModificationPolicy_StrategiesIsReadOnly_WrapAroundOriginalList()
118+
{
119+
// Arrange
120+
var strategies = new List<MappingStrategy> { MappingStrategy.UsePublicSetters };
121+
var policy = new CustomModificationPolicy(strategies);
122+
123+
// Act
124+
strategies.Add(MappingStrategy.UsePublicConstructors);
125+
126+
// Assert - The policy's strategies reflects changes to the original list (wrapped reference)
127+
policy.Strategies.Should().HaveCount(2);
128+
policy.Strategies.Should().Equal(MappingStrategy.UsePublicSetters, MappingStrategy.UsePublicConstructors);
129+
}
130+
}

0 commit comments

Comments
 (0)