Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract dependency free API and tests #71

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 221 additions & 0 deletions JsonLD.Infrastructure.Text.Tests/ConformanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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");
}
}
}
44 changes: 44 additions & 0 deletions JsonLD.Infrastructure.Text.Tests/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace JsonLD.Infrastructure.Text.Tests
{
internal static class DictionaryExtensions
{
public static T Required<T>(this Dictionary<string, object> dictionary, string propertyName) => Extract<T>(dictionary, propertyName, true);

public static T Optional<T>(this Dictionary<string, object> dictionary, string propertyName) => Extract<T>(dictionary, propertyName, false);

private static T Extract<T>(Dictionary<string, object> dictionary, string propertyName, bool isRequired)
{
if (!dictionary.ContainsKey(propertyName))
{
if (isRequired)
{
var message = $"Expected top-level property {propertyName} but only found {string.Join(", ", dictionary.Keys)}";
throw new Exception(message);
}
else
{
return default;
}
}
var value = dictionary[propertyName];
if (typeof(T).IsGenericType) {
var genericType = typeof(T).GetGenericTypeDefinition();
if (genericType == typeof(List<>))
{
var list = Activator.CreateInstance<T>();
var addMethod = typeof(T).GetMethod("Add");
foreach(var item in (IEnumerable)value)
{
addMethod.Invoke(list, new[] { item });
}
return list;
}
}
return (T)value;
}
}
}
16 changes: 16 additions & 0 deletions JsonLD.Infrastructure.Text.Tests/JsonFetcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace JsonLD.Infrastructure.Text.Tests
{
internal static class JsonFetcher
{
public static JsonTestCases GetTestCases(string manifest, string rootDirectory)
{
var json = Test.JsonFetcher.GetJsonAsString(rootDirectory, manifest);
var parsed = TinyJson.JSONParser.FromJson<Dictionary<string, object>>(json);
var sequence = parsed.Required<List<object>>("sequence");
var name = parsed.Required<string>("name");
return new JsonTestCases(name, sequence, rootDirectory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer netstandard, but for now not wanting to change the core test project which is netcoreapp

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's have netstandard as a goal, but we'll get there when we'll get there.

</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\JsonLD.Infrastructure.Text\JsonLD.Infrastructure.Text.csproj" />
<ProjectReference Include="..\test\json-ld.net.tests\json-ld.net.tests.csproj" />
</ItemGroup>

</Project>
50 changes: 50 additions & 0 deletions JsonLD.Infrastructure.Text.Tests/JsonTestCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;

namespace JsonLD.Infrastructure.Text.Tests
{
internal class JsonTestCase
{
private readonly string _dataPath;
private readonly bool _isRemoteDocumentTest;

internal JsonTestCase(Dictionary<string, object> dictionary, string dataPath, bool isRemoteDocumentTest)
{
Id = dictionary.Required<string>("@id");
Type = dictionary.Optional<List<string>>("@type");
Input = dictionary.Required<string>("input");
Expect = dictionary.Required<string>("expect");
Context = dictionary.Optional<string>("context");
Frame = dictionary.Optional<string>("frame");
var options = dictionary.Optional<Dictionary<string, object>>("option");
if (options != null) Options = new JsonTestCaseOptions(options);
_dataPath = dataPath;
_isRemoteDocumentTest = isRemoteDocumentTest;
}

internal string Context { get; }

internal string Expect { get; }

internal string Frame { get; }

internal string Id { get; }

internal string Input { get; }

internal JsonTestCaseOptions Options { get; }

internal IEnumerable<string> Type { get; }

internal string GetContextJson() => Context == null ? null : Test.JsonFetcher.GetJsonAsString(_dataPath, Context);

internal string GetExpandContextJson() => Options?.ExpandContext == null ? null : Test.JsonFetcher.GetJsonAsString(_dataPath, Options.ExpandContext);

internal string GetExpectJson() => Expect == null ? null : Test.JsonFetcher.GetJsonAsString(_dataPath, Expect);

internal string GetFrameJson() => Frame == null ? null : Test.JsonFetcher.GetJsonAsString(_dataPath, Frame);

internal string GetInputJson() => _isRemoteDocumentTest
? Test.JsonFetcher.GetRemoteJsonAsString(Input)
: Test.JsonFetcher.GetJsonAsString(_dataPath, Input);
}
}
31 changes: 31 additions & 0 deletions JsonLD.Infrastructure.Text.Tests/JsonTestCaseOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;

namespace JsonLD.Infrastructure.Text.Tests
{
internal class JsonTestCaseOptions
{
internal JsonTestCaseOptions(Dictionary<string, object> options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
CompactArrays = options.Optional<bool?>("compactArrays");
Base = options.Optional<string>("base");
ExpandContext = options.Optional<string>("expandContext"); // this is temporary and incorrect
ProduceGeneralizedRdf = options.Optional<bool?>("produceGeneralizedRdf");
UseNativeTypes = options.Optional<bool?>("useNativeTypes");
UseRdfType = options.Optional<bool?>("useRdfType");
}

internal string Base { get; private set; }

internal bool? CompactArrays { get; private set; }

internal string ExpandContext { get; private set; }

internal bool? ProduceGeneralizedRdf { get; private set; }

internal bool? UseNativeTypes { get; private set; }

internal bool? UseRdfType { get; private set; }
}
}
Loading