Skip to content

Commit

Permalink
Release 0.6.0 (#17)
Browse files Browse the repository at this point in the history
* All arguments can now be generated as "named arguments". 
* Improved how we generate nullables for predefined types and enums
  • Loading branch information
MilleBo authored Jun 3, 2017
1 parent 1908dc5 commit edf9f64
Show file tree
Hide file tree
Showing 26 changed files with 246 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,15 @@ public void GetArgumentSyntax_WhenUsingIntArray_ShouldGetCorrectCode()
Assert.IsInstanceOf<ArgumentSyntax>(syntax);
Assert.AreEqual("newint[]{1,2}", syntax.ToString());
}

[Test]
public void GetArgumentSyntax_WhenUsingIntArrayAndAsNamedArgument_ShouldGetCorrectCode()
{
var argument = new ArrayInitializationArgument(typeof(int), new List<IArgument>() { new ValueArgument(1), new ValueArgument(2) }, "namedArgument");
var syntax = argument.GetArgumentSyntax();

Assert.IsInstanceOf<ArgumentSyntax>(syntax);
Assert.AreEqual("namedArgument:newint[]{1,2}", syntax.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentSyntax>(syntax);
Assert.AreEqual("namedArgument:newstring()", syntax.ToString());
}

[Test]
public void GetArgumentSyntax_WhenInitializeClassWithArgument_ShouldGetCorrectCode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,15 @@ public void GetArgumentSyntax_WhenUsingDictionary_ShouldGetCorrectCode()
Assert.IsInstanceOf<ArgumentSyntax>(syntax);
Assert.AreEqual("newDictionary<int,int>{[1]=2}", syntax.ToString());
}

[Test]
public void GetArgumentSyntax_WhenUsingDictionaryAsNamedArgument_ShouldGetCorrectCode()
{
var argument = new DictionaryInitializationArgument<int, int>(new Dictionary<int, IArgument>() { [1] = new ValueArgument(2) }, "namedArgument");
var syntax = argument.GetArgumentSyntax();

Assert.IsInstanceOf<ArgumentSyntax>(syntax);
Assert.AreEqual("namedArgument:newDictionary<int,int>{[1]=2}", syntax.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentSyntax>(syntax);
Assert.AreEqual("namedArgument:Do()", syntax.ToString());
}

[Test]
public void GetArgumentSyntax_WhenUsingReference_ShouldGetCode()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ArgumentSyntax>(syntax);
Assert.AreEqual("namedArgument:n=>MyMethod()", syntax.ToString());
}

[Test]
public void GetArgumentSyntax_WhenCreatingWithWithBlock_ShouldGetCorrectCode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentSyntax>(syntax);
Assert.AreEqual("namedArgument:()=>MyMethod()", syntax.ToString());
}

[Test]
public void GetArgumentSyntax_WhenCreatingWithParameter_ShouldGetCorrectCode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentSyntax>(syntax);
Assert.AreEqual("namedArgument:test", syntax.ToString());
}

[Test]
public void GetArgumentSyntax_WhenUsingMethodReference_ShouldGetCode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,15 @@ public void GetArgumentSyntax_WhenUsingType_ShouldGetCode()
Assert.IsInstanceOf<ArgumentSyntax>(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<ArgumentSyntax>(syntax);
Assert.AreEqual("namedArgument:typeof(int)", syntax.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentSyntax>(syntax);
Assert.AreEqual("namedArgument:1", syntax.ToString());
}

[Test]
public void GetArgumentSyntax_WhenUsingBooleanValue_ShouldGetCorrectFormat()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,15 @@ public void GetArgumentSyntax_WhenUsingNormalValue_ShouldGetCode()
Assert.IsInstanceOf<ArgumentSyntax>(syntax);
Assert.AreEqual("variableName", syntax.ToString());
}

[Test]
public void GetArgumentSyntax_WhenUsingNormalValueAsNamedArgument_ShouldGetCode()
{
var argument = new VariableArgument("variableName", "namedArgument");
var syntax = argument.GetArgumentSyntax();

Assert.IsInstanceOf<ArgumentSyntax>(syntax);
Assert.AreEqual("namedArgument:variableName", syntax.ToString());
}
}
}
27 changes: 27 additions & 0 deletions src/Testura.Code.Tests/Generators/Common/TypeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<IArgument> _arguments;

public ArrayInitializationArgument(Type type, IEnumerable<IArgument> arguments)
public ArrayInitializationArgument(Type type, IEnumerable<IArgument> arguments, string namedArgument = null)
: base(namedArgument)
{
if (type == null)
{
Expand All @@ -24,7 +25,7 @@ public ArrayInitializationArgument(Type type, IEnumerable<IArgument> arguments)
_arguments = arguments == null ? new List<IArgument>() : new List<IArgument>(arguments);
}

public ArgumentSyntax GetArgumentSyntax()
protected override ArgumentSyntax CreateArgumentSyntax()
{
SyntaxNodeOrToken[] syntaxNodeOrTokens = new SyntaxNodeOrToken[0];
if (_arguments.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -19,7 +20,7 @@ public BinaryExpressionArgument(IBinaryExpression binaryExpression)
_binaryExpression = binaryExpression;
}

public ArgumentSyntax GetArgumentSyntax()
protected override ArgumentSyntax CreateArgumentSyntax()
{
return SyntaxFactory.Argument(_binaryExpression.GetBinaryExpression());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IArgument> _arguments;
Expand All @@ -15,7 +15,9 @@ public class ClassInitializationArgument : IArgument
public ClassInitializationArgument(
Type type,
IEnumerable<IArgument> arguments = null,
IEnumerable<Type> genericTypes = null)
IEnumerable<Type> genericTypes = null,
string namedArgument = null)
: base(namedArgument)
{
if (type == null)
{
Expand All @@ -27,7 +29,7 @@ public ClassInitializationArgument(
_genericTypes = genericTypes == null ? new List<Type>() : new List<Type>(genericTypes);
}

public ArgumentSyntax GetArgumentSyntax()
protected override ArgumentSyntax CreateArgumentSyntax()
{
if (_genericTypes != null && _genericTypes.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@

namespace Testura.Code.Generators.Common.Arguments.ArgumentTypes
{
public class DictionaryInitializationArgument<T, T2> : IArgument
public class DictionaryInitializationArgument<T, T2> : Argument
{
private readonly Dictionary<T, IArgument> _dictionary;

public DictionaryInitializationArgument(IDictionary<T, IArgument> dictionary)
public DictionaryInitializationArgument(IDictionary<T, IArgument> dictionary, string namedArgument = null)
: base(namedArgument)
{
_dictionary = new Dictionary<T, IArgument>(dictionary);
}

public ArgumentSyntax GetArgumentSyntax()
protected override ArgumentSyntax CreateArgumentSyntax()
{
var syntaxNodeOrTokens = new List<SyntaxNodeOrToken>();
foreach (var dictionaryValue in _dictionary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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))
{
Expand Down
Loading

0 comments on commit edf9f64

Please sign in to comment.