Add a simple unit test for declaration references 19/95919/5
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 25 Apr 2021 19:43:57 +0000 (21:43 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 26 Apr 2021 09:06:16 +0000 (11:06 +0200)
We do not have an explicit test for retaining references, add it.

JIRA: YANGTOOLS-1193
Change-Id: Iba0c656e458db53fbf8a74185addf2be1e2c7f65
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YT1193Test.java [new file with mode: 0644]
parser/yang-parser-impl/src/test/resources/yt1193/bar.yang [new file with mode: 0644]
parser/yang-parser-impl/src/test/resources/yt1193/baz.yang [new file with mode: 0644]
parser/yang-parser-impl/src/test/resources/yt1193/foo.yang [new file with mode: 0644]

diff --git a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YT1193Test.java b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YT1193Test.java
new file mode 100644 (file)
index 0000000..abd73d0
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.parser.impl;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+
+import java.util.Iterator;
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclarationInText;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
+import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
+
+public class YT1193Test {
+    @Test
+    public void testDeclarationReference() throws Exception {
+        final List<DeclaredStatement<?>> declaredRoots = new DefaultYangParserFactory()
+            .createParser(YangParserConfiguration.builder().retainDeclarationReferences(true).build())
+            .addSource(YangTextSchemaSource.forResource(getClass(), "/yt1193/foo.yang"))
+            .addSource(YangTextSchemaSource.forResource(getClass(), "/yt1193/bar.yang"))
+            .addSource(YangTextSchemaSource.forResource(getClass(), "/yt1193/baz.yang"))
+            .buildDeclaredModel();
+        assertEquals(3, declaredRoots.size());
+
+        for (DeclaredStatement<?> stmt : declaredRoots) {
+            switch (stmt.rawArgument()) {
+                case "foo":
+                    assertFooReferences(stmt);
+                    break;
+                case "bar":
+                    assertBarReferences(stmt);
+                    break;
+                case "baz":
+                    assertBazReferences(stmt);
+                    break;
+                default:
+                    throw new IllegalStateException("Unexpected statement " + stmt);
+            }
+        }
+    }
+
+    private static void assertFooReferences(final DeclaredStatement<?> foo) {
+        assertReference(foo, YangStmtMapping.MODULE, 1, 1);
+
+        final Iterator<? extends DeclaredStatement<?>> it = foo.declaredSubstatements().iterator();
+        assertReference(it.next(), YangStmtMapping.NAMESPACE, 2, 3);
+        assertReference(it.next(), YangStmtMapping.PREFIX, 3, 3);
+        assertReference(it.next(), YangStmtMapping.YANG_VERSION, 4, 3);
+        assertReference(it.next(), YangStmtMapping.REVISION, 6, 3);
+        assertReference(it.next(), YangStmtMapping.ORGANIZATION, 8, 3);
+        assertReference(it.next(), YangStmtMapping.DESCRIPTION, 9, 3);
+        assertReference(it.next(), YangStmtMapping.REFERENCE, 10, 3);
+        assertReference(it.next(), YangStmtMapping.CONTACT, 11, 3);
+        assertFooContainerReferences(it.next());
+        assertReference(it.next(), YangStmtMapping.RPC, 25, 3);
+        assertReference(it.next(), YangStmtMapping.NOTIFICATION, 26, 3);
+        assertDeprLeafListReferences(it.next());
+        assertObsoTypedefReferences(it.next());
+        assertFalse(it.hasNext());
+    }
+
+    private static void assertFooContainerReferences(DeclaredStatement<?> foo) {
+        assertReference(foo, YangStmtMapping.CONTAINER, 13, 3);
+
+        final Iterator<? extends DeclaredStatement<?>> it = foo.declaredSubstatements().iterator();
+        assertReference(it.next(), YangStmtMapping.ACTION, 14, 5);
+        assertReference(it.next(), YangStmtMapping.PRESENCE, 22, 5);
+        assertFalse(it.hasNext());
+    }
+
+    private static void assertDeprLeafListReferences(DeclaredStatement<?> depr) {
+        assertReference(depr, YangStmtMapping.LEAF_LIST, 28, 3);
+
+        final Iterator<? extends DeclaredStatement<?>> it = depr.declaredSubstatements().iterator();
+        assertReference(it.next(), YangStmtMapping.TYPE, 29, 5);
+        assertReference(it.next(), YangStmtMapping.UNITS, 36, 5);
+        assertReference(it.next(), YangStmtMapping.STATUS, 37, 5);
+        assertFalse(it.hasNext());
+    }
+
+    private static void assertObsoTypedefReferences(DeclaredStatement<?> obso) {
+        assertReference(obso, YangStmtMapping.TYPEDEF, 40, 3);
+
+        final Iterator<? extends DeclaredStatement<?>> it = obso.declaredSubstatements().iterator();
+        assertReference(it.next(), YangStmtMapping.TYPE, 41, 5);
+        assertReference(it.next(), YangStmtMapping.STATUS, 44, 5);
+        assertFalse(it.hasNext());
+    }
+
+    private static void assertBarReferences(final DeclaredStatement<?> bar) {
+        assertReference(bar, YangStmtMapping.MODULE, 1, 1);
+
+        final Iterator<? extends DeclaredStatement<?>> it = bar.declaredSubstatements().iterator();
+        assertReference(it.next(), YangStmtMapping.NAMESPACE, 2, 3);
+        assertReference(it.next(), YangStmtMapping.PREFIX, 3, 3);
+        assertReference(it.next(), YangStmtMapping.YANG_VERSION, 4, 3);
+        assertReference(it.next(), YangStmtMapping.IMPORT, 6, 3);
+        assertReference(it.next(), YangStmtMapping.IDENTITY, 11, 3);
+        assertReference(it.next(), YangStmtMapping.IDENTITY, 13, 3);
+        assertReference(it.next(), YangStmtMapping.ANYDATA, 17, 3);
+        assertReference(it.next(), YangStmtMapping.ANYXML, 18, 3);
+        assertReference(it.next(), YangStmtMapping.INCLUDE, 20, 3);
+        assertFalse(it.hasNext());
+    }
+
+    private static void assertBazReferences(final DeclaredStatement<?> baz) {
+        assertReference(baz, YangStmtMapping.SUBMODULE, 1, 1);
+
+        final Iterator<? extends DeclaredStatement<?>> it = baz.declaredSubstatements().iterator();
+        assertReference(it.next(), YangStmtMapping.BELONGS_TO, 2, 3);
+        assertReference(it.next(), YangStmtMapping.EXTENSION, 6, 3);
+        assertFalse(it.hasNext());
+    }
+
+    private static void assertReference(final DeclaredStatement<?> foo, final StatementDefinition def, final int line,
+            final int column) {
+        assertEquals(def, foo.statementDefinition());
+
+        final DeclarationReference ref = foo.declarationReference().orElseThrow();
+        assertThat(ref, instanceOf(DeclarationInText.class));
+        final DeclarationInText inText = (DeclarationInText) ref;
+
+        assertEquals(line, inText.startLine());
+        assertEquals(column, inText.startColumn());
+    }
+}
diff --git a/parser/yang-parser-impl/src/test/resources/yt1193/bar.yang b/parser/yang-parser-impl/src/test/resources/yt1193/bar.yang
new file mode 100644 (file)
index 0000000..484d431
--- /dev/null
@@ -0,0 +1,21 @@
+module bar {
+  namespace bar;
+  prefix bar;
+  yang-version 1.1;
+
+  import foo {
+    revision-date 2021-04-26;
+    prefix foo;
+  }
+
+  identity id1;
+
+  identity id2 {
+    base id1;
+  }
+
+  anydata anydata;
+  anyxml anyxml;
+
+  include baz;
+}
diff --git a/parser/yang-parser-impl/src/test/resources/yt1193/baz.yang b/parser/yang-parser-impl/src/test/resources/yt1193/baz.yang
new file mode 100644 (file)
index 0000000..187b5d0
--- /dev/null
@@ -0,0 +1,11 @@
+submodule baz {
+  belongs-to bar {
+    prefix bar;
+  }
+
+  extension ext {
+    argument arg {
+      yin-element true;
+    }
+  }
+}
diff --git a/parser/yang-parser-impl/src/test/resources/yt1193/foo.yang b/parser/yang-parser-impl/src/test/resources/yt1193/foo.yang
new file mode 100644 (file)
index 0000000..6349700
--- /dev/null
@@ -0,0 +1,46 @@
+module foo {
+  namespace foo;
+  prefix foo;
+  yang-version 1.1;
+
+  revision 2021-04-26;
+
+  organization org;
+  description desc;
+  reference ref;
+  contact cont;
+
+  container foo {
+    action act {
+      input {
+
+      }
+      output {
+
+      }
+    }
+    presence presence;
+  }
+
+  rpc rpc;
+  notification notif;
+
+  leaf-list depr {
+    type string {
+      length 1 {
+        error-app-tag tag;
+        error-message msg;
+      }
+    }
+
+    units dinosaurs;
+    status deprecated;
+  }
+
+  typedef obso {
+    type leafref {
+      path /foo:depr;
+    }
+    status obsolete;
+  }
+}