Update YANG lexer/parser to accept free-standing '+'
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / YT1089Test.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  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.stmt;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14
15 import com.google.common.collect.Iterables;
16 import java.util.Iterator;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.ContactEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.OrganizationEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
28
29 public class YT1089Test {
30     @Test
31     public void testPlusLexing() throws Exception {
32         final EffectiveModelContext ctx = StmtTestUtils.parseYangSource("/bugs/YT1089/foo.yang");
33         assertEquals(1, ctx.getModuleStatements().size());
34
35         final Iterator<? extends EffectiveStatement<?, ?>> it =
36                 Iterables.getOnlyElement(ctx.getModuleStatements().values()).effectiveSubstatements().iterator();
37
38         assertThat(it.next(), instanceOf(NamespaceEffectiveStatement.class));
39         assertThat(it.next(), instanceOf(PrefixEffectiveStatement.class));
40
41         EffectiveStatement<?, ?> stmt = it.next();
42         assertThat(stmt, instanceOf(DescriptionEffectiveStatement.class));
43         assertEquals("+something", stmt.argument());
44
45         stmt = it.next();
46         assertThat(stmt, instanceOf(ContactEffectiveStatement.class));
47         assertEquals("contact++", stmt.argument());
48
49         stmt = it.next();
50         assertThat(stmt, instanceOf(OrganizationEffectiveStatement.class));
51         assertEquals("organiza++tion", stmt.argument());
52
53         assertFoo(it.next());
54         assertBar(it.next());
55         assertFalse(it.hasNext());
56     }
57
58     private static void assertFoo(final EffectiveStatement<?, ?> stmt) {
59         assertThat(stmt, instanceOf(LeafEffectiveStatement.class));
60         assertEquals(QName.create("urn:foo", "foo"), stmt.argument());
61
62         final Iterator<? extends EffectiveStatement<?, ?>> it = stmt.effectiveSubstatements().iterator();
63         assertThat(it.next(), instanceOf(TypeEffectiveStatement.class));
64         assertEquals("+", it.next().argument());
65         assertEquals("squotdquot", it.next().argument());
66         assertEquals("++", it.next().argument());
67         assertFalse(it.hasNext());
68     }
69
70     private static void assertBar(final EffectiveStatement<?, ?> stmt) {
71         assertThat(stmt, instanceOf(LeafEffectiveStatement.class));
72         assertEquals(QName.create("urn:foo", "bar"), stmt.argument());
73         final Iterator<? extends EffectiveStatement<?, ?>> it = stmt.effectiveSubstatements().iterator();
74         assertThat(it.next(), instanceOf(TypeEffectiveStatement.class));
75         assertEquals("++", it.next().argument());
76         assertEquals("+ + ++", it.next().argument());
77         assertEquals("++ + +", it.next().argument());
78         assertFalse(it.hasNext());
79     }
80 }