feat(mcp): Finalize C/C++ AST tools with robust testing and bug fixes
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace gencpp {
|
||||
namespace core {
|
||||
|
||||
/**
|
||||
* @brief Base class for all components in the system.
|
||||
*/
|
||||
template <typename T>
|
||||
class BaseComponent {
|
||||
public:
|
||||
virtual ~BaseComponent() = default;
|
||||
|
||||
virtual void Initialize() = 0;
|
||||
virtual void Shutdown() = 0;
|
||||
|
||||
virtual const std::string& GetName() const = 0;
|
||||
|
||||
struct Config {
|
||||
std::string name;
|
||||
int priority;
|
||||
bool enabled;
|
||||
|
||||
class Metadata {
|
||||
public:
|
||||
std::string author;
|
||||
std::string version;
|
||||
};
|
||||
Metadata metadata;
|
||||
};
|
||||
|
||||
protected:
|
||||
BaseComponent(const Config& config) : m_config(config) {}
|
||||
Config m_config;
|
||||
};
|
||||
|
||||
} // namespace core
|
||||
} // namespace gencpp
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
namespace gencpp {
|
||||
namespace util {
|
||||
|
||||
/**
|
||||
* @brief A complex template class demonstrating variadic templates.
|
||||
*/
|
||||
template <typename... Args>
|
||||
class MultiBuffer {
|
||||
public:
|
||||
void SetData(Args... args) {
|
||||
m_data = std::make_tuple(args...);
|
||||
}
|
||||
|
||||
template <size_t I>
|
||||
auto Get() const -> const typename std::tuple_element<I, std::tuple<Args...>>::type& {
|
||||
return std::get<I>(m_data);
|
||||
}
|
||||
|
||||
private:
|
||||
std::tuple<Args...> m_data;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Template specialization example.
|
||||
*/
|
||||
template <typename T>
|
||||
struct TypeTraits {
|
||||
static constexpr bool IsPointer = false;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct TypeTraits<T*> {
|
||||
static constexpr bool IsPointer = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Nested template class.
|
||||
*/
|
||||
template <typename Outer>
|
||||
struct Container {
|
||||
template <typename Inner>
|
||||
struct Wrapper {
|
||||
Inner value;
|
||||
Outer context;
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace util
|
||||
} // namespace gencpp
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "component_registry.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace gencpp {
|
||||
namespace registry {
|
||||
|
||||
ComponentRegistry& ComponentRegistry::Instance() {
|
||||
static ComponentRegistry instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void ComponentRegistry::Register(const std::string& type, ComponentCreator creator) {
|
||||
std::cout << "Registering component type: " << type << std::endl;
|
||||
m_creators[type] = creator;
|
||||
}
|
||||
|
||||
std::unique_ptr<core::BaseComponent<void*>> ComponentRegistry::Create(const std::string& type) {
|
||||
auto it = m_creators.find(type);
|
||||
if (it != m_creators.end()) {
|
||||
return it->second();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace registry
|
||||
} // namespace gencpp
|
||||
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "base_component.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
namespace gencpp {
|
||||
namespace registry {
|
||||
|
||||
class ComponentRegistry {
|
||||
public:
|
||||
using ComponentCreator = std::function<std::unique_ptr<core::BaseComponent<void*>>()>;
|
||||
|
||||
static ComponentRegistry& Instance();
|
||||
|
||||
void Register(const std::string& type, ComponentCreator creator);
|
||||
std::unique_ptr<core::BaseComponent<void*>> Create(const std::string& type);
|
||||
|
||||
class Iterator {
|
||||
public:
|
||||
using MapIterator = std::map<std::string, ComponentCreator>::iterator;
|
||||
|
||||
Iterator(MapIterator it) : m_it(it) {}
|
||||
|
||||
bool operator!=(const Iterator& other) const { return m_it != other.m_it; }
|
||||
void operator++() { ++m_it; }
|
||||
const std::string& GetType() const { return m_it->first; }
|
||||
|
||||
private:
|
||||
MapIterator m_it;
|
||||
};
|
||||
|
||||
Iterator Begin() { return Iterator(m_creators.begin()); }
|
||||
Iterator End() { return Iterator(m_creators.end()); }
|
||||
|
||||
private:
|
||||
ComponentRegistry() = default;
|
||||
std::map<std::string, ComponentCreator> m_creators;
|
||||
};
|
||||
|
||||
} // namespace registry
|
||||
} // namespace gencpp
|
||||
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
|
||||
def verify_files():
|
||||
files = [
|
||||
"base_component.h",
|
||||
"component_registry.h",
|
||||
"component_registry.cpp",
|
||||
"complex_template.h"
|
||||
]
|
||||
base_path = "tests/assets/gencpp_samples"
|
||||
for f in files:
|
||||
p = os.path.join(base_path, f)
|
||||
if os.path.exists(p):
|
||||
print(f"Verified: {f}")
|
||||
else:
|
||||
print(f"Missing: {f}")
|
||||
exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
verify_files()
|
||||
@@ -6,7 +6,7 @@ import sys
|
||||
# Add project root to sys.path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
from src.mcp_client import ts_cpp_get_skeleton, ts_cpp_get_code_outline
|
||||
from src.mcp_client import ts_cpp_get_skeleton, ts_cpp_get_code_outline, ts_cpp_get_definition, ts_cpp_update_definition
|
||||
|
||||
def test_ts_cpp_get_skeleton(tmp_path):
|
||||
cpp_code = """#include <iostream>
|
||||
@@ -69,3 +69,91 @@ void templateFunc(T t) {
|
||||
assert "[Func] templateFunc (Lines 8-9)" in outline
|
||||
finally:
|
||||
mcp_client._resolve_and_check = original_resolve
|
||||
|
||||
def test_exhaustive_gencpp_corpus():
|
||||
base_dir = Path(__file__).parent / "assets" / "gencpp_samples"
|
||||
files_to_test = {
|
||||
"base_component.h": [
|
||||
"BaseComponent",
|
||||
"BaseComponent::Config",
|
||||
"BaseComponent::Config::Metadata"
|
||||
],
|
||||
"complex_template.h": [
|
||||
"MultiBuffer",
|
||||
"MultiBuffer::SetData",
|
||||
"MultiBuffer::Get",
|
||||
"TypeTraits",
|
||||
"Container::Wrapper"
|
||||
],
|
||||
"component_registry.h": [
|
||||
"ComponentRegistry",
|
||||
"ComponentRegistry::Iterator",
|
||||
"ComponentRegistry::Iterator::GetType",
|
||||
"ComponentRegistry::Instance"
|
||||
],
|
||||
"component_registry.cpp": [
|
||||
"ComponentRegistry::Instance",
|
||||
"ComponentRegistry::Register",
|
||||
"ComponentRegistry::Create"
|
||||
]
|
||||
}
|
||||
|
||||
from src import mcp_client
|
||||
original_resolve = mcp_client._resolve_and_check
|
||||
mcp_client._resolve_and_check = lambda path: (Path(path), None)
|
||||
|
||||
try:
|
||||
for filename, symbols in files_to_test.items():
|
||||
path = base_dir / filename
|
||||
assert path.exists(), f"{path} does not exist"
|
||||
|
||||
# 1. Verify skeleton executes without errors
|
||||
skeleton = ts_cpp_get_skeleton(str(path))
|
||||
assert skeleton and "ERROR" not in skeleton
|
||||
|
||||
# 2. Verify code outline executes without errors
|
||||
outline = ts_cpp_get_code_outline(str(path))
|
||||
assert outline and "ERROR" not in outline
|
||||
|
||||
# 3. Verify specific complex symbols can be retrieved via ts_cpp_get_definition
|
||||
for symbol in symbols:
|
||||
definition = ts_cpp_get_definition(str(path), symbol)
|
||||
assert definition and "ERROR" not in definition, f"Failed to get definition for {symbol} in {filename}: {definition}"
|
||||
# Basic check that the symbol name is in the definition (allowing for namespaces)
|
||||
simple_name = symbol.split("::")[-1]
|
||||
assert simple_name in definition
|
||||
finally:
|
||||
mcp_client._resolve_and_check = original_resolve
|
||||
|
||||
def test_ts_cpp_update_definition(tmp_path):
|
||||
asset_path = Path(__file__).parent / "assets" / "gencpp_samples" / "component_registry.cpp"
|
||||
cpp_content = asset_path.read_text(encoding="utf-8")
|
||||
cpp_file = tmp_path / "component_registry.cpp"
|
||||
cpp_file.write_text(cpp_content, encoding="utf-8")
|
||||
|
||||
from src import mcp_client
|
||||
original_resolve = mcp_client._resolve_and_check
|
||||
mcp_client._resolve_and_check = lambda path: (Path(path), None)
|
||||
|
||||
try:
|
||||
new_register_body = """void ComponentRegistry::Register(const std::string& type, ComponentCreator creator) {
|
||||
// Updated body
|
||||
std::cout << "DEBUG: Registering " << type << std::endl;
|
||||
m_creators[type] = creator;
|
||||
if (type == "secret") {
|
||||
std::cout << "Special component" << std::endl;
|
||||
}
|
||||
}"""
|
||||
result = ts_cpp_update_definition(str(cpp_file), "ComponentRegistry::Register", new_register_body)
|
||||
assert "Successfully updated" in result
|
||||
|
||||
updated_content = cpp_file.read_text(encoding="utf-8")
|
||||
assert "// Updated body" in updated_content
|
||||
assert "DEBUG: Registering" in updated_content
|
||||
|
||||
# Verify it still parses by getting definition again
|
||||
definition = ts_cpp_get_definition(str(cpp_file), "ComponentRegistry::Register")
|
||||
assert "// Updated body" in definition
|
||||
assert "DEBUG: Registering" in definition
|
||||
finally:
|
||||
mcp_client._resolve_and_check = original_resolve
|
||||
|
||||
Reference in New Issue
Block a user