Skip to content

Commit

Permalink
Move hardcoded message strings to Resources.resx
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Jul 5, 2017
1 parent 1b4c58e commit e46294b
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Source/ExpressionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Runtime.CompilerServices;
using System.Text;
using System.Collections.ObjectModel;
using Moq.Properties;

namespace Moq
{
Expand Down Expand Up @@ -127,7 +128,7 @@ public void ToString(Expression exp)
ToStringListInit((ListInitExpression)exp);
return;
default:
throw new Exception(string.Format("Unhandled expression type: '{0}'", exp.NodeType));
throw new Exception(string.Format(Resources.UnhandledExpressionType, exp.NodeType));
}
}

Expand All @@ -145,7 +146,7 @@ private void ToStringBinding(MemberBinding binding)
ToStringMemberListBinding((MemberListBinding)binding);
return;
default:
throw new Exception(string.Format("Unhandled binding type '{0}'", binding.BindingType));
throw new Exception(string.Format(Resources.UnhandledBindingType, binding.BindingType));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public static MemberInfoWithTarget<EventInfo, Mock> GetEvent<TMock>(this Action<

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.");
throw new ArgumentException(Resources.ExpressionIsNotEventAttachOrDetachOrIsNotVirtual);
}

addRemove = context.LastInvocation.Invocation.Method;
Expand Down
2 changes: 1 addition & 1 deletion Source/Linq/MockSetupsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private static Expression ConvertToSetupProperty(Expression targetObject, Expres
// We need to go back one level, to the target expression of the Setup call,
// which would be the Mock.Get(foo), where we will actually invoke SetupProperty instead.
if (propertySetup.NodeType != ExpressionType.Call)
throw new NotSupportedException("Unexpected translation of a member access: " + propertySetup.ToStringFixed());
throw new NotSupportedException(string.Format(Resources.UnexpectedTranslationOfMemberAccess, propertySetup.ToStringFixed()));

var propertyCall = (MethodCallExpression)propertySetup;
var mockExpression = propertyCall.Object;
Expand Down
2 changes: 1 addition & 1 deletion Source/Linq/Mocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public static Mock<TResult> FluentMock<T, TResult>(this Mock<T> mock, Expression
}
else
{
throw new NotSupportedException("Unsupported expression: " + setup.ToStringFixed());
throw new NotSupportedException(string.Format(Resources.UnsupportedExpression, setup.ToStringFixed()));
}

info.ReturnType.ThrowIfNotMockeable();
Expand Down
2 changes: 1 addition & 1 deletion Source/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ private static void ThrowParameterMismatch(ParameterInfo[] expected, ParameterIn
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
"Invalid callback. Setup on method with parameters ({0}) cannot invoke callback with parameters ({1}).",
Resources.InvalidCallbackParameterMismatch,
string.Join(",", expected.Select(p => p.ParameterType.Name).ToArray()),
string.Join(",", actual.Select(p => p.ParameterType.Name).ToArray())
));
Expand Down
8 changes: 4 additions & 4 deletions Source/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ private static string FormatSetupsInfo(IEnumerable<IProxyCall> setups)
.ToArray();

return expressionSetups.Length == 0 ?
"No setups configured." :
Environment.NewLine + "Configured setups:" + Environment.NewLine + string.Join(Environment.NewLine, expressionSetups);
Resources.NoSetupsConfigured :
Environment.NewLine + string.Format(Resources.ConfiguredSetups, Environment.NewLine + string.Join(Environment.NewLine, expressionSetups));
}

private static string FormatCallCount(int callCount)
Expand All @@ -431,8 +431,8 @@ private static string FormatInvocations(IEnumerable<ICallContext> invocations)
.ToArray();

return formattedInvocations.Length == 0 ?
"No invocations performed." :
Environment.NewLine + "Performed invocations:" + Environment.NewLine + string.Join(Environment.NewLine, formattedInvocations);
Resources.NoInvocationsPerformed :
Environment.NewLine + string.Format(Resources.PerformedInvocations, Environment.NewLine + string.Join(Environment.NewLine, formattedInvocations));
}

