From edf9f641c8d5b8ae0d4e43c3c780c664cb0ff693 Mon Sep 17 00:00:00 2001 From: MilleBo Date: Sat, 3 Jun 2017 22:05:25 +0200 Subject: [PATCH] Release 0.6.0 (#17) * All arguments can now be generated as "named arguments". * Improved how we generate nullables for predefined types and enums --- .../ArrayInitializeArgumentTests.cs | 10 ++++ .../ClassInitializationArgumentTests.cs | 10 ++++ .../ArgumentTypes/DictionaryArgumentTests.cs | 10 ++++ .../ArgumentTypes/InvocationArgumentTests.cs | 11 +++++ .../ArgumentTypes/LambdaArgumentTests.cs | 17 ++++--- .../ParenthesizedLambdaArgumentTests.cs | 10 ++++ .../ArgumentTypes/ReferenceArgumentTests.cs | 10 ++++ .../ArgumentTypes/TypeOfArgumentTests.cs | 10 ++++ .../ArgumentTypes/ValueArgumentTests.cs | 9 ++++ .../ArgumentTypes/VariableArgumentTests.cs | 10 ++++ .../Generators/Common/TypeGeneratorTests.cs | 27 +++++++++++ .../Arguments/ArgumentTypes/Argument.cs | 29 +++++++++++ .../ArrayInitializationArgument.cs | 7 +-- .../ArgumentTypes/BinaryExpressionArgument.cs | 7 +-- .../ClassinitializationArgument.cs | 8 ++-- .../DictionaryInitializationArgument.cs | 7 +-- .../ArgumentTypes/InvocationArgument.cs | 10 ++-- .../Arguments/ArgumentTypes/LambdaArgument.cs | 10 ++-- .../ParenthesizedLambdaArgument.cs | 10 ++-- .../ArgumentTypes/ReferenceArgument.cs | 7 +-- .../Arguments/ArgumentTypes/TypeOfArgument.cs | 7 +-- .../Arguments/ArgumentTypes/ValueArgument.cs | 10 ++-- .../ArgumentTypes/VariableArgument.cs | 7 +-- .../Generators/Common/TypeGenerator.cs | 48 +++++++++++++------ src/Testura.Code/Properties/AssemblyInfo.cs | 4 +- src/Testura.Code/Testura.Code.csproj | 1 + 26 files changed, 246 insertions(+), 60 deletions(-) create mode 100644 src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/Argument.cs diff --git a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ArrayInitializeArgumentTests.cs b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ArrayInitializeArgumentTests.cs index 801b977..12dcd02 100644 --- a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ArrayInitializeArgumentTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ArrayInitializeArgumentTests.cs @@ -17,5 +17,15 @@ public void GetArgumentSyntax_WhenUsingIntArray_ShouldGetCorrectCode() Assert.IsInstanceOf(syntax); Assert.AreEqual("newint[]{1,2}", syntax.ToString()); } + + [Test] + public void GetArgumentSyntax_WhenUsingIntArrayAndAsNamedArgument_ShouldGetCorrectCode() + { + var argument = new ArrayInitializationArgument(typeof(int), new List() { new ValueArgument(1), new ValueArgument(2) }, "namedArgument"); + var syntax = argument.GetArgumentSyntax(); + + Assert.IsInstanceOf(syntax); + Assert.AreEqual("namedArgument:newint[]{1,2}", syntax.ToString()); + } } } diff --git a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ClassInitializationArgumentTests.cs b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ClassInitializationArgumentTests.cs index f50b6bf..53c8920 100644 --- a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ClassInitializationArgumentTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ClassInitializationArgumentTests.cs @@ -19,6 +19,16 @@ public void GetArgumentSyntax_WhenInitializeClass_ShouldGetCorrectCode() Assert.AreEqual("newstring()", syntax.ToString()); } + [Test] + public void GetArgumentSyntax_WhenInitializeClassAsNamedArgument_ShouldGetCorrectCode() + { + var argument = new ClassInitializationArgument(typeof(String), namedArgument: "namedArgument"); + var syntax = argument.GetArgumentSyntax(); + + Assert.IsInstanceOf(syntax); + Assert.AreEqual("namedArgument:newstring()", syntax.ToString()); + } + [Test] public void GetArgumentSyntax_WhenInitializeClassWithArgument_ShouldGetCorrectCode() { diff --git a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/DictionaryArgumentTests.cs b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/DictionaryArgumentTests.cs index 473c76c..4da5f71 100644 --- a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/DictionaryArgumentTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/DictionaryArgumentTests.cs @@ -17,5 +17,15 @@ public void GetArgumentSyntax_WhenUsingDictionary_ShouldGetCorrectCode() Assert.IsInstanceOf(syntax); Assert.AreEqual("newDictionary{[1]=2}", syntax.ToString()); } + + [Test] + public void GetArgumentSyntax_WhenUsingDictionaryAsNamedArgument_ShouldGetCorrectCode() + { + var argument = new DictionaryInitializationArgument(new Dictionary() { [1] = new ValueArgument(2) }, "namedArgument"); + var syntax = argument.GetArgumentSyntax(); + + Assert.IsInstanceOf(syntax); + Assert.AreEqual("namedArgument:newDictionary{[1]=2}", syntax.ToString()); + } } } diff --git a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/InvocationArgumentTests.cs b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/InvocationArgumentTests.cs index db3a3cc..365e810 100644 --- a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/InvocationArgumentTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/InvocationArgumentTests.cs @@ -20,6 +20,17 @@ public void GetArgumentSyntax_WhenUsingMethod_ShouldGetCode() Assert.AreEqual("Do()", syntax.ToString()); } + [Test] + public void GetArgumentSyntax_WhenUsingMethodAsNamedArgument_ShouldGetCode() + { + var argument = + new InvocationArgument(Statement.Expression.Invoke(new MethodReference("Do")).AsExpression(), namedArgument:"namedArgument"); + var syntax = argument.GetArgumentSyntax(); + + Assert.IsInstanceOf(syntax); + Assert.AreEqual("namedArgument:Do()", syntax.ToString()); + } + [Test] public void GetArgumentSyntax_WhenUsingReference_ShouldGetCode() { diff --git a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/LambdaArgumentTests.cs b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/LambdaArgumentTests.cs index 913f891..8372218 100644 --- a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/LambdaArgumentTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/LambdaArgumentTests.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.CSharp.Syntax; using NUnit.Framework; using Testura.Code.Generators.Common; using Testura.Code.Generators.Common.Arguments.ArgumentTypes; @@ -26,6 +21,16 @@ public void GetArgumentSyntax_WhenCreatingEmpty_ShouldGetCorrectCode() Assert.AreEqual("n=>MyMethod()", syntax.ToString()); } + [Test] + public void GetArgumentSyntax_WhenCreatingEmptyAsNamedArgument_ShouldGetCorrectCode() + { + var argument = new LambdaArgument(Statement.Expression.Invoke("MyMethod").AsExpression(), "n", namedArgument: "namedArgument"); + var syntax = argument.GetArgumentSyntax(); + + Assert.IsInstanceOf(syntax); + Assert.AreEqual("namedArgument:n=>MyMethod()", syntax.ToString()); + } + [Test] public void GetArgumentSyntax_WhenCreatingWithWithBlock_ShouldGetCorrectCode() { diff --git a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ParenthesizedLambdaArgumentTests.cs b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ParenthesizedLambdaArgumentTests.cs index b746868..32ceac8 100644 --- a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ParenthesizedLambdaArgumentTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ParenthesizedLambdaArgumentTests.cs @@ -20,6 +20,16 @@ public void GetArgumentSyntax_WhenCreatingEmpty_ShouldGetCorrectCode() Assert.AreEqual("()=>MyMethod()", syntax.ToString()); } + [Test] + public void GetArgumentSyntax_WhenCreatingEmptyAsNamedArgument_ShouldGetCorrectCode() + { + var argument = new ParenthesizedLambdaArgument(Statement.Expression.Invoke("MyMethod").AsExpression(), namedArgument: "namedArgument"); + var syntax = argument.GetArgumentSyntax(); + + Assert.IsInstanceOf(syntax); + Assert.AreEqual("namedArgument:()=>MyMethod()", syntax.ToString()); + } + [Test] public void GetArgumentSyntax_WhenCreatingWithParameter_ShouldGetCorrectCode() { diff --git a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ReferenceArgumentTests.cs b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ReferenceArgumentTests.cs index 0cede4a..7008750 100644 --- a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ReferenceArgumentTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ReferenceArgumentTests.cs @@ -18,6 +18,16 @@ public void GetArgumentSyntax_WhenUsingVariableReference_ShouldGetCode() Assert.AreEqual("test", syntax.ToString()); } + [Test] + public void GetArgumentSyntax_WhenUsingVariableReferenceAsNamedArgument_ShouldGetCode() + { + var argument = new ReferenceArgument(new VariableReference("test"), "namedArgument"); + var syntax = argument.GetArgumentSyntax(); + + Assert.IsInstanceOf(syntax); + Assert.AreEqual("namedArgument:test", syntax.ToString()); + } + [Test] public void GetArgumentSyntax_WhenUsingMethodReference_ShouldGetCode() { diff --git a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/TypeOfArgumentTests.cs b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/TypeOfArgumentTests.cs index 1eb6b8c..5a9257a 100644 --- a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/TypeOfArgumentTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/TypeOfArgumentTests.cs @@ -16,5 +16,15 @@ public void GetArgumentSyntax_WhenUsingType_ShouldGetCode() Assert.IsInstanceOf(syntax); Assert.AreEqual("typeof(int)", syntax.ToString()); } + + [Test] + public void GetArgumentSyntax_WhenUsingTypeAsNamedArgument_ShouldGetCode() + { + var argument = new TypeOfArgument(typeof(int), "namedArgument"); + var syntax = argument.GetArgumentSyntax(); + + Assert.IsInstanceOf(syntax); + Assert.AreEqual("namedArgument:typeof(int)", syntax.ToString()); + } } } diff --git a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ValueArgumentTests.cs b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ValueArgumentTests.cs index 4406b8c..9226884 100644 --- a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ValueArgumentTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/ValueArgumentTests.cs @@ -18,6 +18,15 @@ public void GetArgumentSyntax_WhenUsingNumberValue_ShouldGetCode() Assert.AreEqual("1", syntax.ToString()); } + public void GetArgumentSyntax_WhenUsingNumberValueAsNamedArgument_ShouldGetCode() + { + var argument = new ValueArgument(1, "namedArgument"); + var syntax = argument.GetArgumentSyntax(); + + Assert.IsInstanceOf(syntax); + Assert.AreEqual("namedArgument:1", syntax.ToString()); + } + [Test] public void GetArgumentSyntax_WhenUsingBooleanValue_ShouldGetCorrectFormat() { diff --git a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/VariableArgumentTests.cs b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/VariableArgumentTests.cs index 6dbfdb5..7f8a662 100644 --- a/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/VariableArgumentTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/Arguments/ArgumentTypes/VariableArgumentTests.cs @@ -16,5 +16,15 @@ public void GetArgumentSyntax_WhenUsingNormalValue_ShouldGetCode() Assert.IsInstanceOf(syntax); Assert.AreEqual("variableName", syntax.ToString()); } + + [Test] + public void GetArgumentSyntax_WhenUsingNormalValueAsNamedArgument_ShouldGetCode() + { + var argument = new VariableArgument("variableName", "namedArgument"); + var syntax = argument.GetArgumentSyntax(); + + Assert.IsInstanceOf(syntax); + Assert.AreEqual("namedArgument:variableName", syntax.ToString()); + } } } diff --git a/src/Testura.Code.Tests/Generators/Common/TypeGeneratorTests.cs b/src/Testura.Code.Tests/Generators/Common/TypeGeneratorTests.cs index 7d3d043..5bb6689 100644 --- a/src/Testura.Code.Tests/Generators/Common/TypeGeneratorTests.cs +++ b/src/Testura.Code.Tests/Generators/Common/TypeGeneratorTests.cs @@ -9,6 +9,8 @@ namespace Testura.Code.Tests.Generators.Common [TestFixture] public class TypeGeneratorTests { + private enum MyCustomEnum { Stuff } + [TestCase(typeof(int), "int")] [TestCase(typeof(double), "double")] [TestCase(typeof(float), "float")] @@ -22,11 +24,36 @@ public class TypeGeneratorTests [TestCase(typeof(byte), "byte")] [TestCase(typeof(char), "char")] [TestCase(typeof(bool), "bool")] + [TestCase(typeof(MyCustomEnum), "MyCustomEnum")] public void Create_WhenCreatingPredefinedTypes_ShouldGenerateCorrectCode(Type type, string expected) { Assert.AreEqual(expected, TypeGenerator.Create(type).ToString()); } + [TestCase(typeof(int?), "int?")] + [TestCase(typeof(double?), "double?")] + [TestCase(typeof(float?), "float?")] + [TestCase(typeof(decimal?), "decimal?")] + [TestCase(typeof(long?), "long?")] + [TestCase(typeof(uint?), "uint?")] + [TestCase(typeof(ushort?), "ushort?")] + [TestCase(typeof(ulong?), "ulong?")] + [TestCase(typeof(sbyte?), "sbyte?")] + [TestCase(typeof(byte?), "byte?")] + [TestCase(typeof(char?), "char?")] + [TestCase(typeof(bool?), "bool?")] + [TestCase(typeof(MyCustomEnum?), "MyCustomEnum?")] + public void Create_WhenCreatingPredefinedTypesAsNullable_ShouldGenerateCorrectCode(Type type, string expected) + { + if (Nullable.GetUnderlyingType(type) != null) + { + // It's Nullable + var o = 0; + } + + Assert.AreEqual(expected, TypeGenerator.Create(type).ToString()); + } + [Test] public void Create_WhenCreatingWithNoPredfinedType_ShouldGenerateCode() { diff --git a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/Argument.cs b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/Argument.cs new file mode 100644 index 0000000..43cd9cc --- /dev/null +++ b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/Argument.cs @@ -0,0 +1,29 @@ +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace Testura.Code.Generators.Common.Arguments.ArgumentTypes +{ + public abstract class Argument : IArgument + { + private readonly string _namedArgument; + + protected Argument(string namedArgument = null) + { + _namedArgument = namedArgument; + } + + public ArgumentSyntax GetArgumentSyntax() + { + var argumentSyntax = CreateArgumentSyntax(); + if (_namedArgument != null) + { + return argumentSyntax.WithNameColon(NameColon(IdentifierName(_namedArgument))); + } + + return argumentSyntax; + } + + protected abstract ArgumentSyntax CreateArgumentSyntax(); + + } +} diff --git a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ArrayInitializationArgument.cs b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ArrayInitializationArgument.cs index 56defc7..cdec05b 100644 --- a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ArrayInitializationArgument.cs +++ b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ArrayInitializationArgument.cs @@ -8,12 +8,13 @@ namespace Testura.Code.Generators.Common.Arguments.ArgumentTypes { - public class ArrayInitializationArgument : IArgument + public class ArrayInitializationArgument : Argument { private readonly Type _type; private readonly IList _arguments; - public ArrayInitializationArgument(Type type, IEnumerable arguments) + public ArrayInitializationArgument(Type type, IEnumerable arguments, string namedArgument = null) + : base(namedArgument) { if (type == null) { @@ -24,7 +25,7 @@ public ArrayInitializationArgument(Type type, IEnumerable arguments) _arguments = arguments == null ? new List() : new List(arguments); } - public ArgumentSyntax GetArgumentSyntax() + protected override ArgumentSyntax CreateArgumentSyntax() { SyntaxNodeOrToken[] syntaxNodeOrTokens = new SyntaxNodeOrToken[0]; if (_arguments.Any()) diff --git a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/BinaryExpressionArgument.cs b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/BinaryExpressionArgument.cs index 123a662..39db5e9 100644 --- a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/BinaryExpressionArgument.cs +++ b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/BinaryExpressionArgument.cs @@ -5,11 +5,12 @@ namespace Testura.Code.Generators.Common.Arguments.ArgumentTypes { - public class BinaryExpressionArgument : IArgument + public class BinaryExpressionArgument : Argument { private readonly IBinaryExpression _binaryExpression; - public BinaryExpressionArgument(IBinaryExpression binaryExpression) + public BinaryExpressionArgument(IBinaryExpression binaryExpression, string namedArgument = null) + : base(namedArgument) { if (binaryExpression == null) { @@ -19,7 +20,7 @@ public BinaryExpressionArgument(IBinaryExpression binaryExpression) _binaryExpression = binaryExpression; } - public ArgumentSyntax GetArgumentSyntax() + protected override ArgumentSyntax CreateArgumentSyntax() { return SyntaxFactory.Argument(_binaryExpression.GetBinaryExpression()); } diff --git a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ClassinitializationArgument.cs b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ClassinitializationArgument.cs index 2b117a0..f9d4cc9 100644 --- a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ClassinitializationArgument.cs +++ b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ClassinitializationArgument.cs @@ -6,7 +6,7 @@ namespace Testura.Code.Generators.Common.Arguments.ArgumentTypes { - public class ClassInitializationArgument : IArgument + public class ClassInitializationArgument : Argument { private readonly Type _type; private readonly IList _arguments; @@ -15,7 +15,9 @@ public class ClassInitializationArgument : IArgument public ClassInitializationArgument( Type type, IEnumerable arguments = null, - IEnumerable genericTypes = null) + IEnumerable genericTypes = null, + string namedArgument = null) + : base(namedArgument) { if (type == null) { @@ -27,7 +29,7 @@ public ClassInitializationArgument( _genericTypes = genericTypes == null ? new List() : new List(genericTypes); } - public ArgumentSyntax GetArgumentSyntax() + protected override ArgumentSyntax CreateArgumentSyntax() { if (_genericTypes != null && _genericTypes.Any()) { diff --git a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/DictionaryInitializationArgument.cs b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/DictionaryInitializationArgument.cs index b944b85..b5f3cf3 100644 --- a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/DictionaryInitializationArgument.cs +++ b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/DictionaryInitializationArgument.cs @@ -7,16 +7,17 @@ namespace Testura.Code.Generators.Common.Arguments.ArgumentTypes { - public class DictionaryInitializationArgument : IArgument + public class DictionaryInitializationArgument : Argument { private readonly Dictionary _dictionary; - public DictionaryInitializationArgument(IDictionary dictionary) + public DictionaryInitializationArgument(IDictionary dictionary, string namedArgument = null) + : base(namedArgument) { _dictionary = new Dictionary(dictionary); } - public ArgumentSyntax GetArgumentSyntax() + protected override ArgumentSyntax CreateArgumentSyntax() { var syntaxNodeOrTokens = new List(); foreach (var dictionaryValue in _dictionary) diff --git a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/InvocationArgument.cs b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/InvocationArgument.cs index 3274552..dce1ec4 100644 --- a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/InvocationArgument.cs +++ b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/InvocationArgument.cs @@ -6,12 +6,13 @@ namespace Testura.Code.Generators.Common.Arguments.ArgumentTypes { - public class InvocationArgument : IArgument + public class InvocationArgument : Argument { private readonly ExpressionSyntax _invocation; private readonly Type _castTo; - public InvocationArgument(ExpressionSyntax invocation, Type castTo = null) + public InvocationArgument(ExpressionSyntax invocation, Type castTo = null, string namedArgument = null) + : base(namedArgument) { if (invocation == null) { @@ -22,7 +23,8 @@ public InvocationArgument(ExpressionSyntax invocation, Type castTo = null) _castTo = castTo ?? typeof(void); } - public InvocationArgument(VariableReference reference, Type castTo = null) + public InvocationArgument(VariableReference reference, Type castTo = null, string namedArgument = null) + : base(namedArgument) { if (reference == null) { @@ -33,7 +35,7 @@ public InvocationArgument(VariableReference reference, Type castTo = null) _castTo = castTo ?? typeof(void); } - public ArgumentSyntax GetArgumentSyntax() + protected override ArgumentSyntax CreateArgumentSyntax() { if (_castTo != typeof(void)) { diff --git a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/LambdaArgument.cs b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/LambdaArgument.cs index ccc98cb..494cde3 100644 --- a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/LambdaArgument.cs +++ b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/LambdaArgument.cs @@ -4,13 +4,14 @@ namespace Testura.Code.Generators.Common.Arguments.ArgumentTypes { - public class LambdaArgument : IArgument + public class LambdaArgument : Argument { private readonly ExpressionSyntax _expressionSyntax; private readonly string _parameterName; private readonly BlockSyntax _blockSyntax; - public LambdaArgument(ExpressionSyntax expressionSyntax, string parameterName) + public LambdaArgument(ExpressionSyntax expressionSyntax, string parameterName, string namedArgument = null) + : base(namedArgument) { if (expressionSyntax == null) { @@ -26,7 +27,8 @@ public LambdaArgument(ExpressionSyntax expressionSyntax, string parameterName) _parameterName = parameterName; } - public LambdaArgument(BlockSyntax blockSyntax, string parameterName) + public LambdaArgument(BlockSyntax blockSyntax, string parameterName, string namedArgument = null) + : base(namedArgument) { if (blockSyntax == null) { @@ -42,7 +44,7 @@ public LambdaArgument(BlockSyntax blockSyntax, string parameterName) _parameterName = parameterName; } - public ArgumentSyntax GetArgumentSyntax() + protected override ArgumentSyntax CreateArgumentSyntax() { SimpleLambdaExpressionSyntax expression = null; diff --git a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ParenthesizedLambdaArgument.cs b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ParenthesizedLambdaArgument.cs index 5f81849..d13358e 100644 --- a/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ParenthesizedLambdaArgument.cs +++ b/src/Testura.Code/Generators/Common/Arguments/ArgumentTypes/ParenthesizedLambdaArgument.cs @@ -7,13 +7,14 @@ namespace Testura.Code.Generators.Common.Arguments.ArgumentTypes { - public class ParenthesizedLambdaArgument : IArgument + public class ParenthesizedLambdaArgument : Argument { private readonly IEnumerable _parameters; private readonly ExpressionSyntax _expressionSyntax; private readonly BlockSyntax _blockSyntax; - public ParenthesizedLambdaArgument(ExpressionSyntax expressionSyntax, IEnumerable parameters = null) + public ParenthesizedLambdaArgument(ExpressionSyntax expressionSyntax, IEnumerable parameters = null, string namedArgument = null) + : base(namedArgument) { if (expressionSyntax == null) { @@ -24,7 +25,8 @@ public ParenthesizedLambdaArgument(ExpressionSyntax expressionSyntax, IEnumerabl _expressionSyntax = expressionSyntax; } - public ParenthesizedLambdaArgument(BlockSyntax blockSyntax, IEnumerable parameters = null) + public ParenthesizedLambdaArgument(BlockSyntax blockSyntax, IEnumerable parameters = null, string namedArgument = null) + : base(namedArgument) { if (blockSyntax == null) { @@ -35,7 +37,7 @@ public ParenthesizedLambdaArgument(BlockSyntax blockSyntax, IEnumerable(OmittedArraySizeExpression())))); } - if (type.IsGenericType) + if (type.IsGenericType && type.GetGenericTypeDefinition() != typeof(Nullable<>)) { return CreateGenericType(type); } @@ -48,72 +48,90 @@ public static TypeSyntax Create(Type type) private static TypeSyntax CheckPredefinedTypes(Type type) { + TypeSyntax typeSyntax = null; + var isNullable = false; + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + type = type.GetGenericArguments()[0]; + isNullable = true; + } + if (type == typeof(int)) { - return PredefinedType(Token(SyntaxKind.IntKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.IntKeyword)); } if (type == typeof(double)) { - return PredefinedType(Token(SyntaxKind.DoubleKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.DoubleKeyword)); } if (type == typeof(long)) { - return PredefinedType(Token(SyntaxKind.LongKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.LongKeyword)); } if (type == typeof(ulong)) { - return PredefinedType(Token(SyntaxKind.ULongKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.ULongKeyword)); } if (type == typeof(float)) { - return PredefinedType(Token(SyntaxKind.FloatKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.FloatKeyword)); } if (type == typeof(byte)) { - return PredefinedType(Token(SyntaxKind.ByteKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.ByteKeyword)); } if (type == typeof(string)) { - return PredefinedType(Token(SyntaxKind.StringKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.StringKeyword)); } if (type == typeof(sbyte)) { - return PredefinedType(Token(SyntaxKind.SByteKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.SByteKeyword)); } if (type == typeof(ushort)) { - return PredefinedType(Token(SyntaxKind.UShortKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.UShortKeyword)); } if (type == typeof(uint)) { - return PredefinedType(Token(SyntaxKind.UIntKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.UIntKeyword)); } if (type == typeof(bool)) { - return PredefinedType(Token(SyntaxKind.BoolKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.BoolKeyword)); } if (type == typeof(char)) { - return PredefinedType(Token(SyntaxKind.CharKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.CharKeyword)); } if (type == typeof(decimal)) { - return PredefinedType(Token(SyntaxKind.DecimalKeyword)); + typeSyntax = PredefinedType(Token(SyntaxKind.DecimalKeyword)); + } + + if (type.IsEnum) + { + typeSyntax = IdentifierName(type.Name); + } + + if (typeSyntax != null && isNullable) + { + return NullableType(typeSyntax); } - return null; + return typeSyntax; } private static TypeSyntax CreateGenericType(Type type) diff --git a/src/Testura.Code/Properties/AssemblyInfo.cs b/src/Testura.Code/Properties/AssemblyInfo.cs index 59e1c4b..92dbc55 100644 --- a/src/Testura.Code/Properties/AssemblyInfo.cs +++ b/src/Testura.Code/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.5.2.0")] -[assembly: AssemblyFileVersion("0.5.2.0")] +[assembly: AssemblyVersion("0.6.0.0")] +[assembly: AssemblyFileVersion("0.6.0.0")] diff --git a/src/Testura.Code/Testura.Code.csproj b/src/Testura.Code/Testura.Code.csproj index 47ba65b..1a96761 100644 --- a/src/Testura.Code/Testura.Code.csproj +++ b/src/Testura.Code/Testura.Code.csproj @@ -79,6 +79,7 @@ +