Skip to content

Commit c7ab193

Browse files
authored
Merge pull request #116 from NeonGraal/sample-dirs
Sample dirs
2 parents 9eb914a + a292a08 commit c7ab193

File tree

883 files changed

+314
-481
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

883 files changed

+314
-481
lines changed

Diff for: test/GqlPlus.ComponentTestBase/SampleChecks.cs

+119-2
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,16 @@ protected async Task CheckErrors(string category, string directory, string file,
8383
() => extra.ShouldBeEmpty("Extra errors"));
8484
}
8585

86-
protected VerifySettings CustomSettings(string category, string group, string file)
86+
protected VerifySettings CustomSettings(string category, string group, string file, string section = "")
8787
{
8888
VerifySettings settings = new();
8989
settings.ScrubEmptyLines();
90-
settings.UseDirectory($"{category}{group}Tests");
9190
settings.UseFileName(file);
91+
if (string.IsNullOrWhiteSpace(section)) {
92+
settings.UseDirectory($"{category}{group}Tests");
93+
} else {
94+
settings.UseDirectory($"{category}{group}Tests/{section}");
95+
}
9296

9397
return settings.CheckAutoVerify();
9498
}
@@ -98,4 +102,117 @@ protected static async Task<string> ReadFile(string file, string extn, params st
98102

99103
protected static async Task<string> ReadSchema(string schema, params string[] dirs)
100104
=> await ReadFile(schema, "graphql+", ["Schema", .. dirs]);
105+
106+
protected static bool IsObjectInput(string input)
107+
=> input is not null && input.Contains("object ", StringComparison.Ordinal);
108+
109+
private static string PascalCase(string? input)
110+
=> string.Concat(TitleWords(input).Select(s => Abbreviations.TryGetValue(s, out string? abbr) ? abbr : s));
111+
112+
private static string CamelCase(string? input)
113+
=> string.Concat(TitleWords(input)
114+
.Select(s => Abbreviations.TryGetValue(s, out string? abbr) ? abbr : s)
115+
.Select((s, i) => i > 0 ? s : s.ToLowerInvariant()));
116+
117+
public static readonly (string, string)[] Replacements = [("dual", "Dual"), ("input", "Inp"), ("output", "Outp")];
118+
119+
protected static string ReplaceName(string? input, string testName)
120+
=> input is null ? ""
121+
: input
122+
.Replace("name", CamelCase(testName), StringComparison.InvariantCulture)
123+
.Replace("Name", PascalCase(testName), StringComparison.InvariantCulture);
124+
125+
protected static string ReplaceObject(string? input, string testName, string objectReplace, string objReplace)
126+
=> input is null ? ""
127+
: input
128+
.Replace("object", objectReplace, StringComparison.InvariantCulture)
129+
.Replace("Obj", objReplace, StringComparison.InvariantCulture)
130+
.Replace("name", CamelCase(testName), StringComparison.InvariantCulture)
131+
.Replace("Name", PascalCase(testName), StringComparison.InvariantCulture);
132+
133+
protected static IEnumerable<string> ReplaceValue(string input, string testName)
134+
=> input
135+
.ThrowIfNull()
136+
.Contains("object ", StringComparison.Ordinal)
137+
? Replacements.Select(r => ReplaceObject(input, testName, r.Item1, r.Item2))
138+
: [ReplaceName(input, testName)];
139+
140+
protected static async Task ReplaceFile(string testDirectory, string testName, Action<string, string, string> action)
141+
{
142+
ArgumentNullException.ThrowIfNull(action);
143+
string input = await ReadSchema(testName, testDirectory);
144+
145+
if (IsObjectInput(input)) {
146+
action.ShouldSatisfyAllConditions(
147+
() => {
148+
foreach ((string label, string abbr) in Replacements) {
149+
action(ReplaceObject(input, abbr, label, abbr), testDirectory, testName + "+" + label);
150+
}
151+
});
152+
} else {
153+
action(ReplaceName(input, testName), testDirectory, testName);
154+
}
155+
}
156+
157+
protected static async Task ReplaceFileAsync(string testDirectory, string testName, Func<string, string, string, Task> action)
158+
{
159+
ArgumentNullException.ThrowIfNull(action);
160+
string input = await ReadSchema(testName, testDirectory);
161+
162+
if (IsObjectInput(input)) {
163+
await WhenAll([.. Replacements
164+
.Select(r => action(ReplaceObject(input, testName, r.Item1, r.Item2), testDirectory, testName + "+" + r.Item1))]);
165+
} else {
166+
await action(ReplaceName(input, testName), testDirectory, testName);
167+
}
168+
}
169+
170+
protected async Task<IEnumerable<string>> SchemaValidDataAll()
171+
{
172+
IEnumerable<Task<IEnumerable<string>>> tasks = SchemaValidData
173+
.Files
174+
.SelectMany(kv => kv.Value.Select(file => (file, dir: kv.Key)))
175+
.Select(async p => ReplaceValue(await ReadSchema(p.file, p.dir), p.file));
176+
177+
return (await Task.WhenAll(tasks))
178+
.SelectMany(i => i);
179+
}
180+
181+
protected async Task<IEnumerable<string>> SchemaValidDataGroup(string group)
182+
{
183+
IEnumerable<Task<IEnumerable<string>>> tasks = SchemaValidData
184+
.Files[group]
185+
.Select(async file => ReplaceValue(await ReadSchema(file, group), file));
186+
187+
return (await Task.WhenAll(tasks))
188+
.SelectMany(i => i);
189+
}
190+
191+
protected static async Task<IEnumerable<string>> ReplaceSchemaKeys(string group)
192+
{
193+
IEnumerable<Task<(string input, string file)>> tasks = SchemaValidData
194+
.Files[group]
195+
.Select(async file => (input: await ReadSchema(file, group), file));
196+
197+
return (await Task.WhenAll(tasks))
198+
.SelectMany(p => IsObjectInput(p.input)
199+
? Replacements.Select(r => p.file + "+" + r.Item1)
200+
: [p.file])
201+
.Order();
202+
}
203+
204+
protected static async Task WhenAll(params Task[] tasks)
205+
{
206+
Task all = Task.WhenAll(tasks);
207+
208+
try {
209+
await all;
210+
} catch (Exception) {
211+
if (all.Exception is not null) {
212+
throw all.Exception;
213+
}
214+
215+
throw;
216+
}
217+
}
101218
}

