Skip to content

Commit 401034b

Browse files
authored
Merge pull request #1 from lucafabbri/copilot/fix-16dfce38-2f48-4260-9cf4-01ed7495508a
Add comprehensive unit test suite for ProjectR with 99 tests covering core business logic
2 parents 219d005 + c127fc5 commit 401034b

78 files changed

Lines changed: 13257 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ProjectR.Tests/DiagnosticsTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using FluentAssertions;
2+
using Microsoft.CodeAnalysis;
3+
using Xunit;
4+
5+
namespace ProjectR.Tests.Diagnostic;
6+
7+
public class DiagnosticsTests
8+
{
9+
[Fact]
10+
public void UnexpectedError_HasCorrectProperties()
11+
{
12+
// Arrange & Act
13+
var diagnostic = ProjectR.Diagnostics.UnexpectedError;
14+
15+
// Assert
16+
diagnostic.Id.Should().Be("PR0001");
17+
diagnostic.Title.ToString().Should().Be("An unexpected error occurred");
18+
diagnostic.MessageFormat.ToString().Should().Be("An unexpected error occurred during mapper generation: '{0}' StackTrace: {1}");
19+
diagnostic.Category.Should().Be("ProjectR.Generator");
20+
diagnostic.DefaultSeverity.Should().Be(DiagnosticSeverity.Error);
21+
diagnostic.IsEnabledByDefault.Should().BeTrue();
22+
}
23+
24+
[Fact]
25+
public void MapperGenerationFailed_HasCorrectProperties()
26+
{
27+
// Arrange & Act
28+
var diagnostic = ProjectR.Diagnostics.MapperGenerationFailed;
29+
30+
// Assert
31+
diagnostic.Id.Should().Be("PR0002");
32+
diagnostic.Title.ToString().Should().Be("Mapper generation failed");
33+
diagnostic.MessageFormat.ToString().Should().Be("Failed to generate an implementation for mapper '{0}'");
34+
diagnostic.Category.Should().Be("ProjectR.Generator");
35+
diagnostic.DefaultSeverity.Should().Be(DiagnosticSeverity.Error);
36+
diagnostic.IsEnabledByDefault.Should().BeTrue();
37+
}
38+
39+
[Fact]
40+
public void NoValidCreationMethod_HasCorrectProperties()
41+
{
42+
// Arrange & Act
43+
var diagnostic = ProjectR.Diagnostics.NoValidCreationMethod;
44+
45+
// Assert
46+
diagnostic.Id.Should().Be("PR0003");
47+
diagnostic.Title.ToString().Should().Be("No valid creation method found");
48+
diagnostic.MessageFormat.ToString().Should().Be("ProjectR could not find a valid constructor or static factory method to create an instance of '{0}' based on the source type.");
49+
diagnostic.Category.Should().Be("ProjectR.Policies");
50+
diagnostic.DefaultSeverity.Should().Be(DiagnosticSeverity.Error);
51+
diagnostic.IsEnabledByDefault.Should().BeTrue();
52+
}
53+
54+
[Fact]
55+
public void UnmappableConstructorParameter_HasCorrectProperties()
56+
{
57+
// Arrange & Act
58+
var diagnostic = ProjectR.Diagnostics.UnmappableConstructorParameter;
59+
60+
// Assert
61+
diagnostic.Id.Should().Be("PR0004");
62+
diagnostic.Title.ToString().Should().Be("Unmappable constructor parameter");
63+
diagnostic.MessageFormat.ToString().Should().Be("The parameter '{0}' of the constructor for '{1}' could not be mapped from any source property.");
64+
diagnostic.Category.Should().Be("ProjectR.Policies");
65+
diagnostic.DefaultSeverity.Should().Be(DiagnosticSeverity.Error);
66+
diagnostic.IsEnabledByDefault.Should().BeTrue();
67+
}
68+
69+
[Fact]
70+
public void UnmappedDestinationProperty_HasCorrectProperties()
71+
{
72+
// Arrange & Act
73+
var diagnostic = ProjectR.Diagnostics.UnmappedDestinationProperty;
74+
75+
// Assert
76+
diagnostic.Id.Should().Be("PR0005");
77+
diagnostic.Title.ToString().Should().Be("Unmapped destination property");
78+
diagnostic.MessageFormat.ToString().Should().Be("The property '{0}' on destination type '{1}' was not mapped.");
79+
diagnostic.Category.Should().Be("ProjectR.Policies");
80+
diagnostic.DefaultSeverity.Should().Be(DiagnosticSeverity.Warning);
81+
diagnostic.IsEnabledByDefault.Should().BeTrue();
82+
}
83+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using ProjectR.Attributes;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace ProjectR.Tests.Attributes;
6+
7+
public class DtoAttributeTests
8+
{
9+
private class TestEntity { }
10+
private class TestDto { }
11+
private class TestMapper { }
12+
13+
[Fact]
14+
public void DtoAttribute_Constructor_SetsEntityTypeCorrectly()
15+
{
16+
// Arrange & Act
17+
var attribute = new DtoAttribute<TestEntity>();
18+
19+
// Assert
20+
attribute.EntityType.Should().Be(typeof(TestEntity));
21+
}
22+
23+
[Fact]
24+
public void DtoMapperAttribute_Constructor_SetsEntityTypeAndMapperTypeCorrectly()
25+
{
26+
// Arrange & Act
27+
var attribute = new DtoMapperAttribute<TestEntity, TestMapper>();
28+
29+
// Assert
30+
attribute.EntityType.Should().Be(typeof(TestEntity));
31+
attribute.MapperType.Should().Be(typeof(TestMapper));
32+
}
33+
34+
[Fact]
35+
public void DtoAttribute_HasCorrectAttributeUsage()
36+
{
37+
// Arrange & Act
38+
var attributeUsage = typeof(DtoAttribute<>).GetCustomAttributes(typeof(AttributeUsageAttribute), false)
39+
.Cast<AttributeUsageAttribute>()
40+
.First();
41+
42+
// Assert
43+
attributeUsage.ValidOn.Should().Be(AttributeTargets.Class);
44+
attributeUsage.AllowMultiple.Should().BeTrue();
45+
attributeUsage.Inherited.Should().BeTrue();
46+
}
47+
48+
[Fact]
49+
public void DtoMapperAttribute_HasCorrectAttributeUsage()
50+
{
51+
// Arrange & Act
52+
var attributeUsage = typeof(DtoMapperAttribute<,>).GetCustomAttributes(typeof(AttributeUsageAttribute), false)
53+
.Cast<AttributeUsageAttribute>()
54+
.First();
55+
56+
// Assert
57+
attributeUsage.ValidOn.Should().Be(AttributeTargets.Class);
58+
attributeUsage.AllowMultiple.Should().BeTrue();
59+
attributeUsage.Inherited.Should().BeTrue();
60+
}
61+
62+
[Fact]
63+
public void DtoMapperAttribute_InheritsFromDtoAttribute()
64+
{
65+
// Arrange & Act & Assert
66+
typeof(DtoMapperAttribute<TestEntity, TestMapper>).Should().BeDerivedFrom<DtoAttribute<TestEntity>>();
67+
}
68+
}

ProjectR.Tests/GlobalUsings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;
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+
}

0 commit comments

Comments
 (0)