Skip to content

Commit

Permalink
BREAKING: Enforce testing for handling free section in grammar composing
Browse files Browse the repository at this point in the history
  • Loading branch information
akphi committed May 27, 2023
1 parent d97a886 commit d817fd6
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package org.finos.legend.engine.language.pure.grammar.test;

import org.finos.legend.engine.language.pure.grammar.test.TestGrammarRoundtrip;
import org.junit.Test;

public class TestFileGenerationGrammarRoundtrip extends TestGrammarRoundtrip.TestGrammarRoundtripTestSuite
Expand Down Expand Up @@ -77,7 +76,7 @@ public void testFileGeneration()
@Test
public void testFileGenerationWithImport()
{
test("###FileGeneration\n" +
testWithSectionInfoPreserved("###FileGeneration\n" +
"import anything::*;\n" +
"Avro model::AvroConfig\n" +
"{\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package org.finos.legend.engine.language.pure.grammar.test;

import org.finos.legend.engine.language.pure.grammar.test.TestGrammarRoundtrip;
import org.junit.Test;

public class TestGenerationSpecificationGrammarRoundtrip extends TestGrammarRoundtrip.TestGrammarRoundtripTestSuite
Expand Down Expand Up @@ -47,7 +46,7 @@ public void testGenerationSpecification()
@Test
public void testGenerationSpecificationWithImport()
{
test("###GenerationSpecification\n" +
testWithSectionInfoPreserved("###GenerationSpecification\n" +
"import anything::*;\n" +
"GenerationSpecification test::x\n" +
"{\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void testServiceWithEmbeddedRuntimeWithOptionalMapping()
@Test
public void testServiceWithImport()
{
test("###Service\n" +
testWithSectionInfoPreserved("###Service\n" +
"import meta::*;\n" +
"Service meta::pure::myServiceSingle\n" +
"{\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public String renderPureModelContextData(PureModelContextData pureModelContextDa
}
}

// FIXME: the following logic should be removed completely when we move to use extensions
Predicate<PackageableElement> isDomainElement = e ->
(e instanceof Class) ||
(e instanceof Association) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.collections.impl.utility.LazyIterate;
import org.eclipse.collections.impl.utility.ListIterate;
import org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser;
import org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer;
import org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposerContext;
Expand Down Expand Up @@ -68,10 +69,15 @@ protected void testComposedGrammar(String protocol, String expected) throws Json
*/
public static void test(String code)
{
test(code, null);
test(code, null, false);
}

private static void test(String code, String message)
public static void testWithSectionInfoPreserved(String code)
{
test(code, null, true);
}

private static void test(String code, String message, boolean keepSectionIndex)
{
PureModelContextData modelData = null;
try
Expand All @@ -87,16 +93,28 @@ private static void test(String code, String message)
}
PureGrammarComposer grammarTransformer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().build());
Assert.assertEquals(message, code, grammarTransformer.renderPureModelContextData(modelData));

if (keepSectionIndex)
{
return;
}

// check for rendering with no section index
PureModelContextData modelDataWithoutSectionIndex = PureModelContextData.newBuilder()
.withOrigin(modelData.origin)
.withSerializer(modelData.serializer)
.withElements(ListIterate.reject(modelData.getElements(), el -> el instanceof SectionIndex)).build();
Assert.assertEquals(message, code, grammarTransformer.renderPureModelContextData(modelDataWithoutSectionIndex));
}

public static void testFormatWithoutSectionIndex(String code, String unformattedCode)
public static void testFormatWithSectionInfoPreserved(String code, String unformattedCode)
{
testFormat(code, unformattedCode, true);
testFormat(code, unformattedCode, false);
}

public static void testFormat(String code, String unformattedCode)
{
testFormat(code, unformattedCode, false);
testFormat(code, unformattedCode, true);
}

/**
Expand Down Expand Up @@ -126,7 +144,7 @@ private static void testFormat(String code, String unformattedCode, boolean omit
String formatted = grammarTransformer.renderPureModelContextData(parsedModel);
Assert.assertEquals(code, formatted);
// NOTE: do not remove the round-trip test for formatted code as this is a very good way to ensure that grammar <-> >protocol is bijective
test(formatted, "Expected formatted code to pass round-trip test");
test(formatted, "Expected formatted code to pass round-trip test", !omitSectionIndex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void testConnection()
@Test
public void testConnectionWithImport()
{
test("Class meta::myClass\n" +
testWithSectionInfoPreserved("Class meta::myClass\n" +
"{\n" +
" name: String[1];\n" +
"}\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,15 +477,11 @@ public void testFunction()
" println('ok');\n" +
" 'a';\n" +
"}\n");

test("###Pure\n" +
"function test::getDateTime(): DateTime[1]\n" +
test("function test::getDateTime(): DateTime[1]\n" +
"{\n" +
" %1970-01-01T00:00:00.000\n" +
"}\n");

test("###Pure\n" +
"function test::getStrictDate(): StrictDate[1]\n" +
test("function test::getStrictDate(): StrictDate[1]\n" +
"{\n" +
" %1970-01-01\n" +
"}\n");
Expand Down Expand Up @@ -711,7 +707,7 @@ public void testFunctionWithQuotedVariables()
@Test
public void testClassWithImport()
{
test("import anything::*;\n" +
testWithSectionInfoPreserved("import anything::*;\n" +
"Class <<goes.businesstemporal>> {goes.doc = 'bla'} anything::A extends B, B\n" +
"[\n" +
" $this.ok->toOne() == 1,\n" +
Expand Down Expand Up @@ -746,7 +742,7 @@ public void testClassWithImport()
@Test
public void testEnumerationWithImport()
{
test("import anything::*;\n" +
testWithSectionInfoPreserved("import anything::*;\n" +
"Profile anything::goes\n" +
"{\n" +
" stereotypes: [test];\n" +
Expand All @@ -764,7 +760,7 @@ public void testEnumerationWithImport()
@Test
public void testAssociationWithImport()
{
test("import anything::*;\n" +
testWithSectionInfoPreserved("import anything::*;\n" +
"Class anything::goes2\n" +
"{\n" +
"}\n" +
Expand All @@ -785,7 +781,7 @@ public void testAssociationWithImport()
@Test
public void testFunctionWithImport()
{
test("import anything::*;\n" +
testWithSectionInfoPreserved("import anything::*;\n" +
"Class anything::goes2\n" +
"{\n" +
"}\n" +
Expand All @@ -805,21 +801,17 @@ public void testFunctionWithImport()
@Test
public void testDefaultValue()
{
test("import test::*;\n" +
"Class my::exampleRootType\n" +
test("Class my::exampleRootType\n" +
"{\n" +
"}\n\n" +

"Class my::exampleSubType extends my::exampleRootType\n" +
"{\n" +
"}\n\n" +

"Enum test::EnumWithDefault\n" +
"{\n" +
" DefaultValue,\n" +
" AnotherValue\n" +
"}\n\n" +

"Class test::A\n" +
"{\n" +
" stringProperty: String[1] = 'default';\n" +
Expand Down Expand Up @@ -881,8 +873,7 @@ public void testMetaFunctionExecutionWithFullPath()
@Test
public void testLambdaWithBiTemporalClass()
{
test("###Pure\n" +
"Class <<temporal.bitemporal>> main::Person\n" +
test("Class <<temporal.bitemporal>> main::Person\n" +
"{\n" +
" name: String[1];\n" +
" firm: main::Firm[1];\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public void testMergeModelMapping()
@Test
public void testMappingWithImport()
{
test("Class anything::goes\n" +
testWithSectionInfoPreserved("Class anything::goes\n" +
"{\n" +
" name: String[*];\n" +
"}\n" +
Expand Down Expand Up @@ -660,8 +660,7 @@ public void testCrossStoreAssociationMappingWithMilestoning()
@Test
public void testAggregationAware()
{
test("###Pure\n" +
"Class test::Product\n" +
test("Class test::Product\n" +
"{\n" +
" id: Integer[1];\n" +
" producer: test::Person[1];\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testRuntime()
@Test
public void testRuntimeWithImport()
{
test("Class meta::mySimpleClass\n" +
testWithSectionInfoPreserved("Class meta::mySimpleClass\n" +
"{\n" +
" name: String[1];\n" +
"}\n" +
Expand Down Expand Up @@ -175,7 +175,6 @@ public void testQuotedRuntime()
"\n" +
"\n" +
"###Runtime\n" +
"import meta::*;\n" +
"Runtime meta::mySimpleRuntime\n" +
"{\n" +
" mappings:\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void testGrammarRoundtripWithoutSectionIndex()
public void testSectionIndexKeepingSectionAndElementsInOrder()
{
// the ordering of sections is preserved
test("###Runtime\n" +
testWithSectionInfoPreserved("###Runtime\n" +
"import a::b::*;\n" +
"import runtime::a::d::*;\n" +
"import runtime::a::e::*;\n" +
Expand Down Expand Up @@ -133,30 +133,29 @@ public void testSectionIndexKeepingSectionAndElementsInOrder()
@Test
public void testSectionWithDuplicatedImports()
{
String unformatted = "###Runtime\n" +
testFormatWithSectionInfoPreserved("###Runtime\n" +
"import a::b::*;\n" +
"import runtime::a1::*;\n" +
"import runtime::a::e::*;\n" +
"import runtime::a1::*;\n" +
"import runtime::a::e::*;\n" +
"import runtime::a::e::*;\n" +
"Runtime meta::mySimpleRuntime\n" +
"{\n" +
" mappings:\n" +
" [\n" +
" ];\n" +
"}\n" +
"\n\n";
testFormat("###Runtime\n" +
"}\n", "###Runtime\n" +
"import a::b::*;\n" +
"import runtime::a1::*;\n" +
"import runtime::a::e::*;\n" +
"import runtime::a1::*;\n" +
"import runtime::a::e::*;\n" +
"import runtime::a::e::*;\n" +
"Runtime meta::mySimpleRuntime\n" +
"{\n" +
" mappings:\n" +
" [\n" +
" ];\n" +
"}\n", unformatted);
"}\n" +
"\n\n");
}

@Test
Expand Down Expand Up @@ -300,7 +299,7 @@ public void testRenderingSectionWithExtensionsWithoutSectionIndex()
"Class A\n" +
"{\n" +
"}\n";
testFormatWithoutSectionIndex("Class A\n" +
testFormat("Class A\n" +
"{\n" +
"}\n" +
"\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class TestAuthenticationGrammarRoundtrip_SecretTypes extends TestGrammarR
public void propertiesSecret()
{
test("###AuthenticationDemo\n" +
"import test::*;\n" +
"AuthenticationDemo demo::demo1\n" +
"{\n" +
" authentication: # UserPassword {\n" +
Expand All @@ -40,7 +39,6 @@ public void propertiesSecret()
public void environmentSecret()
{
test("###AuthenticationDemo\n" +
"import test::*;\n" +
"AuthenticationDemo demo::demo1\n" +
"{\n" +
" authentication: # UserPassword {\n" +
Expand All @@ -57,7 +55,6 @@ public void environmentSecret()
public void systemPropertiesSecret()
{
test("###AuthenticationDemo\n" +
"import test::*;\n" +
"AuthenticationDemo demo::demo1\n" +
"{\n" +
" authentication: # UserPassword {\n" +
Expand All @@ -74,7 +71,6 @@ public void systemPropertiesSecret()
public void awsSecretsManagerSecret_WithDefaultCredentials()
{
test("###AuthenticationDemo\n" +
"import test::*;\n" +
"AuthenticationDemo demo::demo1\n" +
"{\n" +
" authentication: # UserPassword {\n" +
Expand All @@ -96,7 +92,6 @@ public void awsSecretsManagerSecret_WithDefaultCredentials()
public void awsSecretsManagerSecret_WithStaticCredentials()
{
test("###AuthenticationDemo\n" +
"import test::*;\n" +
"AuthenticationDemo demo::demo1\n" +
"{\n" +
" authentication: # UserPassword {\n" +
Expand Down Expand Up @@ -125,9 +120,7 @@ public void awsSecretsManagerSecret_WithStaticCredentials()
@Test
public void awsSecretsManagerSecret_WithSTSAssumeRoleCredentials()
{
test("" +
"###AuthenticationDemo\n" +
"import test::*;\n" +
test("###AuthenticationDemo\n" +
"AuthenticationDemo demo::demo1\n" +
"{\n" +
" authentication: # UserPassword {\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package org.finos.legend.engine.language.pure.grammar.test;

import org.finos.legend.engine.language.pure.grammar.test.TestGrammarRoundtrip;
import org.junit.Test;

public class TestDiagramGrammarRoundtrip extends TestGrammarRoundtrip.TestGrammarRoundtripTestSuite
Expand Down Expand Up @@ -166,7 +165,7 @@ public void testDiagram()
@Test
public void testDiagramWithImport()
{
test("###Diagram\n" +
testWithSectionInfoPreserved("###Diagram\n" +
"import anything::*;\n" +
"Diagram meta::pure::MyDiagram\n" +
"{\n" +
Expand Down
Loading

0 comments on commit d817fd6

Please sign in to comment.