use of org.finos.legend.pure.m3.serialization.runtime.Source in project legend-pure by finos.
the class SourceMutation method perform.
public void perform(PureRuntime pureRuntime) {
if (this.lineRangesToRemoveByFile.notEmpty()) {
SourceRegistry sourceRegistry = pureRuntime.getSourceRegistry();
for (String sourceId : this.lineRangesToRemoveByFile.keysView()) {
IntSet set = calculateLinesToRemove(this.lineRangesToRemoveByFile.get(sourceId));
Source source = sourceRegistry.getSource(sourceId);
if (source == null) {
throw new RuntimeException("Unknown source: " + sourceId);
}
String file = source.getContent();
String[] lines = LINE_SPLITTER.split(file);
StringBuilder buffer = new StringBuilder(file.length());
for (int i = 0; i < lines.length; i++) {
if (!set.contains(i + 1)) {
buffer.append(lines[i]);
}
}
pureRuntime.modify(sourceId, buffer.toString());
}
pureRuntime.compile();
}
}
use of org.finos.legend.pure.m3.serialization.runtime.Source in project legend-pure by finos.
the class AbstractTestFromJson method testFromJsonThrowsValidationErrors_ConstraintOnProperty.
@Test
public void testFromJsonThrowsValidationErrors_ConstraintOnProperty() {
try {
String[] source = { "import meta::json::*;", "Class meta::pure::functions::json::tests::A", "[ TEST_CONTROL: $this.a == 'dave' ]", "{ a:String[1]; }", "Class meta::pure::functions::json::tests::B", "{ b:meta::pure::functions::json::tests::A[1]; }", "function go():Any[*]", "{ let json='{ \"b\": {\"a\": \"fred\" } }'->meta::json::fromJson(meta::pure::functions::json::tests::B, ^meta::json::JSONDeserializationConfig(typeKeyName='@type', failOnUnknownProperties=false));", " assert(!$json->isEmpty(), |''); ", " assert(\'fred\' == $json.b.a, |''); ", "}" };
this.compileTestSource("fromString.pure", StringUtils.join(source, "\n") + "\n");
CoreInstance func = this.runtime.getFunction("go():Any[*]");
this.functionExecution.start(func, FastList.<CoreInstance>newList());
Assert.fail("Expected exception evaluating: \n" + source);
} catch (PureExecutionException e) {
this.assertException(e, "Error populating property 'b' on class 'meta::pure::functions::json::tests::B': \nCould not create new instance of meta::pure::functions::json::tests::A: \nConstraint :[TEST_CONTROL] violated in the Class A");
}
}
use of org.finos.legend.pure.m3.serialization.runtime.Source in project legend-pure by finos.
the class AbstractTestFromJson method failOnMissingField.
@Test
public void failOnMissingField() {
String[] rawSource = { "import meta::json::*;\nimport meta::pure::functions::json::tests::*;", "Class meta::pure::functions::json::tests::MissingData", "{\n missing : Integer[1];\n}", "function missing():Any[*]", "{\n let json = '{}';", " $json -> fromJson(MissingData, ^meta::json::JSONDeserializationConfig(typeKeyName='@type', failOnUnknownProperties=false));\n}" };
String source = StringUtils.join(rawSource, "\n") + "\n";
try {
this.compileTestSource("fromString.pure", source);
CoreInstance func = this.runtime.getFunction("missing():Any[*]");
this.functionExecution.start(func, FastList.<CoreInstance>newList());
Assert.fail("Expected exception evaluating: \n" + source);
} catch (PureExecutionException e) {
this.assertException(e, "Error populating property 'missing' on class 'meta::pure::functions::json::tests::MissingData': \nExpected value(s) of multiplicity [1], found 0 value(s).");
}
}
use of org.finos.legend.pure.m3.serialization.runtime.Source in project legend-pure by finos.
the class AbstractTestFromJson method testFromJsonThrowsValidationErrors_RootConstraintOnPropertyClass.
@Test
public void testFromJsonThrowsValidationErrors_RootConstraintOnPropertyClass() {
try {
String[] source = { "import meta::json::*;", "Class meta::pure::functions::json::tests::A", "{ a:String[1]; }", "Class meta::pure::functions::json::tests::B", "[ TEST_CONTROL: $this.b.a == 'dave' ]", "{ b:meta::pure::functions::json::tests::A[1]; }", "function go():Any[*]", "{\n", "let json='{ \"b\": {\"a\": \"fred\" } }'->meta::json::fromJson(meta::pure::functions::json::tests::B, ^meta::json::JSONDeserializationConfig(typeKeyName='@type', failOnUnknownProperties=false));", " assert(!$json->isEmpty(), |''); ", " assert(\'fred\' == $json.b.a, |''); ", "}" };
this.compileTestSource("fromString.pure", StringUtils.join(source, "\n") + "\n");
CoreInstance func = this.runtime.getFunction("go():Any[*]");
this.functionExecution.start(func, FastList.<CoreInstance>newList());
Assert.fail("Expected exception evaluating: \n" + source);
} catch (PureExecutionException e) {
this.assertException(e, "Could not create new instance of meta::pure::functions::json::tests::B: \nConstraint :[TEST_CONTROL] violated in the Class B");
}
}
use of org.finos.legend.pure.m3.serialization.runtime.Source in project legend-pure by finos.
the class AbstractTestFromJson method deserializationConfig_failOnUnknownProperties.
@Test
public void deserializationConfig_failOnUnknownProperties() {
String[] rawSource = { "import meta::json::*;\nimport meta::pure::functions::json::tests::*;", "Class meta::pure::functions::json::tests::failUnknown", "{\n testField : Integer[1];\n}", "function failUnknown():Any[*]", "{\n let json = '{\"testField\": 1,\"secondProperty\":2}';", "let config = ^JSONDeserializationConfig(typeKeyName='@type', failOnUnknownProperties=true);\n", " $json -> fromJson(failUnknown, $config);\n}" };
String source = StringUtils.join(rawSource, "\n") + "\n";
try {
this.compileTestSource("fromString.pure", source);
CoreInstance func = this.runtime.getFunction("failUnknown():Any[*]");
this.functionExecution.start(func, FastList.<CoreInstance>newList());
Assert.fail("Expected exception evaluating: \n" + source);
} catch (PureExecutionException e) {
this.assertException(e, "Property 'secondProperty' can't be found in class meta::pure::functions::json::tests::failUnknown. ");
}
}
Aggregations