Add support for identityref data type

Change-Id: I369150b83d86a4c22fadf383ee011eee619a4c15
diff --git a/tests/leaf_editing.cpp b/tests/leaf_editing.cpp
index f18af1c..925a238 100644
--- a/tests/leaf_editing.cpp
+++ b/tests/leaf_editing.cpp
@@ -15,6 +15,7 @@
 {
     auto schema = std::make_shared<StaticSchema>();
     schema->addModule("mod");
+    schema->addModule("pizza-module");
     schema->addContainer("", "mod:contA");
     schema->addLeaf("", "mod:leafString", yang::LeafDataTypes::String);
     schema->addLeaf("", "mod:leafDecimal", yang::LeafDataTypes::Decimal);
@@ -22,6 +23,14 @@
     schema->addLeaf("", "mod:leafInt", yang::LeafDataTypes::Int);
     schema->addLeaf("", "mod:leafUint", yang::LeafDataTypes::Uint);
     schema->addLeaf("", "mod:leafBinary", yang::LeafDataTypes::Binary);
+    schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "food"});
+    schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "vehicle"});
+    schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "pizza"});
+    schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "spaghetti"});
+    schema->addIdentity(ModuleValuePair{"mod", "pizza"}, ModuleValuePair{"pizza-module", "hawaii"});
+    schema->addLeafIdentityRef("", "mod:foodIdentRef", ModuleValuePair{"mod", "food"});
+    schema->addLeafIdentityRef("", "mod:pizzaIdentRef", ModuleValuePair{"mod", "pizza"});
+    schema->addLeafIdentityRef("mod:contA", "mod:identInCont", ModuleValuePair{"mod", "pizza"});
     schema->addLeafEnum("", "mod:leafEnum", {"lol", "data", "coze"});
     schema->addLeaf("mod:contA", "mod:leafInCont", yang::LeafDataTypes::String);
     schema->addList("", "mod:list", {"number"});
@@ -117,6 +126,82 @@
                 }
                 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBinary")});
             }
+
+            SECTION("identityRef")
+            {
+                SECTION("foodIdentRef")
+                {
+                    input = "set mod:foodIdentRef ";
+                    expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("foodIdentRef")});
+
+                    SECTION("food")
+                    {
+                        input += "food";
+                        expected.m_data = identityRef_("food");
+                    }
+                    SECTION("mod:food")
+                    {
+                        input += "mod:food";
+                        expected.m_data = identityRef_("mod", "food");
+                    }
+                    SECTION("pizza")
+                    {
+                        input += "pizza";
+                        expected.m_data = identityRef_("pizza");
+                    }
+                    SECTION("mod:pizza")
+                    {
+                        input += "mod:pizza";
+                        expected.m_data = identityRef_("mod", "pizza");
+                    }
+                    SECTION("pizza-module:hawaii")
+                    {
+                        input += "pizza-module:hawaii";
+                        expected.m_data = identityRef_("pizza-module", "hawaii");
+                    }
+                }
+                SECTION("pizzaIdentRef")
+                {
+                    input = "set mod:pizzaIdentRef ";
+                    expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("pizzaIdentRef")});
+                    SECTION("pizza")
+                    {
+                        input += "pizza";
+                        expected.m_data = identityRef_("pizza");
+                    }
+                    SECTION("mod:pizza")
+                    {
+                        input += "mod:pizza";
+                        expected.m_data = identityRef_("mod", "pizza");
+                    }
+                    SECTION("pizza-module:hawaii")
+                    {
+                        input += "pizza-module:hawaii";
+                        expected.m_data = identityRef_("pizza-module", "hawaii");
+                    }
+                }
+                SECTION("mod:contA/identInCont")
+                {
+                    input = "set mod:contA/identInCont ";
+                    expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
+                    expected.m_path.m_nodes.push_back(dataNode_(leaf_("identInCont")));
+                    SECTION("pizza")
+                    {
+                        input += "pizza";
+                        expected.m_data = identityRef_("pizza");
+                    }
+                    SECTION("mod:pizza")
+                    {
+                        input += "mod:pizza";
+                        expected.m_data = identityRef_("mod", "pizza");
+                    }
+                    SECTION("pizza-module:hawaii")
+                    {
+                        input += "pizza-module:hawaii";
+                        expected.m_data = identityRef_("pizza-module", "hawaii");
+                    }
+                }
+            }
         }
 
         command_ command = parser.parseCommand(input, errorStream);
@@ -183,6 +268,31 @@
                 input = "set leafBinary db=ahj";
         }
 
+        SECTION("non-existing identity")
+        {
+            input = "set mod:foodIdentRef identityBLABLA";
+        }
+
+        SECTION("setting identities with wrong bases")
+        {
+            SECTION("set mod:foodIdentRef mod:vehicle")
+            {
+                input = "set mod:foodIdentRef mod:vehicle";
+            }
+            SECTION("set mod:pizzaIdentRef mod:food")
+            {
+                input = "set mod:pizzaIdentRef mod:food";
+            }
+        }
+        SECTION("setting different module identities without prefix")
+        {
+            input = "set mod:pizzaIdentRef hawaii";
+        }
+        SECTION("identity prefix without name")
+        {
+            input = "set mod:contA/identInCont pizza-module:";
+        }
+
         REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
     }
 }
diff --git a/tests/yang.cpp b/tests/yang.cpp
index 5846a23..e19f991 100644
--- a/tests/yang.cpp
+++ b/tests/yang.cpp
@@ -18,6 +18,10 @@
         prefix "example";
     }
 
