Skip to content

Commit

Permalink
Parse Option tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Struan Judd committed Jan 2, 2024
1 parent c27e7da commit da564ea
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/GqlPlus.Verifier/Parse/Schema/ParseEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ internal class EnumDefinition
internal EnumValueAst[] Values { get; set; } = [];
}

internal class ParseEnumDefinition(Parser<EnumValueAst>.D enumValue)
: Parser<EnumDefinition>.I
internal class ParseEnumDefinition(
Parser<EnumValueAst>.D enumValue
) : Parser<EnumDefinition>.I
{
private readonly Parser<EnumValueAst>.L _enumValue = enumValue;

Expand Down
56 changes: 56 additions & 0 deletions src/GqlPlus.Verifier/Parse/Schema/ParseOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Diagnostics.CodeAnalysis;
using GqlPlus.Verifier.Ast;
using GqlPlus.Verifier.Ast.Schema;
using GqlPlus.Verifier.Result;
using GqlPlus.Verifier.Token;

namespace GqlPlus.Verifier.Parse.Schema;

internal class ParseOption(
SimpleName name,
Parser<NullAst>.DA param,
Parser<string>.DA aliases,
Parser<NullAst>.D option,
Parser<OptionDefinition>.D definition
) : DeclarationParser<SimpleName, NullAst, NullAst, OptionDefinition, OptionDeclAst>(name, param, aliases, option, definition)
{
protected override void ApplyDefinition(OptionDeclAst result, OptionDefinition value)
=> result.Settings = value.Settings;

protected override bool ApplyOption(OptionDeclAst result, IResult<NullAst> option) => true;
protected override bool ApplyParameters(OptionDeclAst result, IResultArray<NullAst> parameter) => true;

[return: NotNull]
protected override OptionDeclAst MakeResult(TokenAt at, string? name, string description)
=> new(at, name!, description);
}

internal class OptionDefinition
{
internal OptionSettingAst[] Settings { get; set; } = [];
}

internal class ParseOptionDefinition(
Parser<OptionSettingAst>.D setting
) : Parser<OptionDefinition>.I
{
private readonly Parser<OptionSettingAst>.L _setting = setting;

public IResult<OptionDefinition> Parse<TContext>(TContext tokens, string label)
where TContext : Tokenizer
{
OptionDefinition result = new();

List<OptionSettingAst> values = [];
while (!tokens.Take("}")) {
var setting = _setting.Parse(tokens, "Option Setting");
if (!setting.Required(values.Add)) {
result.Settings = [.. values];
return setting.AsResult(result);
}
}

result.Settings = [.. values];
return result.Ok();
}
}
2 changes: 2 additions & 0 deletions src/GqlPlus.Verifier/Parse/Schema/SchemaParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public static IServiceCollection AddSchemaParsers(this IServiceCollection servic
.AddParser<DirectiveLocation, ParseDirectiveDefinition>()
.AddParser<DirectiveDeclAst, ParseDirective>()
// Option
.AddParser<OptionDefinition, ParseOptionDefinition>()
.AddParser<OptionSettingAst, ParseOptionSetting>()
.AddParser<OptionDeclAst, ParseOption>()
// Types
.AddSingleton<SimpleName>()
// Enum
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using GqlPlus.Verifier.Ast.Schema;

namespace GqlPlus.Verifier.Parse.Schema;

internal sealed class ParseOptionChecks
: BaseAliasedChecks<string, OptionDeclAst>
{
public ParseOptionChecks(Parser<OptionDeclAst>.D parser)
: base(parser) { }

internal static readonly string[] Settings = ["setting"];

protected internal override OptionDeclAst AliasedFactory(string input)
=> new(AstNulls.At, input) { Settings = Settings.OptionSettings() };

protected internal override string AliasesString(string input, string aliases)
=> input + aliases + "{setting='setting'}";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using GqlPlus.Verifier.Ast.Schema;

namespace GqlPlus.Verifier.Parse.Schema;

public sealed class ParseOptionTests(
Parser<OptionDeclAst>.D parser
) : BaseAliasedTests<string>
{
[Theory, RepeatData(Repeats)]
public void WithNameBad_ReturnsFalse(decimal id)
=> _test.False($"{id}{{}}");

[Theory, RepeatData(Repeats)]
public void WithSettings_ReturnsCorrectAst(string name)
=> _test.TrueExpected(
name + "{setting='setting'}",
new OptionDeclAst(AstNulls.At, name) {
Settings = s_settings.OptionSettings(),
});

[Theory, RepeatData(Repeats)]
public void WithSettingsBad_ReturnsFalse(string name)
=> _test.False(name + "{random}");

[Theory, RepeatData(Repeats)]
public void WithSettingsNone_ReturnsTrue(string name)
=> _test.TrueExpected(name + "{}", new OptionDeclAst(AstNulls.At, name));

internal override IBaseAliasedChecks<string> AliasChecks => _test;

private readonly ParseOptionChecks _test = new(parser);
private static readonly string[] s_settings = ["setting"];
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[
!!! I@7/2 : Invalid Output. '_BaseType' not defined. - '_BaseType' !!!,
!!! I@25/3 : Invalid Output. '_Named' not defined. - '_Named' !!!,
!!! I@7/10 : Invalid Output. '_BaseType' not defined. - '_BaseType' !!!,
!!! I@17/10 : Invalid Output. '_TypeKind' not defined. - '_TypeKind' !!!,
!!! I@7/11 : Invalid Output. '_TypeSimple' not defined. - '_TypeSimple' !!!,
!!! I@20/17 : Invalid Output. '_Collection' not defined. - '_Collection' !!!,
!!! I@7/21 : Invalid Output. '_Aliased' not defined. - '_Aliased' !!!,
!!! I@18/23 : Invalid Output. '_Modifier' not defined. - '_Modifier' !!!,
!!! I@18/27 : Invalid Output. '_InputBase' not defined. - '_InputBase' !!!,
!!! I@18/28 : Invalid Output. '_Constant' not defined. - '_Constant' !!!
!!! I@25/4 : Invalid Output. '_Named' not defined. - '_Named' !!!,
!!! I@36/7 : Invalid Output. '_Named' not defined. - '_Named' !!!,
!!! I@7/13 : Invalid Output. '_BaseType' not defined. - '_BaseType' !!!,
!!! I@17/13 : Invalid Output. '_TypeKind' not defined. - '_TypeKind' !!!,
!!! I@7/14 : Invalid Output. '_TypeSimple' not defined. - '_TypeSimple' !!!,
!!! I@20/20 : Invalid Output. '_Collection' not defined. - '_Collection' !!!,
!!! I@7/29 : Invalid Output. '_Aliased' not defined. - '_Aliased' !!!,
!!! I@18/31 : Invalid Output. '_Modifier' not defined. - '_Modifier' !!!,
!!! I@18/35 : Invalid Output. '_InputBase' not defined. - '_InputBase' !!!,
!!! I@18/36 : Invalid Output. '_Constant' not defined. - '_Constant' !!!
]

0 comments on commit da564ea

Please sign in to comment.