Skip to content

Commit

Permalink
Minor fixes in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Barbul committed Jun 9, 2022
1 parent 2da0685 commit eec72d1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
12 changes: 7 additions & 5 deletions tests/Calculator.Core.Tests/CalculatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public CalculatorTest(Calculator calculator, IEnumerable<Operation> operations)
{
this.calculator = calculator;

this.lowPriorityOperator = (LowPriorityOperator)operations.First(o => o is LowPriorityOperator);
this.highPriorityOperator = (HighPriorityOperator)operations.First(o => o is HighPriorityOperator);
this.myUnaryOperator = (MyUnaryOperator)operations.First(o => o is MyUnaryOperator);
this.myFunction = (MyFunction)operations.First(o => o is MyFunction);
Operation[] operationsArray = operations.ToArray();

this.lowPriorityOperator = (LowPriorityOperator)operationsArray.First(o => o is LowPriorityOperator);
this.highPriorityOperator = (HighPriorityOperator)operationsArray.First(o => o is HighPriorityOperator);
this.myUnaryOperator = (MyUnaryOperator)operationsArray.First(o => o is MyUnaryOperator);
this.myFunction = (MyFunction)operationsArray.First(o => o is MyFunction);
}

[Theory]
Expand Down Expand Up @@ -220,7 +222,7 @@ public void Calculate_FunctionWithoutArguments_Calculated()

Assert.Equal(MyFunction.ReturnValue, result);
Assert.Equal(1, this.myFunction.Calls.Count);
Assert.Equal(Array.Empty<Token>(), this.myFunction.Calls[0]);
Assert.Equal(Array.Empty<Operand>(), this.myFunction.Calls[0]);
}

public class LowPriorityOperator : BinaryOperator
Expand Down
46 changes: 21 additions & 25 deletions tests/Calculator.Core.Tests/Parsers/SubformulaParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,68 @@ public class SubformulaParserTest
[Fact]
public void TryParse_SubformulaAtBeginning_Correct()
{
Token? token;
this.parser.TryParse("(1+2)-4", this.context, out token, out _);
bool isParsed = this.parser.TryParse("(1+2)-4", this.context, out Token? token, out _);

Assert.True(isParsed);
this.AssertSubformulaTokenEqual(token, "1+2");
}

[Fact]
public void TryParse_SubformulaNotAtBeginning_Null()
{
Token? token;
this.parser.TryParse("1+(2*4)", this.context, out token, out _);
bool isParsed = this.parser.TryParse("1+(2*4)", this.context, out Token? token, out _);

Assert.False(isParsed);
Assert.Null(token);
}

[Fact]
public void TryParse_DifferentParenthesis_Correct()
[Theory]
[InlineData("(-4)", "-4")]
[InlineData("[0-9]", "0-9")]
[InlineData("{7+3}", "7+3")]
[InlineData("<1+3>*4", "1+3")]
public void TryParse_DifferentParenthesis_Correct(string formula, string expected)
{
Token? token;

this.parser.TryParse("[0-9]", this.context, out token, out _);
Assert.NotNull(token);
this.AssertSubformulaTokenEqual(token, "0-9");

this.parser.TryParse("{7+3}", this.context, out token, out _);
Assert.NotNull(token);
this.AssertSubformulaTokenEqual(token, "7+3");
bool isParsed = this.parser.TryParse(formula, this.context, out Token? token, out _);

this.parser.TryParse("<1+3>*4", this.context, out token, out _);
Assert.True(isParsed);
Assert.NotNull(token);
this.AssertSubformulaTokenEqual(token, "1+3");
this.AssertSubformulaTokenEqual(token, expected);
}

[Fact]
public void TryParse_NoClosingParenthesis_Null()
{
Token? token;
bool isParsed = this.parser.TryParse("(1-2", this.context, out Token? token, out _);

this.parser.TryParse("(1-2", this.context, out token, out _);
Assert.False(isParsed);
Assert.Null(token);
}

[Fact]
public void TryParse_ClosingParenthesisOfDifferentType_Null()
{
Token? token;
bool isParsed = this.parser.TryParse("<3}", this.context, out Token? token, out _);

this.parser.TryParse("<3}", this.context, out token, out _);
Assert.False(isParsed);
Assert.Null(token);
}

[Fact]
public void TryParse_CrossingParenthesisGroupsOfDifferentTypes_Null()
{
Token? token;
bool isParsed = this.parser.TryParse("(1+<3-4)*1>", this.context, out Token? token, out _);

this.parser.TryParse("(1+<3-4)*1>", this.context, out token, out _);
Assert.False(isParsed);
Assert.Null(token);
}

[Fact]
public void TryParse_EmptyString_Null()
{
Token? token;
bool isParsed = this.parser.TryParse(string.Empty, this.context, out Token? token, out _);

this.parser.TryParse(string.Empty, this.context, out token, out _);
Assert.False(isParsed);
Assert.Null(token);
}

Expand Down
20 changes: 10 additions & 10 deletions tests/Calculator.Core.Tests/Parsers/VariableParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,45 @@ public class VariableParserTest
[Fact]
public void TryParse_VariableAtBeginning_Correct()
{
Token? token;
bool isParsed = this.parser.TryParse("$test", this.context, out Token? token, out _);

this.parser.TryParse("$test", this.context, out token, out _);
Assert.True(isParsed);
this.AssertVariableTokenEqual(token, "test");
}

[Fact]
public void TryParse_VariableNotAtBeginning_Null()
{
Token? token;
bool isParsed = this.parser.TryParse("2*$test", this.context, out Token? token, out _);

this.parser.TryParse("2*$test", this.context, out token, out _);
Assert.False(isParsed);
Assert.Null(token);
}

[Fact]
public void TryParse_VariableFollowedByParenthesis_Correct()
{
Token? token;
bool isParsed = this.parser.TryParse("$test()", this.context, out Token? token, out _);

this.parser.TryParse("$test()", this.context, out token, out _);
Assert.True(isParsed);
this.AssertVariableTokenEqual(token, "test");
}

[Fact]
public void TryParse_StartsWithDigit_Null()
{
Token? token;
bool isParsed = this.parser.TryParse("$4fun", this.context, out Token? token, out _);

this.parser.TryParse("$4fun", this.context, out token, out _);
Assert.False(isParsed);
Assert.Null(token);
}

[Fact]
public void TryParse_EmptyString_Null()
{
Token? token;
bool isParsed = this.parser.TryParse(string.Empty, this.context, out Token? token, out _);

this.parser.TryParse(string.Empty, this.context, out token, out _);
Assert.False(isParsed);
Assert.Null(token);
}

Expand Down

0 comments on commit eec72d1

Please sign in to comment.