Skip to content

Commit 95513ca

Browse files
committed
feat: add more tests
1 parent f746008 commit 95513ca

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

test.cc

+71-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66
// NOLINTNEXTLINE
77
using namespace dynapse;
88

9+
class Foo {
10+
public:
11+
void Print() const { std::cout << "Hello, this is " << foo_ << "!" << '\n'; }
12+
[[nodiscard]] std::string Get() const { return foo_; }
13+
14+
// NOLINTNEXTLINE
15+
int aaa = 114514;
16+
// NOLINTNEXTLINE
17+
float bbb = 123.5;
18+
bool ccc = true;
19+
20+
private:
21+
std::string foo_ = "foo";
22+
};
23+
924
int main() {
1025
// clang-format off
1126
DYN_REFLECT_CLASS(String,
@@ -21,9 +36,48 @@ int main() {
2136
}),
2237
DYN_PROPERTY(V, "size", make_any(GetReflect().Construct(new int(0), "int"))),
2338
),
24-
)
39+
);
40+
41+
DYN_REFLECT_CLASS(Foo,
42+
DYN_CONSTRUCTOR([](auto) -> void* { return new Foo(); }),
43+
DYN_DESTRUCTOR([](void* ptr) { delete static_cast<Foo*>(ptr); }),
44+
DYN_DECL_MEMBER_PROPS(
45+
DYN_PROPERTY(RW, "aaa", [](const Any& caller, const Args& args) -> Any {
46+
void* aaa_ptr = new int(caller.As<Foo*>()->aaa);
47+
return GetReflect().Construct(aaa_ptr, "int");
48+
}, [](const Any& caller, const Args& args) -> Any {
49+
std::cout << "Foo::aaa setter invoked!" << '\n';
50+
caller.As<Foo*>()->aaa = args[0].To<int>();
51+
return Any::Null();
52+
}),
53+
DYN_PROPERTY(RW, "bbb", [](const Any& caller, const Args& args) -> Any {
54+
void* bbb_ptr = new float(caller.As<Foo*>()->bbb);
55+
return GetReflect().Construct(bbb_ptr, "float");
56+
}, [](const Any& caller, const Args& args) -> Any {
57+
std::cout << "Foo::bbb setter invoked!" << '\n';
58+
caller.As<Foo*>()->bbb = args[0].To<float>();
59+
return Any::Null();
60+
}),
61+
DYN_PROPERTY(RW, "ccc", [](const Any& caller, const Args& args) -> Any {
62+
void* ccc_ptr = new bool(caller.As<Foo*>()->ccc);
63+
return GetReflect().Construct(ccc_ptr, "bool");
64+
}, [](const Any& caller, const Args& args) -> Any {
65+
std::cout << "Foo::ccc setter invoked!" << '\n';
66+
caller.As<Foo*>()->ccc = args[0].To<bool>();
67+
return Any::Null();
68+
}),
69+
),
70+
DYN_DECL_MEMBER_FUNCS(
71+
DYN_FUNCTION("Print", [](const Any& caller, const Args& args) -> Any {
72+
caller.As<Foo*>()->Print();
73+
return Any::Null();
74+
}),
75+
),
76+
);
2577
// clang-format on
2678

79+
// Test `String`
80+
2781
auto reflect = GetReflect();
2882
auto string = reflect.Construct("String");
2983
string.As<std::string*>()->append("Hello, World!");
@@ -38,5 +92,21 @@ int main() {
3892
string["size"] = reflect.Construct(new int(10), "int");
3993
std::cout << "String.size: " << string["size"].To<int>() << '\n';
4094

95+
// Test `Foo`
96+
97+
auto foo = reflect.Construct("Foo");
98+
foo["Print"]();
99+
std::cout << "Foo.aaa: " << foo["aaa"].To<int>() << '\n';
100+
std::cout << "typeof Foo.aaa: " << TypeOf(foo["aaa"]) << '\n';
101+
// NOLINTNEXTLINE
102+
foo["aaa"] = reflect.Construct(new int(1024), "int");
103+
std::cout << "Foo.aaa: " << foo["aaa"].To<int>() << '\n';
104+
105+
std::cout << "Foo.bbb: " << foo["bbb"].To<float>() << '\n';
106+
std::cout << "typeof Foo.bbb: " << TypeOf(foo["bbb"]) << '\n';
107+
// NOLINTNEXTLINE
108+
foo["bbb"] = reflect.Construct(new float(1024.1024), "float");
109+
std::cout << "Foo.bbb: " << foo["bbb"].To<float>() << '\n';
110+
41111
return 0;
42112
}

0 commit comments

Comments
 (0)