vkdoom_m/libraries/cppdap/src/any_test.cpp
Nikita 45ac7c1d9e
ZScript DAP Debug server (#3009)
* Add STL-standard type traits and functions to TMap to enable for loop iteration

* add third-party range-map library

I have e-mailed the author for clarifcation on the license, will update this when they respond

* vcpkg: Add cppdap and eventpp libraries, update baseline

* DAP implementation

* Add `FileSystem::FileHash()` to get the CRC32 hash

* add `starting_offset` param to `VMDisasm()` for debugger disassembly view

Defaults to `0`, should not change output if it's not set

* Add `VMFrameStack::HasFrames()` to prevent assertions when inspecting in the debugger

* Add `PC` field to VMFrame, ensure that it is updated whenever vm increments/decrements the pc

Does not change alignment, the offsets used in VMFrame still work
We need this for the debugger because we otherwise have no way to get the pc; it was a local in `ExecScriptFunc()`

* `ZCCCompiler::CreateClassTypes()`: ensure SourceLumpName is set for classes derived from non-native classes

* start debug server in d_main, add `vm_debug` cvars and `-debug` CLI arg

* Add documentation for `vm_debug` cvars

* vm_exec: Add debugger hooks for instruction execution events

* c_console: Add debugger hooks for logging events in `PrintString`

* add `.cache/` to gitignore

* vendor cppdap on main @ 6464cd7

Patches:
removed .gitmodules (submodules were thirdparty/json, thirdparty/googletest)
removed thirdparty/googletest, not needed
removed thirdparty/json/docs, thirdparty/json/test, thirdparty/json/benchmarks to prevent massive bloat

* vendor eventpp on master @ 1224dd6

* build: use internal cppdap and eventpp by default

* dap: fix Binary::GetFunctionLineRange()

* fix bug in range_map::find_ranges

* make Binary dap::Source dynamic

* cache source code upon retrieval

* refactor Binary into a class, cleanup PexCache

* fix ending session gracefully

* d_main: Stop debug server in D_Cleanup()

* Fix connecting to debugger when session already started

* cleanup unused stuff in ZScriptDebugger

* always send TerminateEvents on disconnect if initialized

* tweak color display

* Don't cache disassembly lines when scanning scripts

* Cache nodes when getting runtime state

* WIP display locals

* Fix display of static arrays

* Ensure names display in proper order

* dap: Fix struct locals display, add args display

* Support `start` parameter

* Support `filter` parameter

* remove struct unbound native data display

Practically useless for debugging zscript and didn't work properly anyway

* d_main: fix vm_jit and vm_jit not being disabled soon enough

* support breaking on abort exceptions

* dap: refactor game event emitter functions into seperate header

* dap: show native functions on stack

* dap: Remove "Native" from exception handling, simplify exception event emit

* dap: add instruction breakpoints

* dap: fix display of locals not in scope yet

* dap: Make disassembly view display invalid instructions for non-code addresses

* dap: remove dot initializers

* dap: fix local structs in scope

* dap: don't parse the non-used options in the launch/attach requests

* dap: fix local struct view

* dap: Fix displaying objects that aren't their actual types

* dap: Fix action and state handling

* dap: stack display view

* dap: fix object display view

* d_dehacked: set qualified name in addition to the printable name

* dap: fix displaying breakpoint errors when script isn't loaded

* dap: remove debug print

* dap: Display parameter names

* dap: Turn down verbosity of logging

* dap: fix disassembly view

* dap: fix performance problems with arrays

* c_console: emit event only if not PRINT_NODAPEVENT

* dap: improve logging

* dap: update upstream cppdap library to fix deadlocks on no bind

* dap: Fix ending session on client socket closed

* dap: prevent DebugServer.h from pulling in `dap` and `ZScriptDebugger.h`

* dap: fix pause event not being emitted on pause

* dap: remove eventpp emitters, way too slow

* Remove eventpp dependency

* dap: Display correct register names

* dap: Show special inits in registers

* dap: Add stack offset to VMLocalVariable

* dap: fix display of static arrays and local variables on stack vs. registers

* dap: fix displaying function pointers

* dap: tweak color display

* dap: fix scalar display < 4

* dap: add Globals display to debugger

* dap: unify methods to get vmvalue

* dap: rename free method to freeValue to avoid running afoul of macro defs

* fix windows builds

* fix compile on linux

* cleanup

* Fix display of function breakpoints

* dap: Don't send back binary files

* dap: include sbarinfo in script types (no debug support for anything but zscript yet, this is just for returning source info)

* dap: don't show ending session message unless initialized

* fix erroneous commit

* dap: handle evaluate requests

* Fix getting bitfield values

* Add CVars Scope and evaluation

* add running console commands from repl

* dap: disable commands via repl for now

* fix loading functions DECORATE scripts

* Add source information to Dehacked VM functions, add debugging support

* dap: cleanup

* fix resolving archive paths

* don't send source back on native stack frames

* handle `modules` request

* cleanup

* allow evaluating cvars on hover

* fix oob bpinfos

* fix restarting the game blowing out the debug server

* dap: process input events while paused to prevent deadlocks

* fix getting local state

* dap: fix LocalState alignment

* dap: fix DumpStateHelper

* update cppdap protocol version to 1.68.0

* remove cppdap from vcpkg deps

We can't use the upstream version anyway because the maintainers are not merging our patches

* dap: make named variable nodes derive from the same class

* dap: make cvar scope available in native stack frames

* handle local variables with conflicting names

* add I_GetWindowEvent() to win32 to only process window events when debugging is paused

* dap: fix evaluate

* dap: fix display of `out` variables
2025-07-18 00:01:18 -03:00

262 lines
7.5 KiB
C++

// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "dap/any.h"
#include "dap/typeof.h"
#include "dap/types.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace dap {
struct AnyTestObject {
dap::integer i;
dap::number n;
};
DAP_STRUCT_TYPEINFO(AnyTestObject,
"AnyTestObject",
DAP_FIELD(i, "i"),
DAP_FIELD(n, "n"));
inline bool operator==(const AnyTestObject& a, const AnyTestObject& b) {
return a.i == b.i && a.n == b.n;
}
} // namespace dap
namespace {
template <typename T>
struct TestValue {};
template <>
struct TestValue<dap::null> {
static const dap::null value;
};
template <>
struct TestValue<dap::integer> {
static const dap::integer value;
};
template <>
struct TestValue<dap::boolean> {
static const dap::boolean value;
};
template <>
struct TestValue<dap::number> {
static const dap::number value;
};
template <>
struct TestValue<dap::string> {
static const dap::string value;
};
template <>
struct TestValue<dap::array<dap::string>> {
static const dap::array<dap::string> value;
};
template <>
struct TestValue<dap::AnyTestObject> {
static const dap::AnyTestObject value;
};
const dap::null TestValue<dap::null>::value = nullptr;
const dap::integer TestValue<dap::integer>::value = 20;
const dap::boolean TestValue<dap::boolean>::value = true;
const dap::number TestValue<dap::number>::value = 123.45;
const dap::string TestValue<dap::string>::value = "hello world";
const dap::array<dap::string> TestValue<dap::array<dap::string>>::value = {
"one", "two", "three"};
const dap::AnyTestObject TestValue<dap::AnyTestObject>::value = {10, 20.30};
} // namespace
TEST(Any, EmptyConstruct) {
dap::any any;
ASSERT_TRUE(any.is<dap::null>());
ASSERT_FALSE(any.is<dap::boolean>());
ASSERT_FALSE(any.is<dap::integer>());
ASSERT_FALSE(any.is<dap::number>());
ASSERT_FALSE(any.is<dap::object>());
ASSERT_FALSE(any.is<dap::string>());
ASSERT_FALSE(any.is<dap::array<dap::integer>>());
ASSERT_FALSE(any.is<dap::AnyTestObject>());
}
TEST(Any, Boolean) {
dap::any any(dap::boolean(true));
ASSERT_TRUE(any.is<dap::boolean>());
ASSERT_EQ(any.get<dap::boolean>(), dap::boolean(true));
}
TEST(Any, Integer) {
dap::any any(dap::integer(10));
ASSERT_TRUE(any.is<dap::integer>());
ASSERT_EQ(any.get<dap::integer>(), dap::integer(10));
}
TEST(Any, Number) {
dap::any any(dap::number(123.0f));
ASSERT_TRUE(any.is<dap::number>());
ASSERT_EQ(any.get<dap::number>(), dap::number(123.0f));
}
TEST(Any, String) {
dap::any any(dap::string("hello world"));
ASSERT_TRUE(any.is<dap::string>());
ASSERT_EQ(any.get<dap::string>(), dap::string("hello world"));
}
TEST(Any, Array) {
using array = dap::array<dap::integer>;
dap::any any(array({10, 20, 30}));
ASSERT_TRUE(any.is<array>());
ASSERT_EQ(any.get<array>(), array({10, 20, 30}));
}
TEST(Any, Object) {
dap::object o;
o["one"] = dap::integer(1);
o["two"] = dap::integer(2);
o["three"] = dap::integer(3);
dap::any any(o);
ASSERT_TRUE(any.is<dap::object>());
if (any.is<dap::object>()) {
auto got = any.get<dap::object>();
ASSERT_EQ(got.size(), 3U);
ASSERT_EQ(got.count("one"), 1U);
ASSERT_EQ(got.count("two"), 1U);
ASSERT_EQ(got.count("three"), 1U);
ASSERT_TRUE(got["one"].is<dap::integer>());
ASSERT_TRUE(got["two"].is<dap::integer>());
ASSERT_TRUE(got["three"].is<dap::integer>());
ASSERT_EQ(got["one"].get<dap::integer>(), dap::integer(1));
ASSERT_EQ(got["two"].get<dap::integer>(), dap::integer(2));
ASSERT_EQ(got["three"].get<dap::integer>(), dap::integer(3));
}
}
TEST(Any, TestObject) {
dap::any any(dap::AnyTestObject{5, 3.0});
ASSERT_TRUE(any.is<dap::AnyTestObject>());
ASSERT_EQ(any.get<dap::AnyTestObject>().i, 5);
ASSERT_EQ(any.get<dap::AnyTestObject>().n, 3.0);
}
template <typename T>
class AnyT : public ::testing::Test {
protected:
template <typename T0,
typename = std::enable_if<std::is_same<T, T0>::value &&
!std::is_same<T0, dap::null>::value>>
void check_val(const dap::any& any, const T0& expect) {
ASSERT_EQ(any.is<T>(), any.is<T0>());
ASSERT_EQ(any.get<T>(), expect);
}
// Special case for Null assignment, as we can assign nullptr_t to any but
// can't `get()` it
template <typename = dap::null>
void check_val(const dap::any& any, const dap::null& expect) {
ASSERT_EQ(nullptr, expect);
ASSERT_TRUE(any.is<dap::null>());
}
void check_type(const dap::any& any) {
ASSERT_EQ(any.is<dap::null>(), (std::is_same<T, dap::null>::value));
ASSERT_EQ(any.is<dap::integer>(), (std::is_same<T, dap::integer>::value));
ASSERT_EQ(any.is<dap::boolean>(), (std::is_same<T, dap::boolean>::value));
ASSERT_EQ(any.is<dap::number>(), (std::is_same<T, dap::number>::value));
ASSERT_EQ(any.is<dap::string>(), (std::is_same<T, dap::string>::value));
ASSERT_EQ(any.is<dap::array<dap::string>>(),
(std::is_same<T, dap::array<dap::string>>::value));
ASSERT_EQ(any.is<dap::AnyTestObject>(),
(std::is_same<T, dap::AnyTestObject>::value));
}
};
TYPED_TEST_SUITE_P(AnyT);
TYPED_TEST_P(AnyT, CopyConstruct) {
auto val = TestValue<TypeParam>::value;
dap::any any(val);
this->check_type(any);
this->check_val(any, val);
}
TYPED_TEST_P(AnyT, MoveConstruct) {
auto val = TestValue<TypeParam>::value;
dap::any any(std::move(val));
this->check_type(any);
this->check_val(any, val);
}
TYPED_TEST_P(AnyT, Assign) {
auto val = TestValue<TypeParam>::value;
dap::any any;
any = val;
this->check_type(any);
this->check_val(any, val);
}
TYPED_TEST_P(AnyT, MoveAssign) {
auto val = TestValue<TypeParam>::value;
dap::any any;
any = std::move(val);
this->check_type(any);
this->check_val(any, val);
}
TYPED_TEST_P(AnyT, RepeatedAssign) {
dap::string str = "hello world";
auto val = TestValue<TypeParam>::value;
dap::any any;
any = str;
any = val;
this->check_type(any);
this->check_val(any, val);
}
TYPED_TEST_P(AnyT, RepeatedMoveAssign) {
dap::string str = "hello world";
auto val = TestValue<TypeParam>::value;
dap::any any;
any = std::move(str);
any = std::move(val);
this->check_type(any);
this->check_val(any, val);
}
REGISTER_TYPED_TEST_SUITE_P(AnyT,
CopyConstruct,
MoveConstruct,
Assign,
MoveAssign,
RepeatedAssign,
RepeatedMoveAssign);
using AnyTypes = ::testing::Types<dap::null,
dap::integer,
dap::boolean,
dap::number,
dap::string,
dap::array<dap::string>,
dap::AnyTestObject>;
INSTANTIATE_TYPED_TEST_SUITE_P(T, AnyT, AnyTypes);
TEST(Any, Reset) {
dap::any any(dap::integer(10));
ASSERT_TRUE(any.is<dap::integer>());
any.reset();
ASSERT_FALSE(any.is<dap::integer>());
}