2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.path;
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;
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;
26 @RunWith(MockitoJUnitRunner.StrictStubs.class)
27 public class PathExpressionParserTest {
29 private StmtContext<?, ?, ?> ctx;
31 private StatementSourceReference ref;
34 public void before() {
35 doReturn(ref).when(ctx).getStatementSourceReference();
39 public void testDerefPath() {
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 "));
52 public void testInvalidLeftParent() {
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 "));
64 public void testInvalidRightParent() {
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 "));
76 public void testInvalidIdentifier() {
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 "));