-
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
Struan Judd
committed
Jan 13, 2024
1 parent
53679df
commit 67626ea
Showing
6 changed files
with
107 additions
and
36 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
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()); | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
test/GqlPlus.Verifier.ClassTests/Model/ConstantModelChecks.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,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
43
test/GqlPlus.Verifier.ClassTests/Model/ConstantModelTests.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,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(); | ||
} |