Skip to content

Commit 8de1fbb

Browse files
authored
Write a test for static method in IDL interface. (#117)
This patch also changes getCalledConstructorInfo() to static method and rename it with getLastCallInfo() and then use it use generally. ISSUE=#99,#100
1 parent 80f0fbf commit 8de1fbb

5 files changed

+73
-16
lines changed

test/interface_constructor.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ test('When creating two objects, should be differnt instances', async () => {
4343

4444
test('Test for constructor overloading', async () => {
4545
let constructor1 = new bacardi.TestInterface();
46-
expect(constructor1.getCalledConstructorInfo()).toBe('Constructor()');
46+
expect(bacardi.TestInterface.getLastCallInfo()).toBe('Constructor()');
4747

4848
let constructor2 = new bacardi.TestInterface(1);
49-
expect(constructor2.getCalledConstructorInfo()).toBe('Constructor(long)');
49+
expect(bacardi.TestInterface.getLastCallInfo()).toBe('Constructor(long)');
5050

5151
let constructor3 = new bacardi.TestInterface(2, 3);
52-
expect(constructor3.getCalledConstructorInfo())
52+
expect(bacardi.TestInterface.getLastCallInfo())
5353
.toBe('Constructor(long, long)');
5454

5555
let constructor4 = new bacardi.TestInterface('hello', 'world');
56-
expect(constructor4.getCalledConstructorInfo())
56+
expect(bacardi.TestInterface.getLastCallInfo())
5757
.toBe('Constructor(string, string)');
5858
});

test/interface_static_method.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2017 The Bacardi Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as bindings from 'bindings';
18+
19+
const bacardi = bindings('bacardi.node');
20+
21+
test('Basic of static method', async () => {
22+
// We can call the static methods without object instantiation.
23+
bacardi.TestInterface.staticMethod1();
24+
expect(bacardi.TestInterface.getLastCallInfo())
25+
.toBe('static void staticMethod1()');
26+
bacardi.TestInterface.staticMethod2(10, 'test');
27+
expect(bacardi.TestInterface.getLastCallInfo())
28+
.toBe('static boolean staticMethod2(long, string)');
29+
});

test/test_interface.cc

+27-9
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,35 @@
1616

1717
#include "test/test_interface.h"
1818

19-
TestInterface::TestInterface() : called_constructor_info_("Constructor()") {}
19+
// static
20+
std::string TestInterface::last_call_info_;
2021

21-
TestInterface::TestInterface(long createTime)
22-
: called_constructor_info_("Constructor(long)") {}
22+
TestInterface::TestInterface() {
23+
last_call_info_ = "Constructor()";
24+
}
2325

24-
TestInterface::TestInterface(long arg1, long arg2)
25-
: called_constructor_info_("Constructor(long, long)") {}
26+
TestInterface::TestInterface(long number) {
27+
last_call_info_ = "Constructor(long)";
28+
}
2629

27-
TestInterface::TestInterface(const std::string& msg1, const std::string& msg2)
28-
: called_constructor_info_("Constructor(string, string)") {}
30+
TestInterface::TestInterface(long number1, long number2) {
31+
last_call_info_ = "Constructor(long, long)";
32+
}
33+
34+
TestInterface::TestInterface(const std::string& string1,
35+
const std::string& string2) {
36+
last_call_info_ = "Constructor(string, string)";
37+
}
38+
39+
const std::string& TestInterface::GetLastCallInfo() {
40+
return last_call_info_;
41+
}
42+
43+
void TestInterface::StaticMethod1() {
44+
last_call_info_ = "static void staticMethod1()";
45+
}
2946

30-
const std::string& TestInterface::GetCalledConstructorInfo() const {
31-
return called_constructor_info_;
47+
bool TestInterface::StaticMethod2(long number, const std::string& string) {
48+
last_call_info_ = "static boolean staticMethod2(long, string)";
49+
return 0;
3250
}

test/test_interface.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@ class TestInterface {
2626
TestInterface(long number1, long number2);
2727
TestInterface(const std::string& string1, const std::string& string2);
2828

29-
const std::string& GetCalledConstructorInfo() const;
29+
static const std::string& GetLastCallInfo();
30+
31+
static void StaticMethod1();
32+
static bool StaticMethod2(long number, const std::string& string);
3033

3134
private:
32-
const std::string called_constructor_info_;
35+
// FIXME(zino): Currently, we should set this variable in each methods. It's
36+
// not elegance way. We should find a way to get function name and signature
37+
// automatically. (I tried __FUNCTION__ and __PRETTY_FUNCTION__ but they are
38+
// dependent on each platform.
39+
static std::string last_call_info_;
3340
};
3441

3542
#endif // TEST_TEST_INTERFACE_H_

test/test_interface.idl

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
]
2323
interface TestInterface {
2424
// FIXME(zino): It's better to use attribute instead of method.
25-
string getCalledConstructorInfo();
25+
static string getLastCallInfo();
26+
27+
static void staticMethod1();
28+
static boolean staticMethod2(long number, string string);
2629
};

0 commit comments

Comments
 (0)