Skip to content

Commit

Permalink
Make code .NET 3.5 compatible
Browse files Browse the repository at this point in the history
Use compile directives to ensure smooth NuGet package generation.

'IReturnsExtensions' was using 'System.Threading.Tasks' namespace, so
compile directives were also added to it.
  • Loading branch information
Alex Tercete committed Dec 13, 2013
1 parent 7bf05bd commit ad78301
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Source/EmptyDefaultValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
#if !NET3x
using System.Threading.Tasks;
#endif

namespace Moq
{
Expand Down Expand Up @@ -87,11 +89,13 @@ private static object GetReferenceTypeDefault(Type valueType)
{
return new object[0].AsQueryable();
}
#if !NET3x
else if (valueType == typeof(Task))
{
// Task<T> inherits from Task, so just return Task<bool>
return GetCompletedTaskWithResult(false);
}
#endif
else if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
var genericListType = typeof(List<>).MakeGenericType(valueType.GetGenericArguments()[0]);
Expand All @@ -107,13 +111,15 @@ private static object GetReferenceTypeDefault(Type valueType)
.MakeGenericMethod(genericType)
.Invoke(null, new[] { Activator.CreateInstance(genericListType) });
}
#if !NET3x
else if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Task<>))
{
var genericType = valueType.GetGenericArguments()[0];

return GetCompletedTaskWithResult(
genericType.IsValueType ? GetValueTypeDefault(genericType) : GetReferenceTypeDefault(genericType));
}
#endif

return null;
}
Expand All @@ -129,6 +135,7 @@ private static object GetValueTypeDefault(Type valueType)
return Activator.CreateInstance(valueType);
}

#if !NET3x
private static Task GetCompletedTaskWithResult(object result)
{
var type = result.GetType();
Expand All @@ -140,5 +147,6 @@ private static Task GetCompletedTaskWithResult(object result)
setResultMethod.Invoke(tcs, new[] {result});
return (Task) taskProperty.GetValue(tcs, null);
}
#endif
}
}
4 changes: 3 additions & 1 deletion Source/Language/IReturnsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#if !NET3x
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -34,3 +35,4 @@ public static IReturnsResult<TMock> ThrowsAsync<TMock, TResult>(this IReturns<TM
}
}
}
#endif
6 changes: 6 additions & 0 deletions UnitTests/EmptyDefaultValueProviderFixture.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
#if !NET3x
using System.Threading.Tasks;
#endif
using Xunit;

namespace Moq.Tests
Expand Down Expand Up @@ -108,6 +110,7 @@ public void ProvideEmptyQueryableObjects()
Assert.Equal(0, ((IQueryable)value).Cast<object>().Count());
}

#if !NET3x
[Fact]
public void ProvidesDefaultTask()
{
Expand Down Expand Up @@ -142,6 +145,7 @@ public void ProvidesDefaultTaskOfGenericTask()
Assert.True(((Task)value).IsCompleted);
Assert.Equal(default(int), ((Task<Task<int>>) value).Result.Result);
}
#endif

public interface IFoo
{
Expand All @@ -156,9 +160,11 @@ public interface IFoo
IBar[] Bars { get; set; }
IQueryable<int> Queryable { get; }
IQueryable QueryableObjects { get; }
#if !NET3x
Task TaskValue { get; set; }
Task<int> GenericTaskValue { get; set; }
Task<Task<int>> TaskOfGenericTaskValue { get; set; }
#endif
}

public interface IBar { }
Expand Down

0 comments on commit ad78301

Please sign in to comment.