Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion ProjectR.Sample/ProjectR.Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
83 changes: 83 additions & 0 deletions ProjectR.Tests/DiagnosticsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using FluentAssertions;
using Microsoft.CodeAnalysis;
using Xunit;

namespace ProjectR.Tests.Diagnostic;

public class DiagnosticsTests
{
[Fact]
public void UnexpectedError_HasCorrectProperties()
{
// Arrange & Act
var diagnostic = ProjectR.Diagnostics.UnexpectedError;

// Assert
diagnostic.Id.Should().Be("PR0001");
diagnostic.Title.ToString().Should().Be("An unexpected error occurred");
diagnostic.MessageFormat.ToString().Should().Be("An unexpected error occurred during mapper generation: '{0}' StackTrace: {1}");
diagnostic.Category.Should().Be("ProjectR.Generator");
diagnostic.DefaultSeverity.Should().Be(DiagnosticSeverity.Error);
diagnostic.IsEnabledByDefault.Should().BeTrue();
}

[Fact]
public void MapperGenerationFailed_HasCorrectProperties()
{
// Arrange & Act
var diagnostic = ProjectR.Diagnostics.MapperGenerationFailed;

// Assert
diagnostic.Id.Should().Be("PR0002");
diagnostic.Title.ToString().Should().Be("Mapper generation failed");
diagnostic.MessageFormat.ToString().Should().Be("Failed to generate an implementation for mapper '{0}'");
diagnostic.Category.Should().Be("ProjectR.Generator");
diagnostic.DefaultSeverity.Should().Be(DiagnosticSeverity.Error);
diagnostic.IsEnabledByDefault.Should().BeTrue();
}

[Fact]
public void NoValidCreationMethod_HasCorrectProperties()
{
// Arrange & Act
var diagnostic = ProjectR.Diagnostics.NoValidCreationMethod;

// Assert
diagnostic.Id.Should().Be("PR0003");
diagnostic.Title.ToString().Should().Be("No valid creation method found");
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.");
diagnostic.Category.Should().Be("ProjectR.Policies");
diagnostic.DefaultSeverity.Should().Be(DiagnosticSeverity.Error);
diagnostic.IsEnabledByDefault.Should().BeTrue();
}

[Fact]
public void UnmappableConstructorParameter_HasCorrectProperties()
{
// Arrange & Act
var diagnostic = ProjectR.Diagnostics.UnmappableConstructorParameter;

// Assert
diagnostic.Id.Should().Be("PR0004");
diagnostic.Title.ToString().Should().Be("Unmappable constructor parameter");
diagnostic.MessageFormat.ToString().Should().Be("The parameter '{0}' of the constructor for '{1}' could not be mapped from any source property.");
diagnostic.Category.Should().Be("ProjectR.Policies");
diagnostic.DefaultSeverity.Should().Be(DiagnosticSeverity.Error);
diagnostic.IsEnabledByDefault.Should().BeTrue();
}

[Fact]
public void UnmappedDestinationProperty_HasCorrectProperties()
{
// Arrange & Act
var diagnostic = ProjectR.Diagnostics.UnmappedDestinationProperty;

// Assert
diagnostic.Id.Should().Be("PR0005");
diagnostic.Title.ToString().Should().Be("Unmapped destination property");
diagnostic.MessageFormat.ToString().Should().Be("The property '{0}' on destination type '{1}' was not mapped.");
diagnostic.Category.Should().Be("ProjectR.Policies");
diagnostic.DefaultSeverity.Should().Be(DiagnosticSeverity.Warning);
diagnostic.IsEnabledByDefault.Should().BeTrue();
}
}
68 changes: 68 additions & 0 deletions ProjectR.Tests/DtoAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using ProjectR.Attributes;
using FluentAssertions;
using Xunit;

namespace ProjectR.Tests.Attributes;

public class DtoAttributeTests
{
private class TestEntity { }
private class TestDto { }
private class TestMapper { }

[Fact]
public void DtoAttribute_Constructor_SetsEntityTypeCorrectly()
{
// Arrange & Act
var attribute = new DtoAttribute<TestEntity>();

// Assert
attribute.EntityType.Should().Be(typeof(TestEntity));
}

[Fact]
public void DtoMapperAttribute_Constructor_SetsEntityTypeAndMapperTypeCorrectly()
{
// Arrange & Act
var attribute = new DtoMapperAttribute<TestEntity, TestMapper>();

// Assert
attribute.EntityType.Should().Be(typeof(TestEntity));
attribute.MapperType.Should().Be(typeof(TestMapper));
}

[Fact]
public void DtoAttribute_HasCorrectAttributeUsage()
{
// Arrange & Act
var attributeUsage = typeof(DtoAttribute<>).GetCustomAttributes(typeof(AttributeUsageAttribute), false)
.Cast<AttributeUsageAttribute>()
.First();

// Assert
attributeUsage.ValidOn.Should().Be(AttributeTargets.Class);
attributeUsage.AllowMultiple.Should().BeTrue();
attributeUsage.Inherited.Should().BeTrue();
}

[Fact]
public void DtoMapperAttribute_HasCorrectAttributeUsage()
{
// Arrange & Act
var attributeUsage = typeof(DtoMapperAttribute<,>).GetCustomAttributes(typeof(AttributeUsageAttribute), false)
.Cast<AttributeUsageAttribute>()
.First();

// Assert
attributeUsage.ValidOn.Should().Be(AttributeTargets.Class);
attributeUsage.AllowMultiple.Should().BeTrue();
attributeUsage.Inherited.Should().BeTrue();
}

[Fact]
public void DtoMapperAttribute_InheritsFromDtoAttribute()
{
// Arrange & Act & Assert
typeof(DtoMapperAttribute<TestEntity, TestMapper>).Should().BeDerivedFrom<DtoAttribute<TestEntity>>();
}
}
1 change: 1 addition & 0 deletions ProjectR.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
130 changes: 130 additions & 0 deletions ProjectR.Tests/LegacyMappingClassesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using FluentAssertions;
using Microsoft.CodeAnalysis;
using Moq;
using Xunit;

