Skip to content

Commit

Permalink
Test project for visualizer added. ILMerge updated
Browse files Browse the repository at this point in the history
git-svn-id: http://moq.googlecode.com/svn/trunk@604 b33fba48-7441-0410-8d5c-f397f7ceaa6c
  • Loading branch information
marianoor committed Jul 30, 2009
1 parent 07aad31 commit df4bffc
Show file tree
Hide file tree
Showing 24 changed files with 244 additions and 90 deletions.
8 changes: 8 additions & 0 deletions Moq.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moq.Silverlight.Tests", "Un
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moq.Visualizer", "Visualizer\Moq.Visualizer.csproj", "{335EAF40-60EF-40D7-84A8-E33D255FA59F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moq.Visualizer.Tests", "UnitTests.Visualizer\Moq.Visualizer.Tests.csproj", "{A84563A0-8A70-43D9-A1C0-530955413318}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -62,6 +64,12 @@ Global
{335EAF40-60EF-40D7-84A8-E33D255FA59F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{335EAF40-60EF-40D7-84A8-E33D255FA59F}.Release|Any CPU.Build.0 = Release|Any CPU
{335EAF40-60EF-40D7-84A8-E33D255FA59F}.Release|x86.ActiveCfg = Release|Any CPU
{A84563A0-8A70-43D9-A1C0-530955413318}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A84563A0-8A70-43D9-A1C0-530955413318}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A84563A0-8A70-43D9-A1C0-530955413318}.Debug|x86.ActiveCfg = Debug|Any CPU
{A84563A0-8A70-43D9-A1C0-530955413318}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A84563A0-8A70-43D9-A1C0-530955413318}.Release|Any CPU.Build.0 = Release|Any CPU
{A84563A0-8A70-43D9-A1C0-530955413318}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
38 changes: 17 additions & 21 deletions Source/EmptyDefaultValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// http://www.opensource.org/licenses/bsd-license.php]

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand All @@ -61,10 +62,8 @@ public virtual object ProvideDefault(MethodInfo member, object[] arguments)
{
return GetValueTypeDefault(valueType);
}
else
{
return GetReferenceTypeDefault(valueType);
}

return GetReferenceTypeDefault(valueType);
}

private static object GetReferenceTypeDefault(Type valueType)
Expand All @@ -73,49 +72,46 @@ private static object GetReferenceTypeDefault(Type valueType)
{
return Activator.CreateInstance(valueType, 0);
}
else if (valueType == typeof(System.Collections.IEnumerable))
else if (valueType == typeof(IEnumerable))
{
return new object[0];
}
else if (valueType == typeof(IQueryable))
{
return new object[0].AsQueryable();
}
else if (valueType.IsGenericType &&
valueType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
else if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
var genericListType = typeof(List<>).MakeGenericType(
valueType.GetGenericArguments()[0]);
var genericListType = typeof(List<>).MakeGenericType(valueType.GetGenericArguments()[0]);
return Activator.CreateInstance(genericListType);
}
else if (valueType.IsGenericType &&
valueType.GetGenericTypeDefinition() == typeof(IQueryable<>))
else if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(IQueryable<>))
{
var genericType = valueType.GetGenericArguments()[0];
var genericListType = typeof(List<>).MakeGenericType(genericType);
var list = Activator.CreateInstance(genericListType);

return typeof(Queryable).GetMethods()
.Single(x => x.Name == "AsQueryable" && x.IsGenericMethod)
.MakeGenericMethod(genericType)
.Invoke(null, new[] { list });
}
else
{
return null;
.Invoke(null, new[] { Activator.CreateInstance(genericListType) });
}

return null;
}

private static object GetValueTypeDefault(Type valueType)
{
// For nullable value types, return null.
if (valueType.IsGenericType &&
valueType.GetGenericTypeDefinition() == typeof(Nullable<>))
if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return null;
}
else if (valueType.IsAssignableFrom(typeof(int)))
{
return 0;
else
return Activator.CreateInstance(valueType);
}

return Activator.CreateInstance(valueType);
}
}
}
21 changes: 11 additions & 10 deletions Source/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public static TAttribute GetCustomAttribute<TAttribute>(this ICustomAttributePro
}
}

[SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "The linq expression is way more readable this way.")]
public static string Format(this ICallContext invocation)
{
if (invocation.Method.IsPropertyGetter())
Expand All @@ -92,9 +91,10 @@ public static string GetValue(object value)
return "null";
}

if (value is string)
var typedValue = value as string;
if (typedValue != null)
{
return "\"" + (string)value + "\"";
return "\"" + typedValue + "\"";
}

return value.ToString();
Expand Down Expand Up @@ -137,10 +137,7 @@ public static void SetStackTrace(this Exception exception, string stackTrace)

public static bool IsMockeable(this Type typeToMock)
{
return
typeToMock.IsInterface ||
typeToMock.IsAbstract ||
!typeToMock.IsSealed;
return typeToMock.IsInterface || typeToMock.IsAbstract || !typeToMock.IsSealed;
}

public static bool CanOverrideGet(this PropertyInfo property)
Expand Down Expand Up @@ -176,18 +173,22 @@ public static EventInfo GetEvent<TMock>(this Action<TMock> eventExpression, TMoc
eventExpression(mock);

if (context.LastInvocation == null)
{
throw new ArgumentException("Expression is not an event attach or detach, or the event is declared in a class but not marked virtual.");
}

addRemove = context.LastInvocation.Invocation.Method;
}

var ev = addRemove.DeclaringType.GetEvent(
addRemove.Name.Replace("add_", "").Replace("remove_", ""));
addRemove.Name.Replace("add_", "").Replace("remove_", ""));

if (ev == null)
{
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
"Could not locate event for attach or detach method {0}.", addRemove.ToString()));
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
"Could not locate event for attach or detach method {0}.",
addRemove));
}

return ev;
Expand Down
7 changes: 4 additions & 3 deletions Source/IHideObjectMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace Moq
{
Expand All @@ -52,8 +53,8 @@ namespace Moq
public interface IHideObjectMembers
{
/// <summary/>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "We're hiding Object.GetType")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "GetType", Justification = "We're hiding Object.GetType")]
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "We're hiding Object.GetType")]
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "GetType", Justification = "We're hiding Object.GetType")]
[EditorBrowsable(EditorBrowsableState.Never)]
Type GetType();

