From 27d9a80ee4415735b891b036f60abbe7050d5fc0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com>
Date: Tue, 27 Jul 2021 16:32:38 +0200
Subject: [PATCH] Add missing tests for SymbolTable

---
 src/language/utils/SymbolTable.hpp | 28 ++++++++++-----------
 tests/test_SymbolTable.cpp         | 40 ++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 15 deletions(-)

diff --git a/src/language/utils/SymbolTable.hpp b/src/language/utils/SymbolTable.hpp
index 732199456..5d81c6811 100644
--- a/src/language/utils/SymbolTable.hpp
+++ b/src/language/utils/SymbolTable.hpp
@@ -285,14 +285,13 @@ class SymbolTable
     std::vector<Symbol> builtin_function_symbol_list;
 
     for (auto i_stored_symbol : m_symbol_list) {
-      if (use_position.byte < i_stored_symbol.attributes().position().byte)
-        continue;
-
       // Symbol must be defined before the call
-      std::string_view stored_symbol_name = i_stored_symbol.name();
-      if ((stored_symbol_name.size() > symbol.size()) and (stored_symbol_name[symbol.size()] == ':')) {
-        if (stored_symbol_name.substr(0, symbol.size()) == symbol) {
-          builtin_function_symbol_list.push_back(i_stored_symbol);
+      if (use_position.byte >= i_stored_symbol.attributes().position().byte) {
+        std::string_view stored_symbol_name = i_stored_symbol.name();
+        if ((stored_symbol_name.size() > symbol.size()) and (stored_symbol_name[symbol.size()] == ':')) {
+          if (stored_symbol_name.substr(0, symbol.size()) == symbol) {
+            builtin_function_symbol_list.push_back(i_stored_symbol);
+          }
         }
       }
     }
@@ -308,15 +307,14 @@ class SymbolTable
   has(const std::string& symbol, const TAO_PEGTL_NAMESPACE::position& use_position)
   {
     for (auto i_stored_symbol : m_symbol_list) {
-      if (use_position.byte < i_stored_symbol.attributes().position().byte)
-        continue;
-
       // Symbol must be defined before the call
-      std::string_view stored_symbol_name = i_stored_symbol.name();
-      if ((stored_symbol_name.size() == symbol.size()) or
-          (stored_symbol_name.size() > symbol.size() and (stored_symbol_name[symbol.size()] == ':'))) {
-        if (stored_symbol_name.substr(0, symbol.size()) == symbol) {
-          return true;
+      if (use_position.byte >= i_stored_symbol.attributes().position().byte) {
+        std::string_view stored_symbol_name = i_stored_symbol.name();
+        if ((stored_symbol_name.size() == symbol.size()) or
+            (stored_symbol_name.size() > symbol.size() and (stored_symbol_name[symbol.size()] == ':'))) {
+          if (stored_symbol_name.substr(0, symbol.size()) == symbol) {
+            return true;
+          }
         }
       }
     }
diff --git a/tests/test_SymbolTable.cpp b/tests/test_SymbolTable.cpp
index 2be1a3d81..78cd4373b 100644
--- a/tests/test_SymbolTable.cpp
+++ b/tests/test_SymbolTable.cpp
@@ -3,6 +3,7 @@
 
 #include <language/utils/BuiltinFunctionEmbedder.hpp>
 #include <language/utils/SymbolTable.hpp>
+#include <language/utils/TypeDescriptor.hpp>
 
 #include <pegtl/internal/iterator.hpp>
 
@@ -186,6 +187,29 @@ TEST_CASE("SymbolTable", "[language]")
       value_output << symbol.attributes();
       REQUIRE(value_output.str() == "function_id:2");
     }
+
+    {
+      const SymbolTable::Symbol symbol_decl{i_symbol_a->name(), i_symbol_a->attributes()};
+      SymbolTable::Attributes attributes_b = attributes_a;
+      SymbolTable::Symbol symbol("foo", attributes_b);
+
+      symbol = symbol_decl;
+
+      std::stringstream value_output;
+      value_output << symbol.attributes();
+      REQUIRE(value_output.str() == "function_id:2");
+    }
+
+    {
+      SymbolTable::Attributes attributes_b = attributes_a;
+      SymbolTable::Symbol symbol("foo", attributes_b);
+
+      symbol = SymbolTable::Symbol{i_symbol_a->name(), i_symbol_a->attributes()};
+
+      std::stringstream value_output;
+      value_output << symbol.attributes();
+      REQUIRE(value_output.str() == "function_id:2");
+    }
   }
 
   SECTION("FunctionTable")
@@ -220,4 +244,20 @@ TEST_CASE("SymbolTable", "[language]")
     REQUIRE(builtin_function_table.size() == 1);
     REQUIRE(const_builtin_function_table.size() == 1);
   }
+
+  SECTION("TypeEmbedderTable")
+  {
+    std::shared_ptr root_st = std::make_shared<SymbolTable>();
+
+    auto& type_table = root_st->typeEmbedderTable();
+    REQUIRE(type_table.size() == 0);
+
+    const auto& const_type_table = static_cast<const SymbolTable&>(*root_st).typeEmbedderTable();
+    REQUIRE(const_type_table.size() == 0);
+
+    type_table.add(std::make_shared<TypeDescriptor>("a_type"));
+
+    REQUIRE(type_table.size() == 1);
+    REQUIRE(const_type_table.size() == 1);
+  }
 }
-- 
GitLab