Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Jun 8, 2017
1 parent df86115 commit b6bf422
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 16 deletions.
68 changes: 68 additions & 0 deletions xFunc.Maths/Analyzers/DifferentParameterTypeMismatchException.cs
@@ -0,0 +1,68 @@
// Copyright 2012-2017 Dmitry Kischenko
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Runtime.Serialization;
using xFunc.Maths.Resources;

namespace xFunc.Maths.Analyzers
{

/// <summary>
/// Represents an exception when the type of the actual argument does not match the expected parameter type.
/// </summary>
[Serializable]
public class DifferentParameterTypeMismatchException : ParameterTypeMismatchException
{

/// <summary>
/// Initializes a new instance of the <see cref="ParameterTypeMismatchException"/> class.
/// </summary>
public DifferentParameterTypeMismatchException() { }

/// <summary>
/// Initializes a new instance of the <see cref="ParameterTypeMismatchException" /> class.
/// </summary>
/// <param name="expected">The expected parameter type.</param>
/// <param name="actual">The actual parameter type.</param>
/// <param name="index">The index of parameter.</param>
public DifferentParameterTypeMismatchException(ResultType expected, ResultType actual, int index)
: base(expected, actual, string.Format(Resource.DifferentParameterTypeMismatchExceptionError, expected.ToString(), actual.ToString(), index + 1))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ParameterTypeMismatchException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public DifferentParameterTypeMismatchException(string message) : base(message) { }

/// <summary>
/// Initializes a new instance of the <see cref="ParameterTypeMismatchException"/> class.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="inner">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
public DifferentParameterTypeMismatchException(string message, Exception inner) : base(message, inner) { }

/// <summary>
/// Initializes a new instance of the <see cref="ParameterTypeMismatchException"/> class.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
protected DifferentParameterTypeMismatchException(SerializationInfo info, StreamingContext context)
: base(info, context) { }

}

}
16 changes: 13 additions & 3 deletions xFunc.Maths/Analyzers/ParameterTypeMismatchException.cs
Expand Up @@ -35,12 +35,22 @@ public class ParameterTypeMismatchException : Exception
public ParameterTypeMismatchException() { }

/// <summary>
/// Initializes a new instance of the <see cref="ParameterTypeMismatchException"/> class.
/// Initializes a new instance of the <see cref="ParameterTypeMismatchException" /> class.
/// </summary>
/// <param name="expected">The expected parameter type.</param>
/// <param name="actual">The actual parameter type.</param>
public ParameterTypeMismatchException(ResultType expected, ResultType actual)
: base(string.Format(Resource.ParameterTypeMismatchExceptionError, expected.ToString(), actual.ToString()))
: this(expected, actual, string.Format(Resource.ParameterTypeMismatchExceptionError, expected.ToString(), actual.ToString()))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ParameterTypeMismatchException" /> class.
/// </summary>
/// <param name="expected">The expected parameter type.</param>
/// <param name="actual">The actual parameter type.</param>
/// <param name="message">The error message.</param>
public ParameterTypeMismatchException(ResultType expected, ResultType actual, string message) : base(message)
{
this.expected = expected;
this.actual = actual;
Expand All @@ -58,7 +68,7 @@ public ParameterTypeMismatchException(ResultType expected, ResultType actual)
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="inner">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
public ParameterTypeMismatchException(string message, Exception inner) : base(message, inner) { }

/// <summary>
/// Initializes a new instance of the <see cref="ParameterTypeMismatchException"/> class.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions xFunc.Maths/Analyzers/TypeAnalyzer.cs
Expand Up @@ -67,12 +67,12 @@ private ResultType CheckStatistical(DifferentParametersExpression exp)
if (results[0] == ResultType.Number || results[0] == ResultType.Vector)
return ResultType.Number;

throw new ParameterTypeMismatchException();
throw new DifferentParameterTypeMismatchException(ResultType.Number | ResultType.Vector, results[0], 0);
}

for (var i = 0; i < results.Count; i++)
if (results[i] != ResultType.Number)
throw new ParameterTypeMismatchException();
throw new DifferentParameterTypeMismatchException(ResultType.Number, results[i], i);

return ResultType.Number;
}
Expand All @@ -93,7 +93,7 @@ public virtual ResultType Analyze(Abs exp)
if (result == ResultType.Number || result == ResultType.ComplexNumber || result == ResultType.Vector)
return ResultType.Number;

throw new ParameterTypeMismatchException();
throw new ParameterTypeMismatchException(ResultType.Number | ResultType.ComplexNumber | ResultType.Vector, result);
}

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions xFunc.Maths/Resources/Resource.Designer.cs

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