Diff for: test/GqlPlus.ComponentTestBase/SampleSchemaChecks.cs

+2-102
Original file line numberDiff line numberDiff line change
@@ -11,115 +11,15 @@ Parser<IGqlpSchema>.D schemaParser
1111
{
1212
private readonly Parser<IGqlpSchema>.L _schemaParser = schemaParser;
1313

14-
protected IResult<IGqlpSchema> Parse(string schema, string label)
14+
public IResult<IGqlpSchema> Parse(string schema, string label)
1515
{
1616
Tokenizer tokens = new(schema);
1717
return _schemaParser.Parse(tokens, label);
1818
}
1919

20-
protected async Task<IGqlpSchema> ParseSample(string label, string sample, params string[] dirs)
20+
public async Task<IGqlpSchema> ParseSample(string label, string sample, params string[] dirs)
2121
{
2222
string schema = await ReadSchema(sample, dirs);
2323
return Parse(schema, label).Required();
2424
}
25-
26-
protected static bool IsObjectInput(string input)
27-
=> input is not null && input.Contains("object ", StringComparison.Ordinal);
28-
29-
private static string PascalCase(string? input)
30-
=> string.Concat(TitleWords(input).Select(s => Abbreviations.TryGetValue(s, out string? abbr) ? abbr : s));
31-
32-
private static string CamelCase(string? input)
33-
=> string.Concat(TitleWords(input)
34-
.Select(s => Abbreviations.TryGetValue(s, out string? abbr) ? abbr : s)
35-
.Select((s, i) => i > 0 ? s : s.ToLowerInvariant()));
36-
37-
public static readonly (string, string)[] Replacements = [("dual", "Dual"), ("input", "Inp"), ("output", "Outp")];
38-
39-
protected static string ReplaceName(string? input, string testName)
40-
=> input is null ? ""
41-
: input
42-
.Replace("name", CamelCase(testName), StringComparison.InvariantCulture)
43-
.Replace("Name", PascalCase(testName), StringComparison.InvariantCulture);
44-
45-
protected static string ReplaceObject(string? input, string testName, string objectReplace, string objReplace)
46-
=> input is null ? ""
47-
: input
48-
.Replace("object", objectReplace, StringComparison.InvariantCulture)
49-
.Replace("Obj", objReplace, StringComparison.InvariantCulture)
50-
.Replace("name", CamelCase(testName), StringComparison.InvariantCulture)
51-
.Replace("Name", PascalCase(testName), StringComparison.InvariantCulture);
52-
53-
protected static IEnumerable<string> ReplaceValue(string input, string testName)
54-
=> input
55-
.ThrowIfNull()
56-
.Contains("object ", StringComparison.Ordinal)
57-
? Replacements.Select(r => ReplaceObject(input, testName, r.Item1, r.Item2))
58-
: [ReplaceName(input, testName)];
59-
60-
protected static async Task ReplaceFile(string testDirectory, string testName, Action<string, string, string> action)
61-
{
62-
ArgumentNullException.ThrowIfNull(action);
63-
string input = await ReadSchema(testName, testDirectory);
64-
65-
if (IsObjectInput(input)) {
66-
action.ShouldSatisfyAllConditions(
67-
() => {
68-
foreach ((string label, string abbr) in Replacements) {
69-
action(ReplaceObject(input, abbr, label, abbr), testDirectory, testName + "+" + label);
70-
}
71-
});
72-
} else {
73-
action(ReplaceName(input, testName), testDirectory, testName);
74-
}
75-
}
76-
77-
protected static async Task ReplaceFileAsync(string testDirectory, string testName, Func<string, string, string, Task> action)
78-
{
79-
ArgumentNullException.ThrowIfNull(action);
80-
string input = await ReadSchema(testName, testDirectory);
81-
82-
if (IsObjectInput(input)) {
83-
await WhenAll([.. Replacements
84-
.Select(r => action(ReplaceObject(input, testName, r.Item1, r.Item2), testDirectory, testName + "+" + r.Item1))]);
85-
} else {
86-
await action(ReplaceName(input, testName), testDirectory, testName);
87-
}
88-
}
89-
90-
protected async Task<IEnumerable<string>> SchemaValidDataAll()
91-
{
92-
IEnumerable<Task<IEnumerable<string>>> tasks = SchemaValidData
93-
.Files
94-
.SelectMany(kv => kv.Value.Select(file => (file, dir: kv.Key)))
95-
.Select(async p => ReplaceValue(await ReadSchema(p.file, p.dir), p.file));
96-
97-
return (await Task.WhenAll(tasks))
98-
.SelectMany(i => i);
99-
}
100-
101-
protected async Task<IEnumerable<string>> SchemaValidDataGroup(string group)
102-
{
103-
IEnumerable<Task<IEnumerable<string>>> tasks = SchemaValidData
104-
.Files[group]
105-
.Select(async file => ReplaceValue(await ReadSchema(file, group), file));
106-
107-
return (await Task.WhenAll(tasks))
108-
.SelectMany(i => i);
109-
}
110-
111-
protected static async Task WhenAll(params Task[] tasks)
112-
{
113-
Task all = Task.WhenAll(tasks);
114-
115-
try {
116-
await all;
117-
} catch (Exception) {
118-
if (all.Exception is not null) {
119-
throw all.Exception;
120-
}
121-
122-
throw;
123-
}
124-
}
12525
}

