Skip to content

Add tests for ROM and Token #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<Project>

<PropertyGroup>
<VersionPrefix>7.0.0-preview</VersionPrefix>
<VersionPrefix>7.0.1-preview</VersionPrefix>
<LangVersion>8.0</LangVersion>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CurrentYear>$([System.DateTime]::Now.ToString(yyyy))</CurrentYear>
<Authors>Marek Magdziak</Authors>
<Copyright>Copyright 2016-$(CurrentYear) $(Authors) et al. All rights reserved.</Copyright>
<Copyright>Copyright 2016-2019 $(Authors) et al. All rights reserved.</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageIcon>logo.64x64.png</PackageIcon>
Expand Down
1 change: 1 addition & 0 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup Condition="'$(IsTestProject)' == 'true'">
<CoverletOutputFormat>opencover</CoverletOutputFormat>
<CoverletOutput>$(MSBuildThisFileDirectory)../.coverage/$(AssemblyName)/</CoverletOutput>
<Include>[GraphQL-Parser]*</Include>
</PropertyGroup>

</Project>
31 changes: 31 additions & 0 deletions src/GraphQLParser.Tests/MemoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Shouldly;
using Xunit;

Expand All @@ -14,6 +15,9 @@ public void Operators()
rom.Length.ShouldBe(6);
rom.GetHashCode().ShouldNotBe(0);

(rom == new ROM(rom)).ShouldBeTrue();
(rom != new ROM(rom)).ShouldBeFalse();

(rom == str).ShouldBeTrue();
(str == rom).ShouldBeTrue();
(rom != str).ShouldBeFalse();
Expand All @@ -24,11 +28,38 @@ public void Operators()
rom2.Length.ShouldBe(5);
rom2.GetHashCode().ShouldNotBe(0);
rom2.GetHashCode().ShouldNotBe(rom.GetHashCode());
rom.Slice(6).GetHashCode().ShouldBe(0);

(rom2 == str).ShouldBeFalse();
(str == rom2).ShouldBeFalse();
(rom2 != str).ShouldBeTrue();
(str != rom2).ShouldBeTrue();
}

[Fact]
public void Equals_Should_Work()
{
var str = "string";
ROM rom = str;
rom.Equals(rom).ShouldBeTrue();
rom.Equals((object)rom).ShouldBeTrue();
rom.Equals((object)str).ShouldBeFalse();
rom.Equals("strin").ShouldBeFalse();
}

[Fact]
public void ImplicitCast()
{
ROM rom1 = "abc";
ReadOnlyMemory<char> mem = rom1;

mem.Span[0].ShouldBe('a');
mem.Span[1].ShouldBe('b');
mem.Span[2].ShouldBe('c');

ROM rom2 = new char[] { 'd', 'e' };
rom2.Span[0].ShouldBe('d');
rom2.Span[1].ShouldBe('e');
}
}
}
43 changes: 43 additions & 0 deletions src/GraphQLParser.Tests/TokenTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Shouldly;
using Xunit;

namespace GraphQLParser.Tests
{
public class TokenTests
{
[Theory]
[InlineData(TokenKind.EOF, null, "EOF")]
[InlineData(TokenKind.BANG, null, "!")]
[InlineData(TokenKind.DOLLAR, null, "$")]
[InlineData(TokenKind.PAREN_L, null, "(")]
[InlineData(TokenKind.PAREN_R, null, ")")]
[InlineData(TokenKind.SPREAD, null, "...")]
[InlineData(TokenKind.COLON, null, ":")]
[InlineData(TokenKind.EQUALS, null, "=")]
[InlineData(TokenKind.AT, null, "@")]
[InlineData(TokenKind.BRACKET_L, null, "[")]
[InlineData(TokenKind.BRACKET_R, null, "]")]
[InlineData(TokenKind.BRACE_L, null, "{")]
[InlineData(TokenKind.PIPE, null, "|")]
[InlineData(TokenKind.BRACE_R, null, "}")]

[InlineData(TokenKind.NAME, "abc", "Name \"abc\"")]
[InlineData(TokenKind.INT, "42", "Int \"42\"")]
[InlineData(TokenKind.FLOAT, "4.2", "Float \"4.2\"")]
[InlineData(TokenKind.STRING, "def", "String \"def\"")]
[InlineData(TokenKind.COMMENT, "xyz", "# \"xyz\"")]
[InlineData(TokenKind.UNKNOWN, "???", "Unknown \"???\"")]
public void ToStringTest(TokenKind kind, string value, string expectedDescription)
{
var token = new Token(kind, value, 0, 0);
token.ToString().ShouldBe(expectedDescription);
}

[Fact]
public void NotSupported_Token_Should_Throw()
{
Should.Throw<NotSupportedException>(() => new Token((TokenKind)999, "", 0, 0).ToString());
}
}
}
5 changes: 4 additions & 1 deletion src/GraphQLParser/Token.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace GraphQLParser
{
/// <summary>
Expand Down Expand Up @@ -71,7 +73,8 @@ public Token(TokenKind kind, ROM value, int start, int end)
TokenKind.FLOAT => "Float",
TokenKind.STRING => "String",
TokenKind.COMMENT => "#",
_ => string.Empty
TokenKind.UNKNOWN => "Unknown",
_ => throw new NotSupportedException(kind.ToString())
};

private bool HasUniqueValue =>
Expand Down