@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 ); }
private BaseNode parse(String input) { return parse( input, Collections.emptyMap() ); }
@Test public void testEmptyContext() { String inputExpression = "{}"; BaseNode context = parse( inputExpression ); assertThat( context, is( instanceOf( ContextNode.class ) ) ); assertThat( context.getText(), is( inputExpression ) ); ContextNode ctx = (ContextNode) context; assertThat( ctx.getEntries(), is( empty() )); }
@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 testExpressionList() { // TODO review this test is potentially wrong as the list is not homogeneous String inputExpression = "[ 10, foo * bar, true ]"; BaseNode list = parse( inputExpression ); assertThat( list, is( instanceOf( ListNode.class ) ) ); assertThat( list.getResultType(), is( BuiltInType.LIST ) ); assertThat( list.getText(), is( "10, foo * bar, true" ) ); ListNode ln = (ListNode) list; assertThat( ln.getElements().size(), is( 3 ) ); assertThat( ln.getElements().get( 0 ), is( instanceOf( NumberNode.class ) ) ); assertThat( ln.getElements().get( 1 ), is( instanceOf( InfixOpNode.class ) ) ); assertThat( ln.getElements().get( 2 ), is( instanceOf( BooleanNode.class ) ) ); }
@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 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 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 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 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 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 testFunctionInvocationEmptyParams() { String inputExpression = "my.test.Function()"; BaseNode functionBase = parse( inputExpression ); assertThat( functionBase, is( instanceOf( FunctionInvocationNode.class ) ) ); assertThat( functionBase.getText(), is( inputExpression ) ); FunctionInvocationNode function = (FunctionInvocationNode) functionBase; assertThat( function.getName(), is( instanceOf( QualifiedNameNode.class ) ) ); assertThat( function.getName().getText(), is( "my.test.Function" ) ); assertThat( function.getParams(), is( instanceOf( ListNode.class ) ) ); assertThat( function.getParams().getElements(), is( empty() ) ); }
@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 testPathExpression() { String inputExpression = "[ 10, 15 ].size"; BaseNode pathBase = parse( inputExpression ); assertThat( pathBase, is( instanceOf( PathExpressionNode.class ) ) ); assertThat( pathBase.getText(), is( inputExpression ) ); PathExpressionNode pathExpr = (PathExpressionNode) pathBase; assertThat( pathExpr.getExpression(), is( instanceOf( ListNode.class ) ) ); assertThat( pathExpr.getExpression().getText(), is( "10, 15" ) ); assertThat( pathExpr.getName(), is( instanceOf( NameRefNode.class ) ) ); assertThat( pathExpr.getName().getText(), is( "size" ) ); }
@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 testFilterExpression() { String inputExpression = "[ {x:1, y:2}, {x:2, y:3} ][ x=1 ]"; BaseNode filterBase = parse( inputExpression ); assertThat( filterBase, is( instanceOf( FilterExpressionNode.class ) ) ); assertThat( filterBase.getText(), is( inputExpression ) ); FilterExpressionNode filter = (FilterExpressionNode) filterBase; assertThat( filter.getExpression(), is( instanceOf( ListNode.class ) ) ); assertThat( filter.getExpression().getText(), is( "{x:1, y:2}, {x:2, y:3}" ) ); assertThat( filter.getFilter(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( filter.getFilter().getText(), is( "x=1" ) ); }
@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 testFunctionInvocationWithKeyword() { String inputExpression = "date and time( \"2016-07-29T19:47:53\" )"; BaseNode functionBase = parse( inputExpression ); assertThat( functionBase, is( instanceOf( FunctionInvocationNode.class ) ) ); assertThat( functionBase.getText(), is( inputExpression ) ); FunctionInvocationNode function = (FunctionInvocationNode) functionBase; assertThat( function.getName(), is( instanceOf( NameRefNode.class ) ) ); assertThat( function.getName().getText(), is( "date and time" ) ); assertThat( function.getParams(), is( instanceOf( ListNode.class ) ) ); assertThat( function.getParams().getElements().size(), is( 1 ) ); assertThat( function.getParams().getElements().get( 0 ), is( instanceOf( StringNode.class ) ) ); }
@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" ) ); }