-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathConformanceTests.cs
221 lines (199 loc) · 8.8 KB
/
ConformanceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using System.IO;
using JsonLD.Core;
using JsonLD.Util;
using JsonLD.Test;
namespace JsonLD.Infrastructure.Text.Tests
{
public class ConformanceTests
{
[Theory, ClassData(typeof(ConformanceCases))]
public void ConformanceTestPasses(string id, ConformanceCase conformanceCase)
{
string result;
try
{
result = conformanceCase.run();
if (!JsonLdUtils.DeepCompareStrings(result, conformanceCase.output))
{
#if DEBUG
Console.WriteLine(id);
Console.WriteLine("Actual:");
Console.Write(JSONUtils.ToPrettyString(result));
Console.WriteLine("--------------------------");
Console.WriteLine("Expected:");
Console.Write(JSONUtils.ToPrettyString(conformanceCase.output));
Console.WriteLine("--------------------------");
#endif
Assert.True(false, "Returned JSON doesn't match expectations.");
}
}
catch (Exception ex)
{
if (conformanceCase.error == default) throw; // unexpected error
Assert.True(ex.Message.StartsWith(conformanceCase.error), "Resulting error doesn't match expectations.");
}
}
}
public class ConformanceCase
{
public string input { get; set; }
public string context { get; set; }
public string frame { get; set; }
public string output { get; set; }
public string error { get; set; }
public Func<string> run { get; set; }
}
public class ConformanceCases : IEnumerable<object[]>
{
string[] manifests = new[] {
"compact-manifest.jsonld",
"expand-manifest.jsonld",
"flatten-manifest.jsonld",
"frame-manifest.jsonld",
"toRdf-manifest.jsonld",
"fromRdf-manifest.jsonld",
"normalize-manifest.jsonld",
// Test tests are not supported on CORE CLR
#if !PORTABLE && !IS_CORECLR
"error-manifest.jsonld",
"remote-doc-manifest.jsonld",
#endif
};
public ConformanceCases()
{
}
public IEnumerator<object[]> GetEnumerator()
{
var rootDirectory = "W3C";
foreach (string manifest in manifests)
{
var testCases = JsonFetcher.GetTestCases(manifest, rootDirectory);
foreach (var testCase in testCases.Sequence)
{
Func<string> run;
ConformanceCase newCase = new ConformanceCase();
try
{
newCase.input = testCase.GetInputJson();
newCase.context = testCase.GetContextJson();
newCase.frame = testCase.GetFrameJson();
}
catch (Exception ex)
{
throw new Exception($"{testCase.Id} in {testCases.Name}", ex);
}
var options = new JsonLD.Infrastructure.Text.JsonLdOptions("http://json-ld.org/test-suite/tests/" + testCase.Input);
var testType = testCase.Type;
if (testType.Any((s) => s == "jld:NegativeEvaluationTest"))
{
newCase.error = testCase.Expect;
}
else if (testType.Any((s) => s == "jld:PositiveEvaluationTest"))
{
if (testType.Any((s) => new List<string> { "jld:ToRDFTest", "jld:NormalizeTest" }.Contains(s)))
{
newCase.output = File.ReadAllText(Path.Combine("W3C", testCase.Expect));
}
else if (testType.Any((s) => s == "jld:FromRDFTest"))
{
newCase.input = File.ReadAllText(Path.Combine("W3C", testCase.Input));
newCase.output = testCase.GetExpectJson();
}
else
{
newCase.output = testCase.GetExpectJson();
}
}
else
{
throw new Exception("Expecting either positive or negative evaluation test.");
}
if (testCase.Options != null)
{
if (testCase.Options.CompactArrays.HasValue)
{
options.SetCompactArrays(testCase.Options.CompactArrays.Value);
}
if (testCase.Options.Base != default)
{
options.SetBase(testCase.Options.Base);
}
if (testCase.Options.ExpandContext != default)
{
newCase.context = testCase.GetExpandContextJson();
options.SetExpandContext(newCase.context);
}
if (testCase.Options.ProduceGeneralizedRdf.HasValue)
{
options.SetProduceGeneralizedRdf(testCase.Options.ProduceGeneralizedRdf.Value);
}
if (testCase.Options.UseNativeTypes.HasValue)
{
options.SetUseNativeTypes(testCase.Options.UseNativeTypes.Value);
}
if (testCase.Options.UseRdfType.HasValue)
{
options.SetUseRdfType(testCase.Options.UseRdfType.Value);
}
}
if (testType.Any((s) => s == "jld:CompactTest"))
{
run = () => Infrastructure.Text.JsonLdProcessor.Compact(newCase.input, newCase.context, options);
}
else if (testType.Any((s) => s == "jld:ExpandTest"))
{
run = () => Infrastructure.Text.JsonLdProcessor.Expand(newCase.input, options);
}
else if (testType.Any((s) => s == "jld:FlattenTest"))
{
run = () => Infrastructure.Text.JsonLdProcessor.Flatten(newCase.input, newCase.context, options);
}
else if (testType.Any((s) => s == "jld:FrameTest"))
{
run = () => Infrastructure.Text.JsonLdProcessor.Frame(newCase.input, newCase.frame, options);
}
else if (testType.Any((s) => s == "jld:NormalizeTest"))
{
run = () => RDFDatasetUtils.ToNQuads((RDFDataset)Infrastructure.Text.JsonLdProcessor.Normalize(newCase.input, options)).Replace("\n", "\r\n");
}
else if (testType.Any((s) => s == "jld:ToRDFTest"))
{
options.format = "application/nquads";
run = () => Infrastructure.Text.JsonLdProcessor.ToRDF(newCase.input, options).Replace("\n", "\r\n");
}
else if (testType.Any((s) => s == "jld:FromRDFTest"))
{
options.format = "application/nquads";
run = () => Infrastructure.Text.JsonLdProcessor.FromRDF(newCase.input, options);
}
else
{
run = () => { throw new Exception("Couldn't find a test type, apparently."); };
}
if (testCases.AreRemoteDocumentTests)
{
Func<string> innerRun = run;
run = () =>
{
var remoteDoc = options.documentLoader.LoadDocument("https://json-ld.org/test-suite/tests/" + testCase.Input);
newCase.input = remoteDoc.Document;
options.SetBase(remoteDoc.DocumentUrl);
options.SetExpandContext(remoteDoc.Context);
return innerRun();
};
}
newCase.run = run;
yield return new object[] { manifest + testCase.Id, newCase };
}
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new Exception("auggh");
}
}
}