namespace ProjectR.Tests.Mapping;

public class LegacyMappingClassesTests
{
[Fact]
public void MappingInstruction_IsAbstractBaseClass()
{
// Assert
typeof(MappingInstruction).Should().BeAbstract();
}

[Fact]
public void NestedObjectMapperMapping_InheritsFromMappingInstruction()
{
// Assert
typeof(NestedObjectMapperMapping).Should().BeDerivedFrom<MappingInstruction>();
}

[Fact]
public void NestedObjectMapperMapping_Properties_CanBeSetAndRetrieved()
{
// Arrange
var mockSourceProperty = new Mock<IPropertySymbol>();
var mockDestinationProperty = new Mock<IPropertySymbol>();
var mockMapperType = new Mock<INamedTypeSymbol>();
var mapping = new NestedObjectMapperMapping();

// Act
mapping.SourceProperty = mockSourceProperty.Object;
mapping.DestinationProperty = mockDestinationProperty.Object;
mapping.MapperType = mockMapperType.Object;

// Assert
mapping.SourceProperty.Should().Be(mockSourceProperty.Object);
mapping.DestinationProperty.Should().Be(mockDestinationProperty.Object);
mapping.MapperType.Should().Be(mockMapperType.Object);
}

[Fact]
public void ParameterMapping_Properties_CanBeSetAndRetrieved()
{
// Arrange
var mapping = new ParameterMapping();
var sourcePropertyName = "SourceProperty";
var destinationParameterName = "destinationParam";

// Act
mapping.SourcePropertyName = sourcePropertyName;
mapping.DestinationParameterName = destinationParameterName;

// Assert
mapping.SourcePropertyName.Should().Be(sourcePropertyName);
mapping.DestinationParameterName.Should().Be(destinationParameterName);
}

[Fact]
public void PropertyMapping_InheritsFromMappingInstruction()
{
// Assert
typeof(PropertyMapping).Should().BeDerivedFrom<MappingInstruction>();
}

[Fact]
public void PropertyMapping_Properties_CanBeSetAndRetrieved()
{
// Arrange
var mapping = new PropertyMapping();
var sourcePropertyName = "SourceProperty";
var destinationPropertyName = "DestinationProperty";

// Act
mapping.SourcePropertyName = sourcePropertyName;
mapping.DestinationPropertyName = destinationPropertyName;

// Assert
mapping.SourcePropertyName.Should().Be(sourcePropertyName);
mapping.DestinationPropertyName.Should().Be(destinationPropertyName);
}

[Fact]
public void PropertyMappingInstruction_IsAbstractBaseClass()
{
// Assert
typeof(PropertyMappingInstruction).Should().BeAbstract();
}

[Fact]
public void PropertyMappingInstruction_InheritsFromMappingInstruction()
{
// Assert
typeof(PropertyMappingInstruction).Should().BeDerivedFrom<MappingInstruction>();
}

[Fact]
public void PropertyMappingInstruction_Constructor_SetsDestinationProperty()
{
// Arrange
var mockDestinationProperty = new Mock<IPropertySymbol>();

// Act
var instruction = new TestPropertyMappingInstruction(mockDestinationProperty.Object);

// Assert
instruction.DestinationProperty.Should().Be(mockDestinationProperty.Object);
}

[Fact]
public void PropertyMappingInstruction_DestinationProperty_IsReadOnly()
{
// Arrange
var mockDestinationProperty = new Mock<IPropertySymbol>();
var instruction = new TestPropertyMappingInstruction(mockDestinationProperty.Object);

// Assert
var property = typeof(PropertyMappingInstruction).GetProperty(nameof(PropertyMappingInstruction.DestinationProperty));
property!.CanWrite.Should().BeFalse();
property.CanRead.Should().BeTrue();
}

// Test implementation of abstract PropertyMappingInstruction
private class TestPropertyMappingInstruction : PropertyMappingInstruction
{
public TestPropertyMappingInstruction(IPropertySymbol destinationProperty) : base(destinationProperty) { }
}
}
Loading
Loading