Force path expression parsing consume all data
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / path / PathExpressionParserTest.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.path;
9
10 import static org.hamcrest.Matchers.containsString;
11 import static org.hamcrest.Matchers.startsWith;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.doReturn;
16
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
25
26 @RunWith(MockitoJUnitRunner.StrictStubs.class)
27 public class PathExpressionParserTest {
28     @Mock
29     private StmtContext<?, ?, ?> ctx;
30     @Mock
31     private StatementSourceReference ref;
32
33     @Before
34     public void before() {
35         doReturn(ref).when(ctx).getStatementSourceReference();
36     }
37
38     @Test
39     public void testDerefPath() {
40         try {
41             // deref() is not valid as per RFC7950, but YANGTOOLS-968 would allow it
42             new PathExpressionParser().parseExpression(ctx, "deref(../id)/../type");
43             fail("SourceException should have been thrown");
44         } catch (SourceException e) {
45             assertSame(ref, e.getSourceReference());
46             assertThat(e.getMessage(), startsWith("mismatched input '(' expecting "));
47             assertThat(e.getMessage(), containsString(" at 1:5 [at "));
48         }
49     }
50
51     @Test
52     public void testInvalidLeftParent() {
53         try {
54             new PathExpressionParser().parseExpression(ctx, "foo(");
55             fail("SourceException should have been thrown");
56         } catch (SourceException e) {
57             assertSame(ref, e.getSourceReference());
58             assertThat(e.getMessage(), startsWith("extraneous input '(' expecting "));
59             assertThat(e.getMessage(), containsString(" at 1:3 [at "));
60         }
61     }
62
63     @Test
64     public void testInvalidRightParent() {
65         try {
66             new PathExpressionParser().parseExpression(ctx, "foo)");
67             fail("SourceException should have been thrown");
68         } catch (SourceException e) {
69             assertSame(ref, e.getSourceReference());
70             assertThat(e.getMessage(), startsWith("extraneous input ')' expecting "));
71             assertThat(e.getMessage(), containsString(" at 1:3 [at "));
72         }
73     }
74
75     @Test
76     public void testInvalidIdentifier() {
77         try {
78             new PathExpressionParser().parseExpression(ctx, "foo%");
79             fail("SourceException should have been thrown");
80         } catch (SourceException e) {
81             assertSame(ref, e.getSourceReference());
82             assertThat(e.getMessage(), startsWith("token recognition error at: '%' at 1:3 [at "));
83         }
84     }
85 }