-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathConformanceTests.cs
240 lines (217 loc) · 9.92 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using Xunit;
using System.IO;
using JsonLD.Core;
using JsonLD.Util;
namespace JsonLD.Test
{
public class ConformanceTests
{
[Theory, ClassData(typeof(ConformanceCases))]
public void ConformanceTestPasses(string id, ConformanceCase conformanceCase)
{
JToken result = conformanceCase.run();
if (conformanceCase.error != null)
{
Assert.True(((string)result["error"]).StartsWith((string)conformanceCase.error), "Resulting error doesn't match expectations.");
}
else
{
if (!JsonLdUtils.DeepCompare(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.");
}
}
}
}
public class ConformanceCase
{
public JToken input { get; set; }
public JToken context { get; set; }
public JToken frame { get; set; }
public JToken output { get; set; }
public JToken error { get; set; }
public Func<JToken> 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",
"error-manifest.jsonld",
"remote-doc-manifest.jsonld",
};
public ConformanceCases()
{
}
public IEnumerator<object[]> GetEnumerator()
{
var jsonFetcher = new JsonFetcher();
var rootDirectory = "W3C";
foreach (string manifest in manifests)
{
JToken manifestJson = jsonFetcher.GetJson(manifest, rootDirectory);
var isRemoteTest = (string)manifestJson["name"] == "Remote document";
foreach (JObject testcase in manifestJson["sequence"])
{
Func<JToken> run;
ConformanceCase newCase = new ConformanceCase();
// Load input file if not remote test. Remote tests load from the web at test execution time.
if (!isRemoteTest)
{
newCase.input = jsonFetcher.GetJson(testcase["input"], rootDirectory);
}
newCase.context = jsonFetcher.GetJson(testcase["context"], rootDirectory);
newCase.frame = jsonFetcher.GetJson(testcase["frame"], rootDirectory);
var options = new JsonLdOptions("http://json-ld.org/test-suite/tests/" + (string)testcase["input"]);
var testType = (JArray)testcase["@type"];
if (testType.Any((s) => (string)s == "jld:NegativeEvaluationTest"))
{
newCase.error = testcase["expect"];
}
else if (testType.Any((s) => (string)s == "jld:PositiveEvaluationTest"))
{
if (testType.Any((s) => new List<string> {"jld:ToRDFTest", "jld:NormalizeTest"}.Contains((string)s)))
{
newCase.output = File.ReadAllText(Path.Combine("W3C", (string)testcase["expect"]));
}
else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
{
newCase.input = File.ReadAllText(Path.Combine("W3C", (string)testcase["input"]));
newCase.output = jsonFetcher.GetJson(testcase["expect"], rootDirectory);
}
else
{
newCase.output = jsonFetcher.GetJson(testcase["expect"], rootDirectory);
}
}
else
{
throw new Exception("Expecting either positive or negative evaluation test.");
}
JToken optionToken;
JToken value;
if (testcase.TryGetValue("option", out optionToken))
{
JObject optionDescription = (JObject)optionToken;
if (optionDescription.TryGetValue("compactArrays", out value))
{
options.SetCompactArrays((bool)value);
}
if (optionDescription.TryGetValue("base", out value))
{
options.SetBase((string)value);
}
if (optionDescription.TryGetValue("expandContext", out value))
{
newCase.context = jsonFetcher.GetJson(testcase["option"]["expandContext"], rootDirectory);
options.SetExpandContext((JObject)newCase.context);
}
if (optionDescription.TryGetValue("produceGeneralizedRdf", out value))
{
options.SetProduceGeneralizedRdf((bool)value);
}
if (optionDescription.TryGetValue("useNativeTypes", out value))
{
options.SetUseNativeTypes((bool)value);
}
if (optionDescription.TryGetValue("useRdfType", out value))
{
options.SetUseRdfType((bool)value);
}
}
if (testType.Any((s) => (string)s == "jld:CompactTest"))
{
run = () => JsonLdProcessor.Compact(newCase.input, newCase.context, options);
}
else if (testType.Any((s) => (string)s == "jld:ExpandTest"))
{
run = () => JsonLdProcessor.Expand(newCase.input, options);
}
else if (testType.Any((s) => (string)s == "jld:FlattenTest"))
{
run = () => JsonLdProcessor.Flatten(newCase.input, newCase.context, options);
}
else if (testType.Any((s) => (string)s == "jld:FrameTest"))
{
run = () => JsonLdProcessor.Frame(newCase.input, newCase.frame, options);
}
else if (testType.Any((s) => (string)s == "jld:NormalizeTest"))
{
run = () => new JValue(
RDFDatasetUtils.ToNQuads((RDFDataset)JsonLdProcessor.Normalize(newCase.input, options)).Replace("\n", "\r\n")
);
}
else if (testType.Any((s) => (string)s == "jld:ToRDFTest"))
{
options.format = "application/nquads";
run = () => new JValue(
((string)JsonLdProcessor.ToRDF(newCase.input, options)).Replace("\n", "\r\n")
);
}
else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
{
options.format = "application/nquads";
run = () => JsonLdProcessor.FromRDF(newCase.input,options);
}
else
{
run = () => { throw new Exception("Couldn't find a test type, apparently."); };
}
if (isRemoteTest)
{
Func<JToken> innerRun = run;
run = () =>
{
var remoteDoc = options.documentLoader.LoadDocument("https://json-ld.org/test-suite/tests/" + (string)testcase["input"]);
newCase.input = remoteDoc.Document;
options.SetBase(remoteDoc.DocumentUrl);
options.SetExpandContext((JObject)remoteDoc.Context);
return innerRun();
};
}
if (testType.Any((s) => (string)s == "jld:NegativeEvaluationTest"))
{
Func<JToken> innerRun = run;
run = () =>
{
try
{
return innerRun();
}
catch (JsonLdError err)
{
JObject result = new JObject();
result["error"] = err.Message;
return result;
}
};
}
newCase.run = run;
yield return new object[] { manifest + (string)testcase["@id"], newCase };
}
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new Exception("auggh");
}
}
}