Skip to content

Commit

Permalink
Поправил регулярку. Теперь правильно парсятся нецелые и отрицательные…
Browse files Browse the repository at this point in the history
… числа .
  • Loading branch information
rincew1nd committed Nov 24, 2016
1 parent 0a18af1 commit 56bc70c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ UnitTests/bin/
UnitTests/obj/
CodeTranslator/obj/
CodeTranslator/bin/
.vs/
4 changes: 2 additions & 2 deletions CodeTranslator/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private string ParseOperation(string text)
text = substring.ToString();
}

return text;
return PerformCalculation(text);
}

private void CheckBrackets(string text)
Expand Down Expand Up @@ -172,7 +172,7 @@ private string PerformCalculation(string operation)

private string[] OperatorsArray(string operation)
{
var opersRegex = Regex.Match(operation, @"([0-9]+)(([+*/-])([0-9]+))+");
var opersRegex = Regex.Match(operation, @"^(-?[0-9]+,?[0-9]*?)(([+*/-])(-?[0-9]+,?[0-9]*?))+$");

var opers = new string[1];
opers[0] = opersRegex.Groups[1].Value;
Expand Down
30 changes: 13 additions & 17 deletions UnitTests/Calculations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,30 @@ public class TestSuite
[Test]
public void SimpleCalculations()
{
Assert.AreEqual(246, CallCalc("CalculateNumber", "123 + 123"));
Assert.AreEqual(81, CallCalc("CalculateNumber", "123 - 42"));
Assert.AreEqual(369, CallCalc("CalculateNumber", "123 * 3"));
Assert.AreEqual(61.5, CallCalc("CalculateNumber", "123 / 2"));
var translator = new Translator();
Assert.AreEqual(246, translator.PrivateMethod("CalculateNumber", "123 + 123"));
Assert.AreEqual(81, translator.PrivateMethod("CalculateNumber", "123 - 42"));
Assert.AreEqual(369, translator.PrivateMethod("CalculateNumber", "123 * 3"));
Assert.AreEqual(61.5, translator.PrivateMethod("CalculateNumber", "123 / 2"));
}

[Test]
public void BooleanCalculation()
{
Assert.AreEqual(1, CallCalc("CalculateNumber", "1 | 0"));
Assert.AreEqual(0, CallCalc("CalculateNumber", "!1 | 0"));
Assert.AreEqual(0, CallCalc("CalculateNumber", "1 & 0"));
Assert.AreEqual(1, CallCalc("CalculateNumber", "1 & 1"));
Assert.AreEqual(1, CallCalc("CalculateNumber", "1 & !0"));
var translator = new Translator();
Assert.AreEqual(1, translator.PrivateMethod("CalculateNumber", "1 | 0"));
Assert.AreEqual(0, translator.PrivateMethod("CalculateNumber", "!1 | 0"));
Assert.AreEqual(0, translator.PrivateMethod("CalculateNumber", "1 & 0"));
Assert.AreEqual(1, translator.PrivateMethod("CalculateNumber", "1 & 1"));
Assert.AreEqual(1, translator.PrivateMethod("CalculateNumber", "1 & !0"));
}

[Test]
public void AlotOfBrackets()
{
CallCalc("ParseOperation", "((12-43*(3-1)-10)-12)-34+34-(12/3)");
}

public object CallCalc(string method, params object[] text)
{
var translator = new Translator();
var mi = translator.GetType().GetMethod(
"CalculateNumber", BindingFlags.NonPublic | BindingFlags.Instance);
return mi != null ? mi.Invoke(translator, text) : null;
translator.PrivateMethod("ParseOperation", "((12-43*(3-1)-10)-12)-34+34-(9/2)");
translator.PrivateMethod("ParseOperation", "((12-43*(3-1)-10)-12)-34+34-(8/2)");
}
}
}
1 change: 1 addition & 0 deletions UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Compile Include="Calculations.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Syntax.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CodeTranslator\CodeTranslator.csproj">
Expand Down
18 changes: 18 additions & 0 deletions UnitTests/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace UnitTests
{
static class Utils
{
public static object PrivateMethod(this object obj, string method, params object[] text)
{
var mi = obj.GetType().GetMethod(method, BindingFlags.NonPublic | BindingFlags.Instance);
return mi != null ? mi.Invoke(obj, text) : null;
}
}
}

0 comments on commit 56bc70c

Please sign in to comment.