From af0a21f7ca9e78b72cc1a6ede01208fde972e46e Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Wed, 10 Jun 2020 17:10:19 +0200
Subject: [PATCH] Add tests for FunctionSymbolId and clean-up the class

---
 src/language/utils/FunctionSymbolId.hpp | 12 ++++--
 tests/CMakeLists.txt                    |  1 +
 tests/test_FunctionSymbolId.cpp         | 56 +++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 3 deletions(-)
 create mode 100644 tests/test_FunctionSymbolId.cpp

diff --git a/src/language/utils/FunctionSymbolId.hpp b/src/language/utils/FunctionSymbolId.hpp
index d6f0973f7..f96b783fa 100644
--- a/src/language/utils/FunctionSymbolId.hpp
+++ b/src/language/utils/FunctionSymbolId.hpp
@@ -16,14 +16,13 @@ class FunctionSymbolId
   std::shared_ptr<SymbolTable> m_symbol_table = nullptr;
 
  public:
-  PUGS_INLINE uint64_t
+  [[nodiscard]] PUGS_INLINE uint64_t
   id() const noexcept
   {
     return m_function_id;
   }
 
-  PUGS_INLINE
-  const SymbolTable&
+  [[nodiscard]] PUGS_INLINE const SymbolTable&
   symbolTable() const
   {
     Assert(m_symbol_table, "FunctionSymbolId is not initialized properly");
@@ -37,7 +36,14 @@ class FunctionSymbolId
     return os;
   }
 
+  FunctionSymbolId& operator=(const FunctionSymbolId&) = default;
+  FunctionSymbolId& operator=(FunctionSymbolId&&) = default;
+
   FunctionSymbolId() = default;
+
+  FunctionSymbolId(const FunctionSymbolId&) = default;
+  FunctionSymbolId(FunctionSymbolId&&)      = default;
+
   FunctionSymbolId(uint64_t function_id, const std::shared_ptr<SymbolTable>& symbol_table)
     : m_function_id(function_id), m_symbol_table(symbol_table)
   {}
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index e695a79da..195aa699a 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -58,6 +58,7 @@ add_executable (unit_tests
   test_FakeProcessor.cpp
   test_ForProcessor.cpp
   test_FunctionProcessor.cpp
+  test_FunctionSymbolId.cpp
   test_FunctionTable.cpp
   test_IfProcessor.cpp
   test_IncDecExpressionProcessor.cpp
diff --git a/tests/test_FunctionSymbolId.cpp b/tests/test_FunctionSymbolId.cpp
new file mode 100644
index 000000000..3213f86d1
--- /dev/null
+++ b/tests/test_FunctionSymbolId.cpp
@@ -0,0 +1,56 @@
+#include <catch2/catch.hpp>
+
+#include <language/utils/FunctionSymbolId.hpp>
+#include <language/utils/SymbolTable.hpp>
+#include <utils/PugsAssert.hpp>
+
+#include <sstream>
+
+// clazy:excludeall=non-pod-global-static
+
+TEST_CASE("FunctionSymbolId", "[language]")
+{
+  std::shared_ptr<SymbolTable> s = std::make_shared<SymbolTable>();
+  const FunctionSymbolId f{2, s};
+  REQUIRE(f.id() == 2);
+  REQUIRE(&f.symbolTable() == &(*s));
+
+  {
+    FunctionSymbolId g{f};
+    REQUIRE(g.id() == 2);
+  }
+
+  {
+    FunctionSymbolId h{4, s};
+    FunctionSymbolId g{std::move(h)};
+
+    REQUIRE(g.id() == 4);
+  }
+
+  {
+    FunctionSymbolId g;
+    g = f;
+    REQUIRE(g.id() == 2);
+  }
+
+  {
+    FunctionSymbolId g;
+    FunctionSymbolId h{4, s};
+    g = std::move(h);
+    REQUIRE(g.id() == 4);
+  }
+
+  {
+    std::ostringstream sout;
+    sout << f;
+
+    REQUIRE(sout.str() == "2");
+  }
+
+#ifndef NDEBUG
+  {
+    std::shared_ptr<SymbolTable> unset_s;
+    REQUIRE_THROWS_AS((FunctionSymbolId{0, unset_s}.symbolTable()), AssertError);
+  }
+#endif
+}
-- 
GitLab