Diff for: test/GqlPlus.ComponentTestBase/SchemaDataBase.cs

-13
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@ protected static IEnumerable<string> ReplaceKeys(IDictionary<string, string> inp
1717
: [k])
1818
.Order();
1919

20-
protected static async Task<IEnumerable<string>> ReplaceSchemaKeys(string group)
21-
{
22-
IEnumerable<Task<(string input, string file)>> tasks = SchemaValidData
23-
.Files[group]
24-
.Select(async file => (input: await ReadSchema(file, group), file));
25-
26-
return (await Task.WhenAll(tasks))
27-
.SelectMany(p => IsObjectInput(p.input)
28-
? Replacements.Select(r => p.file + "+" + r.Item1)
29-
: [p.file])
30-
.Order();
31-
}
32-
3320
protected static IEnumerable<string> ReplaceValues(IDictionary<string, string> inputs)
3421
=> inputs
3522
.ThrowIfNull()
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,11 @@
1-
using GqlPlus.Abstractions.Schema;
2-
using GqlPlus.Convert;
3-
using GqlPlus.Merging;
4-
using GqlPlus.Parsing;
5-
using GqlPlus.Result;
1+
using GqlPlus.Convert;
62

73
namespace GqlPlus.Sample;
84

95
public class SchemaJsonTests(
10-
Parser<IGqlpSchema>.D schemaParser,
11-
IMerge<IGqlpSchema> schemaMerger,
12-
IModelAndRender schemaRenderer
13-
) : SchemaDataBase(schemaParser)
6+
ISchemaVerifyChecks checks
7+
) : TestSchemaVerify(checks)
148
{
15-
[Fact]
16-
public async Task Json_All()
17-
=> await Verify_Model(await SchemaValidDataAll(), "!ALL");
18-
19-
[Theory]
20-
[ClassData(typeof(SchemaValidData))]
21-
public async Task Json_Groups(string group)
22-
=> await Verify_Model(await SchemaValidDataGroup(group), "!" + group);
23-
24-
[Theory]
25-
[ClassData(typeof(SamplesSchemaMergesData))]
26-
public async Task Json_Merges(string model)
27-
=> await ReplaceFileAsync("Merges", model, Verify_Model);
28-
29-
[Theory]
30-
[ClassData(typeof(SamplesSchemaObjectsData))]
31-
public async Task Json_Objects(string model)
32-
=> await ReplaceFileAsync("Objects", model, Verify_Model);
33-
34-
[Theory]
35-
[ClassData(typeof(SamplesSchemaGlobalsData))]
36-
public async Task Json_Globals(string global)
37-
=> await ReplaceFileAsync("Globals", global, Verify_Model);
38-
39-
[Theory]
40-
[ClassData(typeof(SamplesSchemaSimpleData))]
41-
public async Task Json_Simple(string simple)
42-
=> await ReplaceFileAsync("Simple", simple, Verify_Model);
43-
44-
[Theory]
45-
[ClassData(typeof(SamplesSchemaData))]
46-
public async Task JsonSchema(string sample)
47-
{
48-
IGqlpSchema ast = await ParseSample("Schema", sample);
49-
50-
Structured result = ModelAsts([ast]);
51-
52-
await Verify(result.ToJson(), "json", CustomSettings("Schema", "Json", sample));
53-
}
54-
55-
[Theory]
56-
[ClassData(typeof(SamplesSchemaSpecificationData))]
57-
public async Task Json_Spec(string sample)
58-
{
59-
IGqlpSchema ast = await ParseSample("Spec", sample, "Specification");
60-
61-
Structured result = ModelAsts([ast]);
62-
63-
await Verify(result.ToJson(), "json", CustomSettings("Spec", "Json", sample));
64-
}
65-
66-
private async Task Verify_Model(string input, string testDirectory, string test)
67-
=> await Verify_Model([input], test);
68-
69-
private async Task Verify_Model(IEnumerable<string> inputs, string test)
70-
{
71-
IEnumerable<IGqlpSchema> asts = inputs.Select(input => Parse(input, "Sample").Required());
72-
73-
Structured result = ModelAsts(asts);
74-
75-
await Verify(result.ToJson(), "json", CustomSettings("Sample", "Json", test));
76-
}
77-
78-
private Structured ModelAsts(IEnumerable<IGqlpSchema> asts)
79-
{
80-
IGqlpSchema schema = schemaMerger.Merge(asts).First();
81-
82-
Structured result = schemaRenderer.RenderAst(schema, schemaRenderer.WithBuiltIns());
83-
84-
return result;
85-
}
9+
protected override Task VerifyResult(Structured result, string label, string test, string testDirectory)
10+
=> Verify(result.ToJson(), "json", CustomSettings(label, "Json", test, testDirectory));
8611
}

0 commit comments

Comments
 (0)