add: DelimiterSemanticToken and tests

This commit is contained in:
jackfiled 2024-03-09 21:44:37 +08:00
parent f6a4ad4a44
commit e92b02a208
7 changed files with 104 additions and 12 deletions

View File

@ -19,7 +19,7 @@ public enum DelimiterType
LeftParenthesis, LeftParenthesis,
RightParenthesis, RightParenthesis,
LeftSquareBracket, LeftSquareBracket,
RightBracket RightSquareBracket
} }
public enum KeywordType public enum KeywordType

View File

@ -33,19 +33,32 @@ public class DelimiterSemanticToken : SemanticToken
public static bool TryParse(uint linePos, uint characterPos, LinkedListNode<char> now, public static bool TryParse(uint linePos, uint characterPos, LinkedListNode<char> now,
out DelimiterSemanticToken? token) out DelimiterSemanticToken? token)
{ {
switch (now.Value) Dictionary<char, DelimiterType> delimiterMap = new()
{ {
case ',': { ',', DelimiterType.Comma },
token = new DelimiterSemanticToken { '.', DelimiterType.Period },
{ { ':', DelimiterType.Colon },
LinePos = linePos, CharacterPos = characterPos, { ';', DelimiterType.Semicolon },
LiteralValue = ",", DelimiterType = DelimiterType.Comma { '(', DelimiterType.LeftParenthesis },
}; { ')', DelimiterType.RightParenthesis },
return true; { '[', DelimiterType.LeftSquareBracket },
default: { ']', DelimiterType.RightSquareBracket }
token = null; };
return false;
if (!delimiterMap.TryGetValue(now.Value, out DelimiterType value))
{
token = null;
return false;
} }
token = new DelimiterSemanticToken
{
LinePos = linePos,
CharacterPos = characterPos,
LiteralValue = new string([now.Value]),
DelimiterType = value
};
return true;
} }
} }

View File

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Canon.Core\Canon.Core.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1 @@
global using Xunit;

View File

@ -0,0 +1,27 @@
using Canon.Core.Enums;
using Canon.Core.LexicalParser;
namespace Canon.Tests.LexicalParserTests;
public class DelimiterTests
{
[Theory]
[InlineData(",123", DelimiterType.Comma)]
[InlineData(".123", DelimiterType.Period)]
[InlineData(":123", DelimiterType.Colon)]
[InlineData(";123", DelimiterType.Semicolon)]
[InlineData("(123)", DelimiterType.LeftParenthesis)]
[InlineData(").", DelimiterType.RightParenthesis)]
[InlineData("[asd", DelimiterType.LeftSquareBracket)]
[InlineData("]asd", DelimiterType.RightSquareBracket)]
public void SmokeTest(string input, DelimiterType type)
{
LinkedList<char> content = Utils.GetLinkedList(input);
Assert.True(DelimiterSemanticToken.TryParse(0, 0, content.First!,
out DelimiterSemanticToken? token));
Assert.NotNull(token);
Assert.Equal(type, token.DelimiterType);
}
}

16
Canon.Tests/Utils.cs Normal file
View File

@ -0,0 +1,16 @@
namespace Canon.Tests;
public static class Utils
{
public static LinkedList<char> GetLinkedList(string content)
{
LinkedList<char> list = [];
foreach(char c in content)
{
list.AddLast(c);
}
return list;
}
}

View File

@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{CA16
scripts\integration_test.py = scripts\integration_test.py scripts\integration_test.py = scripts\integration_test.py
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Canon.Tests", "Canon.Tests\Canon.Tests.csproj", "{E5F2B97B-3766-466D-9309-BA361F0CE15E}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -30,5 +32,9 @@ Global
{3D1C0BA2-57F2-41B2-B024-7A0E54A91DA0}.Debug|Any CPU.Build.0 = Debug|Any CPU {3D1C0BA2-57F2-41B2-B024-7A0E54A91DA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D1C0BA2-57F2-41B2-B024-7A0E54A91DA0}.Release|Any CPU.ActiveCfg = Release|Any CPU {3D1C0BA2-57F2-41B2-B024-7A0E54A91DA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D1C0BA2-57F2-41B2-B024-7A0E54A91DA0}.Release|Any CPU.Build.0 = Release|Any CPU {3D1C0BA2-57F2-41B2-B024-7A0E54A91DA0}.Release|Any CPU.Build.0 = Release|Any CPU
{E5F2B97B-3766-466D-9309-BA361F0CE15E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5F2B97B-3766-466D-9309-BA361F0CE15E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5F2B97B-3766-466D-9309-BA361F0CE15E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5F2B97B-3766-466D-9309-BA361F0CE15E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal