|
| 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