-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassTest.cpp
97 lines (74 loc) · 2.21 KB
/
ClassTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "BridgingTest.h"
namespace facebook::react {
using namespace std::literals;
struct TestClass {
TestClass(std::shared_ptr<CallInvoker> invoker) : invoker_(invoker) {}
double add(jsi::Runtime &, int a, float b) {
return a + b;
}
jsi::Object getObject(jsi::Runtime &, jsi::Object obj) {
return obj;
}
AsyncPromise<std::string> getPromise(jsi::Runtime &rt, std::string result) {
auto promise = AsyncPromise<std::string>(rt, invoker_);
promise.resolve(result);
return promise;
}
std::string
callFunc(jsi::Runtime &, SyncCallback<std::string(int)> func, int num) {
return func(num);
}
void callAsync(jsi::Runtime &, AsyncCallback<> callback) {
callback();
}
private:
std::shared_ptr<CallInvoker> invoker_;
};
TEST_F(BridgingTest, callFromJsTest) {
auto instance = TestClass(invoker);
EXPECT_EQ(
3.0,
bridging::callFromJs<double>(
rt, &TestClass::add, invoker, &instance, 1, 2.0));
auto object = jsi::Object(rt);
EXPECT_TRUE(jsi::Object::strictEquals(
rt,
object,
bridging::callFromJs<jsi::Object>(
rt, &TestClass::getObject, invoker, &instance, object)));
auto promise = bridging::callFromJs<jsi::Object>(
rt,
&TestClass::getPromise,
invoker,
&instance,
jsi::String::createFromAscii(rt, "hi"));
auto then = promise.getPropertyAsFunction(rt, "then");
std::string result;
then.callWithThis(
rt,
promise,
bridging::toJs(
rt, [&](std::string res) { result = res; }, invoker));
flushQueue();
EXPECT_EQ("hi"s, result);
auto func = function("(num) => String(num)");
EXPECT_EQ(
"1"s,
bridging::callFromJs<jsi::String>(
rt, &TestClass::callFunc, invoker, &instance, func, 1)
.utf8(rt));
bool called = false;
func = bridging::toJs(
rt, [&] { called = true; }, invoker);
bridging::callFromJs<void>(
rt, &TestClass::callAsync, invoker, &instance, func);
flushQueue();
EXPECT_TRUE(called);
}
} // namespace facebook::react