diff --git a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs index b87ea348f8..b9868c3689 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/Enums/EnumFromStringSwitchMapping.cs @@ -1,5 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Riok.Mapperly.Emit.Syntax; using Riok.Mapperly.Helpers; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; @@ -55,7 +56,7 @@ private SwitchExpressionArmSyntax BuildArmIgnoreCase(string ignoreCaseSwitchDesi { // { } s var pattern = RecursivePattern() - .WithPropertyPatternClause(PropertyPatternClause()) + .WithPropertyPatternClause(PropertyPatternClause().AddTrailingSpace()) .WithDesignation(SingleVariableDesignation(Identifier(ignoreCaseSwitchDesignatedVariableName))); // source.Value1 diff --git a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachSetDictionaryExistingTargetMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachSetDictionaryExistingTargetMapping.cs index 1fb6eeddf5..390020ff40 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachSetDictionaryExistingTargetMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/ExistingTarget/ForEachSetDictionaryExistingTargetMapping.cs @@ -48,7 +48,7 @@ public override IEnumerable Build(TypeMappingBuildContext ctx, var castedVariable = ctx.NameBuilder.New(ExplicitCastVariableName); target = IdentifierName(castedVariable); - yield return LocalDeclarationStatement(DeclareVariable(castedVariable, cast)); + yield return ctx.SyntaxFactory.DeclareLocalVariable(castedVariable, cast); } if (_ensureCapacity != null) diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Condition.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Condition.cs index 8a4cc48242..e1fc13c903 100644 --- a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Condition.cs +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Condition.cs @@ -30,7 +30,7 @@ public IfStatementSyntax If( ElseClauseSyntax? elseClause = null; if (elseStatements != null) { - elseClause = ElseClause(Block(elseStatements)).AddLeadingLineFeed(Indentation).AddTrailingLineFeed(Indentation); + elseClause = ElseClause(Block(elseStatements)).AddLeadingLineFeed(Indentation); } return IfStatement( diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Null.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Null.cs index aa2d1b0020..d2ce9b6cda 100644 --- a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Null.cs +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Null.cs @@ -51,7 +51,7 @@ public StatementSyntax IfNullReturnOrThrow(ExpressionSyntax expression, Expressi { StatementSyntax ifExpression = returnOrThrowExpression switch { - ThrowExpressionSyntax throwSyntax => ThrowStatement(throwSyntax.Expression), + ThrowExpressionSyntax throwSyntax => AddIndentation().ThrowStatement(throwSyntax.Expression), _ => AddIndentation().Return(returnOrThrowExpression), }; diff --git a/test/Riok.Mapperly.Tests/GeneratedMethod.cs b/test/Riok.Mapperly.Tests/GeneratedMethod.cs index 5659fbcdd8..cd9684245b 100644 --- a/test/Riok.Mapperly.Tests/GeneratedMethod.cs +++ b/test/Riok.Mapperly.Tests/GeneratedMethod.cs @@ -1,12 +1,9 @@ -using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Riok.Mapperly.Tests; public class GeneratedMethod { - private const string MethodIndentation = " "; - public GeneratedMethod(MethodDeclarationSyntax declarationSyntax) { Name = declarationSyntax.Identifier.ToString(); @@ -27,17 +24,19 @@ public GeneratedMethod(MethodDeclarationSyntax declarationSyntax) /// The cleaned body. private static string ExtractBody(MethodDeclarationSyntax declarationSyntax) { - var body = - declarationSyntax.Body - ?.NormalizeWhitespace() - .ToFullString() - .Trim(' ', '\r', '\n') - .Trim('{', '}') - .Trim(' ', '\r', '\n') - .ReplaceLineEndings() ?? string.Empty; - var lines = body.Split(Environment.NewLine); - return lines.Length == 0 - ? string.Empty - : string.Join(Environment.NewLine, lines.Select(l => l.StartsWith(MethodIndentation) ? l[MethodIndentation.Length..] : l)); + if (declarationSyntax.Body == null) + return string.Empty; + + var body = declarationSyntax.Body + .ToFullString() + .Trim(' ', '\r', '\n') + .TrimStart('{') + .TrimEnd('}') + .Trim('\r', '\n') + .ReplaceLineEndings(); + var lines = body.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var indentionCount = lines[0].TakeWhile(x => x == ' ').Count(); + var indention = lines[0][..indentionCount]; + return string.Join(Environment.NewLine, lines.Select(l => l.StartsWith(indention) ? l[indentionCount..] : l)).Trim(' ', '\r', '\n'); } } diff --git a/test/Riok.Mapperly.Tests/Mapping/DictionaryTest.cs b/test/Riok.Mapperly.Tests/Mapping/DictionaryTest.cs index 0079fcd97b..a9adfc042f 100644 --- a/test/Riok.Mapperly.Tests/Mapping/DictionaryTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/DictionaryTest.cs @@ -31,7 +31,6 @@ public void DictionaryToSameDictionaryDeepCloning() { target[item.Key] = item.Value; } - return target; """ ); @@ -51,7 +50,6 @@ public void DictionaryToDictionaryExplicitCastedValue() { target[item.Key] = (int)item.Value; } - return target; """ ); @@ -71,7 +69,6 @@ public void DictionaryToDictionaryNullableToNonNullable() { target[item.Key] = item.Value == null ? throw new System.ArgumentNullException(nameof(item.Value)) : item.Value.Value; } - return target; """ ); @@ -98,7 +95,6 @@ TestSourceBuilderOptions.Default with { target[item.Key] = item.Value == null ? default : item.Value.Value; } - return target; """ ); @@ -118,7 +114,6 @@ public void DictionaryToIDictionaryExplicitCastedValue() { target[item.Key] = (int)item.Value; } - return target; """ ); @@ -138,7 +133,6 @@ public void KeyValueEnumerableToIDictionary() { target[item.Key] = item.Value; } - return target; """ ); @@ -158,7 +152,6 @@ public void CustomDictionaryToIDictionary() { target[item.Key] = item.Value; } - return target; """ ); @@ -178,7 +171,6 @@ public void CustomKeyValueListToIDictionary() { target[item.Key] = item.Value; } - return target; """ ); @@ -199,7 +191,6 @@ public void DictionaryToCustomDictionary() { target[item.Key] = item.Value; } - return target; """ ); @@ -223,7 +214,6 @@ public void DictionaryToCustomDictionaryWithObjectFactory() { target[item.Key] = item.Value; } - return target; """ ); @@ -285,7 +275,6 @@ public void KeyValueEnumerableToExistingDictionary() { target.EnsureCapacity(sourceCount + target.Count); } - foreach (var item in source) { target[item.Key] = item.Value; @@ -323,7 +312,6 @@ string IDictionary.this[string key] { targetDict[item.Key] = item.Value; } - return target; """ ); @@ -357,7 +345,6 @@ public string this[string key] { target[item.Key] = item.Value; } - return target; """ ); @@ -394,7 +381,6 @@ string IDictionary.this[string key] { targetDict[item.Key] = item.Value; } - return target; """ ); @@ -428,7 +414,6 @@ string IDictionary.this[string key] { targetDict[item.Key] = item.Value; } - return target; """ ); @@ -461,7 +446,6 @@ public string this[string key] { target[item.Key] = item.Value; } - return target; """ ); diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumFallbackValueTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumFallbackValueTest.cs index 467d9e3d0a..a58d343c91 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumFallbackValueTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumFallbackValueTest.cs @@ -100,9 +100,9 @@ public void StringToEnumFallbackValueShouldSwitchIgnoreCase() """ return source switch { - { } s when s.Equals(nameof(global::E1.A), System.StringComparison.OrdinalIgnoreCase) => global::E1.A, - { } s when s.Equals(nameof(global::E1.B), System.StringComparison.OrdinalIgnoreCase) => global::E1.B, - { } s when s.Equals(nameof(global::E1.C), System.StringComparison.OrdinalIgnoreCase) => global::E1.C, + {} s when s.Equals(nameof(global::E1.A), System.StringComparison.OrdinalIgnoreCase) => global::E1.A, + {} s when s.Equals(nameof(global::E1.B), System.StringComparison.OrdinalIgnoreCase) => global::E1.B, + {} s when s.Equals(nameof(global::E1.C), System.StringComparison.OrdinalIgnoreCase) => global::E1.C, _ => global::E1.Unknown, }; """ diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs index 43cb1795fa..788cf883f2 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs @@ -216,14 +216,14 @@ public void NullableEnumToOtherNullableEnumShouldCast() TestHelper .GenerateMapper(source) .Should() - .HaveSingleMethodBody("return source == null ? default(global::E2? ) : (global::E2)source.Value;"); + .HaveSingleMethodBody("return source == null ? default(global::E2?) : (global::E2)source.Value;"); } [Fact] public void EnumToOtherNullableEnumShouldCast() { var source = TestSourceBuilder.Mapping("E1", "E2?", "enum E1 {A, B, C}", "enum E2 {A, B, C}"); - TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (global::E2? )(global::E2)source;"); + TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (global::E2?)(global::E2)source;"); } [Fact] @@ -260,9 +260,9 @@ public void StringToEnumShouldSwitchIgnoreCase() """ return source switch { - { } s when s.Equals(nameof(global::E1.A), System.StringComparison.OrdinalIgnoreCase) => global::E1.A, - { } s when s.Equals(nameof(global::E1.B), System.StringComparison.OrdinalIgnoreCase) => global::E1.B, - { } s when s.Equals(nameof(global::E1.C), System.StringComparison.OrdinalIgnoreCase) => global::E1.C, + {} s when s.Equals(nameof(global::E1.A), System.StringComparison.OrdinalIgnoreCase) => global::E1.A, + {} s when s.Equals(nameof(global::E1.B), System.StringComparison.OrdinalIgnoreCase) => global::E1.B, + {} s when s.Equals(nameof(global::E1.C), System.StringComparison.OrdinalIgnoreCase) => global::E1.C, _ => System.Enum.Parse(source, true), }; """ diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumerableDeepCloningTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumerableDeepCloningTest.cs index 5603cc1ba8..2b73dd58eb 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumerableDeepCloningTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumerableDeepCloningTest.cs @@ -43,7 +43,6 @@ public void ArrayOfNullablePrimitiveTypesToNonNullableArrayDeepCloning() { target[i] = source[i] == null ? throw new System.NullReferenceException($"Sequence {nameof(source)}, contained a null value at index {i}.") : source[i].Value; } - return target; """ ); @@ -58,12 +57,11 @@ public void ArrayOfPrimitiveTypesToNullablePrimitiveTypesArrayDeepCloning() .Should() .HaveSingleMethodBody( """ - var target = new int? [source.Length]; + var target = new int?[source.Length]; for (var i = 0; i < source.Length; i++) { - target[i] = (int? )source[i]; + target[i] = (int?)source[i]; } - return target; """ ); @@ -88,7 +86,6 @@ public void ArrayCustomClassToArrayCustomClassDeepCloning() { target[i] = MapToB(source[i]); } - return target; """ ); @@ -105,7 +102,7 @@ public void ArrayToArrayOfStringDeepCloning() public void ArrayToArrayOfNullableStringDeepCloning() { var source = TestSourceBuilder.Mapping("string[]", "string?[]", TestSourceBuilderOptions.WithDeepCloning); - TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (string? [])source.Clone();"); + TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (string?[])source.Clone();"); } [Fact] @@ -134,7 +131,6 @@ public void ArrayToArrayOfMutableStructDeepCloning() { target[i] = MapToA(source[i]); } - return target; """ ); @@ -168,7 +164,6 @@ public void CollectionToArrayOfMutableStructDeepCloning() target[i] = MapToA(item); i++; } - return target; """ ); @@ -199,7 +194,6 @@ public void ArrayToArrayOfMutableStructDeepCloningLoopNameTaken() { target[i1] = MapToA(i[i1]); } - return target; """ ); @@ -223,7 +217,6 @@ public void ArrayToArrayOfMutableStructDeepCloningTargetNameTaken() { target1[i] = MapToA(target[i]); } - return target1; """ ); diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumerableExistingTargetTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumerableExistingTargetTest.cs index e72f90a016..bcc6b53dc3 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumerableExistingTargetTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumerableExistingTargetTest.cs @@ -32,7 +32,6 @@ public void EnumerableToExistingCustomCollection() { target.EnsureCapacity(sourceCount + target.Count); } - foreach (var item in source) { target.Add(item); diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumerableSetTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumerableSetTest.cs index 865ba42718..42a3d80eb6 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumerableSetTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumerableSetTest.cs @@ -77,7 +77,6 @@ public void ExistingEnumerableToExistingSet() { target.Values.Add(item); } - return target; """ ); @@ -102,12 +101,10 @@ public void ExistingEnumerableToExistingHashSet() { target.Values.EnsureCapacity(sourceCount + target.Values.Count); } - foreach (var item in source.Values) { target.Values.Add(item); } - return target; """ ); diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs index 19d20467bf..d6f2641e1b 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs @@ -37,7 +37,6 @@ public void ArrayOfNullablePrimitiveTypesToNonNullableArray() { target[i] = source[i] == null ? throw new System.NullReferenceException($"Sequence {nameof(source)}, contained a null value at index {i}.") : source[i].Value; } - return target; """ ); @@ -52,12 +51,11 @@ public void ArrayOfPrimitiveTypesToNullablePrimitiveTypesArray() .Should() .HaveSingleMethodBody( """ - var target = new int? [source.Length]; + var target = new int?[source.Length]; for (var i = 0; i < source.Length; i++) { - target[i] = (int? )source[i]; + target[i] = (int?)source[i]; } - return target; """ ); @@ -84,7 +82,6 @@ public void ArrayCustomClassNullableToArrayCustomClassNonNullable() { target[i] = source[i] == null ? throw new System.NullReferenceException($"Sequence {nameof(source)}, contained a null value at index {i}.") : source[i]!; } - return target; """ ); @@ -94,7 +91,7 @@ public void ArrayCustomClassNullableToArrayCustomClassNonNullable() public void ArrayCustomClassNonNullableToArrayCustomClassNullable() { var source = TestSourceBuilder.Mapping("B[]", "B?[]", "class B { public int Value {get; set; }}"); - TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (global::B? [])source;"); + TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (global::B?[])source;"); } [Fact] @@ -108,7 +105,7 @@ public void ArrayToArrayOfString() public void ArrayToArrayOfNullableString() { var source = TestSourceBuilder.Mapping("string[]", "string?[]"); - TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (string? [])source;"); + TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (string?[])source;"); } [Fact] @@ -139,7 +136,6 @@ public void ArrayToArrayOfCastedTypes() { target[i] = (int)source[i]; } - return target; """ ); @@ -201,7 +197,6 @@ public void ReadOnlyCollectionToIListOfDifferentTypes() { target.Add((long)item); } - return target; """ ); @@ -223,7 +218,6 @@ public void ReadOnlyCollectionToArrayOfDifferentTypes() target[i] = item.ToString(); i++; } - return target; """ ); @@ -276,7 +270,6 @@ public void ListToArrayOfCastedTypes() target[i] = (long)item; i++; } - return target; """ ); @@ -332,7 +325,6 @@ public void EnumerableToCustomCollection() { target.Add((int)item); } - return target; """ ); @@ -378,7 +370,6 @@ public void ReadOnlyCollectionToList() { target.Value.Add(item); } - return target; """ ); @@ -403,12 +394,10 @@ public void EnumerableToStack() { target.Value.EnsureCapacity(sourceCount + target.Value.Count); } - foreach (var item in source.Value) { target.Value.Push((long)item); } - return target; """ ); @@ -433,12 +422,10 @@ public void EnumerableToQueue() { target.Value.EnsureCapacity(sourceCount + target.Value.Count); } - foreach (var item in source.Value) { target.Value.Enqueue((long)item); } - return target; """ ); @@ -545,7 +532,6 @@ public void EnumerableToCustomCollectionWithObjectFactory() { target.Add((int)item); } - return target; """ ); diff --git a/test/Riok.Mapperly.Tests/Mapping/MemoryTest.cs b/test/Riok.Mapperly.Tests/Mapping/MemoryTest.cs index b92b6589e8..5a21b60996 100644 --- a/test/Riok.Mapperly.Tests/Mapping/MemoryTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/MemoryTest.cs @@ -299,14 +299,13 @@ public void ArrayToReadOnlyMemoryOfCastedTypes() .Should() .HaveMapMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -343,15 +342,14 @@ public void ReadOnlyMemoryToCreatedList() .Should() .HaveMapMethodBody( """ - var target = new global::B(); - target.Value.EnsureCapacity(source.Value.Span.Length + target.Value.Count); - foreach (var item in source.Value.Span) - { - target.Value.Add(item); - } - - return target; - """ + var target = new global::B(); + target.Value.EnsureCapacity(source.Value.Span.Length + target.Value.Count); + foreach (var item in source.Value.Span) + { + target.Value.Add(item); + } + return target; + """ ); } diff --git a/test/Riok.Mapperly.Tests/Mapping/NullableTest.cs b/test/Riok.Mapperly.Tests/Mapping/NullableTest.cs index 01e112c200..7689334d7a 100644 --- a/test/Riok.Mapperly.Tests/Mapping/NullableTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/NullableTest.cs @@ -43,8 +43,7 @@ public void NullableToNullableShouldWork() public void NullablePrimitiveToOtherNullablePrimitiveShouldWork() { var source = TestSourceBuilder.Mapping("decimal?", "int?"); - - TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody(@"return source == null ? default(int? ) : (int)source.Value;"); + TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return source == null ? default(int?) : (int)source.Value;"); } [Fact] @@ -140,8 +139,7 @@ TestSourceBuilderOptions.Default with public void NonNullableToNullableValueType() { var source = TestSourceBuilder.Mapping("DateTime", "DateTime?"); - - TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (global::System.DateTime? )source;"); + TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (global::System.DateTime?)source;"); } [Fact] diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs index 18d473f538..5127610d28 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs @@ -510,7 +510,7 @@ static partial class Mapper .Should() .HaveMapMethodBody( """ - var target = new global::TargetRecord(record.EnumValue != null ? MapToTargetEnum(record.EnumValue.Value) : default(global::TargetEnum? )); + var target = new global::TargetRecord(record.EnumValue != null ? MapToTargetEnum(record.EnumValue.Value) : default(global::TargetEnum?)); return target; """ ); @@ -542,7 +542,7 @@ static partial class Mapper .Should() .HaveMapMethodBody( """ - var target = new global::TargetRecord(record.StructValue != null ? MapToTargetStruct(record.StructValue.Value) : default(global::TargetStruct? )); + var target = new global::TargetRecord(record.StructValue != null ? MapToTargetStruct(record.StructValue.Value) : default(global::TargetStruct?)); return target; """ ); diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs index 60cc985982..fbca4c831e 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs @@ -138,7 +138,6 @@ public void AutoFlattenedPropertyNullablePath() { target.ValueId = source.Value.Id; } - return target; """ ); @@ -166,7 +165,6 @@ public void AutoFlattenedMultiplePropertiesNullablePath() target.ValueId = source.Value.Id.ToString(); target.ValueName = source.Value.Name; } - return target; """ ); @@ -192,7 +190,6 @@ public void AutoFlattenedPropertyNullableValueTypePath() { target.IdValue = source.Id.Value.Value; } - return target; """ ); @@ -218,7 +215,6 @@ public void AutoFlattenedPropertyNullableValueTypePathShouldResolve() { target.PropInteger = source.Prop.Value.Integer.ToString(); } - return target; """ ); @@ -268,7 +264,6 @@ public void AutoFlattenedMultiplePropertiesPathDisabledNullable() { target.ValueId = source.Value.Id.ToString(); } - target.ValueName = source.Value?.Name; return target; """ @@ -521,10 +516,8 @@ public void ManualNestedPropertyNullablePath() target.Value2.Value2.Id2 = source.Value1.Value1.Id1; target.Value2.Value2.Id20 = source.Value1.Value1.Id10; } - target.Value2.Id200 = source.Value1.Id100; } - return target; """ ); diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs index 7fb624d93c..61550d316a 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs @@ -45,7 +45,6 @@ public void NullableIntToNonNullableIntProperty() { target.Value = source.Value.Value; } - return target; """ ); @@ -75,7 +74,6 @@ TestSourceBuilderOptions.Default with { target.Value = source.Value.Value; } - return target; """ ); @@ -110,7 +108,6 @@ TestSourceBuilderOptions.Default with { throw new System.ArgumentNullException(nameof(source.Value.Value)); } - return target; """ ); @@ -136,7 +133,6 @@ public void NullableStringToNonNullableStringProperty() { target.Value = source.Value; } - return target; """ ); @@ -164,7 +160,6 @@ public void NullableClassToNonNullableClassProperty() { target.Value = MapToD(source.Value); } - return target; """ ); @@ -216,7 +211,6 @@ TestSourceBuilderOptions.Default with { target.Value = source.Value; } - return target; """ ); @@ -251,7 +245,6 @@ TestSourceBuilderOptions.Default with { throw new System.ArgumentNullException(nameof(source.Value)); } - return target; """ ); @@ -326,7 +319,6 @@ public void NullableClassToNullableClassProperty() { target.Value = MapToD(source.Value); } - return target; """ ); @@ -358,7 +350,6 @@ TestSourceBuilderOptions.Default with { target.Value = MapToD(source.Value); } - return target; """ ); @@ -395,7 +386,6 @@ TestSourceBuilderOptions.Default with { throw new System.ArgumentNullException(nameof(source.Value)); } - return target; """ ); @@ -423,7 +413,6 @@ public void DisabledNullableClassPropertyToNonNullableProperty() { target.Value = MapToD(source.Value); } - return target; """ ); @@ -451,7 +440,6 @@ public void NullableClassPropertyToDisabledNullableProperty() { target.Value = MapToD(source.Value); } - return target; """ ); @@ -487,7 +475,6 @@ TestSourceBuilderOptions.Default with { throw new System.ArgumentNullException(nameof(source.Value)); } - return target; """ ); @@ -586,7 +573,6 @@ public void ShouldUseUserImplementedMappingWithNonNullableValueType() { target.Test = Map(y.Test.Value); } - return target; """ ); diff --git a/test/Riok.Mapperly.Tests/Mapping/ParseTest.cs b/test/Riok.Mapperly.Tests/Mapping/ParseTest.cs index 32dcca8048..c899acde4c 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ParseTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ParseTest.cs @@ -23,7 +23,7 @@ public void ParseableBuiltInClass() public void ParseableBuiltNullableInClass() { var source = TestSourceBuilder.Mapping("string?", "int?"); - TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return source == null ? default(int? ) : int.Parse(source);"); + TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return source == null ? default(int?) : int.Parse(source);"); } [Fact] diff --git a/test/Riok.Mapperly.Tests/Mapping/SpanTest.cs b/test/Riok.Mapperly.Tests/Mapping/SpanTest.cs index 45afcb9674..c529cb489c 100644 --- a/test/Riok.Mapperly.Tests/Mapping/SpanTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/SpanTest.cs @@ -43,14 +43,13 @@ public void SpanToSpanOfCastedTypes() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -63,14 +62,13 @@ public void SpanToReadOnlySpanOfCastedTypes() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -83,14 +81,13 @@ public void ReadOnlySpanToReadOnlySpanOfCastedTypes() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -103,14 +100,13 @@ public void ReadOnlySpanToSpanOfCastedTypes() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -151,14 +147,13 @@ public void ArrayToSpanOfCastedTypes() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -171,14 +166,13 @@ public void ArrayToReadOnlySpanOfCastedTypes() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -191,14 +185,13 @@ public void SpanToArrayOfCastedTypes() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -211,14 +204,13 @@ public void ReadOnlySpanToArrayOfCastedTypes() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -260,15 +252,14 @@ public void SpanToList() .Should() .HaveSingleMethodBody( """ - var target = new global::System.Collections.Generic.List(); - target.EnsureCapacity(source.Length + target.Count); - foreach (var item in source) - { - target.Add(item); - } - - return target; - """ + var target = new global::System.Collections.Generic.List(); + target.EnsureCapacity(source.Length + target.Count); + foreach (var item in source) + { + target.Add(item); + } + return target; + """ ); } @@ -281,15 +272,14 @@ public void SpanToStack() .Should() .HaveSingleMethodBody( """ - var target = new global::System.Collections.Generic.Stack(); - target.EnsureCapacity(source.Length + target.Count); - foreach (var item in source) - { - target.Push(item); - } - - return target; - """ + var target = new global::System.Collections.Generic.Stack(); + target.EnsureCapacity(source.Length + target.Count); + foreach (var item in source) + { + target.Push(item); + } + return target; + """ ); } @@ -302,15 +292,14 @@ public void SpanToQueue() .Should() .HaveSingleMethodBody( """ - var target = new global::System.Collections.Generic.Queue(); - target.EnsureCapacity(source.Length + target.Count); - foreach (var item in source) - { - target.Enqueue(item); - } - - return target; - """ + var target = new global::System.Collections.Generic.Queue(); + target.EnsureCapacity(source.Length + target.Count); + foreach (var item in source) + { + target.Enqueue(item); + } + return target; + """ ); } @@ -382,14 +371,13 @@ public void SpanToSpanWithClassTypeDeepCloning() .Should() .HaveMapMethodBody( """ - var target = new global::A[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = MapToA(source[i]); - } - - return target; - """ + var target = new global::A[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = MapToA(source[i]); + } + return target; + """ ); } @@ -402,14 +390,13 @@ public void SpanToSpanOfCastedTypesDeepCloning() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -422,14 +409,13 @@ public void SpanToReadOnlySpanOfCastedTypesDeepCloning() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -442,14 +428,13 @@ public void ReadOnlySpanToReadOnlySpanOfCastedTypesDeepCloning() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -462,14 +447,13 @@ public void ReadOnlySpanToSpanOfCastedTypesDeepCloning() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -494,14 +478,13 @@ public void ArrayToReadOnlySpanWithClassTypeDeepCloning() .Should() .HaveMapMethodBody( """ - var target = new global::A[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = MapToA(source[i]); - } - - return target; - """ + var target = new global::A[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = MapToA(source[i]); + } + return target; + """ ); } @@ -514,14 +497,13 @@ public void ArrayToReadOnlySpanOfCastedTypesDeepCloning() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -546,14 +528,13 @@ public void ReadOnlySpanToArrayWithClassTypeDeepCloning() .Should() .HaveMapMethodBody( """ - var target = new global::A[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = MapToA(source[i]); - } - - return target; - """ + var target = new global::A[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = MapToA(source[i]); + } + return target; + """ ); } @@ -566,14 +547,13 @@ public void ReadOnlySpanToArrayOfCastedTypesDeepCloning() .Should() .HaveSingleMethodBody( """ - var target = new long[source.Length]; - for (var i = 0; i < source.Length; i++) - { - target[i] = (long)source[i]; - } - - return target; - """ + var target = new long[source.Length]; + for (var i = 0; i < source.Length; i++) + { + target[i] = (long)source[i]; + } + return target; + """ ); } @@ -643,15 +623,14 @@ public void SpanToReadOnlyList() .Should() .HaveSingleMethodBody( """ - var target = new global::B(); - target.Value.EnsureCapacity(source.Value.Length + target.Value.Count); - foreach (var item in source.Value) - { - target.Value.Add(item); - } - - return target; - """ + var target = new global::B(); + target.Value.EnsureCapacity(source.Value.Length + target.Value.Count); + foreach (var item in source.Value) + { + target.Value.Add(item); + } + return target; + """ ); } diff --git a/test/Riok.Mapperly.Tests/Mapping/ValueTupleTest.cs b/test/Riok.Mapperly.Tests/Mapping/ValueTupleTest.cs index 3eda5ccf11..b62b7a3bb0 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ValueTupleTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ValueTupleTest.cs @@ -574,7 +574,7 @@ public void QueryableTupleToQueryableTuple() .HaveSingleMethodBody( """ #nullable disable - return System.Linq.Queryable.Select(source, x => new global::System.ValueTuple(x.A, x.B)); + return System.Linq.Queryable.Select(source, x => new global::System.ValueTuple(x.A, x.B)); #nullable enable """ ); @@ -594,7 +594,7 @@ public void QueryableTupleToIQueryableValueTuple() .HaveSingleMethodBody( """ #nullable disable - return System.Linq.Queryable.Select(source, x => new global::System.ValueTuple(x.A, x.B)); + return System.Linq.Queryable.Select(source, x => new global::System.ValueTuple(x.A, x.B)); #nullable enable """ );