#endregion
Expand Down
108 changes: 108 additions & 0 deletions Source/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Source/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,40 @@ Expected invocation on the mock once, but was {4} times: {1}</value>
<data name="VerifyOnExtensionMethod" xml:space="preserve">
<value>Invalid verify on an extension method: {0}</value>
</data>
<data name="UnhandledBindingType" xml:space="preserve">
<value>Unhandled binding type: {0}</value>
</data>
<data name="UnhandledExpressionType" xml:space="preserve">
<value>Unhandled expression type: {0}</value>
</data>
<data name="ConfiguredSetups" xml:space="preserve">
<value>Configured setups: {0}</value>
</data>
<data name="DelaysMustBeGreaterThanZero" xml:space="preserve">
<value>Delays have to be greater than zero to ensure an async callback is used.</value>
</data>
<data name="ExpressionIsNotEventAttachOrDetachOrIsNotVirtual" xml:space="preserve">
<value>Expression is not an event attach or detach, or the event is declared in a class but not marked virtual.</value>
</data>
<data name="InvalidCallbackParameterMismatch" xml:space="preserve">
<value>Invalid callback. Setup on method with parameters ({0}) cannot invoke callback with parameters ({1}).</value>
</data>
<data name="MinDelayMustBeLessThanMaxDelay" xml:space="preserve">
<value>Mininum delay has to be lower than maximum delay.</value>
</data>
<data name="NoInvocationsPerformed" xml:space="preserve">
<value>No invocations performed.</value>
</data>
<data name="NoSetupsConfigured" xml:space="preserve">
<value>No setups configured.</value>
</data>
<data name="PerformedInvocations" xml:space="preserve">
<value>Performed invocations: {0}</value>
</data>
<data name="UnexpectedTranslationOfMemberAccess" xml:space="preserve">
<value>Unexpected translation of a member access: {0}</value>
</data>
<data name="UseItExprIsNullRatherThanNullArgumentValue" xml:space="preserve">
<value>Use ItExpr.IsNull&lt;TValue&gt; rather than a null argument value, as it prevents proper method lookup.</value>
</data>
</root>
4 changes: 2 additions & 2 deletions Source/Protected/ProtectedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,15 @@ private static Type[] ToArgTypes(object[] args)
{
if (args == null)
{
throw new ArgumentException("Use ItExpr.IsNull<TValue> rather than a null argument value, as it prevents proper method lookup.");
throw new ArgumentException(Resources.UseItExprIsNullRatherThanNullArgumentValue);
}

var types = new Type[args.Length];
for (int index = 0; index < args.Length; index++)
{
if (args[index] == null)
{
throw new ArgumentException("Use ItExpr.IsNull<TValue> rather than a null argument value, as it prevents proper method lookup.");
throw new ArgumentException(Resources.UseItExprIsNullRatherThanNullArgumentValue);
}

var expr = args[index] as Expression;
Expand Down
5 changes: 3 additions & 2 deletions Source/ReturnsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Moq.Language;
using Moq.Language.Flow;
using Moq.Properties;

namespace Moq
{
Expand Down Expand Up @@ -138,7 +139,7 @@ public static IReturnsResult<TMock> ThrowsAsync<TMock, TResult>(this IReturns<TM
private static TimeSpan GetDelay(TimeSpan minDelay, TimeSpan maxDelay, Random random)
{
if (!(minDelay < maxDelay))
throw new ArgumentException("Mininum delay has to be lower than maximum delay.");
throw new ArgumentException(Resources.MinDelayMustBeLessThanMaxDelay);

var min = (int)minDelay.Ticks;
var max = (int)maxDelay.Ticks;
Expand All @@ -149,7 +150,7 @@ private static TimeSpan GetDelay(TimeSpan minDelay, TimeSpan maxDelay, Random ra
private static void GuardPositiveDelay(TimeSpan delay)
{
if (!(delay > TimeSpan.Zero))
throw new ArgumentException("Delays have to be greater than zero to ensure an async callback is used.");
throw new ArgumentException(Resources.DelaysMustBeGreaterThanZero);
}

private static IReturnsResult<TMock> DelayedResult<TMock, TResult>(IReturns<TMock, Task<TResult>> mock,
Expand Down

0 comments on commit e46294b

Please sign in to comment.