diff --git a/src/Unitverse.Core.Tests/Resources/UnmanagedConstraint.txt b/src/Unitverse.Core.Tests/Resources/UnmanagedConstraint.txt new file mode 100644 index 00000000..9fbd1b53 --- /dev/null +++ b/src/Unitverse.Core.Tests/Resources/UnmanagedConstraint.txt @@ -0,0 +1,3 @@ +public struct Foo where T : unmanaged { + public void Bar() { } +} \ No newline at end of file diff --git a/src/Unitverse.Core.Tests/TestClasses.Designer.cs b/src/Unitverse.Core.Tests/TestClasses.Designer.cs index 3e0ebbc3..2ac4bc87 100644 --- a/src/Unitverse.Core.Tests/TestClasses.Designer.cs +++ b/src/Unitverse.Core.Tests/TestClasses.Designer.cs @@ -2181,6 +2181,17 @@ public static string TypeGenericDisambiguation { } } + /// + /// Looks up a localized string similar to public struct Foo<T> where T : unmanaged { + /// public void Bar() { } + ///}. + /// + public static string UnmanagedConstraint { + get { + return ResourceManager.GetString("UnmanagedConstraint", resourceCulture); + } + } + /// /// Looks up a localized string similar to // # UseConstructorForTestClassSetUp=true ///namespace TestNamespace diff --git a/src/Unitverse.Core.Tests/TestClasses.resx b/src/Unitverse.Core.Tests/TestClasses.resx index e0f867ca..deca5987 100644 --- a/src/Unitverse.Core.Tests/TestClasses.resx +++ b/src/Unitverse.Core.Tests/TestClasses.resx @@ -346,6 +346,9 @@ Resources\TypeGenericDisambiguation.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + + Resources\UnmanagedConstraint.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + Resources\UseConstructorForClassSetup.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 diff --git a/src/Unitverse.Core.Tests/UnitTestGeneratorTests.cs b/src/Unitverse.Core.Tests/UnitTestGeneratorTests.cs index 8ac59fb6..9b18c652 100644 --- a/src/Unitverse.Core.Tests/UnitTestGeneratorTests.cs +++ b/src/Unitverse.Core.Tests/UnitTestGeneratorTests.cs @@ -78,17 +78,22 @@ public static IEnumerable TestClassResourceNames entryKeys = entryKeys.Where(x => x.IndexOf("FileScoped", StringComparison.OrdinalIgnoreCase) < 0).ToList(); #endif - var baseSet = entryKeys.Cast().ToArray() - .CrossJoin(frameworks) + var baseSet = entryKeys.Select(x => new List { x }); + + var secondarySet = frameworks .CrossJoin(mocks) - .CrossJoin(AssertionFrameworkVariations); + .CrossJoin(AssertionFrameworkVariations) + .CrossJoin(AutoFixtureVariations); - int autoFixtureRow = 0; + int secondaryRow = 0; foreach (var row in baseSet) { - autoFixtureRow++; - var autoFixtureValues = AutoFixtureVariations[autoFixtureRow % AutoFixtureVariations.Count]; - yield return row.Concat(autoFixtureValues).ToArray(); + for (int i = 0; i < 50; i++) + { + secondaryRow++; + var secondaryValues = secondarySet[secondaryRow % secondarySet.Count]; + yield return row.Concat(secondaryValues).ToArray(); + } } } } diff --git a/src/Unitverse.Core/Frameworks/IMockingFramework.cs b/src/Unitverse.Core/Frameworks/IMockingFramework.cs index 1a8984b8..cd4bf0c0 100644 --- a/src/Unitverse.Core/Frameworks/IMockingFramework.cs +++ b/src/Unitverse.Core/Frameworks/IMockingFramework.cs @@ -26,6 +26,6 @@ public interface IMockingFramework ExpressionSyntax GetAssertionFor(IMethodSymbol dependencyMethod, string mockFieldName, SemanticModel model, IFrameworkSet frameworkSet, IEnumerable parameters); - ExpressionSyntax? GetObjectCreationExpression(TypeSyntax typeSyntax); + ExpressionSyntax? GetObjectCreationExpression(TypeSyntax typeSyntax, bool isReferenceType); } } \ No newline at end of file diff --git a/src/Unitverse.Core/Frameworks/Mocking/AutoFixtureMockingAdaptor.cs b/src/Unitverse.Core/Frameworks/Mocking/AutoFixtureMockingAdaptor.cs index 2950dcd5..54b87651 100644 --- a/src/Unitverse.Core/Frameworks/Mocking/AutoFixtureMockingAdaptor.cs +++ b/src/Unitverse.Core/Frameworks/Mocking/AutoFixtureMockingAdaptor.cs @@ -45,7 +45,7 @@ public TypeSyntax GetFieldType(TypeSyntax type) return _targetFramework.GetFieldType(type); } - public ExpressionSyntax GetObjectCreationExpression(TypeSyntax typeSyntax) + public ExpressionSyntax GetObjectCreationExpression(TypeSyntax typeSyntax, bool isReferenceType) { return AutoFixtureHelper.Create(typeSyntax, _context); } diff --git a/src/Unitverse.Core/Frameworks/Mocking/FakeItEasyMockingFramework.cs b/src/Unitverse.Core/Frameworks/Mocking/FakeItEasyMockingFramework.cs index f28b56fa..84524ff1 100644 --- a/src/Unitverse.Core/Frameworks/Mocking/FakeItEasyMockingFramework.cs +++ b/src/Unitverse.Core/Frameworks/Mocking/FakeItEasyMockingFramework.cs @@ -77,7 +77,7 @@ private static ExpressionSyntax ACallTo(ExpressionSyntax methodCall) return Generate.MemberInvocation("A", "CallTo", Generate.ParenthesizedLambdaExpression(methodCall)); } - public ExpressionSyntax? GetObjectCreationExpression(TypeSyntax type) + public ExpressionSyntax? GetObjectCreationExpression(TypeSyntax type, bool isReferenceType) { return null; } diff --git a/src/Unitverse.Core/Frameworks/Mocking/JustMockMockingFramework.cs b/src/Unitverse.Core/Frameworks/Mocking/JustMockMockingFramework.cs index 8e7004a0..7fb9a19e 100644 --- a/src/Unitverse.Core/Frameworks/Mocking/JustMockMockingFramework.cs +++ b/src/Unitverse.Core/Frameworks/Mocking/JustMockMockingFramework.cs @@ -82,7 +82,7 @@ private static ExpressionSyntax Mock(string methodName, ExpressionSyntax methodC return Generate.MemberInvocation("Mock", methodName, Generate.ParenthesizedLambdaExpression(methodCall)); } - public ExpressionSyntax? GetObjectCreationExpression(TypeSyntax type) + public ExpressionSyntax? GetObjectCreationExpression(TypeSyntax type, bool isReferenceType) { return null; } diff --git a/src/Unitverse.Core/Frameworks/Mocking/MoqAutoMockMockingFramework.cs b/src/Unitverse.Core/Frameworks/Mocking/MoqAutoMockMockingFramework.cs index 7425b4d8..ff0ab4e0 100644 --- a/src/Unitverse.Core/Frameworks/Mocking/MoqAutoMockMockingFramework.cs +++ b/src/Unitverse.Core/Frameworks/Mocking/MoqAutoMockMockingFramework.cs @@ -53,16 +53,16 @@ public override ExpressionSyntax GetFieldInitializer(TypeSyntax type) return base.GetFieldInitializer(type); } - public override ExpressionSyntax? GetObjectCreationExpression(TypeSyntax type) + public override ExpressionSyntax? GetObjectCreationExpression(TypeSyntax type, bool isReferenceType) { - if (IsActive) + if (IsActive && isReferenceType) { _context.MocksUsed = true; _context.CurrentMethod.AddRequirement(Requirements.AutoMocker); return Generate.MemberInvocation("mocker", Generate.GenericName("CreateInstance", type)); } - return base.GetObjectCreationExpression(type); + return base.GetObjectCreationExpression(type, isReferenceType); } } } \ No newline at end of file diff --git a/src/Unitverse.Core/Frameworks/Mocking/MoqMockingFramework.cs b/src/Unitverse.Core/Frameworks/Mocking/MoqMockingFramework.cs index 81f3f013..5e19cc5e 100644 --- a/src/Unitverse.Core/Frameworks/Mocking/MoqMockingFramework.cs +++ b/src/Unitverse.Core/Frameworks/Mocking/MoqMockingFramework.cs @@ -118,7 +118,7 @@ private static ExpressionSyntax Mock(string actionName, string mockFieldName, Ex return Generate.MemberInvocation(mockFieldName, actionName, SyntaxFactory.SimpleLambdaExpression(Generate.Parameter("mock"), methodCall)); } - public virtual ExpressionSyntax? GetObjectCreationExpression(TypeSyntax type) + public virtual ExpressionSyntax? GetObjectCreationExpression(TypeSyntax type, bool isReferenceType) { return null; } diff --git a/src/Unitverse.Core/Frameworks/Mocking/NSubstituteMockingFramework.cs b/src/Unitverse.Core/Frameworks/Mocking/NSubstituteMockingFramework.cs index 134c3cef..f668f94e 100644 --- a/src/Unitverse.Core/Frameworks/Mocking/NSubstituteMockingFramework.cs +++ b/src/Unitverse.Core/Frameworks/Mocking/NSubstituteMockingFramework.cs @@ -72,7 +72,7 @@ public ExpressionSyntax GetAssertionFor(IMethodSymbol dependencyMethod, string m return MockingHelper.GetMethodCall(dependencyMethod, received, MockingHelper.TranslateArgumentFunc(GetArgument, parameters), frameworkSet.Context); } - public ExpressionSyntax? GetObjectCreationExpression(TypeSyntax type) + public ExpressionSyntax? GetObjectCreationExpression(TypeSyntax type, bool isReferenceType) { return null; } diff --git a/src/Unitverse.Core/Generation/CompilationUnitStrategy.cs b/src/Unitverse.Core/Generation/CompilationUnitStrategy.cs index 6e33ffe8..bc5c1be4 100644 --- a/src/Unitverse.Core/Generation/CompilationUnitStrategy.cs +++ b/src/Unitverse.Core/Generation/CompilationUnitStrategy.cs @@ -90,8 +90,9 @@ public void AddTypeParameterAliases(ClassModel classModel, IGenerationContext co if (constraint != null) { - var typeConstraints = constraint.Constraints.OfType().Select(x => x.Type).Select(x => classModel.SemanticModel.GetTypeInfo(x)) ?? Enumerable.Empty(); - ITypeSymbol[] constrainableTypes = typeConstraints.Select(x => x.Type).WhereNotNull().Where(x => !(x is IErrorTypeSymbol)).ToArray(); + var typeConstraints = constraint.Constraints.OfType().Select(x => x.Type); + var typeConstraintInfo = typeConstraints.Select(x => classModel.SemanticModel.GetTypeInfo(x)) ?? Enumerable.Empty(); + ITypeSymbol[] constrainableTypes = typeConstraintInfo.Select(x => x.Type).WhereNotNull().Where(x => !(x is IErrorTypeSymbol)).ToArray(); if (constrainableTypes.Any()) { derivedType = TypeHelper.FindDerivedNonAbstractType(constrainableTypes); @@ -100,6 +101,15 @@ public void AddTypeParameterAliases(ClassModel classModel, IGenerationContext co nameSyntax = SyntaxFactory.IdentifierName(derivedType.ToFullName()); } } + else + { + var structOrUnmanaged = typeConstraints.OfType().Select(x => x.Identifier.Text).Any(x => + string.Equals(x, "struct", StringComparison.OrdinalIgnoreCase) || string.Equals(x, "unmanaged", StringComparison.OrdinalIgnoreCase)); + if (structOrUnmanaged) + { + nameSyntax = SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System"), SyntaxFactory.IdentifierName("Int32")); + } + } } if (!context.GenericTypes.ContainsKey(parameter.Identifier.ValueText)) diff --git a/src/Unitverse.Core/Models/ClassModel.cs b/src/Unitverse.Core/Models/ClassModel.cs index 0b140a29..e374254b 100644 --- a/src/Unitverse.Core/Models/ClassModel.cs +++ b/src/Unitverse.Core/Models/ClassModel.cs @@ -264,7 +264,7 @@ public ExpressionSyntax GetObjectCreationExpression(IFrameworkSet frameworkSet, if (forSetupMethod) { - var mockedCreationExpression = frameworkSet.MockingFramework.GetObjectCreationExpression(TypeSyntax); + var mockedCreationExpression = frameworkSet.MockingFramework.GetObjectCreationExpression(TypeSyntax, TypeSymbol.IsReferenceType); if (mockedCreationExpression != null) { return mockedCreationExpression;