Skip to content

Commit

Permalink
Cleaned up compile and PMD errors and warnings. Refactored named func…
Browse files Browse the repository at this point in the history
…tion reference unit tests to seperate found and not found tests.
  • Loading branch information
david-waltermire committed Dec 29, 2024
1 parent 8f5cf4d commit fd3ca06
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
*/
public class NamedFunctionReference implements IExpression {
@NonNull
private IEnhancedQName name;
private final IEnhancedQName name;
private final int arity;

/**
* Construct a new Metapath variable reference CST node.
*
* @param name
* the variable name
* the function name
* @param arity
* the number of function arguments
*/
public NamedFunctionReference(@NonNull IEnhancedQName name, int arity) {
this.name = name;
Expand All @@ -46,6 +48,11 @@ public IEnhancedQName getName() {
return name;
}

/**
* Get the expected number of function arguments for this lookup.
*
* @return the number of arguments
*/
public int getArity() {
return arity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapKey;
import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.math.BigDecimal;
Expand Down Expand Up @@ -267,6 +268,11 @@ public static IDayTimeDurationItem dayTimeDuration(@NonNull String value) {
return IDayTimeDurationItem.valueOf(value);
}

@NonNull
public static IEnhancedQName qname(@NonNull String namespace, @NonNull String localname) {
return IEnhancedQName.of(namespace, localname);
}

private TestUtils() {
// disable construction
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static com.github.seregamorph.hamcrest.MoreMatchers.where;
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.bool;
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.integer;
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.qname;
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.string;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
Expand All @@ -26,6 +27,7 @@
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
import gov.nist.secauto.metaschema.core.metapath.MetapathException;
import gov.nist.secauto.metaschema.core.metapath.StaticContext;
import gov.nist.secauto.metaschema.core.metapath.StaticMetapathException;
import gov.nist.secauto.metaschema.core.metapath.antlr.FailingErrorListener;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10;
import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10Lexer;
Expand All @@ -36,12 +38,9 @@
import gov.nist.secauto.metaschema.core.metapath.cst.logic.If;
import gov.nist.secauto.metaschema.core.metapath.cst.logic.ValueComparison;
import gov.nist.secauto.metaschema.core.metapath.function.ComparisonFunctions;
import gov.nist.secauto.metaschema.core.metapath.function.DefaultFunction;
import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
import gov.nist.secauto.metaschema.core.metapath.function.library.FnString;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUuidItem;
Expand Down Expand Up @@ -266,27 +265,47 @@ void testForwardstepChild() {

static Stream<Arguments> testNamedFunctionRef() {
return Stream.of(
Arguments.of("fn:string#1", "fn:string", null),
Arguments.of("fn:string#4", null, "MPST0017: unable to find function with name 'fn:string' having arity '4'"));
Arguments.of("fn:string#1", qname(MetapathConstants.NS_METAPATH_FUNCTIONS, "string"), 1));
}

@ParameterizedTest
@MethodSource
void testNamedFunctionRef(@NonNull String metapath, @NonNull String expectedQname,
@NonNull String expectedExceptionMessage) {
void testNamedFunctionRef(
@NonNull String metapath,
@NonNull IEnhancedQName expectedQname,
int expectedArity) {
StaticContext staticContext = StaticContext.builder().build();
DynamicContext dynamicContext = new DynamicContext(staticContext);

IFunction result = IMetapathExpression.compile(metapath, staticContext)
.evaluateAs(null, IMetapathExpression.ResultType.ITEM, dynamicContext);
assertAll(
() -> assertEquals(expectedQname, result == null ? null : result.getQName()),
() -> assertEquals(expectedArity, result == null ? null : result.arity()));
}

static Stream<Arguments> testNamedFunctionRefNotFound() {
return Stream.of(
Arguments.of("fn:string#4"));
}

@ParameterizedTest
@MethodSource("testNamedFunctionRefNotFound")
void testNamedFunctionRefNotFound(
@NonNull String metapath) {
StaticContext staticContext = StaticContext.builder().build();
DynamicContext dynamicContext = new DynamicContext(staticContext);

if (expectedQname != null) {
IFunction result = IMetapathExpression.compile(metapath, staticContext).evaluateAs(null,
IMetapathExpression.ResultType.ITEM, dynamicContext);
assertEquals(expectedQname, result.getQName().toString());
} else if (expectedExceptionMessage != null) {
MetapathException thrown = assertThrows(MetapathException.class,
() -> IMetapathExpression.compile(metapath, staticContext).evaluateAs(null,
IMetapathExpression.ResultType.ITEM, dynamicContext));
assertTrue(thrown.getMessage().contains(expectedExceptionMessage));
}
MetapathException thrown = assertThrows(MetapathException.class,
() -> IMetapathExpression.compile(metapath, staticContext).evaluateAs(null,
IMetapathExpression.ResultType.ITEM, dynamicContext));
Throwable cause = thrown.getCause();

assertEquals(
StaticMetapathException.NO_FUNCTION_MATCH,
cause instanceof StaticMetapathException
? ((StaticMetapathException) cause).getCode()
: null);
}

static Stream<Arguments> testComparison() {
Expand Down

0 comments on commit fd3ca06

Please sign in to comment.