3 changes: 3 additions & 0 deletions xFunc.Maths/Resources/Resource.resx
Expand Up @@ -183,4 +183,7 @@
<data name="VectorCrossException" xml:space="preserve">
<value>The size of vector should equal to 3.</value>
</data>
<data name="DifferentParameterTypeMismatchExceptionError" xml:space="preserve">
<value>The parameter #{2} has wrong type (Expected: {0}; Actual: {1}).</value>
</data>
</root>
3 changes: 3 additions & 0 deletions xFunc.Maths/Resources/Resource.ru.resx
Expand Up @@ -183,4 +183,7 @@
<data name="VectorCrossException" xml:space="preserve">
<value>Размер вектора должен быть равер 3.</value>
</data>
<data name="DifferentParameterTypeMismatchExceptionError" xml:space="preserve">
<value>Параметр #{2} имеет не соответствующий тип (Ожидается: {0}; Фактически: {1}).</value>
</data>
</root>
3 changes: 3 additions & 0 deletions xFunc.Maths/Resources/Resource.uk.resx
Expand Up @@ -183,4 +183,7 @@
<data name="VectorCrossException" xml:space="preserve">
<value>Розмір вектора має дорівнювати 3.</value>
</data>
<data name="DifferentParameterTypeMismatchExceptionError" xml:space="preserve">
<value>Параметр #{2} має неправильний тип. (Очікується: {0}; Фактично: {1}).</value>
</data>
</root>
1 change: 1 addition & 0 deletions xFunc.Maths/xFunc.Maths.csproj
Expand Up @@ -45,6 +45,7 @@
<Compile Include="Analyzers\Formatters\CommonFormatter.cs" />
<Compile Include="Analyzers\Formatters\IFormatter.cs" />
<Compile Include="Analyzers\IAnalyzer.cs" />
<Compile Include="Analyzers\DifferentParameterTypeMismatchException.cs" />
<Compile Include="Analyzers\Simplifier.cs" />
<Compile Include="Analyzers\TypeAnalyzer.cs" />
<Compile Include="Builder.cs" />
Expand Down
25 changes: 15 additions & 10 deletions xFunc.Tests/Analyzers/TypeAnalyzerTest.cs
Expand Up @@ -49,6 +49,11 @@ private void TestException(IExpression exp)
Assert.Throws<ParameterTypeMismatchException>(() => exp.Analyze(analyzer));
}

private void TestDiffParamException(IExpression exp)
{
Assert.Throws<DifferentParameterTypeMismatchException>(() => exp.Analyze(analyzer));
}

#region Standard

[Fact]
Expand Down Expand Up @@ -2140,7 +2145,7 @@ public void TestAvgException()
{
var exp = new Avg(new[] { new Bool(false), new Bool(false) }, 2);

TestException(exp);
TestDiffParamException(exp);
}

[Fact]
Expand Down Expand Up @@ -2172,7 +2177,7 @@ public void TestCountException()
{
var exp = new Count(new[] { new Bool(false), new Bool(false) }, 2);

TestException(exp);
TestDiffParamException(exp);
}

[Fact]
Expand Down Expand Up @@ -2204,7 +2209,7 @@ public void TestMaxException()
{
var exp = new Max(new[] { new Bool(false), new Bool(false) }, 2);

TestException(exp);
TestDiffParamException(exp);
}

[Fact]
Expand Down Expand Up @@ -2236,7 +2241,7 @@ public void TestMinException()
{
var exp = new Min(new[] { new Bool(false), new Bool(false) }, 2);

TestException(exp);
TestDiffParamException(exp);
}

[Fact]
Expand Down Expand Up @@ -2268,7 +2273,7 @@ public void TestProductException()
{
var exp = new Product(new[] { new Bool(false), new Bool(false) }, 2);

TestException(exp);
TestDiffParamException(exp);
}

[Fact]
Expand Down Expand Up @@ -2300,7 +2305,7 @@ public void TestStdevException()
{
var exp = new Stdev(new[] { new Bool(false), new Bool(false) }, 2);

TestException(exp);
TestDiffParamException(exp);
}

[Fact]
Expand Down Expand Up @@ -2332,7 +2337,7 @@ public void TestStdevpException()
{
var exp = new Stdevp(new[] { new Bool(false), new Bool(false) }, 2);

TestException(exp);
TestDiffParamException(exp);
}

[Fact]
Expand Down Expand Up @@ -2364,7 +2369,7 @@ public void TestSumException()
{
var exp = new Sum(new[] { new Bool(false), new Bool(false) }, 2);

TestException(exp);
TestDiffParamException(exp);
}

[Fact]
Expand Down Expand Up @@ -2396,7 +2401,7 @@ public void TestVarException()
{
var exp = new Var(new[] { new Bool(false), new Bool(false) }, 2);

TestException(exp);
TestDiffParamException(exp);
}

[Fact]
Expand Down Expand Up @@ -2428,7 +2433,7 @@ public void TestVarpException()
{
var exp = new Varp(new[] { new Bool(false), new Bool(false) }, 2);

TestException(exp);
TestDiffParamException(exp);
}

#endregion Statistical
Expand Down

0 comments on commit b6bf422

Please sign in to comment.