Skip to content

Commit 058d7e1

Browse files
committed
Adds tests for JsonLdApi.FromRDF
1 parent a04af73 commit 058d7e1

33 files changed

+3158
-69
lines changed

src/json-ld.net/Core/JsonLdApi.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public virtual JToken Compact(Context activeCtx, string activeProperty, JToken e
123123
// 7)
124124
JArray keys = new JArray(element.GetKeys());
125125

126-
if (!opts.GetPreserveOrder())
126+
//if (opts.GetSortGraphNodes())
127127
{
128128
keys.SortInPlace();
129129
}
@@ -435,8 +435,7 @@ public virtual JToken Compact(Context activeCtx, string activeProperty, JToken e
435435
/// <returns></returns>
436436
/// <exception cref="JsonLdError">JsonLdError</exception>
437437
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
438-
public virtual JToken Expand(Context activeCtx, string activeProperty, JToken element
439-
)
438+
public virtual JToken Expand(Context activeCtx, string activeProperty, JToken element)
440439
{
441440
// 1)
442441
if (element.IsNull())
@@ -497,7 +496,7 @@ public virtual JToken Expand(Context activeCtx, string activeProperty, JToken el
497496
// 7)
498497
JArray keys = new JArray(element.GetKeys());
499498

500-
if (!opts.GetPreserveOrder())
499+
//if (opts.GetSortGraphNodes())
501500
{
502501
keys.SortInPlace();
503502
}
@@ -819,7 +818,7 @@ public virtual JToken Expand(Context activeCtx, string activeProperty, JToken el
819818
// 7.6.2)
820819
JArray indexKeys = new JArray(value.GetKeys());
821820

822-
if (!opts.GetPreserveOrder())
821+
//if (opts.GetSortGraphNodes())
823822
{
824823
indexKeys.SortInPlace();
825824
}
@@ -1306,7 +1305,7 @@ private void GenerateNodeMap(JToken element, JObject nodeMap,
13061305
// 6.11)
13071306
JArray keys = new JArray(element.GetKeys());
13081307

1309-
if (!opts.GetPreserveOrder())
1308+
//if (!opts.GetSortGraphNodes())
13101309
{
13111310
keys.SortInPlace();
13121311
}
@@ -1457,7 +1456,7 @@ private void Frame(JsonLdApi.FramingContext state, JObject nodes
14571456
// add matches to output
14581457
JArray ids = new JArray(matches.GetKeys());
14591458

1460-
if (!opts.GetPreserveOrder())
1459+
//if (!opts.GetSortGraphs())
14611460
{
14621461
ids.SortInPlace();
14631462
}
@@ -1525,7 +1524,7 @@ private void Frame(JsonLdApi.FramingContext state, JObject nodes
15251524
JObject element = (JObject)matches[id];
15261525
JArray props = new JArray(element.GetKeys());
15271526

1528-
if (!opts.GetPreserveOrder())
1527+
//if (!opts.GetSortGraphNodes())
15291528
{
15301529
props.SortInPlace();
15311530
}
@@ -1607,7 +1606,7 @@ private void Frame(JsonLdApi.FramingContext state, JObject nodes
16071606
// handle defaults
16081607
props = new JArray(frame.GetKeys());
16091608

1610-
if (!opts.GetPreserveOrder())
1609+
//if (!opts.GetSortGraphNodes())
16111610
{
16121611
props.SortInPlace();
16131612
}
@@ -2152,7 +2151,7 @@ public virtual JArray FromRDF(RDFDataset dataset)
21522151
// 6)
21532152
JArray ids = new JArray(defaultGraph.GetKeys());
21542153

2155-
if (!opts.GetPreserveOrder())
2154+
if (!opts.GetSortGraphs())
21562155
{
21572156
ids.SortInPlace();
21582157
}
@@ -2168,7 +2167,7 @@ public virtual JArray FromRDF(RDFDataset dataset)
21682167
// 6.1.2)
21692168
JArray keys = new JArray(graphMap[subject_1].GetKeys());
21702169

2171-
if (!opts.GetPreserveOrder())
2170+
if (!opts.GetSortGraphNodes())
21722171
{
21732172
keys.SortInPlace();
21742173
}

src/json-ld.net/Core/JsonLdOptions.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using JsonLD.Core;
21
using Newtonsoft.Json.Linq;
32

43
namespace JsonLD.Core
@@ -43,8 +42,9 @@ public virtual JsonLD.Core.JsonLdOptions Clone()
4342

4443
private bool produceGeneralizedRdf = false;
4544

46-
private bool preserveOrder = false;
45+
private bool sortGraphs = true;
4746

47+
private bool sortGraphNodes = true;
4848
// base options
4949
// frame options
5050
// rdf conversion options
@@ -149,16 +149,25 @@ public virtual void SetProduceGeneralizedRdf(bool produceGeneralizedRdf)
149149
this.produceGeneralizedRdf = produceGeneralizedRdf;
150150
}
151151

152-
public virtual bool GetPreserveOrder()
152+
public virtual bool GetSortGraphs()
153153
{
154-
return preserveOrder;
154+
return sortGraphs;
155155
}
156156

157-
public virtual void SetPreserveOrder(bool preserveOrder)
157+
public virtual void SetSortGraphs(bool sortGraphs)
158158
{
159-
this.preserveOrder = preserveOrder;
159+
this.sortGraphs = sortGraphs;
160160
}
161161

162+
public virtual bool GetSortGraphNodes()
163+
{
164+
return sortGraphNodes;
165+
}
166+
167+
public virtual void SetSortGraphNodes(bool sortGraphNodes)
168+
{
169+
this.sortGraphNodes = sortGraphNodes;
170+
}
162171
public string format = null;
163172

164173
public bool useNamespaces = false;

src/json-ld.net/Core/JsonLdProcessor.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ namespace JsonLD.Core
1313
public class JsonLdProcessor
1414
{
1515
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
16-
public static JObject Compact(JToken input, JToken context, JsonLdOptions
17-
opts)
16+
public static JObject Compact(JToken input, JToken context, JsonLdOptions opts)
1817
{
1918
// 1)
2019
// TODO: look into java futures/promises
@@ -202,10 +201,7 @@ public static JToken Flatten(JToken input, JToken context, JsonLdOptions opts)
202201
}
203202
JArray keys = new JArray(graph.GetKeys());
204203

205-
if (!opts.GetPreserveOrder())
206-
{
207-
keys.SortInPlace();
208-
}
204+
keys.SortInPlace();
209205

210206
foreach (string id in keys)
211207
{
@@ -221,10 +217,7 @@ public static JToken Flatten(JToken input, JToken context, JsonLdOptions opts)
221217
// 6)
222218
JArray keys_1 = new JArray(defaultGraph.GetKeys());
223219

224-
if (!opts.GetPreserveOrder())
225-
{
226-
keys_1.SortInPlace();
227-
}
220+
keys_1.SortInPlace();
228221

229222
foreach (string id_1 in keys_1)
230223
{

src/json-ld.net/Core/NormalizeUtils.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ public virtual object HashBlankNodes(IEnumerable<string> unnamed_)
4545
// done, name blank nodes
4646
bool named = false;
4747
IList<string> hashes = new List<string>(unique.Keys);
48-
hashes.SortInPlace();
48+
49+
//if (!options.GetSortGraphs())
50+
{
51+
hashes.SortInPlace();
52+
}
53+
4954
foreach (string hash in hashes)
5055
{
5156
string bnode = unique[hash];
@@ -73,7 +78,12 @@ public virtual object HashBlankNodes(IEnumerable<string> unnamed_)
7378
// names duplicate hash bnodes
7479
// enumerate duplicate hash groups in sorted order
7580
hashes = new List<string>(duplicates.Keys);
76-
hashes.SortInPlace();
81+
82+
//if (!options.GetSortGraphs())
83+
{
84+
hashes.SortInPlace();
85+
}
86+
7787
// process each group
7888
for (int pgi = 0; ; pgi++)
7989
{
@@ -108,8 +118,13 @@ public virtual object HashBlankNodes(IEnumerable<string> unnamed_)
108118
normalized.Add(RDFDatasetUtils.ToNQuad(quad, quad.ContainsKey("name"
109119
) && !(quad["name"] == null) ? (string)((IDictionary<string,object>)((IDictionary<string,object>)quad)["name"])["value"] : null));
110120
}
111-
// sort normalized output
112-
normalized.SortInPlace();
121+
122+
//if (!options.GetSortGraphs())
123+
{
124+
// sort normalized output
125+
normalized.SortInPlace();
126+
}
127+
113128
// handle output format
114129
if (options.format != null)
115130
{
@@ -141,8 +156,12 @@ public virtual object HashBlankNodes(IEnumerable<string> unnamed_)
141156
{
142157
if (n_2 == group.Count)
143158
{
144-
// name bnodes in hash order
145-
results.SortInPlace(new _IComparer_145());
159+
//if (!options.GetSortGraphs())
160+
{
161+
// name bnodes in hash order
162+
results.SortInPlace(new _IComparer_145());
163+
}
164+
146165
foreach (NormalizeUtils.HashResult r in results)
147166
{
148167
// name all bnodes in path namer in

src/json-ld.net/json-ld.net.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Description>JSON-LD processor for .NET
44

55
Implements the W3C JSON-LD 1.0 standard.</Description>
6-
<VersionPrefix>1.0.6</VersionPrefix>
6+
<VersionPrefix>1.0.7.1</VersionPrefix>
77
<Authors>NuGet;linked-data-dotnet</Authors>
88
<TargetFrameworks>netstandard1.3;netstandard2.0;netcoreapp2.1</TargetFrameworks>
99
<AssemblyName>json-ld.net</AssemblyName>
@@ -17,6 +17,7 @@ Implements the W3C JSON-LD 1.0 standard.</Description>
1717
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1818
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
20+
<IsPackable>true</IsPackable>
2021
</PropertyGroup>
2122
<ItemGroup>
2223
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />

test/json-ld.net.tests/ConformanceTests.cs

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
54

65
using Newtonsoft.Json.Linq;
76
using Xunit;
8-
using Xunit.Extensions;
97
using System.IO;
10-
using Newtonsoft.Json;
118
using JsonLD.Core;
129
using JsonLD.Util;
1310

@@ -16,7 +13,7 @@ namespace JsonLD.Test
1613
public class ConformanceTests
1714
{
1815
[Theory, ClassData(typeof(ConformanceCases))]
19-
public void ConformanceTestPasses(string id, string testname, ConformanceCase conformanceCase)
16+
public void ConformanceTestPasses(string id, ConformanceCase conformanceCase)
2017
{
2118
JToken result = conformanceCase.run();
2219
if (conformanceCase.error != null)
@@ -77,20 +74,21 @@ public ConformanceCases()
7774

7875
public IEnumerator<object[]> GetEnumerator()
7976
{
77+
var jsonFetcher = new JsonFetcher();
78+
var rootDirectory = "W3C";
79+
8080
foreach (string manifest in manifests)
8181
{
82-
JToken manifestJson;
83-
84-
manifestJson = GetJson(manifest);
82+
JToken manifestJson = jsonFetcher.GetJson(manifest, rootDirectory);
8583

8684
foreach (JObject testcase in manifestJson["sequence"])
8785
{
8886
Func<JToken> run;
8987
ConformanceCase newCase = new ConformanceCase();
9088

91-
newCase.input = GetJson(testcase["input"]);
92-
newCase.context = GetJson(testcase["context"]);
93-
newCase.frame = GetJson(testcase["frame"]);
89+
newCase.input = jsonFetcher.GetJson(testcase["input"], rootDirectory);
90+
newCase.context = jsonFetcher.GetJson(testcase["context"], rootDirectory);
91+
newCase.frame = jsonFetcher.GetJson(testcase["frame"], rootDirectory);
9492

9593
var options = new JsonLdOptions("http://json-ld.org/test-suite/tests/" + (string)testcase["input"]);
9694

@@ -109,11 +107,11 @@ public IEnumerator<object[]> GetEnumerator()
109107
else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
110108
{
111109
newCase.input = File.ReadAllText(Path.Combine("W3C", (string)testcase["input"]));
112-
newCase.output = GetJson(testcase["expect"]);
110+
newCase.output = jsonFetcher.GetJson(testcase["expect"], rootDirectory);
113111
}
114112
else
115113
{
116-
newCase.output = GetJson(testcase["expect"]);
114+
newCase.output = jsonFetcher.GetJson(testcase["expect"], rootDirectory);
117115
}
118116
}
119117
else
@@ -138,7 +136,7 @@ public IEnumerator<object[]> GetEnumerator()
138136
}
139137
if (optionDescription.TryGetValue("expandContext", out value))
140138
{
141-
newCase.context = GetJson(testcase["option"]["expandContext"]);
139+
newCase.context = jsonFetcher.GetJson(testcase["option"]["expandContext"], rootDirectory);
142140
options.SetExpandContext((JObject)newCase.context);
143141
}
144142
if (optionDescription.TryGetValue("produceGeneralizedRdf", out value))
@@ -227,32 +225,11 @@ public IEnumerator<object[]> GetEnumerator()
227225

228226
newCase.run = run;
229227

230-
yield return new object[] { manifest + (string)testcase["@id"], (string)testcase["name"], newCase };
228+
yield return new object[] { manifest + (string)testcase["@id"], newCase };
231229
}
232230
}
233231
}
234232

235-
private JToken GetJson(JToken j)
236-
{
237-
try
238-
{
239-
if (j == null || j.Type == JTokenType.Null) return null;
240-
using (Stream manifestStream = File.OpenRead(Path.Combine("W3C", (string)j)))
241-
using (TextReader reader = new StreamReader(manifestStream))
242-
using (JsonReader jreader = new Newtonsoft.Json.JsonTextReader(reader)
243-
{
244-
DateParseHandling = DateParseHandling.None
245-
})
246-
{
247-
return JToken.ReadFrom(jreader);
248-
}
249-
}
250-
catch (Exception e)
251-
{ // TODO: this should not be here, figure out why this is needed or catch specific exception.
252-
return null;
253-
}
254-
}
255-
256233
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
257234
{
258235
throw new Exception("auggh");

test/json-ld.net.tests/JsonFetcher.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.IO;
5+
6+
namespace JsonLD.Test
7+
{
8+
public class JsonFetcher
9+
{
10+
public JToken GetJson(JToken j, string rootDirectory)
11+
{
12+
try
13+
{
14+
if (j == null || j.Type == JTokenType.Null) return null;
15+
using (Stream manifestStream = File.OpenRead(Path.Combine(rootDirectory, (string)j)))
16+
using (TextReader reader = new StreamReader(manifestStream))
17+
using (JsonReader jreader = new JsonTextReader(reader)
18+
{
19+
DateParseHandling = DateParseHandling.None
20+
})
21+
{
22+
return JToken.ReadFrom(jreader);
23+
}
24+
}
25+
catch (Exception e)
26+
{ // TODO: this should not be here, figure out why this is needed or catch specific exception.
27+
return null;
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)