Browse Source

Improve KvDocument class test coverage

- add missing test cases
- change signature of "removePair" method
Patrick-Christopher Mattulat 3 years ago
parent
commit
3503863e2e

+ 2 - 2
include/ls_std/io/kv/KvDocument.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-12-25
- * Changed:         2021-05-02
+ * Changed:         2021-07-15
  *
  * */
 
@@ -28,7 +28,7 @@ namespace ls_std
       void clear();
       std::map<ls_std::kv_key, ls_std::KvPair> getPairs();
       bool hasPair(const ls_std::kv_key &_key);
-      void removePair(const ls_std::kv_key &_key);
+      bool removePair(const ls_std::kv_key &_key);
 
     private:
 

+ 3 - 6
source/ls_std/io/kv/KvDocument.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-12-25
- * Changed:         2021-05-02
+ * Changed:         2021-07-15
  *
  * */
 
@@ -40,12 +40,9 @@ bool ls_std::KvDocument::hasPair(const ls_std::kv_key &_key)
   return this->_hasPair(_key);
 }
 
-void ls_std::KvDocument::removePair(const ls_std::kv_key &_key)
+bool ls_std::KvDocument::removePair(const ls_std::kv_key &_key)
 {
-  if (_hasPair(_key))
-  {
-    this->pairs.erase(_key);
-  }
+  return this->pairs.erase(_key) == 1;
 }
 
 bool ls_std::KvDocument::_hasPair(const ls_std::kv_key &_key)

+ 23 - 19
test/cases/io/kv/KvDocumentTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-12-25
- * Changed:         2021-05-02
+ * Changed:         2021-07-15
  *
  * */
 
@@ -31,32 +31,28 @@ namespace
     ls_std::KvDocument document{};
     ls_std::KvPair pair{"port", "13088"};
 
-    ASSERT_TRUE(document.getPairs().empty());
     ASSERT_TRUE(document.addPair(pair));
     ASSERT_EQ(1, document.getPairs().size());
   }
 
-  TEST_F(KvDocumentTest, addPairNegative)
+  TEST_F(KvDocumentTest, addPair_retry_to_add_pair)
   {
     ls_std::KvDocument document{};
     ls_std::KvPair pair{"port", "13088"};
 
-    ASSERT_TRUE(document.getPairs().empty());
     ASSERT_TRUE(document.addPair(pair));
-    ASSERT_EQ(1, document.getPairs().size());
-
     ASSERT_FALSE(document.addPair(pair));
   }
 
   TEST_F(KvDocumentTest, clear)
   {
+    // preparation
+
     ls_std::KvDocument document{};
     ls_std::KvPair pair{"port", "13088"};
+    document.addPair(pair);
 
-    ASSERT_TRUE(document.getPairs().empty());
-    ASSERT_TRUE(document.addPair(pair));
-    ASSERT_FALSE(document.getPairs().empty());
-    ASSERT_EQ(1, document.getPairs().size());
+    // check
 
     document.clear();
     ASSERT_TRUE(document.getPairs().empty());
@@ -72,29 +68,37 @@ namespace
   TEST_F(KvDocumentTest, hasPair)
   {
     ls_std::KvDocument document{};
-    ASSERT_TRUE(document.getPairs().empty());
-    ASSERT_TRUE(document.addPair(ls_std::KvPair{"port", "80"}));
+    document.addPair(ls_std::KvPair{"port", "80"});
+
     ASSERT_TRUE(document.hasPair("port"));
   }
 
-  TEST_F(KvDocumentTest, hasPairNegative)
+  TEST_F(KvDocumentTest, hasPair_no_pairs_available)
   {
     ls_std::KvDocument document{};
-    ASSERT_TRUE(document.getPairs().empty());
     ASSERT_FALSE(document.hasPair("port"));
   }
 
   TEST_F(KvDocumentTest, removePair)
   {
+    // preparation
+
     ls_std::KvDocument document{};
-    ASSERT_TRUE(document.addPair(ls_std::KvPair{"port", "80"}));
-    ASSERT_TRUE(document.addPair(ls_std::KvPair{"host", "localhost"}));
-    ASSERT_TRUE(document.addPair(ls_std::KvPair{"protocol", "TCP"}));
-    ASSERT_EQ(3, document.getPairs().size());
+    document.addPair(ls_std::KvPair{"port", "80"});
+    document.addPair(ls_std::KvPair{"host", "localhost"});
+    document.addPair(ls_std::KvPair{"protocol", "TCP"});
+
+    // remove pair and check
 
-    document.removePair("protocol");
+    ASSERT_TRUE(document.removePair("protocol"));
     ASSERT_EQ(2, document.getPairs().size());
     ASSERT_TRUE(document.hasPair("port"));
     ASSERT_TRUE(document.hasPair("host"));
   }
+
+  TEST_F(KvDocumentTest, removePair_no_pair_available)
+  {
+    ls_std::KvDocument document{};
+    ASSERT_FALSE(document.removePair("port"));
+  }
 }