Skip to content

Commit

Permalink
Render over function in prefix way
Browse files Browse the repository at this point in the history
  • Loading branch information
gs-ssh16 committed Nov 4, 2024
1 parent 7a25c3f commit f2f087c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

import org.apache.commons.lang3.StringUtils;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.set.ImmutableSet;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.list.mutable.ListAdapter;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.utility.LazyIterate;
Expand Down Expand Up @@ -295,6 +297,7 @@ public String returnChar()
return this.isRenderingHTML() ? "</BR>\n" : "\n";
}

private static final ImmutableSet<String> CORE_FUNCTIONS_WITH_PREFIX_RENDERING = Sets.immutable.of("if", "over");

// ----------------------------------------------- GENERAL -----------------------------------------------

Expand Down Expand Up @@ -820,7 +823,7 @@ else if (parameters.size() == 1)
+ (toCreateNewLine ? this.returnChar() + DEPRECATED_PureGrammarComposerCore.computeIndentationString(this, getTabSize(1)) : " ")
+ possiblyAddParenthesis(function, parameters.get(1), this);
}
else if ("if".equals(function))
else if (function != null && CORE_FUNCTIONS_WITH_PREFIX_RENDERING.contains(function))
{
return HelperValueSpecificationGrammarComposer.renderFunctionName(function, this) + "(" +
(this.isRenderingPretty() ? this.returnChar() + DEPRECATED_PureGrammarComposerCore.computeIndentationString(this, getTabSize(1)) : "") +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.finos.legend.engine.language.pure.grammar.to.DEPRECATED_PureGrammarComposerCore;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda;
import org.finos.legend.engine.shared.core.ObjectMapperFactory;
import org.finos.legend.engine.shared.core.api.grammar.RenderStyle;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -443,10 +444,25 @@ public void testLambdaEndingOnComment()

static void testLambda(String text)
{
testLambda(text, text);
testLambda(text, text, RenderStyle.STANDARD);
}

static void testLambda(String text, String formattedText)
{
testLambda(text, formattedText, RenderStyle.STANDARD);
}

static void testLambdaPretty(String text)
{
testLambda(text, text, RenderStyle.PRETTY);
}

static void testLambdaPretty(String text, String formattedText)
{
testLambda(text, formattedText, RenderStyle.PRETTY);
}

static void testLambda(String text, String formattedText, RenderStyle renderStyle)
{
Lambda postJSON_lambda;
try
Expand All @@ -459,6 +475,6 @@ static void testLambda(String text, String formattedText)
{
throw new RuntimeException(e);
}
Assert.assertEquals(formattedText, postJSON_lambda.accept(DEPRECATED_PureGrammarComposerCore.Builder.newInstance().build()));
Assert.assertEquals(formattedText, postJSON_lambda.accept(DEPRECATED_PureGrammarComposerCore.Builder.newInstance().withRenderStyle(renderStyle).build()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.junit.Test;

import static org.finos.legend.engine.language.pure.grammar.test.roundtrip.TestLambdaRoundtrip.testLambda;
import static org.finos.legend.engine.language.pure.grammar.test.roundtrip.TestLambdaRoundtrip.testLambdaPretty;

public class TestRelation
{
Expand Down Expand Up @@ -91,4 +92,36 @@ public void testCast()
{
testLambda("|test::Person.all()->meta::pure::functions::lang::cast(@Relation<(someCol:String, someCol:String)>)");
}

@Test
public void testOver()
{
testLambda("|over(~a)");
testLambda("|over(~[a, b], !c->descending())");
testLambda("|over(~[a, b], !c->descending(), [])");
testLambdaPretty(
"|over(\n" +
" ~a\n" +
")"
);
testLambdaPretty(
"|over(\n" +
" ~[\n" +
" a,\n" +
" b\n" +
" ],\n" +
" !c->descending()\n" +
")"
);
testLambdaPretty(
"|over(\n" +
" ~[\n" +
" a,\n" +
" b\n" +
" ],\n" +
" !c->descending(),\n" +
" []\n" +
")"
);
}
}

0 comments on commit f2f087c

Please sign in to comment.