+    identity pineapple {
+        base "example:fruit";
+    }
+
     augment /example:a {
         container augmentedContainer {
         }
@@ -36,6 +40,28 @@
     namespace "http://example.com/example-sports";
     prefix coze;
 
+    identity drink {
+    }
+
+    identity voda {
+        base "drink";
+    }
+
+    identity food {
+    }
+
+    identity fruit {
+        base "food";
+    }
+
+    identity pizza {
+        base "food";
+    }
+
+    identity hawaii {
+        base "pizza";
+    }
+
     container a {
         container a2 {
             container a3 {
@@ -116,6 +142,25 @@
         type enumTypedefRestricted;
     }
 
+    leaf foodIdentLeaf {
+        type identityref {
+            base "food";
+        }
+    }
+
+    leaf pizzaIdentLeaf {
+        type identityref {
+            base "pizza";
+        }
+    }
+
+    leaf foodDrinkIdentLeaf {
+        type identityref {
+            base "food";
+            base "drink";
+        }
+    }
+
     list _list {
         key number;
 
@@ -279,6 +324,110 @@
 
             REQUIRE(ys.leafEnumHasValue(path, node, value));
         }
+        SECTION("leafIdentityIsValid")
+        {
+            ModuleValuePair value;
+
+            SECTION("foodIdentLeaf")
+            {
+                node.first = "example-schema";
+                node.second = "foodIdentLeaf";
+
+                SECTION("food")
+                {
+                    value.second = "food";
+                }
+                SECTION("example-schema:food")
+                {
+                    value.first = "example-schema";
+                    value.second = "food";
+                }
+                SECTION("pizza")
+                {
+                    value.second = "pizza";
+                }
+                SECTION("example-schema:pizza")
+                {
+                    value.first = "example-schema";
+                    value.second = "pizza";
+                }
+                SECTION("hawaii")
+                {
+                    value.second = "hawaii";
+                }
+                SECTION("example-schema:hawaii")
+                {
+                    value.first = "example-schema";
+                    value.second = "hawaii";
+                }
+                SECTION("fruit")
+                {
+                    value.second = "fruit";
+                }
+                SECTION("example-schema:fruit")
+                {
+                    value.first = "example-schema";
+                    value.second = "fruit";
+                }
+                SECTION("second-schema:pineapple")
+                {
+                    value.first = "second-schema";
+                    value.second = "pineapple";
+                }
+            }
+
+            SECTION("pizzaIdentLeaf")
+            {
+                node.first = "example-schema";
+                node.second = "pizzaIdentLeaf";
+
+                SECTION("pizza")
+                {
+                    value.second = "pizza";
+                }
+                SECTION("example-schema:pizza")
+                {
+                    value.first = "example-schema";
+                    value.second = "pizza";
+                }
+                SECTION("hawaii")
+                {
+                    value.second = "hawaii";
+                }
+                SECTION("example-schema:hawaii")
+                {
+                    value.first = "example-schema";
+                    value.second = "hawaii";
+                }
+            }
+
+            SECTION("foodDrinkIdentLeaf")
+            {
+                node.first = "example-schema";
+                node.second = "foodDrinkIdentLeaf";
+
+                SECTION("food")
+                {
+                    value.second = "food";
+                }
+                SECTION("example-schema:food")
+                {
+                    value.first = "example-schema";
+                    value.second = "food";
+                }
+                SECTION("drink")
+                {
+                    value.second = "drink";
+                }
+                SECTION("example-schema:drink")
+                {
+                    value.first = "example-schema";
+                    value.second = "drink";
+                }
+            }
+            REQUIRE(ys.leafIdentityIsValid(path, node, value));
+        }
+
         SECTION("listHasKey")
         {
             std::string key;
@@ -381,6 +530,7 @@
                        "example-schema:leafDecimal", "example-schema:leafBool", "example-schema:leafInt",
                        "example-schema:leafUint", "example-schema:leafEnum", "example-schema:leafEnumTypedef",
                        "example-schema:leafEnumTypedefRestricted", "example-schema:leafEnumTypedefRestricted2",
+                       "example-schema:foodIdentLeaf", "example-schema:pizzaIdentLeaf", "example-schema:foodDrinkIdentLeaf",
                        "example-schema:_list", "example-schema:twoKeyList", "second-schema:bla"};
             }
 
@@ -460,5 +610,43 @@
         {
             REQUIRE(!ys.isModule(path, "notAModule"));
         }
+
+        SECTION("leafIdentityIsValid")
+        {
+            ModuleValuePair value;
+            SECTION("pizzaIdentLeaf")
+            {
+                node.first = "example-schema";
+                node.second = "pizzaIdentLeaf";
+
+                SECTION("wrong base ident")
+                {
+                    SECTION("food")
+                    {
+                        value.second = "food";
+                    }
+                    SECTION("fruit")
+                    {
+                        value.second = "fruit";
+                    }
+                }
+                SECTION("non-existent identity")
+                {
+                    value.second = "nonexistent";
+                }
+                SECTION("weird module")
+                {
+                    value.first = "ahahaha";
+                    value.second = "pizza";
+                }
+            }
+            SECTION("different module identity, but withotu prefix")
+            {
+                node.first = "example-schema";
+                node.second = "foodIdentLeaf";
+                value.second = "pineapple";
+            }
+            REQUIRE_FALSE(ys.leafIdentityIsValid(path, node, value));
+        }
     }
 }