-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmytro Barbul
committed
Jun 11, 2022
1 parent
8dfa080
commit 31b537f
Showing
9 changed files
with
212 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...tor.Samples.ExtendingCalculator.Tests/Calculator.Samples.ExtendingCalculator.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Calculator.Samples.ExtendingCalculator\Calculator.Samples.ExtendingCalculator.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
53 changes: 53 additions & 0 deletions
53
samples/Calculator.Samples.ExtendingCalculator.Tests/Parsers/StringOperandParserTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Calculator.Core.Operands; | ||
using Calculator.Core.ParsingContexts; | ||
using Calculator.Core.Tokens; | ||
using Calculator.Samples.ExtendingCalculator.Parsers; | ||
|
||
namespace Calculator.Samples.ExtendingCalculator.Tests.Parsers; | ||
|
||
public class StringOperandParserTest | ||
{ | ||
private readonly StringOperandParser parser = new(); | ||
|
||
[Fact] | ||
public void TryParse_SimpleString_Parsed() | ||
{ | ||
bool isParsed = this.parser.TryParse("\"test\"", ParsingContext.Initial, out Token? token, out int parsedLength); | ||
|
||
Assert.True(isParsed); | ||
Operand<string> stringOperand = Assert.IsType<Operand<string>>(token); | ||
Assert.Equal(6, parsedLength); | ||
Assert.Equal("test", stringOperand.Value); | ||
} | ||
|
||
[Fact] | ||
public void TryParse_EscapedQuotes_Parsed() | ||
{ | ||
bool isParsed = this.parser.TryParse("\"test \\\" test\" something else", ParsingContext.Initial, out Token? token, out int parsedLength); | ||
|
||
Assert.True(isParsed); | ||
Operand<string> stringOperand = Assert.IsType<Operand<string>>(token); | ||
Assert.Equal(14, parsedLength); | ||
Assert.Equal("test \" test", stringOperand.Value); | ||
} | ||
|
||
[Fact] | ||
public void TryParse_EscapedBackslash_Parsed() | ||
{ | ||
bool isParsed = this.parser.TryParse("\"test\\\\\" test\" something else", ParsingContext.Initial, out Token? token, out int parsedLength); | ||
|
||
Assert.True(isParsed); | ||
Operand<string> stringOperand = Assert.IsType<Operand<string>>(token); | ||
Assert.Equal(8, parsedLength); | ||
Assert.Equal("test\\", stringOperand.Value); | ||
} | ||
|
||
[Fact] | ||
public void TryParse_NoClosingQuote_NotParsed() | ||
{ | ||
bool isParsed = this.parser.TryParse("\"test", ParsingContext.Initial, out Token? token, out _); | ||
|
||
Assert.False(isParsed); | ||
Assert.Null(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
13 changes: 13 additions & 0 deletions
13
samples/Calculator.Samples.ExtendingCalculator/Calculator.Samples.ExtendingCalculator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Calculator.Core\Calculator.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
40 changes: 40 additions & 0 deletions
40
samples/Calculator.Samples.ExtendingCalculator/Operators/LengthOperator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Calculator.Core.Operands; | ||
using Calculator.Core.Tokens; | ||
|
||
namespace Calculator.Samples.ExtendingCalculator.Operators; | ||
|
||
public class LengthOperator : UnaryOperator | ||
{ | ||
public override string Text => "||"; | ||
|
||
protected override Operand ExecuteUnaryOperator(Operand operand) | ||
{ | ||
return operand switch | ||
{ | ||
ListOperand listOperand => this.GetLengthOfList(listOperand), | ||
Operand<string> stringOperand => this.GetLengthOfString(stringOperand), | ||
_ => throw new ArgumentException("Length operator can be performed only on list or string"), | ||
}; | ||
} | ||
|
||
private Operand GetLengthOfList(ListOperand listOperand) | ||
{ | ||
decimal sqrSum = 0; | ||
foreach (Operand listOperandOperand in listOperand.Operands) | ||
{ | ||
if (listOperandOperand is not Operand<decimal> decimalOperand) | ||
{ | ||
throw new ArgumentException($"Operator {this.Text} can only be performed on list of decimals"); | ||
} | ||
|
||
sqrSum += decimalOperand.Value * decimalOperand.Value; | ||
} | ||
|
||
return new Operand<decimal>((decimal)Math.Sqrt((double)sqrSum)); | ||
} | ||
|
||
private Operand GetLengthOfString(Operand<string> stringOperand) | ||
{ | ||
return new Operand<decimal>(stringOperand.Value.Length); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
samples/Calculator.Samples.ExtendingCalculator/Parsers/StringOperandParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Calculator.Core.Operands; | ||
using Calculator.Core.Parsers; | ||
using Calculator.Core.ParsingContexts; | ||
using Calculator.Core.Tokens; | ||
|
||
namespace Calculator.Samples.ExtendingCalculator.Parsers; | ||
|
||
public class StringOperandParser : IParser | ||
{ | ||
public bool TryParse( | ||
ReadOnlySpan<char> formula, | ||
ParsingContext context, | ||
[NotNullWhen(true)] out Token? token, | ||
out int parsedLength) | ||
{ | ||
token = null; | ||
parsedLength = default; | ||
|
||
if (formula[0] != '"') | ||
{ | ||
return false; | ||
} | ||
|
||
bool isEscaped = false; | ||
int i; | ||
for (i = 1; i < formula.Length; i++) | ||
{ | ||
if (formula[i] == '\\' && !isEscaped) | ||
{ | ||
isEscaped = true; | ||
} | ||
else if (formula[i] == '"' && !isEscaped) | ||
{ | ||
break; | ||
} | ||
else | ||
{ | ||
isEscaped = false; | ||
} | ||
} | ||
|
||
if (i == formula.Length) | ||
{ | ||
return false; | ||
} | ||
|
||
token = new Operand<string>( | ||
formula[1..i] | ||
.ToString() | ||
.Replace("\\\"", "\"") | ||
.Replace("\\\\", "\\")); | ||
parsedLength = i + 1; | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters