@Override public Type getResultType() { return value.getResultType(); }
@Override public Type getResultType() { if ( thenExpression.getResultType().equals(elseExpression.getResultType()) ) { return thenExpression.getResultType(); } else { return BuiltInType.UNKNOWN; } }
@Test public void testNullLiteral() { String inputExpression = "null"; BaseNode nullLit = parse( inputExpression ); assertThat( nullLit, is( instanceOf( NullNode.class ) ) ); assertThat( nullLit.getResultType(), is( BuiltInType.UNKNOWN ) ); assertLocation( inputExpression, nullLit ); }
@Test public void testIntegerLiteral() { String inputExpression = "10"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( NumberNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); }
@Test public void testFloatLiteral() { String inputExpression = "10.5"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( NumberNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); }
@Test public void testBooleanTrueLiteral() { String inputExpression = "true"; BaseNode bool = parse( inputExpression ); assertThat( bool, is( instanceOf( BooleanNode.class ) ) ); assertThat( bool.getResultType(), is( BuiltInType.BOOLEAN ) ); assertLocation( inputExpression, bool ); }
@Test public void testBooleanFalseLiteral() { String inputExpression = "false"; BaseNode bool = parse( inputExpression ); assertThat( bool, is( instanceOf( BooleanNode.class ) ) ); assertThat( bool.getResultType(), is( BuiltInType.BOOLEAN ) ); assertLocation( inputExpression, bool ); }
@Test public void testLogicalNegation() { String inputExpression = "not ( true )"; BaseNode neg = parse( inputExpression ); assertThat( neg, is( instanceOf( FunctionInvocationNode.class ) ) ); assertThat( neg.getResultType(), is( BuiltInType.UNKNOWN ) ); assertThat( neg.getText(), is( "not ( true )" ) ); FunctionInvocationNode not = (FunctionInvocationNode) neg; assertThat( not.getParams().getElements().get( 0 ), is( instanceOf( BooleanNode.class ) ) ); assertThat( not.getParams().getElements().get( 0 ).getResultType(), is( BuiltInType.BOOLEAN ) ); assertThat( not.getParams().getElements().get( 0 ).getText(), is( "true" ) ); }
@Test public void testParensWithLiteral() { String inputExpression = "(10.5 )"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( NumberNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertThat( number.getText(), is( "10.5" ) ); }
@Test public void testEmptyList() { String inputExpression = "[]"; BaseNode list = parse( inputExpression ); assertThat( list, is( instanceOf( ListNode.class ) ) ); assertThat( list.getResultType(), is( BuiltInType.LIST ) ); assertThat( list.getText(), is( inputExpression ) ); ListNode ln = (ListNode) list; assertThat( ln.getElements(), is( empty() )); }
@Test public void testStringLiteral() { String inputExpression = "\"some string\""; BaseNode stringLit = parse( inputExpression ); assertThat( stringLit, is( instanceOf( StringNode.class ) ) ); assertThat( stringLit.getResultType(), is( BuiltInType.STRING ) ); assertLocation( inputExpression, stringLit ); assertThat(stringLit.getText(), is(inputExpression)); }
@Test public void testNegativeIntegerLiteral() { String inputExpression = "-10"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.NEGATIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10" ) ); }
@Test public void testNameReference() { String inputExpression = "someSimpleName"; BaseNode nameRef = parse( inputExpression, mapOf( entry("someSimpleName", BuiltInType.STRING) ) ); assertThat( nameRef, is( instanceOf( NameRefNode.class ) ) ); assertThat( nameRef.getResultType(), is( BuiltInType.STRING ) ); assertLocation( inputExpression, nameRef ); }
@Test public void testNegativeFloatLiteral() { String inputExpression = "-10.5"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.NEGATIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10.5" ) ); }
@Test public void testPositiveIntegerLiteral() { String inputExpression = "+10"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.POSITIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10" ) ); }
@Test public void testComparisonInFixOp() { String inputExpression = "foo >= bar * 10"; BaseNode infix = parse( inputExpression ); assertThat( infix, is( instanceOf( InfixOpNode.class ) ) ); assertThat( infix.getResultType(), is( BuiltInType.BOOLEAN ) ); assertThat( infix.getText(), is( inputExpression ) ); InfixOpNode in = (InfixOpNode) infix; assertThat( in.getLeft(), is( instanceOf( NameRefNode.class ) ) ); assertThat( in.getLeft().getText(), is( "foo" ) ); assertThat( in.getRight(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( in.getRight().getText(), is( "bar * 10" ) ); }
@Test public void testPositiveFloatLiteral() { String inputExpression = "+10.5"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.POSITIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10.5" ) ); }
@Test public void testQuantifiedExpressionSome() { String inputExpression = "some item in order.items satisfies item.price > 100"; BaseNode someBase = parse( inputExpression ); assertThat( someBase, is( instanceOf( QuantifiedExpressionNode.class ) ) ); assertThat( someBase.getText(), is( inputExpression ) ); assertThat( someBase.getResultType(), is( BuiltInType.BOOLEAN ) ); QuantifiedExpressionNode someExpr = (QuantifiedExpressionNode) someBase; assertThat( someExpr.getQuantifier(), is( QuantifiedExpressionNode.Quantifier.SOME ) ); assertThat( someExpr.getIterationContexts().size(), is( 1 ) ); assertThat( someExpr.getIterationContexts().get( 0 ).getText(), is( "item in order.items" ) ); assertThat( someExpr.getExpression().getText(), is( "item.price > 100" ) ); }
@Test public void testContextPathExpression2() { String inputExpression = "{ x : { y : \"foo\" } }.x.y"; BaseNode pathBase = parse( inputExpression ); assertThat( pathBase, is( instanceOf( PathExpressionNode.class ) ) ); assertThat( pathBase.getText(), is( inputExpression ) ); assertThat( pathBase.getResultType(), is( BuiltInType.STRING ) ); PathExpressionNode pathExpr = (PathExpressionNode) pathBase; assertThat( pathExpr.getExpression(), is( instanceOf( ContextNode.class ) ) ); assertThat( pathExpr.getExpression().getText(), is( "{ x : { y : \"foo\" } }" ) ); assertThat( pathExpr.getName(), is( instanceOf( QualifiedNameNode.class ) ) ); assertThat( pathExpr.getName().getText(), is( "x.y" ) ); }
@Test public void testIfExpression() { String inputExpression = "if applicant.age < 18 then \"declined\" else \"accepted\""; BaseNode ifBase = parse( inputExpression ); assertThat( ifBase, is( instanceOf( IfExpressionNode.class ) ) ); assertThat( ifBase.getText(), is( inputExpression ) ); assertThat( ifBase.getResultType(), is( BuiltInType.STRING ) ); IfExpressionNode ifExpr = (IfExpressionNode) ifBase; assertThat( ifExpr.getCondition().getText(), is( "applicant.age < 18" ) ); assertThat( ifExpr.getThenExpression().getText(), is( "\"declined\"" ) ); assertThat( ifExpr.getElseExpression().getText(), is( "\"accepted\"" ) ); }