Skip to content

Commit

Permalink
Test and tidy coveragr
Browse files Browse the repository at this point in the history
  • Loading branch information
Struan Judd committed Jan 13, 2024
1 parent 53679df commit 67626ea
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 36 deletions.
38 changes: 38 additions & 0 deletions src/GqlPlus.Verifier/Model/ConstantModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using GqlPlus.Verifier.Ast;
using GqlPlus.Verifier.Rendering;

namespace GqlPlus.Verifier.Model;

internal class ConstantModel : Structured<SimpleModel, ConstantModel>, IRendering
{
internal ConstantModel(SimpleModel value)
: base(value) { }

internal ConstantModel(IEnumerable<ConstantModel> values)
: base(values) { }

internal ConstantModel(Dictionary<SimpleModel, ConstantModel> values)
: base(values) { }

public RenderStructure Render()
=> Map.Count > 0 ? new RenderStructure("_ConstantMap", Map.ToDictionary(
p => p.Key.Render().Value!,
p => p.Value.Render()))
: List.Count > 0 ? new RenderStructure("_ConstantList", List.Select(c => c.Render()))
: Value is not null ? Value.Render()
: new("");
}

internal static class ConstantHelper
{
internal static ConstantModel ToModel(this ConstantAst constant)
=> constant.Fields.Count > 0 ? new(constant.Fields.ToModel())
: constant.Values.Length > 0 ? new(constant.Values.Select(v => v.ToModel()))
: constant.Value is not null ? new(constant.Value.ToModel())
: new(new SimpleModel());

internal static Dictionary<SimpleModel, ConstantModel> ToModel(this AstObject<ConstantAst> constant)
=> constant.ToDictionary(
p => p.Key.ToModel(),
p => p.Value.ToModel());
}
10 changes: 0 additions & 10 deletions src/GqlPlus.Verifier/Rendering/RenderStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ public RenderStructure(string tag, Dictionary<RenderValue, RenderStructure> map,
public static implicit operator RenderStructure(RenderValue value)
=> new("", value);

public RenderStructure Add(RenderStructure value)
{
if (value.IsEmpty) {
return this;
}

List.Add(value);
return this;
}

public RenderStructure Add(string key, RenderStructure value)
{
if (value.IsEmpty) {
Expand Down
14 changes: 6 additions & 8 deletions src/GqlPlus.Verifier/Rendering/RenderTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using YamlDotNet.Core;
using System.Diagnostics.CodeAnalysis;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;

Expand All @@ -8,16 +9,13 @@ internal class RenderTypeConverter : IYamlTypeConverter
{
public static readonly IYamlTypeConverter Instance = new RenderTypeConverter();

public bool Accepts(Type type) => type == typeof(RenderStructure) || type == typeof(RenderValue);
public bool Accepts(Type type) => type == typeof(RenderStructure);

[ExcludeFromCodeCoverage]
public object? ReadYaml(IParser parser, Type type) => throw new NotImplementedException();

public void WriteYaml(IEmitter emitter, object? yaml, Type type)
{
if (yaml is RenderValue value) {
WriteValue(emitter, value, "");
return;
}

if (yaml is RenderStructure model) {
var plainImplicit = string.IsNullOrWhiteSpace(model.Tag);
var tag = plainImplicit ? new TagName() : new TagName("!" + model.Tag);
Expand Down Expand Up @@ -50,7 +48,7 @@ public void WriteYaml(IEmitter emitter, object? yaml, Type type)
}
}

private void WriteValue(IEmitter emitter, RenderValue value, string tag)
private static void WriteValue(IEmitter emitter, RenderValue value, string tag)
{
var isString = !string.IsNullOrWhiteSpace(value.String);
var plainImplicit = string.IsNullOrWhiteSpace(tag) && !isString;
Expand Down
25 changes: 7 additions & 18 deletions src/GqlPlus.Verifier/Structured.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ internal class Structured<TValue, TStruct>
where TValue : notnull
where TStruct : Structured<TValue, TStruct>
{
internal class Dict : Dictionary<TValue, TStruct> { }
internal class Dict : Dictionary<TValue, TStruct>
{
internal Dict() : base() { }
internal Dict(IDictionary<TValue, TStruct> dictionary)
: base(dictionary) { }
}

internal TValue? Value { get; }
internal List<TStruct> List { get; } = [];
Expand All @@ -16,21 +21,5 @@ internal Structured(TValue value)
internal Structured(IEnumerable<TStruct> values)
=> List = [.. values];
internal Structured(Dictionary<TValue, TStruct> values)
=> Map = (Dict)values;

public static implicit operator TStruct(Structured<TValue, TStruct> structured)
=> (TStruct)structured;
}

internal static class StructuredHelper
{
internal static TStruct ToStructured<TSource, TValue, TStruct>(this IEnumerable<TSource> sources, Func<TSource, TStruct> value)
where TValue : notnull
where TStruct : Structured<TValue, TStruct>
=> new Structured<TValue, TStruct>(sources.Select(value));

internal static TStruct ToStructured<TSource, TValue, TStruct>(this IEnumerable<TSource> sources, Func<TSource, TValue> key, Func<TSource, TStruct> value)
where TValue : notnull
where TStruct : Structured<TValue, TStruct>
=> new Structured<TValue, TStruct>(sources.ToDictionary(key, value));
=> Map = new(values);
}
13 changes: 13 additions & 0 deletions test/GqlPlus.Verifier.ClassTests/Model/ConstantModelChecks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using GqlPlus.Verifier.Ast;
using GqlPlus.Verifier.Rendering;

namespace GqlPlus.Verifier.Model;

internal sealed class ConstantModelChecks
: ModelBaseChecks<string, ConstantAst>
{
protected override IRendering AstToModel(ConstantAst ast)
=> ast.ToModel();
protected override ConstantAst NewBaseAst(string input)
=> new FieldKeyAst(AstNulls.At, input);
}
43 changes: 43 additions & 0 deletions test/GqlPlus.Verifier.ClassTests/Model/ConstantModelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using GqlPlus.Verifier.Ast;

namespace GqlPlus.Verifier.Model;

public class ConstantModelTests : ModelBaseTests<string>
{
[Theory, RepeatData(Repeats)]
public void Model_List(string value)
=> _checks.AstExpected(
new(AstNulls.At, value.ConstantList()),
["- " + value, "- " + value]);

[Theory, RepeatData(Repeats)]
public void Model_Object(string key, string value)
=> _checks.AstExpected(
new(AstNulls.At, value.ConstantObject(key)),
["!_ConstantMap", key + ": " + value, value + ": " + key]);

//[Theory, RepeatData(Repeats)]
//public void Model_All(string name, string contents, string[] parameters, string[] aliases, ConstantOption option, ConstantLocation[] locations)
// => _checks.AstExpected(
// new(AstNulls.At, name) {
// Aliases = aliases,
// Description = contents,
// Locations = locations.Combine(),
// Option = option,
// Constants = parameters.Constants(),
// },
// ["!_Constant",
// $"aliases: [{string.Join(", ", aliases)}]",
// "description: " + ModelBaseChecks.YamlQuoted(contents),
// "locations: !_Set(_Location) " + ExpectedLocations(locations),
// "name: " + name,
// "parameters:",
// .. parameters.Select(p => "- !_Constant ''"),
// "repeatable: " + (option == ConstantOption.Repeatable).TrueFalse()]);

internal override IModelBaseChecks<string> BaseChecks => _checks;
protected override string[] ExpectedBase(string input)
=> [input];

private readonly ConstantModelChecks _checks = new();
}

0 comments on commit 67626ea

Please sign in to comment.