private static async Task <JArray> ExpandAsync(RemoteDocument doc, Uri documentLocation, JsonLdProcessorOptions options = null) { var activeContext = new JsonLdContext { Base = documentLocation }; if (options.Base != null) { activeContext.Base = options.Base; } var processor = new JsonLdProcessor(options); if (options.ExpandContext != null) { var expandObject = options.ExpandContext as JObject; if (expandObject != null) { var contextProperty = expandObject.Property("@context"); if (contextProperty != null) { activeContext = processor.ProcessContext(activeContext, contextProperty.Value); } else { activeContext = processor.ProcessContext(activeContext, expandObject); } } else { activeContext = processor.ProcessContext(activeContext, options.ExpandContext); } } if (doc.ContextUrl != null) { var contextDoc = await LoadJsonAsync(doc.ContextUrl, options); if (contextDoc.Document is string) { contextDoc.Document = JToken.Parse(contextDoc.Document as string); } activeContext = processor.ProcessContext(activeContext, contextDoc.Document as JToken); } if (doc.Document is string) { doc.Document = JToken.Parse(doc.Document as string); } return(processor.Expand(activeContext, null, doc.Document as JToken)); }
public void ExpandTests(string inputPath, string contextPath, string expectedOutputPath, string baseIri, string processorMode, string expandContextPath, bool compactArrays) { var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath, compactArrays); var inputJson = File.ReadAllText(inputPath); var expectedOutputJson = File.ReadAllText(expectedOutputPath); var inputElement = JToken.Parse(inputJson); var expectedOutputElement = JToken.Parse(expectedOutputJson); // Expand tests should not have a context parameter Assert.Null(contextPath); var actualOutputElement = JsonLdProcessor.Expand(inputElement, processorOptions); Assert.True(JToken.DeepEquals(actualOutputElement, expectedOutputElement), $"Error processing expand test {Path.GetFileName(inputPath)}.\nActual output does not match expected output.\nExpected:\n{expectedOutputElement}\n\nActual:\n{actualOutputElement}"); }
public virtual void ExpandTests(string testId, JsonLdTestType testType, string inputPath, string contextPath, string expectedOutputPath, JsonLdErrorCode expectErrorCode, string baseIri, string processorMode, string expandContextPath, bool compactArrays, string rdfDirection) { var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath, compactArrays, rdfDirection); var inputJson = File.ReadAllText(inputPath); var inputElement = JToken.Parse(inputJson); // Expand tests should not have a context parameter Assert.Null(contextPath); switch (testType) { case JsonLdTestType.PositiveEvaluationTest: var actualOutputElement = JsonLdProcessor.Expand(inputElement, processorOptions); var expectedOutputJson = File.ReadAllText(expectedOutputPath); var expectedOutputElement = JToken.Parse(expectedOutputJson); Assert.True(DeepEquals(actualOutputElement, expectedOutputElement), $"Error processing expand test {Path.GetFileName(inputPath)}.\nActual output does not match expected output.\nExpected:\n{expectedOutputElement}\n\nActual:\n{actualOutputElement}"); break; case JsonLdTestType.NegativeEvaluationTest: var exception = Assert.Throws <JsonLdProcessorException>( () => JsonLdProcessor.Expand(inputElement, processorOptions)); Assert.Equal(expectErrorCode, exception.ErrorCode); break; case JsonLdTestType.PositiveSyntaxTest: // Expect test to run without throwing processing errors var _ = JsonLdProcessor.Expand(inputElement, processorOptions); break; case JsonLdTestType.NegativeSyntaxTest: Assert.ThrowsAny <JsonLdProcessorException>(() => JsonLdProcessor.Expand(inputElement, processorOptions)); break; } }