Search in sources :

Example 1 with Source

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();
    }
}
Also used : SourceRegistry(org.finos.legend.pure.m3.serialization.runtime.SourceRegistry) IntSet(org.eclipse.collections.api.set.primitive.IntSet) MutableIntSet(org.eclipse.collections.api.set.primitive.MutableIntSet) Source(org.finos.legend.pure.m3.serialization.runtime.Source)

Example 2 with Source

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");
    }
}
Also used : PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Test(org.junit.Test)

Example 3 with Source

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).");
    }
}
Also used : PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Test(org.junit.Test)

Example 4 with Source

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");
    }
}
Also used : PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Test(org.junit.Test)

Example 5 with Source

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. ");
    }
}
Also used : PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)69 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)63 Source (org.finos.legend.pure.m3.serialization.runtime.Source)36 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)21 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)17 RuntimeTestScriptBuilder (org.finos.legend.pure.m3.RuntimeTestScriptBuilder)16 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)14 SourceMutation (org.finos.legend.pure.m3.SourceMutation)11 SetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 RichIterable (org.eclipse.collections.api.RichIterable)10 Parser (org.finos.legend.pure.m3.serialization.grammar.Parser)10 PureParserException (org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException)10 PureException (org.finos.legend.pure.m4.exception.PureException)9 MutableList (org.eclipse.collections.api.list.MutableList)7 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)7 ThreadLocalTransactionContext (org.finos.legend.pure.m4.transaction.framework.ThreadLocalTransactionContext)7 MutableSet (org.eclipse.collections.api.set.MutableSet)6 TypeView (org.finos.legend.pure.m3.coreinstance.meta.pure.diagram.TypeView)6 InstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation)6