Skip to content

Commit f817674

Browse files
committed
Bugfixes. Now passing all W3C error tests as well.
1 parent 1083685 commit f817674

File tree

988 files changed

+20865
-42
lines changed

Some content is hidden

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

988 files changed

+20865
-42
lines changed

JsonLD.sln

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{BADA6F
1212
.nuget\NuGet.targets = .nuget\NuGet.targets
1313
EndProjectSection
1414
EndProject
15-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonLD.Test", "tests\JsonLD.Test\JsonLD.Test.csproj", "{8BAC5CEF-6505-4DFC-9D78-3A55EAB9B9DE}"
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonLD.Test", "tests\JsonLD.Test\JsonLD.Test.csproj", "{D41C622A-4470-4BE1-AB20-D9862B628840}"
1616
EndProject
1717
Global
1818
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -24,10 +24,10 @@ Global
2424
{C2DC1FFE-CD13-430E-B2FF-01574F72CACD}.Debug|Any CPU.Build.0 = Debug|Any CPU
2525
{C2DC1FFE-CD13-430E-B2FF-01574F72CACD}.Release|Any CPU.ActiveCfg = Release|Any CPU
2626
{C2DC1FFE-CD13-430E-B2FF-01574F72CACD}.Release|Any CPU.Build.0 = Release|Any CPU
27-
{8BAC5CEF-6505-4DFC-9D78-3A55EAB9B9DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28-
{8BAC5CEF-6505-4DFC-9D78-3A55EAB9B9DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
29-
{8BAC5CEF-6505-4DFC-9D78-3A55EAB9B9DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
30-
{8BAC5CEF-6505-4DFC-9D78-3A55EAB9B9DE}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{D41C622A-4470-4BE1-AB20-D9862B628840}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{D41C622A-4470-4BE1-AB20-D9862B628840}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{D41C622A-4470-4BE1-AB20-D9862B628840}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{D41C622A-4470-4BE1-AB20-D9862B628840}.Release|Any CPU.Build.0 = Release|Any CPU
3131
EndGlobalSection
3232
GlobalSection(SolutionProperties) = preSolution
3333
HideSolutionNode = FALSE

src/JsonLD/Core/Context.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,11 @@ public virtual JToken CompactValue(string activeProperty, JObject value)
134134
/// <returns></returns>
135135
/// <exception cref="JsonLdError">JsonLdError</exception>
136136
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
137-
public virtual JsonLD.Core.Context Parse(JToken localContext, JArray remoteContexts
138-
)
137+
public virtual JsonLD.Core.Context Parse(JToken localContext, List<string> remoteContexts)
139138
{
140139
if (remoteContexts == null)
141140
{
142-
remoteContexts = new JArray();
141+
remoteContexts = new List<string>();
143142
}
144143
// 1. Initialize result to the result of cloning active context.
145144
JsonLD.Core.Context result = ((JsonLD.Core.Context)this.Clone());
@@ -181,7 +180,7 @@ public virtual JsonLD.Core.Context Parse(JToken localContext, JArray remoteConte
181180
}
182181
remoteContexts.Add(uri);
183182
// 3.2.3: Dereference context
184-
RemoteDocument rd = null; // this.options.documentLoader.LoadDocument(uri);
183+
RemoteDocument rd = this.options.documentLoader.LoadDocument(uri);
185184
JToken remoteContext = rd.document;
186185
if (!(remoteContext is JObject) || !((JObject)remoteContext
187186
).ContainsKey("@context"))
@@ -305,7 +304,7 @@ public virtual JsonLD.Core.Context Parse(JToken localContext, JArray remoteConte
305304
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
306305
public virtual JsonLD.Core.Context Parse(JToken localContext)
307306
{
308-
return this.Parse(localContext, new JArray());
307+
return this.Parse(localContext, new List<string>());
309308
}
310309

311310
/// <summary>

src/JsonLD/Core/DocumentLoader.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
//using System;
2-
//using System.Collections;
3-
//using System.IO;
4-
//using JsonLDNet.Core;
5-
//using JsonLDNet.Util;
1+
using System;
2+
using System.Collections;
3+
using System.IO;
4+
using JsonLD.Core;
5+
using JsonLD.Util;
66

7-
//namespace JsonLDNet.Core
8-
//{
9-
// public class DocumentLoader
10-
// {
11-
// /// <exception cref="JsonLDNet.Core.JsonLdError"></exception>
12-
// public virtual RemoteDocument LoadDocument(string url)
13-
// {
14-
// RemoteDocument doc = new RemoteDocument(url, null);
15-
// try
16-
// {
17-
// doc.Document = FromURL(new URL(url));
18-
// }
19-
// catch (Exception)
20-
// {
21-
// new JsonLdError(JsonLdError.Error.LoadingRemoteContextFailed, url);
22-
// }
23-
// return doc;
24-
// }
7+
namespace JsonLD.Core
8+
{
9+
public class DocumentLoader
10+
{
11+
/// <exception cref="JsonLDNet.Core.JsonLdError"></exception>
12+
public virtual RemoteDocument LoadDocument(string url)
13+
{
14+
RemoteDocument doc = new RemoteDocument(url, null);
15+
try
16+
{
17+
doc.Document = JSONUtils.FromURL(new Uri(url));
18+
}
19+
catch (Exception)
20+
{
21+
throw new JsonLdError(JsonLdError.Error.LoadingRemoteContextFailed, url);
22+
}
23+
return doc;
24+
}
2525

2626
// /// <summary>An HTTP Accept header that prefers JSONLD.</summary>
2727
// /// <remarks>An HTTP Accept header that prefers JSONLD.</remarks>
@@ -159,5 +159,5 @@
159159
// httpClient = nextHttpClient;
160160
// }
161161
// }
162-
// }
163-
//}
162+
}
163+
}

src/JsonLD/Core/JsonLdOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public virtual void SetProduceGeneralizedRdf(bool produceGeneralizedRdf)
153153

154154
public string outputForm = null;
155155

156-
//public DocumentLoader documentLoader = new DocumentLoader();
156+
public DocumentLoader documentLoader = new DocumentLoader();
157157
// TODO: THE FOLLOWING ONLY EXIST SO I DON'T HAVE TO DELETE A LOT OF CODE,
158158
// REMOVE IT WHEN DONE
159159
}

src/JsonLD/Util/JSONUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static JToken FromReader(TextReader r)
4949

5050
using (var reader = new JsonTextReader(r))
5151
{
52-
var result = (JObject)serializer.Deserialize(reader);
52+
var result = (JToken)serializer.Deserialize(reader);
5353
return result;
5454
}
5555
}
@@ -148,10 +148,10 @@ public static string ToString(JToken obj)
148148
/// </exception>
149149
/// <exception cref="System.IO.IOException">If there was an error resolving the resource.
150150
/// </exception>
151-
public static JToken FromURL(URL url)
151+
public static JToken FromURL(Uri url)
152152
{
153-
var req = HttpWebRequest.Create(new Uri(url.ToString()));
154-
req.Headers.Add("Accept", AcceptHeader);
153+
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
154+
req.Accept = AcceptHeader;
155155
WebResponse resp = req.GetResponse();
156156
Stream stream = resp.GetResponseStream();
157157
return FromInputStream(stream);

src/JsonLD/Util/URL.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ public static URL Parse(string url)
114114
}
115115
rval.pathname = rval.path;
116116
ParseAuthority(rval);
117-
rval.normalizedPath = RemoveDotSegments(rval.pathname, !string.Empty.Equals(rval.
118-
authority));
117+
rval.normalizedPath = RemoveDotSegments(rval.pathname, !string.Empty.Equals(rval.authority));
119118
if (!string.Empty.Equals(rval.query))
120119
{
121120
rval.path += "?" + rval.query;

tests/JsonLD.Test/ConformanceTests.cs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
using Newtonsoft.Json.Linq;
8+
using Xunit;
9+
using Xunit.Extensions;
10+
using System.IO;
11+
using Newtonsoft.Json;
12+
using JsonLD.Core;
13+
using JsonLD.Util;
14+
15+
namespace JsonLD.Test
16+
{
17+
public class ConformanceTests
18+
{
19+
[Theory, ClassData(typeof(ConformanceCases))]
20+
public void ConformanceTestPasses(string id, string testname, ConformanceCase conformanceCase)
21+
{
22+
JToken result = conformanceCase.run();
23+
if (id.Contains("error"))
24+
{
25+
Assert.True(((string)result["error"]).StartsWith((string)conformanceCase.error), "errors don't match");
26+
}
27+
else
28+
{
29+
Assert.True(JsonLdUtils.DeepCompare(result, conformanceCase.output), "returned JSON matches expectations");
30+
}
31+
}
32+
}
33+
34+
public class ConformanceCase
35+
{
36+
public JToken input { get; set; }
37+
public JToken context { get; set; }
38+
public JToken frame { get; set; }
39+
public JToken output { get; set; }
40+
public JToken error { get; set; }
41+
public Func<JToken> run { get; set; }
42+
}
43+
44+
public class ConformanceCases: IEnumerable<object[]>
45+
{
46+
string[] manifests = new[] { "compact-manifest.jsonld", "error-manifest.jsonld", "expand-manifest.jsonld", "flatten-manifest.jsonld", "frame-manifest.jsonld", "normalize-manifest.jsonld" };
47+
48+
public ConformanceCases()
49+
{
50+
51+
}
52+
53+
public IEnumerator<object[]> GetEnumerator()
54+
{
55+
foreach (string manifest in manifests)
56+
{
57+
JToken manifestJson;
58+
59+
manifestJson = GetJson(manifest);
60+
61+
foreach (var testcase in manifestJson["sequence"])
62+
{
63+
Func<JToken> run;
64+
ConformanceCase newCase = new ConformanceCase();
65+
66+
newCase.input = GetJson(testcase["input"]);
67+
newCase.output = GetJson(testcase["expect"]);
68+
newCase.context = GetJson(testcase["context"]);
69+
newCase.frame = GetJson(testcase["frame"]);
70+
newCase.error = testcase["expect"];
71+
72+
var options = new JsonLdOptions("http://json-ld.org/test-suite/tests/" + testcase["input"]);
73+
74+
if (manifest.StartsWith("compact"))
75+
{
76+
if (((string)testcase["@id"]).Contains("0070"))
77+
{
78+
options.SetCompactArrays(false);
79+
}
80+
run = () => JsonLdProcessor.Compact(newCase.input, newCase.context, options);
81+
}
82+
else if (manifest.StartsWith("expand"))
83+
{
84+
if (((string)testcase["@id"]).Contains("0076"))
85+
{
86+
options.SetBase("http://example/base/");
87+
}
88+
if (((string)testcase["@id"]).Contains("0077"))
89+
{
90+
newCase.context = GetJson(testcase["option"]["expandContext"]);
91+
options.SetExpandContext((JObject)newCase.context);
92+
}
93+
run = () => JsonLdProcessor.Expand(newCase.input, options);
94+
}
95+
else if (manifest.StartsWith("error"))
96+
{
97+
newCase.output = new JObject();
98+
newCase.output["error"] = newCase.error;
99+
run = () => {
100+
try {
101+
JsonLdProcessor.Flatten(newCase.input, newCase.context, options);
102+
}
103+
catch (JsonLdError err)
104+
{
105+
JObject result = new JObject();
106+
result["error"] = err.Message;
107+
return result;
108+
}
109+
return new JValue((object)null);
110+
};
111+
}
112+
else if (manifest.StartsWith("flatten"))
113+
{
114+
if (((string)testcase["@id"]).Contains("0044"))
115+
{
116+
options.SetCompactArrays(false);
117+
}
118+
run = () => JsonLdProcessor.Flatten(newCase.input, newCase.context, options);
119+
}
120+
else if (manifest.StartsWith("frame"))
121+
{
122+
run = () => JsonLdProcessor.Frame(newCase.input, newCase.frame, options);
123+
}
124+
else if (manifest.StartsWith("remote-doc"))
125+
{
126+
run = () =>
127+
{
128+
var doc = new DocumentLoader().LoadDocument("http://json-ld.org/test-suite/tests/" + testcase["input"]).Document;
129+
return JsonLdProcessor.Expand(doc, options);
130+
};
131+
}
132+
else
133+
{
134+
continue;
135+
run = () => { throw new Exception(); };
136+
}
137+
138+
newCase.run = run;
139+
140+
yield return new object[] { manifest + testcase["@id"], (string)testcase["name"], newCase };
141+
}
142+
}
143+
}
144+
145+
private JToken GetJson(JToken j)
146+
{
147+
try {
148+
if (j.Type == JTokenType.Null) return null;
149+
using ( Stream manifestStream = File.OpenRead("W3C\\" + j))
150+
using (TextReader reader = new StreamReader(manifestStream))
151+
using (JsonReader jreader = new Newtonsoft.Json.JsonTextReader(reader))
152+
{
153+
return JToken.ReadFrom(jreader);
154+
}
155+
}
156+
catch
157+
{
158+
return null;
159+
}
160+
}
161+
162+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
163+
{
164+
throw new Exception("auggh");
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)