Expand All @@ -69,4 +70,4 @@ public interface IHideObjectMembers
[EditorBrowsable(EditorBrowsableState.Never)]
bool Equals(object other);
}
}
}
50 changes: 22 additions & 28 deletions Source/Interceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ public void Intercept(ICallContext invocation)
// Special case for events.
if (!FluentMockContext.IsActive)
{
if (IsEventAttach(invocation))
if (invocation.Method.IsEventAttach())
{
var delegateInstance = (Delegate)invocation.Arguments[0];
// TODO: validate we can get the event?
EventInfo eventInfo = GetEventFromName(invocation.Method.Name.Replace("add_", ""));
var eventInfo = GetEventFromName(invocation.Method.Name.Substring(4));

if (delegateInstance != null)
{
Expand All @@ -180,11 +180,11 @@ public void Intercept(ICallContext invocation)

return;
}
else if (IsEventDetach(invocation))
else if (invocation.Method.IsEventDetach())
{
var delegateInstance = (Delegate)invocation.Arguments[0];
// TODO: validate we can get the event?
EventInfo eventInfo = GetEventFromName(invocation.Method.Name.Replace("remove_", ""));
var eventInfo = GetEventFromName(invocation.Method.Name.Substring(7));

if (delegateInstance != null)
{
Expand Down Expand Up @@ -258,18 +258,6 @@ public void Intercept(ICallContext invocation)
}
}

private static bool IsEventAttach(ICallContext invocation)
{
return invocation.Method.IsSpecialName &&
invocation.Method.Name.StartsWith("add_", StringComparison.Ordinal);
}

private static bool IsEventDetach(ICallContext invocation)
{
return invocation.Method.IsSpecialName &&
invocation.Method.Name.StartsWith("remove_", StringComparison.Ordinal);
}

/// <summary>
/// Get an eventInfo for a given event name. Search type ancestors depth first if necessary.
/// </summary>
Expand All @@ -283,8 +271,10 @@ private EventInfo GetEventFromName(string eventName)
var currentType = depthFirstProgress.Dequeue();
var eventInfo = currentType.GetEvent(eventName);
if (eventInfo != null)
{
return eventInfo;
//else
}

foreach (var implementedType in GetAncestorTypes(currentType))
{
depthFirstProgress.Enqueue(implementedType);
Expand All @@ -302,7 +292,7 @@ private static IEnumerable<Type> GetAncestorTypes(Type initialType)
Type baseType = initialType.BaseType;
if (baseType != null)
{
return new Type[] { baseType };
return new[] { baseType };
}

return initialType.GetInterfaces();
Expand All @@ -320,7 +310,8 @@ private void ThrowIfReturnValueRequired(IProxyCall call, ICallContext invocation
{
throw new MockException(
MockException.ExceptionReason.ReturnValueRequired,
behavior, invocation);
behavior,
invocation);
}
}
}
Expand All @@ -338,23 +329,24 @@ public ExpressionKey(string fixedString, List<object> values)

public override bool Equals(object obj)
{
if (Object.ReferenceEquals(this, obj))
if (object.ReferenceEquals(this, obj))
{
return true;
}

var key = obj as ExpressionKey;

if (key == null)
{
return false;
}

var eq = key.fixedString == this.fixedString &&
key.values.Count == this.values.Count;

var i = 0;
var eq = key.fixedString == this.fixedString && key.values.Count == this.values.Count;

while (eq && i < this.values.Count)
var index = 0;
while (eq && index < this.values.Count)
{
eq |= this.values[i] == key.values[i];
i++;
eq |= this.values[index] == key.values[index];
index++;
}

return eq;
Expand All @@ -367,7 +359,9 @@ public override int GetHashCode()
foreach (var value in values)
{
if (value != null)
{
hash ^= value.GetHashCode();
}
}

return hash;
Expand Down
2 changes: 1 addition & 1 deletion Source/Matchers/LazyEvalMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace Moq
{
internal class LazyEvalMatcher : IMatcher
{
Expression matcherExpression;
private Expression matcherExpression;

public void Initialize(Expression matcherExpression)
{
Expand Down
10 changes: 10 additions & 0 deletions Source/MemberInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public static string GetName(this Type type)
return type.Name;
}

public static bool IsEventAttach(this MethodBase method)
{
return method.IsSpecialName && method.Name.StartsWith("add_", StringComparison.Ordinal);
}

public static bool IsEventDetach(this MethodBase method)
{
return method.IsSpecialName && method.Name.StartsWith("remove_", StringComparison.Ordinal);
}

public static bool IsPropertyGetter(this MethodBase method)
{
return method.IsSpecialName && method.Name.StartsWith("get_", StringComparison.Ordinal);
Expand Down
Binary file modified Tools/ILMerge.doc
Binary file not shown.
Binary file modified Tools/ILMerge.exe
Binary file not shown.
43 changes: 43 additions & 0 deletions UnitTests.Visualizer/MockContextViewModelFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.ComponentModel;
using Xunit;
using System.Linq;
using System;

namespace Moq.Visualizer.Tests
{
public class MockContextViewModelFixture
{
[Fact]
public void CtorSetsBehavior()
{
var target = new MockContextViewModel(new Mock<IComponent>(MockBehavior.Strict));

Assert.Equal(MockBehavior.Strict, target.Behavior);
}

[Fact]
public void CtorSetsCallBase()
{
var target = new MockContextViewModel(new Mock<IComponent> { CallBase = false });

Assert.Equal(false, target.CallBase);
}

[Fact]
public void CtorSetsDefaultValue()
{
var target = new MockContextViewModel(new Mock<IComponent> { DefaultValue = DefaultValue.Empty });

Assert.Equal(DefaultValue.Empty, target.DefaultValue);
}

[Fact]
public void AddMockViewModelToMocks()
{
var target = new MockContextViewModel(new Mock<IComponent>());

Assert.Equal(1, target.Mocks.Count());
Assert.NotNull(target.Mocks.ElementAt(0));
}
}
}
Loading

0 comments on commit df4bffc

Please sign in to comment.